diff options
author | Tim Kientzle <kientzle@acm.org> | 2022-02-01 19:33:41 -0800 |
---|---|---|
committer | Tim Kientzle <kientzle@acm.org> | 2022-02-01 19:33:41 -0800 |
commit | e93b6c3009982d951dbb27c8dc0d02069273a56a (patch) | |
tree | e9409dbb8dc4ae8b7f08a9077fed77e6c79b19e6 /test_utils/test_main.c | |
parent | 3665c7587d6561f0209da1716f86fbebb9a26778 (diff) | |
download | libarchive-e93b6c3009982d951dbb27c8dc0d02069273a56a.tar.gz |
Reorganize test code a bit
A few guiding principles:
* Each test source file includes ONLY "test.h" to make it easy
to create new tests.
* Each test suite has a "test.h" that includes "test_util/test_common.h"
to get access to all the common testing utility functions.
So "test_common.h" is then responsible for including
any smaller headers that declare specific pieces of
shared test functionality.
I've also pulled some test filtering logic that was _only_ used
in test_main.c into that file, and repurposed "test_utils.[ch]"
for common utility code. (Eventually, a lot of the assertion
helpers currently in "test_main.c" should probably be organized
into one or more source files of their own.)
Diffstat (limited to 'test_utils/test_main.c')
-rw-r--r-- | test_utils/test_main.c | 102 |
1 files changed, 101 insertions, 1 deletions
diff --git a/test_utils/test_main.c b/test_utils/test_main.c index 48ae2628..467e367e 100644 --- a/test_utils/test_main.c +++ b/test_utils/test_main.c @@ -3462,6 +3462,12 @@ assertion_entry_compare_acls(const char *file, int line, * DEFINE_TEST(test_function) * for each test. */ +struct test_list_t +{ + void (*func)(void); + const char *name; + int failures; +}; /* Use "list.h" to declare all of the test functions. */ #undef DEFINE_TEST @@ -3753,6 +3759,100 @@ success: return p; } +/* Filter tests against a glob pattern. Returns non-zero if test matches + * pattern, zero otherwise. A '^' at the beginning of the pattern negates + * the return values (i.e. returns zero for a match, non-zero otherwise. + */ +static int +test_filter(const char *pattern, const char *test) +{ + int retval = 0; + int negate = 0; + const char *p = pattern; + const char *t = test; + + if (p[0] == '^') + { + negate = 1; + p++; + } + + while (1) + { + if (p[0] == '\\') + p++; + else if (p[0] == '*') + { + while (p[0] == '*') + p++; + if (p[0] == '\\') + p++; + if ((t = strchr(t, p[0])) == 0) + break; + } + if (p[0] != t[0]) + break; + if (p[0] == '\0') { + retval = 1; + break; + } + p++; + t++; + } + + return (negate) ? !retval : retval; +} + +static int +get_test_set(int *test_set, int limit, const char *test) +{ + int start, end; + int idx = 0; + + if (test == NULL) { + /* Default: Run all tests. */ + for (;idx < limit; idx++) + test_set[idx] = idx; + return (limit); + } + if (*test >= '0' && *test <= '9') { + const char *vp = test; + start = 0; + while (*vp >= '0' && *vp <= '9') { + start *= 10; + start += *vp - '0'; + ++vp; + } + if (*vp == '\0') { + end = start; + } else if (*vp == '-') { + ++vp; + if (*vp == '\0') { + end = limit - 1; + } else { + end = 0; + while (*vp >= '0' && *vp <= '9') { + end *= 10; + end += *vp - '0'; + ++vp; + } + } + } else + return (-1); + if (start < 0 || end >= limit || start > end) + return (-1); + while (start <= end) + test_set[idx++] = start++; + } else { + for (start = 0; start < limit; ++start) { + const char *name = tests[start].name; + if (test_filter(test, name)) + test_set[idx++] = start; + } + } + return ((idx == 0)?-1:idx); +} + int main(int argc, char **argv) { @@ -4049,7 +4149,7 @@ main(int argc, char **argv) do { int test_num; - test_num = get_test_set(test_set, limit, *argv, tests); + test_num = get_test_set(test_set, limit, *argv); if (test_num < 0) { printf("*** INVALID Test %s\n", *argv); free(refdir_alloc); |