lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include "fnmatch.h" |
| 4 | |
| 5 | struct { |
| 6 | const char *name; |
| 7 | const char *pattern; |
| 8 | int flags; |
| 9 | int expected; |
| 10 | } tests[] = { |
| 11 | { "lib", "*LIB*", FNM_PERIOD, FNM_NOMATCH }, |
| 12 | { "lib", "*LIB*", FNM_CASEFOLD|FNM_PERIOD, 0 }, |
| 13 | { "a/b", "a[/]b", 0, 0 }, |
| 14 | { "a/b", "a[/]b", FNM_PATHNAME, FNM_NOMATCH }, |
| 15 | { "a/b", "[a-z]/[a-z]", 0, 0 }, |
| 16 | { "a/b", "*", FNM_PATHNAME, FNM_NOMATCH }, |
| 17 | { "a/b", "*[/]b", FNM_PATHNAME, FNM_NOMATCH }, |
| 18 | { "a/b", "*[b]", FNM_PATHNAME, FNM_NOMATCH }, |
| 19 | { "a/b", "[*]/b", 0, FNM_NOMATCH }, |
| 20 | { "*/b", "[*]/b", 0, 0 }, |
| 21 | { "a/b", "[?]/b", 0, FNM_NOMATCH }, |
| 22 | { "?/b", "[?]/b", 0, 0 }, |
| 23 | { "a/b", "[[a]/b", 0, 0 }, |
| 24 | { "[/b", "[[a]/b", 0, 0 }, |
| 25 | { "a/b", "\\*/b", 0, FNM_NOMATCH }, |
| 26 | { "*/b", "\\*/b", 0, 0 }, |
| 27 | { "a/b", "\\?/b", 0, FNM_NOMATCH }, |
| 28 | { "?/b", "\\?/b", 0, 0 }, |
| 29 | { "[/b", "[/b", 0, 0 }, |
| 30 | { "[/b", "\\[/b", 0, 0 }, |
| 31 | { "aa/b", "?""?/b", 0, 0 }, |
| 32 | { "aa/b", "?""?""?b", 0, 0 }, |
| 33 | { "aa/b", "?""?""?b", FNM_PATHNAME, FNM_NOMATCH }, |
| 34 | { ".a/b", "?a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, |
| 35 | { "a/.b", "a/?b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, |
| 36 | { ".a/b", "*a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, |
| 37 | { "a/.b", "a/*b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, |
| 38 | { ".a/b", "[.]a/b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, |
| 39 | { "a/.b", "a/[.]b", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, |
| 40 | { "a/b", "*/?", FNM_PATHNAME|FNM_PERIOD, 0 }, |
| 41 | { "a/b", "?/*", FNM_PATHNAME|FNM_PERIOD, 0 }, |
| 42 | { ".a/b", ".*/?", FNM_PATHNAME|FNM_PERIOD, 0 }, |
| 43 | { "a/.b", "*/.?", FNM_PATHNAME|FNM_PERIOD, 0 }, |
| 44 | { "a/.b", "*/*", FNM_PATHNAME|FNM_PERIOD, FNM_NOMATCH }, |
| 45 | { "a/.b", "*?*/*", FNM_PERIOD, 0 }, |
| 46 | { "a./b", "*[.]/b", FNM_PATHNAME|FNM_PERIOD, 0 }, |
| 47 | { "a/b", "*[[:alpha:]]/*[[:alnum:]]", FNM_PATHNAME, 0 }, |
| 48 | { "a/b", "*[![:digit:]]*/[![:d-d]", FNM_PATHNAME, 0 }, |
| 49 | { "a/[", "*[![:digit:]]*/[[:d-d]", FNM_PATHNAME, 0 }, |
| 50 | { "a/[", "*[![:digit:]]*/[![:d-d]", FNM_PATHNAME, FNM_NOMATCH }, |
| 51 | { "a.b", "a?b", FNM_PATHNAME|FNM_PERIOD, 0 }, |
| 52 | { "a.b", "a*b", FNM_PATHNAME|FNM_PERIOD, 0 }, |
| 53 | { "a.b", "a[.]b", FNM_PATHNAME|FNM_PERIOD, 0 }, |
| 54 | { "a/b", "*a*", FNM_PATHNAME|FNM_LEADING_DIR, 0 }, |
| 55 | { "ab/c", "*a?", FNM_PATHNAME|FNM_LEADING_DIR, 0 }, |
| 56 | { "ab/c", "a?", FNM_PATHNAME|FNM_LEADING_DIR, 0 }, |
| 57 | { "a/b", "?*/?", FNM_PATHNAME, 0 }, |
| 58 | { "/b", "*/?", FNM_PATHNAME, 0 }, |
| 59 | { "/b", "**/?", FNM_PATHNAME, 0 }, |
| 60 | }; |
| 61 | |
| 62 | int |
| 63 | main (void) |
| 64 | { |
| 65 | size_t i; |
| 66 | int errors = 0; |
| 67 | |
| 68 | for (i = 0; i < sizeof (tests) / sizeof (*tests); i++) |
| 69 | { |
| 70 | int match; |
| 71 | |
| 72 | match = fnmatch (tests[i].pattern, tests[i].name, tests[i].flags); |
| 73 | |
| 74 | printf ("[%2zd] %s %s %s -> %s\n", i, tests[i].pattern, |
| 75 | match == 0 ? "matches" : "does not match", |
| 76 | tests[i].name, |
| 77 | match != tests[i].expected ? "FAIL" : "OK"); |
| 78 | |
| 79 | if (match != tests[i].expected) |
| 80 | ++errors ; |
| 81 | } |
| 82 | |
| 83 | return errors != 0; |
| 84 | } |