Update copyright dates with scripts/update-copyrights.
[glibc.git] / posix / bug-regex11.c
blobdd0f97c302a11ddc9e8b7a5d07241a2da58db3b4
1 /* Regular expression tests.
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <sys/types.h>
21 #include <mcheck.h>
22 #include <regex.h>
23 #include <stdio.h>
24 #include <stdlib.h>
26 /* Tests supposed to match. */
27 struct
29 const char *pattern;
30 const char *string;
31 int flags, nmatch;
32 regmatch_t rm[5];
33 } tests[] = {
34 /* Test for newline handling in regex. */
35 { "[^~]*~", "\nx~y", 0, 2, { { 0, 3 }, { -1, -1 } } },
36 /* Other tests. */
37 { "a(.*)b", "a b", REG_EXTENDED, 2, { { 0, 3 }, { 1, 2 } } },
38 { ".*|\\([KIO]\\)\\([^|]*\\).*|?[KIO]", "10~.~|P|K0|I10|O16|?KSb", 0, 3,
39 { { 0, 21 }, { 15, 16 }, { 16, 18 } } },
40 { ".*|\\([KIO]\\)\\([^|]*\\).*|?\\1", "10~.~|P|K0|I10|O16|?KSb", 0, 3,
41 { { 0, 21 }, { 8, 9 }, { 9, 10 } } },
42 { "^\\(a*\\)\\1\\{9\\}\\(a\\{0,9\\}\\)\\([0-9]*;.*[^a]\\2\\([0-9]\\)\\)",
43 "a1;;0a1aa2aaa3aaaa4aaaaa5aaaaaa6aaaaaaa7aaaaaaaa8aaaaaaaaa9aa2aa1a0", 0,
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 } } },
55 /* More tests on backreferences. */
56 { "()\\1", "x", REG_EXTENDED, 2, { { 0, 0 }, { 0, 0 } } },
57 { "()x\\1", "x", REG_EXTENDED, 2, { { 0, 1 }, { 0, 0 } } },
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 } } },
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 } } },
65 { "()(b)\\1c\\2", "bcb", REG_EXTENDED, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } },
66 { "(b())\\2\\1", "bbbb", REG_EXTENDED, 3, { { 0, 2 }, { 0, 1 }, { 1, 1 } } },
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 } } },
70 { "(bb())\\2\\1", "bbbb", REG_EXTENDED, 3, { { 0, 4 }, { 0, 2 }, { 2, 2 } } },
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 } } },
75 { "^(.?)(.?)(.?)(.?)(.?).?\\5\\4\\3\\2\\1$",
76 "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
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 } } },
81 #if 0
82 /* XXX Not used since they fail so far. */
83 { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$|^.?$",
84 "ababababa", REG_EXTENDED, 1, { { 0, 9 } } },
85 { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$",
86 "level", REG_NOSUB | REG_EXTENDED, 0, { { -1, -1 } } },
87 { "^(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?).?\\9\\8\\7\\6\\5\\4\\3\\2\\1$",
88 "ababababa", REG_EXTENDED, 1, { { 0, 9 } } },
89 #endif
92 int
93 main (void)
95 regex_t re;
96 regmatch_t rm[5];
97 size_t i;
98 int n, ret = 0;
100 mtrace ();
102 for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
104 n = regcomp (&re, tests[i].pattern, tests[i].flags);
105 if (n != 0)
107 char buf[500];
108 regerror (n, &re, buf, sizeof (buf));
109 printf ("%s: regcomp %zd failed: %s\n", tests[i].pattern, i, buf);
110 ret = 1;
111 continue;
114 if (regexec (&re, tests[i].string, tests[i].nmatch, rm, 0))
116 printf ("%s: regexec %zd failed\n", tests[i].pattern, i);
117 ret = 1;
118 regfree (&re);
119 continue;
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)
126 if (tests[i].rm[n].rm_so == -1 && tests[i].rm[n].rm_eo == -1)
127 break;
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);
130 ret = 1;
131 break;
134 regfree (&re);
137 return ret;