diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2022-06-26 22:51:27 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2022-07-08 19:17:35 +0200 |
commit | 359d71e90050a8b83f7bc7d2ecd4ff29c477cc22 (patch) | |
tree | df2b7a585b78e6776446d4ca9be62a22db27f0a7 /tests/libio/get_line.c | |
parent | 2087cc237cd0fe1ed29ebf891648bacb46f4833b (diff) |
Cleanup: rename libfstream to libio, split headers
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests/libio/get_line.c')
-rw-r--r-- | tests/libio/get_line.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/tests/libio/get_line.c b/tests/libio/get_line.c new file mode 100644 index 0000000..66fced5 --- /dev/null +++ b/tests/libio/get_line.c @@ -0,0 +1,166 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * get_line.c + * + * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> + */ +#include "config.h" + +#include "io/file.h" +#include "../test.h" + +typedef struct { + size_t line_num; + const char *str; +} line_t; + +static void run_test_case(const line_t *lines, size_t count, + int flags) +{ + size_t i, line_num, old_line_num; + istream_t *fp; + char *line; + int ret; + + fp = istream_open_file(STRVALUE(TESTFILE)); + TEST_NOT_NULL(fp); + + line_num = 1; + line = NULL; + + for (i = 0; i < count; ++i) { + old_line_num = line_num; + ret = istream_get_line(fp, &line, &line_num, flags); + + TEST_ASSERT(line_num >= old_line_num); + TEST_EQUAL_I(ret, 0); + TEST_NOT_NULL(line); + + TEST_EQUAL_UI(line_num, lines[i].line_num); + TEST_STR_EQUAL(line, lines[i].str); + + free(line); + line = NULL; + line_num += 1; + } + + ret = istream_get_line(fp, &line, &line_num, flags); + TEST_ASSERT(ret > 0); + + sqfs_destroy(fp); +} + +static const line_t lines_raw[] = { + { 1, "" }, + { 2, "The quick" }, + { 3, " " }, + { 4, " brown fox " }, + { 5, "" }, + { 6, "jumps over" }, + { 7, "the" }, + { 8, "lazy" }, + { 9, "" }, + { 10, "dog" }, + { 11, "" }, +}; + +static const line_t lines_ltrim[] = { + { 1, "" }, + { 2, "The quick" }, + { 3, "" }, + { 4, "brown fox " }, + { 5, "" }, + { 6, "jumps over" }, + { 7, "the" }, + { 8, "lazy" }, + { 9, "" }, + { 10, "dog" }, + { 11, "" }, +}; + +static const line_t lines_rtrim[] = { + { 1, "" }, + { 2, "The quick" }, + { 3, "" }, + { 4, " brown fox" }, + { 5, "" }, + { 6, "jumps over" }, + { 7, "the" }, + { 8, "lazy" }, + { 9, "" }, + { 10, "dog" }, + { 11, "" }, +}; + +static const line_t lines_trim[] = { + { 1, "" }, + { 2, "The quick" }, + { 3, "" }, + { 4, "brown fox" }, + { 5, "" }, + { 6, "jumps over" }, + { 7, "the" }, + { 8, "lazy" }, + { 9, "" }, + { 10, "dog" }, + { 11, "" }, +}; + +static const line_t lines_no_empty[] = { + { 2, "The quick" }, + { 3, " " }, + { 4, " brown fox " }, + { 6, "jumps over" }, + { 7, "the" }, + { 8, "lazy" }, + { 10, "dog" }, +}; + +static const line_t lines_no_empty_ltrim[] = { + { 2, "The quick" }, + { 4, "brown fox " }, + { 6, "jumps over" }, + { 7, "the" }, + { 8, "lazy" }, + { 10, "dog" }, +}; + +static const line_t lines_no_empty_rtrim[] = { + { 2, "The quick" }, + { 4, " brown fox" }, + { 6, "jumps over" }, + { 7, "the" }, + { 8, "lazy" }, + { 10, "dog" }, +}; + +static const line_t lines_no_empty_trim[] = { + { 2, "The quick" }, + { 4, "brown fox" }, + { 6, "jumps over" }, + { 7, "the" }, + { 8, "lazy" }, + { 10, "dog" }, +}; + +int main(int argc, char **argv) +{ + (void)argc; (void)argv; + + run_test_case(lines_raw, 11, 0); + run_test_case(lines_ltrim, 11, ISTREAM_LINE_LTRIM); + run_test_case(lines_rtrim, 11, ISTREAM_LINE_RTRIM); + run_test_case(lines_trim, 11, + ISTREAM_LINE_LTRIM | ISTREAM_LINE_RTRIM); + + run_test_case(lines_no_empty, 7, ISTREAM_LINE_SKIP_EMPTY); + run_test_case(lines_no_empty_ltrim, 6, + ISTREAM_LINE_SKIP_EMPTY | ISTREAM_LINE_LTRIM); + run_test_case(lines_no_empty_rtrim, 6, + ISTREAM_LINE_SKIP_EMPTY | ISTREAM_LINE_RTRIM); + run_test_case(lines_no_empty_trim, 6, + ISTREAM_LINE_SKIP_EMPTY | ISTREAM_LINE_LTRIM | + ISTREAM_LINE_RTRIM); + + return EXIT_SUCCESS; +} |