]>
Commit | Line | Data |
---|---|---|
0d35c242 | 1 | /* Regular expression tests. |
04277e02 | 2 | Copyright (C) 2002-2019 Free Software Foundation, Inc. |
4f38745c RM |
3 | This file is part of the GNU C Library. |
4 | Contributed by Jakub Jelinek <jakub@redhat.com>, 2002. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Lesser General Public | |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
4f38745c RM |
19 | |
20 | #include <sys/types.h> | |
21 | #include <mcheck.h> | |
22 | #include <regex.h> | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | ||
ab635ab2 | 26 | /* Tests supposed to match. */ |
0d35c242 RM |
27 | struct |
28 | { | |
29 | const char *pattern; | |
30 | const char *string; | |
ab635ab2 | 31 | int flags, nmatch; |
d4ece865 | 32 | regmatch_t rm[5]; |
0d35c242 RM |
33 | } tests[] = { |
34 | /* Test for newline handling in regex. */ | |
ab635ab2 | 35 | { "[^~]*~", "\nx~y", 0, 2, { { 0, 3 }, { -1, -1 } } }, |
0d35c242 | 36 | /* Other tests. */ |
d0b96fc4 | 37 | { "a(.*)b", "a b", REG_EXTENDED, 2, { { 0, 3 }, { 1, 2 } } }, |
ab635ab2 | 38 | { ".*|\\([KIO]\\)\\([^|]*\\).*|?[KIO]", "10~.~|P|K0|I10|O16|?KSb", 0, 3, |
0d35c242 | 39 | { { 0, 21 }, { 15, 16 }, { 16, 18 } } }, |
ab635ab2 | 40 | { ".*|\\([KIO]\\)\\([^|]*\\).*|?\\1", "10~.~|P|K0|I10|O16|?KSb", 0, 3, |
d4ece865 UD |
41 | { { 0, 21 }, { 8, 9 }, { 9, 10 } } }, |
42 | { "^\\(a*\\)\\1\\{9\\}\\(a\\{0,9\\}\\)\\([0-9]*;.*[^a]\\2\\([0-9]\\)\\)", | |
43 | "a1;;0a1aa2aaa3aaaa4aaaaa5aaaaaa6aaaaaaa7aaaaaaaa8aaaaaaaaa9aa2aa1a0", 0, | |
134abcb5 UD |
44 | 5, { { 0, 67 }, { 0, 0 }, { 0, 1 }, { 1, 67 }, { 66, 67 } } }, |
45 | /* Test for BRE expression anchoring. POSIX says just that this may match; | |
46 | in glibc regex it always matched, so avoid changing it. */ | |
47 | { "\\(^\\|foo\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } }, | |
48 | { "\\(foo\\|^\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } }, | |
49 | /* In ERE this must be treated as an anchor. */ | |
50 | { "(^|foo)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } }, | |
51 | { "(foo|^)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } }, | |
52 | /* Here ^ cannot be treated as an anchor according to POSIX. */ | |
53 | { "(^|foo)bar", "(^|foo)bar", 0, 2, { { 0, 10 }, { -1, -1 } } }, | |
54 | { "(foo|^)bar", "(foo|^)bar", 0, 2, { { 0, 10 }, { -1, -1 } } }, | |
d0b96fc4 | 55 | /* More tests on backreferences. */ |
97fd3a30 UD |
56 | { "()\\1", "x", REG_EXTENDED, 2, { { 0, 0 }, { 0, 0 } } }, |
57 | { "()x\\1", "x", REG_EXTENDED, 2, { { 0, 1 }, { 0, 0 } } }, | |
d0b96fc4 UD |
58 | { "()\\1*\\1*", "", REG_EXTENDED, 2, { { 0, 0 }, { 0, 0 } } }, |
59 | { "([0-9]).*\\1(a*)", "7;7a6", REG_EXTENDED, 3, { { 0, 4 }, { 0, 1 }, { 3, 4 } } }, | |
60 | { "([0-9]).*\\1(a*)", "7;7a", REG_EXTENDED, 3, { { 0, 4 }, { 0, 1 }, { 3, 4 } } }, | |
97fd3a30 UD |
61 | { "(b)()c\\1", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 1 }, { 1, 1 } } }, |
62 | { "()(b)c\\2", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } }, | |
63 | { "a(b)()c\\1", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 2 }, { 2, 2 } } }, | |
64 | { "a()(b)c\\2", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 1 }, { 1, 2 } } }, | |
97fd3a30 | 65 | { "()(b)\\1c\\2", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } }, |
d0b96fc4 | 66 | { "(b())\\2\\1", "bbbb", REG_EXTENDED, 3, { { 0, 2 }, { 0, 1 }, { 1, 1 } } }, |
97fd3a30 UD |
67 | { "a()(b)\\1c\\2", "abcb", REG_EXTENDED, 3, { { 0, 4 }, { 1, 1 }, { 1, 2 } } }, |
68 | { "a()d(b)\\1c\\2", "adbcb", REG_EXTENDED, 3, { { 0, 5 }, { 1, 1 }, { 2, 3 } } }, | |
69 | { "a(b())\\2\\1", "abbbb", REG_EXTENDED, 3, { { 0, 3 }, { 1, 2 }, { 2, 2 } } }, | |
d0b96fc4 | 70 | { "(bb())\\2\\1", "bbbb", REG_EXTENDED, 3, { { 0, 4 }, { 0, 2 }, { 2, 2 } } }, |
7c1be3ec UD |
71 | { "^([^,]*),\\1,\\1$", "a,a,a", REG_EXTENDED, 2, { { 0, 5 }, { 0, 1 } } }, |
72 | { "^([^,]*),\\1,\\1$", "ab,ab,ab", REG_EXTENDED, 2, { { 0, 8 }, { 0, 2 } } }, | |
73 | { "^([^,]*),\\1,\\1,\\1$", "abc,abc,abc,abc", REG_EXTENDED, 2, | |
74 | { { 0, 15 }, { 0, 3 } } }, | |
46bf9de7 UD |
75 | { "^(.?)(.?)(.?)(.?)(.?).?\\5\\4\\3\\2\\1$", |
76 | "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } }, | |
bb3f4825 UD |
77 | { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$", |
78 | "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } }, | |
79 | { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$", | |
80 | "abcdedcba", REG_EXTENDED, 1, { { 0, 9 } } }, | |
46bf9de7 UD |
81 | #if 0 |
82 | /* XXX Not used since they fail so far. */ | |
bb3f4825 UD |
83 | { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$", |
84 | "ababababa", REG_EXTENDED, 1, { { 0, 9 } } }, | |
3ccd8d27 UD |
85 | { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$", |
86 | "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } }, | |
bb3f4825 UD |
87 | { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$", |
88 | "ababababa", REG_EXTENDED, 1, { { 0, 9 } } }, | |
d0b96fc4 | 89 | #endif |
0d35c242 RM |
90 | }; |
91 | ||
4f38745c RM |
92 | int |
93 | main (void) | |
94 | { | |
95 | regex_t re; | |
d4ece865 | 96 | regmatch_t rm[5]; |
ab635ab2 RM |
97 | size_t i; |
98 | int n, ret = 0; | |
4f38745c RM |
99 | |
100 | mtrace (); | |
101 | ||
0d35c242 | 102 | for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i) |
4f38745c | 103 | { |
ab635ab2 | 104 | n = regcomp (&re, tests[i].pattern, tests[i].flags); |
0d35c242 RM |
105 | if (n != 0) |
106 | { | |
107 | char buf[500]; | |
108 | regerror (n, &re, buf, sizeof (buf)); | |
d0b96fc4 | 109 | printf ("%s: regcomp %zd failed: %s\n", tests[i].pattern, i, buf); |
0d35c242 RM |
110 | ret = 1; |
111 | continue; | |
112 | } | |
4f38745c | 113 | |
0d35c242 RM |
114 | if (regexec (&re, tests[i].string, tests[i].nmatch, rm, 0)) |
115 | { | |
d0b96fc4 | 116 | printf ("%s: regexec %zd failed\n", tests[i].pattern, i); |
0d35c242 RM |
117 | ret = 1; |
118 | regfree (&re); | |
119 | continue; | |
120 | } | |
4f38745c | 121 | |
0d35c242 RM |
122 | for (n = 0; n < tests[i].nmatch; ++n) |
123 | if (rm[n].rm_so != tests[i].rm[n].rm_so | |
124 | || rm[n].rm_eo != tests[i].rm[n].rm_eo) | |
125 | { | |
126 | if (tests[i].rm[n].rm_so == -1 && tests[i].rm[n].rm_eo == -1) | |
127 | break; | |
d0b96fc4 UD |
128 | printf ("%s: regexec %zd match failure rm[%d] %d..%d\n", |
129 | tests[i].pattern, i, n, rm[n].rm_so, rm[n].rm_eo); | |
0d35c242 RM |
130 | ret = 1; |
131 | break; | |
132 | } | |
133 | ||
134 | regfree (&re); | |
135 | } | |
4f38745c | 136 | |
ab635ab2 | 137 | return ret; |
4f38745c | 138 | } |