summaryrefslogtreecommitdiff
path: root/test_utils/test_utils.c
diff options
context:
space:
mode:
authorAndres Mejia <amejia004@gmail.com>2012-09-19 16:42:10 -0400
committerAndres Mejia <amejia004@gmail.com>2012-09-19 16:42:10 -0400
commitbb923ab058beeae1fdad105c846b558496a3d9e0 (patch)
tree2264f955b57d5fcb59068da8201ec25a57d19b56 /test_utils/test_utils.c
parent31dfef8b1155239d7ce9c3d251f98471544b01fa (diff)
downloadlibarchive-bb923ab058beeae1fdad105c846b558496a3d9e0.tar.gz
Implement filtering of test cases run using glob pattern matching.
A test case will run if it matches a specified glob pattern. Test cases can also be filtered out by including a '^' at the beginning of a pattern.
Diffstat (limited to 'test_utils/test_utils.c')
-rw-r--r--test_utils/test_utils.c58
1 files changed, 47 insertions, 11 deletions
diff --git a/test_utils/test_utils.c b/test_utils/test_utils.c
index e8ae5ff7..48ab6fbd 100644
--- a/test_utils/test_utils.c
+++ b/test_utils/test_utils.c
@@ -29,6 +29,51 @@
#include <stdlib.h>
#include <string.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++;
+ while (p[0] != t[0])
+ t++;
+ }
+ if (p[0] != t[0])
+ break;
+ if (p[0] == '\0' && t[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)
{
@@ -70,19 +115,10 @@ int get_test_set(int *test_set, int limit, const char *test,
while (start <= end)
test_set[idx++] = start++;
} else {
- size_t len = strlen(test);
for (start = 0; start < limit; ++start) {
const char *name = tests[start].name;
- const char *p;
-
- while ((p = strchr(name, test[0])) != NULL) {
- if (strncmp(p, test, len) == 0) {
- test_set[idx++] = start;
- break;
- } else
- name = p + 1;
- }
-
+ if (test_filter(test, name))
+ test_set[idx++] = start;
}
}
return ((idx == 0)?-1:idx);