aboutsummaryrefslogtreecommitdiff
path: root/lib/io
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-01-31 11:30:46 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-01-31 18:04:25 +0100
commit72c8155d9fc0eaeac72c053f46ebb7b231d4596a (patch)
tree5758865289c52fa93f56e3fe743bb40c283c5233 /lib/io
parentcdccc69c62579b0c13b35fad0728079652b8f3c9 (diff)
Reintegrate test code with library code
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/io')
-rw-r--r--lib/io/Makemodule.am13
-rw-r--r--lib/io/test/get_line.c166
-rw-r--r--lib/io/test/get_line.txt11
-rw-r--r--lib/io/test/sparse_fb.c66
4 files changed, 256 insertions, 0 deletions
diff --git a/lib/io/Makemodule.am b/lib/io/Makemodule.am
index c331124..17952f4 100644
--- a/lib/io/Makemodule.am
+++ b/lib/io/Makemodule.am
@@ -14,3 +14,16 @@ libio_a_SOURCES += lib/io/src/unix/ostream.c lib/io/src/unix/istream.c
endif
noinst_LIBRARIES += libio.a
+
+test_get_line_SOURCES = lib/io/test/get_line.c
+test_get_line_LDADD = libio.a libcompat.a
+test_get_line_CPPFLAGS = $(AM_CPPFLAGS)
+test_get_line_CPPFLAGS += -DTESTFILE=$(top_srcdir)/lib/io/test/get_line.txt
+
+test_sparse_fb_SOURCES = lib/io/test/sparse_fb.c
+test_sparse_fb_LDADD = libio.a libutil.a libcompat.a
+
+check_PROGRAMS += test_get_line test_sparse_fb
+TESTS += test_get_line test_sparse_fb
+
+EXTRA_DIST += $(top_srcdir)/lib/io/test/get_line.txt
diff --git a/lib/io/test/get_line.c b/lib/io/test/get_line.c
new file mode 100644
index 0000000..2d0f9b7
--- /dev/null
+++ b/lib/io/test/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 "util/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_drop(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;
+}
diff --git a/lib/io/test/get_line.txt b/lib/io/test/get_line.txt
new file mode 100644
index 0000000..a1994f0
--- /dev/null
+++ b/lib/io/test/get_line.txt
@@ -0,0 +1,11 @@
+
+The quick
+
+ brown fox
+
+jumps over
+the
+lazy
+
+dog
+
diff --git a/lib/io/test/sparse_fb.c b/lib/io/test/sparse_fb.c
new file mode 100644
index 0000000..fa4b840
--- /dev/null
+++ b/lib/io/test/sparse_fb.c
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * get_line.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+
+#include "io/ostream.h"
+#include "util/test.h"
+#include "util/util.h"
+
+static ostream_t dummy;
+static size_t total = 0;
+
+static int dummy_append(ostream_t *strm, const void *data, size_t size)
+{
+ bool bret;
+
+ TEST_ASSERT(strm == &dummy);
+ TEST_NOT_NULL(data);
+ TEST_ASSERT(size > 0);
+
+ bret = is_memory_zero(data, size);
+ TEST_ASSERT(bret);
+
+ bret = SZ_ADD_OV(total, size, &total);
+ TEST_ASSERT(!bret);
+ return 0;
+}
+
+static int dummy_flush(ostream_t *strm)
+{
+ TEST_ASSERT(strm == &dummy);
+ return 0;
+}
+
+static ostream_t dummy = {
+ {
+ 1,
+ NULL,
+ NULL,
+ },
+ dummy_append,
+ NULL,
+ dummy_flush,
+ NULL,
+};
+
+int main(int argc, char **argv)
+{
+ size_t ref;
+ int ret;
+ (void)argc; (void)argv;
+
+ ref = 131072 + 1337;
+
+ ret = ostream_append_sparse(&dummy, ref);
+ TEST_EQUAL_I(ret, 0);
+
+ ret = ostream_flush(&dummy);
+ TEST_EQUAL_I(ret, 0);
+
+ TEST_EQUAL_UI(ref, total);
+ return EXIT_SUCCESS;
+}