]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Implement filtering of test cases run using glob pattern matching.
authorAndres Mejia <amejia004@gmail.com>
Wed, 19 Sep 2012 20:42:10 +0000 (16:42 -0400)
committerAndres Mejia <amejia004@gmail.com>
Wed, 19 Sep 2012 20:42:10 +0000 (16:42 -0400)
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.

test_utils/test_utils.c

index e8ae5ff7425d686616de24331505221ebbe7f61d..48ab6fbdd57e81be1766900f5c6cff9eb9f5785d 100644 (file)
 #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);