From d87bffd89b9c0a26a65f0c629250fa87902b6cb8 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 25 Sep 2020 20:42:45 +0200 Subject: Add a more usefull getline-like function to libfstream Signed-off-by: David Oberhollenzer --- tests/Makemodule.am | 9 ++- tests/get_line.c | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/get_line.txt | 11 ++++ tests/test.h | 5 ++ tests/test_tar.h | 5 -- 5 files changed, 188 insertions(+), 6 deletions(-) create mode 100644 tests/get_line.c create mode 100644 tests/get_line.txt (limited to 'tests') diff --git a/tests/Makemodule.am b/tests/Makemodule.am index 94bfd4b..58cd27e 100644 --- a/tests/Makemodule.am +++ b/tests/Makemodule.am @@ -230,6 +230,11 @@ fstree_fuzz_LDADD = libfstree.a libcompat.a tar_fuzz_SOURCES = tests/tar_fuzz.c tar_fuzz_LDADD = libtar.a libfstream.a libcompat.a +test_get_line_SOURCES = tests/get_line.c tests/test.h +test_get_line_LDADD = libfstream.a libcompat.a +test_get_line_CPPFLAGS = $(AM_CPPFLAGS) +test_get_line_CPPFLAGS += -DTESTFILE=$(top_srcdir)/tests/get_line.txt + check_PROGRAMS += test_mknode_simple test_mknode_slink test_mknode_reg check_PROGRAMS += test_mknode_dir test_gen_inode_numbers test_add_by_path check_PROGRAMS += test_get_path test_fstree_sort test_fstree_from_file @@ -245,6 +250,7 @@ check_PROGRAMS += test_tar_sparse_gnu test_tar_sparse_gnu0 test_tar_sparse_gnu1 check_PROGRAMS += test_tar_sparse_gnu2 test_tar_sparse_gnu3 check_PROGRAMS += test_tar_xattr_bsd test_tar_xattr_schily check_PROGRAMS += test_tar_xattr_schily_bin test_tar_target_filled +check_PROGRAMS += test_get_line noinst_PROGRAMS += fstree_fuzz tar_fuzz @@ -261,7 +267,7 @@ TESTS += test_tar_pax5 TESTS += test_tar_sparse_gnu test_tar_sparse_gnu0 TESTS += test_tar_sparse_gnu1 test_tar_sparse_gnu2 test_tar_sparse_gnu3 TESTS += test_tar_xattr_bsd test_tar_xattr_schily -TESTS += test_tar_xattr_schily_bin test_tar_target_filled +TESTS += test_tar_xattr_schily_bin test_tar_target_filled test_get_line if CORPORA_TESTS check_SCRIPTS += tests/cantrbry.sh tests/test_tar_sqfs.sh tests/pack_dir_root.sh @@ -274,3 +280,4 @@ EXTRA_DIST += $(top_srcdir)/tests/fstree1.txt EXTRA_DIST += $(top_srcdir)/tests/corpus/cantrbry.tar.xz EXTRA_DIST += $(top_srcdir)/tests/corpus/cantrbry.sha512 EXTRA_DIST += $(top_srcdir)/tests/pack_dir_root.txt.ref +EXTRA_DIST += $(top_srcdir)/tests/get_line.txt diff --git a/tests/get_line.c b/tests/get_line.c new file mode 100644 index 0000000..c317c0e --- /dev/null +++ b/tests/get_line.c @@ -0,0 +1,164 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * get_line.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "fstream.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(void) +{ + 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; +} diff --git a/tests/get_line.txt b/tests/get_line.txt new file mode 100644 index 0000000..a1994f0 --- /dev/null +++ b/tests/get_line.txt @@ -0,0 +1,11 @@ + +The quick + + brown fox + +jumps over +the +lazy + +dog + diff --git a/tests/test.h b/tests/test.h index 7d38fd0..bc3ac92 100644 --- a/tests/test.h +++ b/tests/test.h @@ -13,6 +13,11 @@ #include #include +#define STR(x) #x +#define STRVALUE(x) STR(x) + +#define TEST_PATH STRVALUE(TESTPATH) + #if defined(__GNUC__) || defined(__clang__) # define ATTRIB_UNUSED __attribute__ ((unused)) #else diff --git a/tests/test_tar.h b/tests/test_tar.h index 9ec2b12..0d5ccc1 100644 --- a/tests/test_tar.h +++ b/tests/test_tar.h @@ -11,9 +11,4 @@ #include "tar.h" #include "test.h" -#define STR(x) #x -#define STRVALUE(x) STR(x) - -#define TEST_PATH STRVALUE(TESTPATH) - #endif /* TEST_TAR_H */ -- cgit v1.2.3