summaryrefslogtreecommitdiff
path: root/test_utils
diff options
context:
space:
mode:
authorTim Kientzle <kientzle@acm.org>2022-02-01 19:33:41 -0800
committerTim Kientzle <kientzle@acm.org>2022-02-01 19:33:41 -0800
commite93b6c3009982d951dbb27c8dc0d02069273a56a (patch)
treee9409dbb8dc4ae8b7f08a9077fed77e6c79b19e6 /test_utils
parent3665c7587d6561f0209da1716f86fbebb9a26778 (diff)
downloadlibarchive-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')
-rw-r--r--test_utils/test_common.h2
-rw-r--r--test_utils/test_main.c102
-rw-r--r--test_utils/test_utils.c96
-rw-r--r--test_utils/test_utils.h10
4 files changed, 105 insertions, 105 deletions
diff --git a/test_utils/test_common.h b/test_utils/test_common.h
index 42119c06..6250235c 100644
--- a/test_utils/test_common.h
+++ b/test_utils/test_common.h
@@ -471,4 +471,6 @@ void assertVersion(const char *prog, const char *base);
#include <dmalloc.h>
#endif
+#include "test_utils.h"
+
#endif /* TEST_COMMON_H */
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);
diff --git a/test_utils/test_utils.c b/test_utils/test_utils.c
index db6c31b2..b7966761 100644
--- a/test_utils/test_utils.c
+++ b/test_utils/test_utils.c
@@ -32,100 +32,6 @@
#include <string.h>
#include <assert.h>
-/* 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;
-}
-
-int get_test_set(int *test_set, int limit, const char *test,
- struct test_list_t *tests)
-{
- 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);
-}
-
static inline uint64_t
xorshift64(uint64_t *state)
{
@@ -146,7 +52,7 @@ xorshift64(uint64_t *state)
* took ~22 seconds, whereas using a xorshift random number generator (that can
* be inlined) reduces it to ~17 seconds on QEMU RISC-V.
*/
-void
+static void
fill_with_pseudorandom_data_seed(uint64_t seed, void *buffer, size_t size)
{
uint64_t *aligned_buffer;
diff --git a/test_utils/test_utils.h b/test_utils/test_utils.h
index 3f61f6b2..41457890 100644
--- a/test_utils/test_utils.h
+++ b/test_utils/test_utils.h
@@ -30,15 +30,7 @@
#include <stddef.h>
#include <stdint.h>
-struct test_list_t
-{
- void (*func)(void);
- const char *name;
- int failures;
-};
-
-int get_test_set(int *, int, const char *, struct test_list_t *);
+/* Fill a buffer with pseudorandom data */
void fill_with_pseudorandom_data(void* buffer, size_t size);
-void fill_with_pseudorandom_data_seed(uint64_t seed, void* buffer, size_t size);
#endif /* TEST_UTILS_H */