diff options
Diffstat (limited to 'libmailutils/tests/t-streamshift.c')
-rw-r--r-- | libmailutils/tests/t-streamshift.c | 336 |
1 files changed, 176 insertions, 160 deletions
diff --git a/libmailutils/tests/t-streamshift.c b/libmailutils/tests/t-streamshift.c index 2fae0fa51..594916366 100644 --- a/libmailutils/tests/t-streamshift.c +++ b/libmailutils/tests/t-streamshift.c @@ -14,22 +14,22 @@ DESCRIPTION repeated pattern of 256 characters. OPTIONS - -a + -a, --alnum Fill the pattern with alphanumeric characters only. - -b SIZE + -b, --bufsize=SIZE Set buffer size for the shift operation. This option has effect only if the data are shifted in place, i.e. if mailfromd is compiled with mailutils version 3.9.90 or later. - -d + -d, --dump Dump the resulting stream on stdout. - -i FILE + -i, --init-file=FILE Initialize temporary stream from FILE. - -s SIZE + -s, --init-size=SIZE Set initial size for the temporary stream. Default is four times the MU_STREAM_DEFBUFSIZ constant (32768). @@ -63,203 +63,219 @@ LICENSE #include <mailutils/mailutils.h> static unsigned long -strnum(char const *str) +strnum (char const *str) { - char *p; - unsigned long n; - errno = 0; - n = strtol(str, &p, 10); - assert(*p == 0 && errno == 0); - return n; + char *p; + unsigned long n; + errno = 0; + n = strtol (str, &p, 10); + assert (*p == 0 && errno == 0); + return n; } char pattern[256]; static void -gen_pattern(int alnum) +gen_pattern (int alnum) { - int i, c; + int i, c; - for (i = c = 0; i < sizeof(pattern); i++, c++) { - if (alnum) { - while (!mu_isalnum(c)) - c = (c + 1) % sizeof(pattern); - } - pattern[i] = c; + for (i = c = 0; i < sizeof(pattern); i++, c++) + { + if (alnum) + { + while (!mu_isalnum (c)) + c = (c + 1) % sizeof (pattern); } + pattern[i] = c; + } } static void -stream_fill(mu_stream_t str, size_t size) +stream_fill (mu_stream_t str, size_t size) { - while (size) { - size_t n = sizeof(pattern); - if (n > size) - n = size; - MU_ASSERT(mu_stream_write(str, pattern, n, NULL)); - size -= n; - } + while (size) + { + size_t n = sizeof (pattern); + if (n > size) + n = size; + MU_ASSERT (mu_stream_write (str, pattern, n, NULL)); + size -= n; + } } static void -stream_verify_part(mu_stream_t str, int patstart, mu_off_t off, mu_off_t size) +stream_verify_part (mu_stream_t str, int patstart, mu_off_t off, mu_off_t size) { - char buffer[sizeof(pattern)]; - char pat[sizeof(pattern)]; - int i; - - for (i = 0; i < sizeof(pat); i++) { - pat[i] = pattern[patstart]; - patstart = (patstart + 1) % sizeof(pattern); - } + char buffer[sizeof(pattern)]; + char pat[sizeof(pattern)]; + int i; + + for (i = 0; i < sizeof(pat); i++) + { + pat[i] = pattern[patstart]; + patstart = (patstart + 1) % sizeof (pattern); + } - MU_ASSERT(mu_stream_seek(str, off, MU_SEEK_SET, NULL)); - while (size) { - size_t n = sizeof(buffer); - if (n > size) - n = size; - MU_ASSERT(mu_stream_read(str, buffer, n, NULL)); - if (memcmp(buffer, pat, n)) { - mu_off_t pos; - MU_ASSERT(mu_stream_seek(str, 0, MU_SEEK_CUR, &pos)); - fprintf(stderr, "%lu: chunk differs\n", pos - n); - exit(1); - } - size -= n; + MU_ASSERT (mu_stream_seek (str, off, MU_SEEK_SET, NULL)); + while (size) + { + size_t n = sizeof (buffer); + if (n > size) + n = size; + MU_ASSERT (mu_stream_read (str, buffer, n, NULL)); + if (memcmp (buffer, pat, n)) + { + mu_off_t pos; + MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_CUR, &pos)); + fprintf (stderr, "%lu: chunk differs\n", pos - n); + exit (1); } + size -= n; + } } static void -stream_verify(mu_stream_t str, mu_off_t isize, mu_off_t a, mu_off_t b) +stream_verify (mu_stream_t str, mu_off_t isize, mu_off_t a, mu_off_t b) { - mu_off_t size; + mu_off_t size; - MU_ASSERT(mu_stream_seek(str, 0, MU_SEEK_END, &size)); - if (size != isize + a - b) { - fprintf(stderr, - "actual and expected sizes differ: %lu != %lu\n", - (unsigned long) size, - (unsigned long) (isize + a - b)); - exit(1); - } + MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_END, &size)); + if (size != isize + a - b) + { + fprintf (stderr, + "actual and expected sizes differ: %lu != %lu\n", + (unsigned long) size, + (unsigned long) (isize + a - b)); + exit (1); + } - stream_verify_part(str, 0, 0, a < b ? a : b); - stream_verify_part(str, b % sizeof(pattern), a, size); + stream_verify_part (str, 0, 0, a < b ? a : b); + stream_verify_part (str, b % sizeof (pattern), a, size); } static void -file_verify_part(mu_stream_t str, mu_stream_t orig, mu_off_t str_off, - mu_off_t orig_off, mu_off_t size) +file_verify_part (mu_stream_t str, mu_stream_t orig, mu_off_t str_off, + mu_off_t orig_off, mu_off_t size) { - char str_buf[sizeof(pattern)]; - char orig_buf[sizeof(pattern)]; + char str_buf[sizeof (pattern)]; + char orig_buf[sizeof (pattern)]; - MU_ASSERT(mu_stream_seek(str, str_off, MU_SEEK_SET, NULL)); - MU_ASSERT(mu_stream_seek(orig, orig_off, MU_SEEK_SET, NULL)); - while (size) { - size_t n = sizeof(str_buf); - if (n > size) - n = size; - MU_ASSERT(mu_stream_read(str, str_buf, n, NULL)); - MU_ASSERT(mu_stream_read(orig, orig_buf, n, NULL)); - if (memcmp(str_buf, orig_buf, n)) { - mu_off_t pos; - MU_ASSERT(mu_stream_seek(str, 0, MU_SEEK_CUR, &pos)); - fprintf(stderr, "%lu: chunk differs\n", pos - n); - exit(1); - } - size -= n; + MU_ASSERT (mu_stream_seek (str, str_off, MU_SEEK_SET, NULL)); + MU_ASSERT (mu_stream_seek (orig, orig_off, MU_SEEK_SET, NULL)); + while (size) + { + size_t n = sizeof (str_buf); + if (n > size) + n = size; + MU_ASSERT (mu_stream_read (str, str_buf, n, NULL)); + MU_ASSERT (mu_stream_read (orig, orig_buf, n, NULL)); + if (memcmp (str_buf, orig_buf, n)) + { + mu_off_t pos; + MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_CUR, &pos)); + fprintf (stderr, "%lu: chunk differs\n", pos - n); + exit (1); } + size -= n; + } } static void -file_verify(mu_stream_t str, mu_stream_t orig, mu_off_t isize, - mu_off_t a, mu_off_t b) +file_verify (mu_stream_t str, mu_stream_t orig, mu_off_t isize, + mu_off_t a, mu_off_t b) { - mu_off_t size; + mu_off_t size; - MU_ASSERT(mu_stream_seek(str, 0, MU_SEEK_END, &size)); - if (size != isize + a - b) { - fprintf(stderr, - "actual and expected sizes differ: %lu != %lu\n", - (unsigned long) size, - (unsigned long) (isize + a - b)); - exit(1); - } - file_verify_part(str, orig, 0, 0, a < b ? a : b); - file_verify_part(str, orig, a, b, size - a); + MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_END, &size)); + if (size != isize + a - b) + { + fprintf (stderr, + "actual and expected sizes differ: %lu != %lu\n", + (unsigned long) size, + (unsigned long) (isize + a - b)); + exit (1); + } + file_verify_part (str, orig, 0, 0, a < b ? a : b); + file_verify_part (str, orig, a, b, size - a); } int -main(int argc, char **argv) +main (int argc, char **argv) { - int ascii_mode = 0; - size_t bs = 0; - mu_off_t from_off; - mu_off_t to_off; - mu_off_t init_size = 0; - char *init_file = NULL; - int dump_opt = 0; - mu_stream_t temp; - mu_stream_t str; - int c; + int ascii_mode = 0; + size_t bs = 0; + mu_off_t from_off; + mu_off_t to_off; + mu_off_t init_size = 0; + char *init_file = NULL; + int dump_opt = 0; + mu_stream_t temp; + mu_stream_t str; - while ((c = getopt(argc, argv, "ab:di:s:")) != EOF) { - switch (c) { - case 'a': - ascii_mode = 1; - break; - case 'b': - bs = strnum(optarg); - break; - case 'd': - dump_opt = 1; - break; - case 'i': - init_file = optarg; - break; - case 's': - init_size = strnum(optarg); - break; - default: - exit(2); - } - } + struct mu_option options[] = { + { "alnum", 'a', NULL, MU_OPTION_DEFAULT, + "fill the pattern with alphanumeric characters only", + mu_c_incr, &ascii_mode }, + { "bufsize", 'b', "N", MU_OPTION_DEFAULT, + "size of the buffer for shift operations", mu_c_size, &bs }, + { "dump", 'd', NULL, MU_OPTION_DEFAULT, + "dump the resulting stream on the stdout at the end of the run", + mu_c_incr, &dump_opt }, + { "init-file", 'i', "FILE", MU_OPTION_DEFAULT, + "initialize source stream from FILE", + mu_c_string, &init_file }, + { "init-size", 's', "N", MU_OPTION_DEFAULT, + "initial size of the stream", mu_c_off, &init_size }, + MU_OPTION_END + }; - if (argc != optind + 2) { - fprintf(stderr, - "usage: %s [-ad] [-b SIZE] [-i FILE] [-s SIZE] " - "-f OFF -t OFF\n", - argv[0]); - exit(2); - } + mu_set_program_name (argv[0]); + mu_cli_simple (argc, argv, + MU_CLI_OPTION_OPTIONS, options, + MU_CLI_OPTION_PROG_DOC, "mu_stream_shift test", + MU_CLI_OPTION_PROG_ARGS, "FROM_OFF TO_OFF", + MU_CLI_OPTION_EX_USAGE, 2, + MU_CLI_OPTION_RETURN_ARGC, &argc, + MU_CLI_OPTION_RETURN_ARGV, &argv, + MU_CLI_OPTION_END); - from_off = strnum(argv[optind]); - to_off = strnum(argv[optind+1]); - - MU_ASSERT(mu_temp_file_stream_create(&temp, NULL, 0)); - if (init_file) { - MU_ASSERT(mu_file_stream_create(&str, init_file, - MU_STREAM_READ)); - MU_ASSERT(mu_stream_copy(temp, str, 0, NULL)); - if (init_size) - MU_ASSERT(mu_stream_truncate(temp, init_size)); - } else { - gen_pattern(ascii_mode); - if (!init_size) - init_size = 4 * MU_STREAM_DEFBUFSIZ; - stream_fill(temp, init_size); - } - MU_ASSERT(mu_stream_seek(temp, 0, MU_SEEK_CUR, &init_size)); + if (argc != 2) + { + mu_error ("expected exactly two arguments; try %s --help for assistance", + mu_program_name); + exit(2); + } - MU_ASSERT(mu_stream_shift(temp, to_off, from_off, bs)); - if (init_file) - file_verify(temp, str, init_size, to_off, from_off); - else - stream_verify(temp, init_size, to_off, from_off); - if (dump_opt) { - MU_ASSERT(mu_stream_seek(temp, 0, MU_SEEK_SET, NULL)); - MU_ASSERT(mu_stream_copy(mu_strout, temp, 0, NULL)); - } - return 0; + from_off = strnum (argv[0]); + to_off = strnum (argv[1]); + + MU_ASSERT (mu_temp_file_stream_create (&temp, NULL, 0)); + if (init_file) + { + MU_ASSERT (mu_file_stream_create (&str, init_file, MU_STREAM_READ)); + MU_ASSERT (mu_stream_copy (temp, str, 0, NULL)); + if (init_size) + MU_ASSERT (mu_stream_truncate (temp, init_size)); + } + else + { + gen_pattern (ascii_mode); + if (!init_size) + init_size = 4 * MU_STREAM_DEFBUFSIZ; + stream_fill (temp, init_size); + } + MU_ASSERT (mu_stream_seek (temp, 0, MU_SEEK_CUR, &init_size)); + + MU_ASSERT (mu_stream_shift (temp, to_off, from_off, bs)); + if (init_file) + file_verify (temp, str, init_size, to_off, from_off); + else + stream_verify (temp, init_size, to_off, from_off); + if (dump_opt) + { + MU_ASSERT (mu_stream_seek (temp, 0, MU_SEEK_SET, NULL)); + MU_ASSERT (mu_stream_copy (mu_strout, temp, 0, NULL)); + } + return 0; } |