]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-compression: Convert unit tests to use create_ostream_auto() API
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 27 Nov 2024 13:29:17 +0000 (15:29 +0200)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Fri, 17 Jan 2025 08:40:01 +0000 (10:40 +0200)
src/lib-compression/bench-compression.c
src/lib-compression/test-compression.c

index 655079ddb61c63ab5e09df3881010b634b8e3421..47659aace3e02cf9e35f519b786a2eeb6ad7eae4 100644 (file)
@@ -1,12 +1,13 @@
 /* Copyright (c) 2020 Dovecot authors, see the included COPYING file */
 
 #include "lib.h"
-#include "buffer.h"
+#include "array.h"
 #include "istream.h"
 #include "ostream.h"
 #include "randgen.h"
 #include "time-util.h"
 #include "strnum.h"
+#include "settings.h"
 #include "compression.h"
 
 #include <stdio.h>
  * compressed and how long it took.
  */
 
-static void bench_compression_speed(const struct compression_handler *handler,
-                                   unsigned int level, unsigned long block_count)
+static void
+bench_compression_speed(const struct compression_handler *handler,
+                       struct event *event, unsigned long block_count)
 {
        struct istream *is = i_stream_create_file("decompressed.bin", 1024);
        struct ostream *os = o_stream_create_file("compressed.bin", 0, 0644, 0);
-       struct ostream *os_compressed = handler->create_ostream(os, level);
+       struct ostream *os_compressed = handler->create_ostream_auto(os, event);
        o_stream_unref(&os);
 
        const unsigned char *data;
@@ -43,7 +45,10 @@ static void bench_compression_speed(const struct compression_handler *handler,
        if (is->stream_errno != 0)
                printf("Error: %s\n", i_stream_get_error(is));
 
-       i_assert(o_stream_finish(os_compressed) == 1);
+       int ret = o_stream_finish(os_compressed);
+       i_assert(ret != 0);
+       if (ret < 0)
+               i_fatal("%s", o_stream_get_error(os_compressed));
        o_stream_unref(&os_compressed);
        i_stream_unref(&is);
 
@@ -94,29 +99,36 @@ static void bench_compression_speed(const struct compression_handler *handler,
 
 static void print_usage(const char *prog)
 {
-       fprintf(stderr, "Usage: %s block_size count level\n", prog);
-       fprintf(stderr, "Runs with 1000 8k blocks using level 6 if nothing given\n");
+       fprintf(stderr, "Usage: %s [<block_size> [<count> [<compression settings>]]]\n", prog);
+       fprintf(stderr, "Runs with 1000 8k blocks if nothing given\n");
        lib_exit(1);
 }
 
 int main(int argc, const char *argv[])
 {
-       unsigned int level = 6;
        lib_init();
 
        unsigned long block_size = 8192UL;
        unsigned long block_count = 1000UL;
 
+       ARRAY_TYPE(const_string) set_array;
+       t_array_init(&set_array, 4);
+
        if (argc >= 3) {
                if (str_to_ulong(argv[1], &block_size) < 0 ||
                    str_to_ulong(argv[2], &block_count) < 0) {
                        fprintf(stderr, "Invalid parameters\n");
                        print_usage(argv[0]);
                }
-               if (argc == 4 &&
-                   str_to_uint(argv[3], &level) < 0) {
-                       fprintf(stderr, "Invalid parameters\n");
-                       print_usage(argv[0]);
+               while (argc > 3) {
+                       const char *key, *value;
+                       if (!t_split_key_value_eq(argv[3], &key, &value)) {
+                               fprintf(stderr, "Invalid parameters\n");
+                               print_usage(argv[0]);
+                       }
+                       array_push_back(&set_array, &key);
+                       array_push_back(&set_array, &value);
+                       argc--; argv++;
                }
                if (argc > 4) {
                        print_usage(argv[0]);
@@ -125,6 +137,10 @@ int main(int argc, const char *argv[])
                print_usage(argv[0]);
        }
 
+       struct settings_simple set;
+       array_append_zero(&set_array);
+       settings_simple_init(&set, array_front(&set_array));
+
        unsigned char buf[block_size];
        printf("Input data is %lu blocks of %lu bytes\n\n", block_count, block_size);
 
@@ -155,14 +171,15 @@ int main(int argc, const char *argv[])
 
        for (unsigned int i = 0; compression_handlers[i].name != NULL; i++) T_BEGIN {
                if (compression_handlers[i].create_istream != NULL &&
-                   compression_handlers[i].create_ostream != NULL) {
-                       bench_compression_speed(&compression_handlers[i], level,
-                                               block_count);
+                   compression_handlers[i].create_ostream_auto != NULL) {
+                       bench_compression_speed(&compression_handlers[i],
+                                               set.event, block_count);
                }
        } T_END;
 
        i_unlink("decompressed.bin");
        i_unlink("compressed.bin");
+       settings_simple_deinit(&set);
 
        lib_deinit();
 }
index f690c7b9d505c8322bd10ef6173dc0e15da345e7..4d8cce35bd973e000136694920d1d88334ae26ef 100644 (file)
@@ -8,6 +8,7 @@
 #include "sha1.h"
 #include "randgen.h"
 #include "test-common.h"
+#include "settings.h"
 #include "compression.h"
 #include "iostream-lz4.h"
 
@@ -16,6 +17,8 @@
 #include <unistd.h>
 #include <fcntl.h>
 
+static struct settings_simple set;
+
 static void test_compression_handler_detect(const struct compression_handler *handler)
 {
        const unsigned char test_data[] = {'h','e','l','l','o',' ',
@@ -35,7 +38,7 @@ static void test_compression_handler_detect(const struct compression_handler *ha
        buffer = buffer_create_dynamic(default_pool, 1024);
 
        test_output = test_ostream_create(buffer);
-       output = handler->create_ostream(test_output, 1);
+       output = handler->create_ostream_auto(test_output, set.event);
        o_stream_unref(&test_output);
 
        /* write data at once */
@@ -89,7 +92,7 @@ test_compression_handler_short(const struct compression_handler *handler,
 
        buffer = buffer_create_dynamic(default_pool, 1024);
        test_output = test_ostream_create(buffer);
-       output = handler->create_ostream(test_output, 1);
+       output = handler->create_ostream_auto(test_output, set.event);
        o_stream_unref(&test_output);
 
        /* write data at once */
@@ -131,7 +134,7 @@ test_compression_handler_empty(const struct compression_handler *handler,
                                   handler->name, autodetect ? "yes" : "no"));
        buffer = buffer_create_dynamic(default_pool, 128);
        test_output = test_ostream_create(buffer);
-       output = handler->create_ostream(test_output, 1);
+       output = handler->create_ostream_auto(test_output, set.event);
        o_stream_unref(&test_output);
        test_assert(o_stream_finish(output) == 1);
        o_stream_unref(&output);
@@ -176,7 +179,7 @@ test_compression_handler_seek(const struct compression_handler *handler,
 
        buffer = buffer_create_dynamic(default_pool, 1024);
        test_output = test_ostream_create(buffer);
-       output = handler->create_ostream(test_output, 1);
+       output = handler->create_ostream_auto(test_output, set.event);
        o_stream_unref(&test_output);
 
        /* write data at once */
@@ -240,7 +243,7 @@ test_compression_handler_reset(const struct compression_handler *handler,
 
        buffer = buffer_create_dynamic(default_pool, 1024);
        test_output = test_ostream_create(buffer);
-       output = handler->create_ostream(test_output, 1);
+       output = handler->create_ostream_auto(test_output, set.event);
        o_stream_unref(&test_output);
 
        /* write data at once */
@@ -301,7 +304,7 @@ test_compression_handler(const struct compression_handler *handler,
        if (fd == -1)
                i_fatal("creat(%s) failed: %m", path);
        file_output = o_stream_create_fd_file(fd, 0, FALSE);
-       output = handler->create_ostream(file_output, 1);
+       output = handler->create_ostream_auto(file_output, set.event);
        sha1_init(&sha1);
 
        /* 1) write lots of easily compressible data */
@@ -390,7 +393,8 @@ test_compression_handler_partial_parent_write(const struct compression_handler *
        buffer_t *buffer = t_buffer_create(64);
        buffer_t *compressed_data = t_buffer_create(256);
        struct ostream *os = test_ostream_create_nonblocking(buffer, 64);
-       struct ostream *os_compressed = handler->create_ostream(os, 9);
+       struct ostream *os_compressed =
+               handler->create_ostream_auto(os, set.event);
        o_stream_unref(&os);
 
        unsigned char input_buffer[64];
@@ -487,7 +491,7 @@ test_compression_handler_random_io(const struct compression_handler *handler,
                                                          i_rand_minmax(1, 512));
 
                /* Create compressor output stream */
-               output2 = handler->create_ostream(output1, i_rand_minmax(1, 6));
+               output2 = handler->create_ostream_auto(output1, set.event);
 
                /* Compress the data incrementally */
                in_pos = out_pos = 0;
@@ -608,7 +612,7 @@ test_compression_handler_large_random_io(const struct compression_handler *handl
        input = i_stream_create_from_data(randomness, RANDOMNESS_SIZE);
 
        temp_output = iostream_temp_create(".temp.", 0);
-       output = handler->create_ostream(temp_output, i_rand_minmax(1, 6));
+       output = handler->create_ostream_auto(temp_output, set.event);
 
        switch (o_stream_send_istream(output, input)) {
        case OSTREAM_SEND_ISTREAM_RESULT_ERROR_INPUT:
@@ -679,7 +683,7 @@ test_compression_handler_errors(const struct compression_handler *handler,
        buffer_t *odata = buffer_create_dynamic(pool_datastack_create(), 65535);
        unsigned char buf[IO_BLOCK_SIZE];
        struct ostream *os = test_ostream_create(odata);
-       struct ostream *output = handler->create_ostream(os, 1);
+       struct ostream *output = handler->create_ostream_auto(os, set.event);
        o_stream_unref(&os);
 
        for (unsigned int i = 0; i < 10; i++) {
@@ -731,7 +735,7 @@ static void test_compression_int(bool autodetect)
 
        for (i = 0; compression_handlers[i].name != NULL; i++) {
                if (compression_handlers[i].create_istream != NULL &&
-                   compression_handlers[i].create_ostream != NULL &&
+                   compression_handlers[i].create_ostream_auto != NULL &&
                    (!autodetect ||
                     compression_handlers[i].is_compressed != NULL)) T_BEGIN {
                        if (compression_handlers[i].is_compressed != NULL &&
@@ -805,13 +809,13 @@ static void test_gz(const char *str1, const char *str2, bool autodetect)
        buf_output = o_stream_create_buffer(buf);
        o_stream_set_finish_via_child(buf_output, FALSE);
 
-       output = gz->create_ostream(buf_output, 6);
+       output = gz->create_ostream_auto(buf_output, set.event);
        o_stream_nsend_str(output, str1);
        test_assert(o_stream_finish(output) > 0);
        o_stream_destroy(&output);
 
        if (str2[0] != '\0') {
-               output = gz->create_ostream(buf_output, 6);
+               output = gz->create_ostream_auto(buf_output, set.event);
                o_stream_nsend_str(output, "world");
                test_assert(o_stream_finish(output) > 0);
                o_stream_destroy(&output);
@@ -1047,7 +1051,7 @@ static void test_compress_file(const char *in_path, const char *out_path)
 
        sha1_init(&sha1);
        file_output = o_stream_create_fd_file(fd_out, 0, FALSE);
-       output = handler->create_ostream(file_output, 1);
+       output = handler->create_ostream_auto(file_output, set.event);
        input = i_stream_create_fd_autoclose(&fd_in, IO_BLOCK_SIZE);
        while (i_stream_read_more(input, &data, &size) > 0) {
                sha1_loop(&sha1, data, size);
@@ -1097,9 +1101,20 @@ static void test_compression_ext(void)
        test_end();
 }
 
+static void test_compression_init(void)
+{
+       settings_simple_init(&set, NULL);
+}
+
+static void test_compression_deinit(void)
+{
+       settings_simple_deinit(&set);
+}
+
 int main(int argc, char *argv[])
 {
        static void (*const test_functions[])(void) = {
+               test_compression_init,
                test_compression,
                test_istream_decompression_try,
                test_gz_concat,
@@ -1108,6 +1123,7 @@ int main(int argc, char *argv[])
                test_gz_large_header,
                test_lz4_small_header,
                test_compression_ext,
+               test_compression_deinit,
                NULL
        };
        if (argc == 2) {
@@ -1115,7 +1131,11 @@ int main(int argc, char *argv[])
                return 0;
        }
        if (argc == 3) {
+               lib_init();
+               settings_simple_init(&set, NULL);
                test_compress_file(argv[1], argv[2]);
+               settings_simple_deinit(&set);
+               lib_deinit();
                return 0;
        }
        return test_run(test_functions);