diff options
author | David Robillard <d@drobilla.net> | 2022-11-25 09:55:27 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-11-25 10:08:49 -0500 |
commit | 0569a7ad70a82d68548bbe4cf79bcc64d5f9878d (patch) | |
tree | 1eefbd728f33f31b0d270b5c0e76754a6ced7913 | |
parent | 112fa18bfa293629ccb90fbd952aa6e61398da80 (diff) | |
download | zix-0569a7ad70a82d68548bbe4cf79bcc64d5f9878d.tar.gz zix-0569a7ad70a82d68548bbe4cf79bcc64d5f9878d.tar.bz2 zix-0569a7ad70a82d68548bbe4cf79bcc64d5f9878d.zip |
Constrain test parameters to reasonable limits
-rw-r--r-- | test/test_args.h | 18 | ||||
-rw-r--r-- | test/test_btree.c | 4 | ||||
-rw-r--r-- | test/test_ring.c | 6 | ||||
-rw-r--r-- | test/test_tree.c | 11 |
4 files changed, 31 insertions, 8 deletions
diff --git a/test/test_args.h b/test/test_args.h new file mode 100644 index 0000000..e212a94 --- /dev/null +++ b/test/test_args.h @@ -0,0 +1,18 @@ +// Copyright 2022 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#ifndef ZIX_TEST_ARGS_H +#define ZIX_TEST_ARGS_H + +#include <stddef.h> +#include <stdlib.h> + +static inline size_t +zix_test_size_arg(const char* const string, const size_t min, const size_t max) +{ + const size_t size = strtoul(string, NULL, 10); + + return size < min ? min : size > max ? max : size; +} + +#endif // ZIX_TEST_ARGS_H diff --git a/test/test_btree.c b/test/test_btree.c index 259bb2a..a6f724a 100644 --- a/test/test_btree.c +++ b/test/test_btree.c @@ -6,6 +6,7 @@ #include "zix/btree.h" #include "failing_allocator.h" +#include "test_args.h" #include "test_data.h" #include "zix/allocator.h" @@ -628,7 +629,8 @@ main(int argc, char** argv) test_failed_alloc(); const unsigned n_tests = 3U; - const size_t n_elems = (argc > 1) ? strtoul(argv[1], NULL, 10) : 131072U; + const size_t n_elems = + (argc > 1) ? zix_test_size_arg(argv[1], 4U, 1U << 20U) : 131072U; printf("Running %u tests with %" PRIuPTR " elements", n_tests, n_elems); for (unsigned i = 0; i < n_tests; ++i) { diff --git a/test/test_ring.c b/test/test_ring.c index 7209ac0..c61b3a3 100644 --- a/test/test_ring.c +++ b/test/test_ring.c @@ -4,6 +4,7 @@ #undef NDEBUG #include "failing_allocator.h" +#include "test_args.h" #include "zix/attributes.h" #include "zix/ring.h" @@ -185,9 +186,10 @@ main(int argc, char** argv) } const unsigned size = - (argc > 1) ? (unsigned)strtoul(argv[1], NULL, 10) : 1024; + (argc > 1) ? (unsigned)zix_test_size_arg(argv[1], 4U, 1U << 20U) : 1024U; - n_writes = (argc > 2) ? (unsigned)strtoul(argv[2], NULL, 10) : size * 1024; + n_writes = (argc > 2) ? (unsigned)zix_test_size_arg(argv[2], 4U, 1U << 20U) + : size * 1024; test_failed_alloc(); test_ring(size); diff --git a/test/test_tree.c b/test/test_tree.c index fcab59d..700729b 100644 --- a/test/test_tree.c +++ b/test/test_tree.c @@ -4,6 +4,7 @@ #undef NDEBUG #include "failing_allocator.h" +#include "test_args.h" #include "test_data.h" #include "zix/allocator.h" @@ -250,7 +251,7 @@ int main(int argc, char** argv) { const unsigned n_tests = 3; - unsigned n_elems = 0; + size_t n_elems = 0; assert(!zix_tree_iter_next(NULL)); assert(!zix_tree_iter_prev(NULL)); @@ -259,9 +260,9 @@ main(int argc, char** argv) test_failed_alloc(); if (argc == 1) { - n_elems = 100000; + n_elems = 100000U; } else { - n_elems = (unsigned)strtoul(argv[1], NULL, 10); + n_elems = zix_test_size_arg(argv[1], 4U, 1U << 20U); if (argc > 2) { seed = strtoul(argv[2], NULL, 10); } else { @@ -269,13 +270,13 @@ main(int argc, char** argv) } } - if (n_elems == 0) { + if (!n_elems) { fprintf(stderr, "USAGE: %s [N_ELEMS]\n", argv[0]); return 1; } printf( - "Running %u tests with %u elements (seed %zu)", n_tests, n_elems, seed); + "Running %u tests with %zu elements (seed %zu)", n_tests, n_elems, seed); for (unsigned i = 0; i < n_tests; ++i) { printf("."); |