1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21: #include <sys/types.h>
22: #include <mcheck.h>
23: #include <regex.h>
24: #include <stdio.h>
25: #include <stdlib.h>
26:
27:
28: struct
29: {
30: const char *pattern;
31: const char *string;
32: int flags, nmatch;
33: regmatch_t rm[5];
34: } tests[] = {
35:
36: { "[^~]*~", "\nx~y", 0, 2, { { 0, 3 }, { -1, -1 } } },
37:
38: { "a(.*)b", "a b", REG_EXTENDED, 2, { { 0, 3 }, { 1, 2 } } },
39: { ".*|\\([KIO]\\)\\([^|]*\\).*|?[KIO]", "10~.~|P|K0|I10|O16|?KSb", 0, 3,
40: { { 0, 21 }, { 15, 16 }, { 16, 18 } } },
41: { ".*|\\([KIO]\\)\\([^|]*\\).*|?\\1", "10~.~|P|K0|I10|O16|?KSb", 0, 3,
42: { { 0, 21 }, { 8, 9 }, { 9, 10 } } },
43: { "^\\(a*\\)\\1\\{9\\}\\(a\\{0,9\\}\\)\\([0-9]*;.*[^a]\\2\\([0-9]\\)\\)",
44: "a1;;0a1aa2aaa3aaaa4aaaaa5aaaaaa6aaaaaaa7aaaaaaaa8aaaaaaaaa9aa2aa1a0", 0,
45: 5, { { 0, 67 }, { 0, 0 }, { 0, 1 }, { 1, 67 }, { 66, 67 } } },
46:
47:
48: { "\\(^\\|foo\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
49: { "\\(foo\\|^\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
50:
51: { "(^|foo)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } },
52: { "(foo|^)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } },
53:
54: { "(^|foo)bar", "(^|foo)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
55: { "(foo|^)bar", "(foo|^)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
56:
57: { "()\\1", "x", REG_EXTENDED, 2, { { 0, 0 }, { 0, 0 } } },
58: { "()x\\1", "x", REG_EXTENDED, 2, { { 0, 1 }, { 0, 0 } } },
59: { "()\\1*\\1*", "", REG_EXTENDED, 2, { { 0, 0 }, { 0, 0 } } },
60: { "([0-9]).*\\1(a*)", "7;7a6", REG_EXTENDED, 3, { { 0, 4 }, { 0, 1 }, { 3, 4 } } },
61: { "([0-9]).*\\1(a*)", "7;7a", REG_EXTENDED, 3, { { 0, 4 }, { 0, 1 }, { 3, 4 } } },
62: { "(b)()c\\1", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 1 }, { 1, 1 } } },
63: { "()(b)c\\2", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } },
64: { "a(b)()c\\1", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 2 }, { 2, 2 } } },
65: { "a()(b)c\\2", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 1 }, { 1, 2 } } },
66: { "()(b)\\1c\\2", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } },
67: { "(b())\\2\\1", "bbbb", REG_EXTENDED, 3, { { 0, 2 }, { 0, 1 }, { 1, 1 } } },
68: { "a()(b)\\1c\\2", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 1 }, { 1, 2 } } },
69: { "a()d(b)\\1c\\2", "adbcb", REG_EXTENDED, 3, { { 0, 5 }, { 1, 1 }, { 2, 3 } } },
70: { "a(b())\\2\\1", "abbbb", REG_EXTENDED, 3, { { 0, 3 }, { 1, 2 }, { 2, 2 } } },
71: { "(bb())\\2\\1", "bbbb", REG_EXTENDED, 3, { { 0, 4 }, { 0, 2 }, { 2, 2 } } },
72: { "^([^,]*),\\1,\\1$", "a,a,a", REG_EXTENDED, 2, { { 0, 5 }, { 0, 1 } } },
73: { "^([^,]*),\\1,\\1$", "ab,ab,ab", REG_EXTENDED, 2, { { 0, 8 }, { 0, 2 } } },
74: { "^([^,]*),\\1,\\1,\\1$", "abc,abc,abc,abc", REG_EXTENDED, 2,
75: { { 0, 15 }, { 0, 3 } } },
76: { "^(.?)(.?)(.?)(.?)(.?).?\\5\\4\\3\\2\\1$",
77: "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
78: { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
79: "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
80: { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
81: "abcdedcba", REG_EXTENDED, 1, { { 0, 9 } } },
82: #if 0
83:
84: { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
85: "ababababa", REG_EXTENDED, 1, { { 0, 9 } } },
86: { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$",
87: "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
88: { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$",
89: "ababababa", REG_EXTENDED, 1, { { 0, 9 } } },
90: #endif
91: };
92:
93: int
94: main (void)
95: {
96: regex_t re;
97: regmatch_t rm[5];
98: size_t i;
99: int n, ret = 0;
100:
101: mtrace ();
102:
103: for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
104: {
105: n = regcomp (&re, tests[i].pattern, tests[i].flags);
106: if (n != 0)
107: {
108: char buf[500];
109: regerror (n, &re, buf, sizeof (buf));
110: printf ("%s: regcomp %zd failed: %s\n", tests[i].pattern, i, buf);
111: ret = 1;
112: continue;
113: }
114:
115: if (regexec (&re, tests[i].string, tests[i].nmatch, rm, 0))
116: {
117: printf ("%s: regexec %zd failed\n", tests[i].pattern, i);
118: ret = 1;
119: regfree (&re);
120: continue;
121: }
122:
123: for (n = 0; n < tests[i].nmatch; ++n)
124: if (rm[n].rm_so != tests[i].rm[n].rm_so
125: || rm[n].rm_eo != tests[i].rm[n].rm_eo)
126: {
127: if (tests[i].rm[n].rm_so == -1 && tests[i].rm[n].rm_eo == -1)
128: break;
129: printf ("%s: regexec %zd match failure rm[%d] %d..%d\n",
130: tests[i].pattern, i, n, rm[n].rm_so, rm[n].rm_eo);
131: ret = 1;
132: break;
133: }
134:
135: regfree (&re);
136: }
137:
138: return ret;
139: }