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., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <sys/types.h>
31 /* Tests supposed to match. */
39 /* Test for newline handling in regex. */
40 { "[^~]*~", "\nx~y", 0, 2, { { 0, 3 }, { -1, -1 } } },
42 { "a(.*)b", "a b", REG_EXTENDED
, 2, { { 0, 3 }, { 1, 2 } } },
43 { ".*|\\([KIO]\\)\\([^|]*\\).*|?[KIO]", "10~.~|P|K0|I10|O16|?KSb", 0, 3,
44 { { 0, 21 }, { 15, 16 }, { 16, 18 } } },
45 { ".*|\\([KIO]\\)\\([^|]*\\).*|?\\1", "10~.~|P|K0|I10|O16|?KSb", 0, 3,
46 { { 0, 21 }, { 8, 9 }, { 9, 10 } } },
47 { "^\\(a*\\)\\1\\{9\\}\\(a\\{0,9\\}\\)\\([0-9]*;.*[^a]\\2\\([0-9]\\)\\)",
48 "a1;;0a1aa2aaa3aaaa4aaaaa5aaaaaa6aaaaaaa7aaaaaaaa8aaaaaaaaa9aa2aa1a0", 0,
49 5, { { 0, 67 }, { 0, 0 }, { 0, 1 }, { 1, 67 }, { 66, 67 } } },
50 /* Test for BRE expression anchoring. POSIX says just that this may match;
51 in glibc regex it always matched, so avoid changing it. */
52 { "\\(^\\|foo\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
53 { "\\(foo\\|^\\)bar", "bar", 0, 2, { { 0, 3 }, { -1, -1 } } },
54 /* In ERE this must be treated as an anchor. */
55 { "(^|foo)bar", "bar", REG_EXTENDED
, 2, { { 0, 3 }, { -1, -1 } } },
56 { "(foo|^)bar", "bar", REG_EXTENDED
, 2, { { 0, 3 }, { -1, -1 } } },
57 /* Here ^ cannot be treated as an anchor according to POSIX. */
58 { "(^|foo)bar", "(^|foo)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
59 { "(foo|^)bar", "(foo|^)bar", 0, 2, { { 0, 10 }, { -1, -1 } } },
60 /* More tests on backreferences. */
61 { "()\\1", "x", REG_EXTENDED
, 2, { { 0, 0 }, { 0, 0 } } },
62 { "()x\\1", "x", REG_EXTENDED
, 2, { { 0, 1 }, { 0, 0 } } },
63 { "()\\1*\\1*", "", REG_EXTENDED
, 2, { { 0, 0 }, { 0, 0 } } },
64 { "([0-9]).*\\1(a*)", "7;7a6", REG_EXTENDED
, 3, { { 0, 4 }, { 0, 1 }, { 3, 4 } } },
65 { "([0-9]).*\\1(a*)", "7;7a", REG_EXTENDED
, 3, { { 0, 4 }, { 0, 1 }, { 3, 4 } } },
66 { "(b)()c\\1", "bcb", REG_EXTENDED
, 3, { { 0, 3 }, { 0, 1 }, { 1, 1 } } },
67 { "()(b)c\\2", "bcb", REG_EXTENDED
, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } },
68 { "a(b)()c\\1", "abcb", REG_EXTENDED
, 3, { { 0, 4 }, { 1, 2 }, { 2, 2 } } },
69 { "a()(b)c\\2", "abcb", REG_EXTENDED
, 3, { { 0, 4 }, { 1, 1 }, { 1, 2 } } },
70 { "()(b)\\1c\\2", "bcb", REG_EXTENDED
, 3, { { 0, 3 }, { 0, 0 }, { 0, 1 } } },
71 { "(b())\\2\\1", "bbbb", REG_EXTENDED
, 3, { { 0, 2 }, { 0, 1 }, { 1, 1 } } },
72 { "a()(b)\\1c\\2", "abcb", REG_EXTENDED
, 3, { { 0, 4 }, { 1, 1 }, { 1, 2 } } },
73 { "a()d(b)\\1c\\2", "adbcb", REG_EXTENDED
, 3, { { 0, 5 }, { 1, 1 }, { 2, 3 } } },
74 { "a(b())\\2\\1", "abbbb", REG_EXTENDED
, 3, { { 0, 3 }, { 1, 2 }, { 2, 2 } } },
75 { "(bb())\\2\\1", "bbbb", REG_EXTENDED
, 3, { { 0, 4 }, { 0, 2 }, { 2, 2 } } },
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 } } },
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 } } },
105 for (i
= 0; i
< sizeof (tests
) / sizeof (tests
[0]); ++i
)
107 n
= regcomp (&re
, tests
[i
].pattern
, tests
[i
].flags
);
111 regerror (n
, &re
, buf
, sizeof (buf
));
112 printf ("%s: regcomp %lu failed: %s\n", tests
[i
].pattern
, i
, buf
);
117 if (regexec (&re
, tests
[i
].string
, tests
[i
].nmatch
, rm
, 0))
119 printf ("%s: regexec %lu failed\n", tests
[i
].pattern
, i
);
125 for (n
= 0; n
< tests
[i
].nmatch
; ++n
)
126 if (rm
[n
].rm_so
!= tests
[i
].rm
[n
].rm_so
127 || rm
[n
].rm_eo
!= tests
[i
].rm
[n
].rm_eo
)
129 if (tests
[i
].rm
[n
].rm_so
== -1 && tests
[i
].rm
[n
].rm_eo
== -1)
131 printf ("%s: regexec %lu match failure rm[%d] %d..%d\n",
132 tests
[i
].pattern
, i
, n
, rm
[n
].rm_so
, rm
[n
].rm_eo
);