aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-04 20:55:41 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-05 09:16:39 +0200
commit8e86f894afe775e39b8d02c95dd55f97a3465d27 (patch)
tree4187bf58797e2a9a5813704271e00f89d3674bde
parent1370963723917eed6c93e28c9970a2b27be57ea4 (diff)
libio: add a simple unit test for the memory istream_t implementation
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--lib/io/Makemodule.am6
-rw-r--r--lib/io/test/istream_mem.c78
2 files changed, 83 insertions, 1 deletions
diff --git a/lib/io/Makemodule.am b/lib/io/Makemodule.am
index 7427b42..5ec1f7e 100644
--- a/lib/io/Makemodule.am
+++ b/lib/io/Makemodule.am
@@ -19,10 +19,14 @@ endif
noinst_LIBRARIES += libio.a
-LIBIO_TESTS = test_get_line test_sparse_fb test_istream_read \
+LIBIO_TESTS = test_istream_mem test_get_line test_sparse_fb test_istream_read \
test_istream_skip test_stream_splice test_dir_iterator \
test_dir_tree_iterator test_dir_tree_iterator2 test_dir_tree_iterator3
+test_istream_mem_SOURCES = lib/io/test/istream_mem.c
+test_istream_mem_LDADD = libio.a libcompat.a
+test_istream_mem_CPPFLAGS = $(AM_CPPFLAGS)
+
test_get_line_SOURCES = lib/io/test/get_line.c
test_get_line_LDADD = libio.a libcompat.a
test_get_line_CPPFLAGS = $(AM_CPPFLAGS)
diff --git a/lib/io/test/istream_mem.c b/lib/io/test/istream_mem.c
new file mode 100644
index 0000000..c74b1f5
--- /dev/null
+++ b/lib/io/test/istream_mem.c
@@ -0,0 +1,78 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * istream_mem.c
+ *
+ * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+
+#include "util/test.h"
+#include "io/mem.h"
+
+static const size_t end0 = 449; /* region 1: filled with 'A' */
+static const size_t end1 = 521; /* region 2: filled with 'B' */
+static const size_t end2 = 941; /* region 3: filled with 'C' */
+
+static unsigned char data[941];
+
+static unsigned char byte_at_offset(size_t off)
+{
+ if (off < end0)
+ return 'A';
+ if (off < end1)
+ return 'B';
+ return 'C';
+}
+
+static void init_buffer(void)
+{
+ for (size_t i = 0; i < end2; ++i)
+ data[i] = byte_at_offset(i);
+}
+
+int main(int argc, char **argv)
+{
+ bool eat_all = true;
+ size_t i, diff;
+ istream_t *in;
+ int ret;
+ (void)argc; (void)argv;
+
+ init_buffer();
+
+ in = istream_memory_create("memstream.txt", 61, data, sizeof(data));
+ TEST_NOT_NULL(in);
+ TEST_EQUAL_UI(((sqfs_object_t *)in)->refcount, 1);
+
+ TEST_STR_EQUAL(istream_get_filename(in), "memstream.txt");
+ TEST_NOT_NULL(in->buffer);
+ TEST_EQUAL_UI(in->buffer_used, 0);
+
+ for (i = 0; i < end2; i += diff) {
+ ret = istream_precache(in);
+ TEST_EQUAL_I(ret, 0);
+
+ if ((end2 - i) >= 61) {
+ TEST_NOT_NULL(in->buffer);
+ TEST_EQUAL_UI(in->buffer_used, 61);
+ } else {
+ TEST_NOT_NULL(in->buffer);
+ TEST_EQUAL_UI(in->buffer_used, (end2 - i));
+ }
+
+ for (size_t j = 0; j < in->buffer_used; ++j) {
+ TEST_EQUAL_UI(in->buffer[j], byte_at_offset(i + j));
+ }
+
+ diff = eat_all ? in->buffer_used : (in->buffer_used / 2);
+ eat_all = !eat_all;
+
+ in->buffer += diff;
+ in->buffer_used -= diff;
+ }
+
+ TEST_EQUAL_UI(in->buffer_used, 0);
+ TEST_ASSERT(in->eof);
+ sqfs_drop(in);
+ return EXIT_SUCCESS;
+}