* nss_files/files-XXX.c (_nss_files_getXXent_r): Restore saved errno.
[glibc.git] / posix / bug-regex11.c
blobfdc78f94129c84339b2a3662f0bd3f3edad6d9c5
1 /* Regular expression tests.
2 Copyright (C) 2002, 2003 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <sys/types.h>
22 #include <mcheck.h>
23 #include <regex.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 /* Tests supposed to match. */
28 struct
30 const char *pattern;
31 const char *string;
32 int flags, nmatch;
33 regmatch_t rm[5];
34 } tests[] = {
35 /* Test for newline handling in regex. */
36 { "[^~]*~", "\nx~y", 0, 2, { { 0, 3 }, { -1, -1 } } },
37 /* Other tests. */
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 /* Test for BRE expression anchoring. POSIX says just that this may match;
47 in glibc regex it always matched, so avoid changing it. */
48 { "\\(^\\|foo\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
49 { "\\(foo\\|^\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
50 /* In ERE this must be treated as an anchor. */
51 { "(^|foo)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } },
52 { "(foo|^)bar", "bar", REG_EXTENDED, 2, { { 0, 3 }, { -1, -1 } } },
53 /* Here ^ cannot be treated as an anchor according to POSIX. */
54 { "(^|foo)bar", "(^|foo)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
55 { "(foo|^)bar", "(foo|^)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
56 /* More tests on backreferences. */
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 /* XXX Not used since they fail so far. */
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
93 int
94 main (void)
96 regex_t re;
97 regmatch_t rm[5];
98 size_t i;
99 int n, ret = 0;
101 mtrace ();
103 for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
105 n = regcomp (&re, tests[i].pattern, tests[i].flags);
106 if (n != 0)
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;
115 if (regexec (&re, tests[i].string, tests[i].nmatch, rm, 0))
117 printf ("%s: regexec %zd failed\n", tests[i].pattern, i);
118 ret = 1;
119 regfree (&re);
120 continue;
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)
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;
135 regfree (&re);
138 return ret;