summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-20 10:54:04 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-20 10:57:28 +0200
commit5fa51247a3a40707f36be045e53d95a173b43cc9 (patch)
tree53cf446c75c7a1048d880601b8de2245df8299e5 /tests
parentb7faff24bb44d32dd316144dc3f9091937772d2d (diff)
Add simple test case for block processor ordering
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests')
-rw-r--r--tests/Makemodule.am12
-rw-r--r--tests/blk_proc_order.c130
2 files changed, 141 insertions, 1 deletions
diff --git a/tests/Makemodule.am b/tests/Makemodule.am
index 60312dc..6772c14 100644
--- a/tests/Makemodule.am
+++ b/tests/Makemodule.am
@@ -73,6 +73,15 @@ test_str_table_SOURCES = tests/str_table.c
test_str_table_LDADD = libutil.a
test_str_table_CPPFLAGS = $(AM_CPPFLAGS) -DTESTPATH=$(top_srcdir)/tests
+test_blk_proc_order_SOURCES = tests/blk_proc_order.c
+test_blk_proc_order_CPPFLAGS = $(AM_CPPFLAGS)
+test_blk_proc_order_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS)
+test_blk_proc_order_LDADD = libutil.a libcompress.a $(ZLIB_LIBS) $(PTHREAD_LIBS)
+
+if HAVE_PTHREAD
+test_blk_proc_order_CPPFLAGS += -DHAVE_PTHREAD
+endif
+
check_PROGRAMS += test_canonicalize_name test_mknode_simple test_mknode_slink
check_PROGRAMS += test_mknode_reg test_mknode_dir test_gen_inode_table
check_PROGRAMS += test_add_by_path test_get_path test_fstree_sort
@@ -80,6 +89,7 @@ check_PROGRAMS += test_fstree_from_file test_fstree_init test_fstree_xattr
check_PROGRAMS += test_tar_ustar test_tar_pax test_tar_gnu test_tar_sparse_gnu
check_PROGRAMS += test_tar_sparse_gnu1 test_tar_sparse_gnu2
check_PROGRAMS += test_tar_xattr_bsd test_tar_xattr_schily test_str_table
+check_PROGRAMS += test_blk_proc_order
TESTS += test_canonicalize_name test_mknode_simple test_mknode_slink
TESTS += test_mknode_reg test_mknode_dir test_gen_inode_table
@@ -87,6 +97,6 @@ TESTS += test_add_by_path test_get_path test_fstree_sort test_fstree_from_file
TESTS += test_fstree_init test_fstree_xattr test_tar_ustar test_tar_pax
TESTS += test_tar_gnu test_tar_sparse_gnu test_tar_sparse_gnu1
TESTS += test_tar_sparse_gnu2 test_tar_xattr_bsd test_tar_xattr_schily
-TESTS += test_str_table
+TESTS += test_str_table test_blk_proc_order
EXTRA_DIST += $(top_srcdir)/tests/tar $(top_srcdir)/tests/words.txt
diff --git a/tests/blk_proc_order.c b/tests/blk_proc_order.c
new file mode 100644
index 0000000..eea8852
--- /dev/null
+++ b/tests/blk_proc_order.c
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * add_by_path.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+
+#include "block_processor.h"
+#include "compress.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <unistd.h>
+
+#ifdef HAVE_PTHREAD
+#include <pthread.h>
+#endif
+
+static ssize_t dummy_do_block(compressor_t *cmp, const uint8_t *in,
+ size_t size, uint8_t *out, size_t outsize)
+{
+ int delay, result;
+ (void)cmp; (void)size; (void)outsize;
+
+ delay = ((int *)in)[0];
+ result = ((int *)in)[1];
+
+ memcpy(out, in, result);
+ sleep(delay);
+ return (size_t)result >= size ? 0 : result;
+}
+
+static compressor_t *dummy_create_copy(compressor_t *cmp)
+{
+ compressor_t *cpy = malloc(sizeof(*cpy));
+ assert(cpy != NULL);
+ *cpy = *cmp;
+ return cpy;
+}
+
+static void dummy_destroy(compressor_t *cmp)
+{
+ free(cmp);
+}
+
+static compressor_t *dummy_create(void)
+{
+ compressor_t *cmp = calloc(1, sizeof(*cmp));
+ assert(cmp != NULL);
+
+ cmp->do_block = dummy_do_block;
+ cmp->create_copy = dummy_create_copy;
+ cmp->destroy = dummy_destroy;
+
+ return cmp;
+}
+
+static unsigned int blk_index = 0;
+
+#ifdef HAVE_PTHREAD
+static pthread_t main_thread;
+#endif
+
+static int block_callback(void *user, block_t *blk)
+{
+ assert(main_thread == pthread_self());
+ assert(blk->index == blk_index++);
+
+ if (blk->index == 4) {
+ assert(blk->size == 4 * sizeof(int));
+ assert(blk->flags & BLK_DONT_COMPRESS);
+ assert(blk->flags & BLK_DONT_CHECKSUM);
+ assert(!(blk->flags & BLK_IS_COMPRESSED));
+ } else {
+ if (blk->index & 0x01) {
+ assert(!(blk->flags & BLK_IS_COMPRESSED));
+ assert(blk->size == 4 * sizeof(int));
+ } else {
+ assert(blk->flags & BLK_IS_COMPRESSED);
+ assert(blk->size == 2 * sizeof(int));
+ }
+ }
+
+ (void)user;
+ return 0;
+}
+
+int main(void)
+{
+ block_processor_t *proc;
+ compressor_t *cmp = dummy_create();
+ block_t *blk;
+ int i;
+
+#ifdef HAVE_PTHREAD
+ main_thread = pthread_self();
+#endif
+
+ proc = block_processor_create(4 * sizeof(int), cmp, 4,
+ NULL, block_callback);
+ assert(proc != NULL);
+
+ for (i = 0; i < 4; ++i) {
+ blk = calloc(1, sizeof(*blk) + 4 * sizeof(int));
+ assert(blk != NULL);
+ blk->size = 4 * sizeof(int);
+ blk->index = i;
+ ((int *)blk->data)[0] = 4 - i;
+ ((int *)blk->data)[1] = (i & 0x01 ? 4 : 2) * sizeof(int);
+
+ assert(block_processor_enqueue(proc, blk) == 0);
+ }
+
+ blk = calloc(1, sizeof(*blk) + 4 * sizeof(int));
+ assert(blk != NULL);
+ blk->size = 4 * sizeof(int);
+ blk->index = i;
+ blk->flags |= BLK_DONT_COMPRESS | BLK_DONT_CHECKSUM;
+ ((int *)blk->data)[0] = 0;
+ ((int *)blk->data)[1] = (i & 0x01 ? 4 : 2) * sizeof(int);
+ assert(block_processor_enqueue(proc, blk) == 0);
+
+ assert(block_processor_finish(proc) == 0);
+
+ block_processor_destroy(proc);
+ cmp->destroy(cmp);
+ return EXIT_SUCCESS;
+}