ircu2.10.12 pk910 fork
[ircu2.10.12-pk.git] / ircd / test / ircd_match_t.c
1 /*
2  * ircd_match_t.c - test cases for irc glob matching
3  */
4
5 #include "ircd_log.h"
6 #include "match.h"
7 #include <stdio.h>
8 #include <string.h>
9
10 struct match_test {
11   const char *glob;
12   const char *should_match;
13   const char *shouldnt_match;
14 };
15
16 const struct match_test match_tests[] = {
17   { "\\*",
18     "*\0",
19     "a\0*PeacefuL*\0" },
20   { "*a*",
21     "a\0pizza\0abe\0brack\0",
22     "b\0" },
23   { "?",
24     "*\0a\0?\0",
25     "*PeacefuL*\0pizza\0???\0" },
26   { "abc",
27     "abc\0",
28     "abcd\0cabc\0" },
29   { "*abc",
30     "abc\0fooabc\0ababc\0",
31     "abra\0abcd\0" },
32   { "\\?",
33     "?\0",
34     "a\0" },
35   { NULL, NULL, NULL }
36 };
37
38 void do_match_test(const struct match_test *test)
39 {
40   const char *candidate;
41   unsigned int matched, not_matched;
42   int res;
43
44   for (candidate = test->should_match, matched = 0;
45        *candidate;
46        candidate += strlen(candidate) + 1, ++matched) {
47     res = match(test->glob, candidate);
48     if (res != 0) {
49       fprintf(stderr, "\"%s\" failed to match \"%s\".\n", test->glob, candidate);
50       assert(0);
51     }
52   }
53
54   for (candidate = test->shouldnt_match, not_matched = 0;
55        *candidate;
56        candidate += strlen(candidate) + 1, ++not_matched) {
57     res = match(test->glob, candidate);
58     if (res == 0) {
59       fprintf(stderr, "\"%s\" incorrectly matched \"%s\".\n", test->glob, candidate);
60       assert(0);
61     }
62   }
63
64   printf("Passed: %s (%u matches, %u non-matches)\n",
65          test->glob, matched, not_matched);
66 }
67
68 int main(int argc, char *argv[])
69 {
70   const struct match_test *match;
71   for (match = match_tests; match->glob; ++match)
72     do_match_test(match);
73   return 0;
74 }