cvs: Rebuild without gnuregex library
[dragonfly.git] / gnu / lib / libregex / test / psx-basic.c
1 /* psx-basic.c: Test POSIX basic regular expressions.  */
2
3 #include "test.h"
4
5
6 void
7 test_posix_basic ()
8 {
9   /* Intervals can only match up to RE_DUP_MAX occurences of anything.  */
10   char dup_max_plus_one[6];
11   sprintf (dup_max_plus_one, "%d", RE_DUP_MAX + 1);
12
13   printf ("\nStarting POSIX basic tests.\n");
14   t = posix_basic_test;
15
16   re_set_syntax (RE_SYNTAX_POSIX_MINIMAL_BASIC);
17
18   test_posix_generic ();
19
20   printf ("\nContinuing POSIX basic tests.\n");
21
22 /* Grouping tests that are not the same.  */
23
24   test_should_match = false;
25   invalid_pattern (REG_EPAREN, PARENS_TO_OPS ("a)"));
26
27   test_should_match = true;
28                                                 /* Special characters.  */
29   MATCH_SELF ("*");
30   test_match ("\\(*\\)", "*");
31   test_match ("\\(^*\\)", "*");
32   test_match ("**", "***");
33   test_match ("***", "****");
34
35   MATCH_SELF ("{");                                     /* of extended...  */
36   MATCH_SELF ("()");                                    /* also non-Posix.  */
37   MATCH_SELF ("a+");
38   MATCH_SELF ("a?");
39   MATCH_SELF ("a|b");
40   MATCH_SELF ("a|");                                    /* No alternations, */
41   MATCH_SELF ("|a");                                    /* so OK if empty.  */
42   MATCH_SELF ("a||");
43   test_match ("\\(|a\\)", "|a");
44   test_match ("\\(a|\\)", "a|");
45   test_match ("a\\+", "a+");
46   test_match ("a\\?", "a?");
47   test_match ("a\\|b", "a|b");
48   test_match ("^*", "*");
49   test_match ("^+", "+");
50   test_match ("^?", "?");
51   test_match ("^{", "{");
52                                               /* Valid subexpressions
53                                                  (empty) in basic only.  */
54   test_match ("\\(\\)", "");
55
56   test_match ("a\\(\\)", "a");
57   test_match ("\\(\\)b", "b");
58   test_match ("a\\(\\)b", "ab");
59   TEST_REGISTERS ("a\\(\\)b", "ab", 0, 2, 1, 1, -1, -1);
60
61   test_match ("\\(\\)*", "");
62   test_match ("\\(\\(\\)\\)*", "");
63                                                 /* Valid back references.  */
64
65   /* N.B.: back references to subexpressions that include a * are
66      undefined in the spec.  The tests are in here to see if we handle
67      the situation consistently, but if it fails any of them, it doesn't
68      matter.  */
69
70   test_match ("\\(\\)\\1", "");
71   TEST_REGISTERS ("\\(\\)\\1", "", 0, 0, 0, 0, -1, -1);
72
73   test_match ("\\(\\(\\)\\)\\(\\)\\2", "");
74
75   test_match ("\\(a\\)\\1", "aa");
76   TEST_REGISTERS ("\\(a\\)\\1", "aa", 0, 2, 0, 1, -1, -1);
77   TEST_REGISTERS ("\\(a\\)\\1", "xaax", 1, 3, 1, 2, -1, -1);
78
79   test_match ("\\(\\(a\\)\\)\\1", "aa");
80   test_match ("\\(a\\)\\(b\\)\\2\\1", "abba");
81
82   test_match ("\\(a\\)*\\1", "aa");
83   TEST_REGISTERS ("\\(a\\)*\\1", "aa", 0, 2, 0, 1, -1, -1);
84   TEST_REGISTERS ("\\(a\\)*\\1", "xaax", 0, 0, -1, -1, -1, -1);
85
86   test_match ("\\(\\(a\\)\\2b\\)*", "aab");
87   TEST_REGISTERS ("\\(\\(a\\)\\2b\\)*", "aab", 0, 3, 0, 3, 0, 1);
88   TEST_REGISTERS ("\\(\\(a\\)\\2b\\)*", "xaabx", 0, 0, -1, -1, -1, -1);
89
90   test_match ("\\(a*\\)*\\1", "");
91   test_match ("\\(a*\\)*\\1", "aa");
92   TEST_REGISTERS ("\\(a*\\)*\\1", "aa", 0, 2, 0, 1, -1, -1);
93   TEST_REGISTERS ("\\(a*\\)*\\1", "xaax", 0, 0, 0, 0, -1, -1);
94
95   test_match ("\\(a*\\)*\\1", "");
96   test_match ("\\(a*\\)*\\1", "aa");
97   test_match ("\\(\\(a*\\)*\\)*\\1", "aa");
98   test_match ("\\(ab*\\)*\\1", "abab");
99   TEST_REGISTERS ("\\(ab*\\)*\\1", "abab", 0, 4, 0, 2, -1, -1);
100   TEST_REGISTERS ("\\(ab*\\)*\\1", "xababx", 0, 0, -1, -1, -1, -1);
101
102   test_match ("\\(a*\\)ab\\1", "aaba");
103   TEST_REGISTERS ("\\(a*\\)ab\\1", "aaba", 0, 4, 0, 1, -1, -1);
104   TEST_REGISTERS ("\\(a*\\)ab\\1", "xaabax", 1, 5, 1, 2, -1, -1);
105
106   test_match ("\\(a*\\)*ab\\1", "aaba");
107   TEST_REGISTERS ("\\(a*\\)*ab\\1", "aaba", 0, 4, 0, 1, -1, -1);
108   TEST_REGISTERS ("\\(a*\\)*ab\\1", "xaabax", 1, 5, 1, 2, -1, -1);
109
110   test_match ("\\(\\(a*\\)b\\)*\\2", "abb");
111   TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2", "abb", 0, 3, 2, 3, 2, 2);
112   TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2", "xabbx", 0, 0, -1, -1, -1, -1);
113
114   /* Different from above.  */
115   test_match ("\\(\\(a*\\)b*\\)*\\2", "aa");
116   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "aa", 0, 2, 0, 1, 0, 1);
117   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "xaax", 0, 0, 0, 0, 0, 0);
118
119   test_match ("\\(\\(a*\\)b*\\)*\\2", "aba");
120   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "aba", 0, 3, 0, 2, 0, 1);
121   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "xabax", 0, 0, 0, 0, 0, 0);
122
123   test_match ("\\(\\(a*\\)b\\)*\\2", "aababa");
124   TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2", "aababa", 0, 6, 3, 5, 3, 4);
125   TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2", "xaababax", 0, 0, -1, -1, -1, -1);
126
127   test_match ("\\(\\(a*\\)b*\\)*\\2", "aabaa");
128   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "aabaa", 0, 5, 0, 3, 0, 2);
129   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "xaabaax", 0, 0, 0, 0, 0, 0);
130
131   test_match ("\\(\\(a*\\)b*\\)*\\2", "aabbaa");
132   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "aabbaa", 0, 6, 0, 4, 0, 2);
133   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "xaabbaax", 0, 0, 0, 0, 0, 0);
134
135   test_match ("\\(\\(a*\\)b*\\)*\\2", "abaabaa");
136   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "abaabaa", 0, 7, 2, 5, 2, 4);
137   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "xaababaax", 0, 0, 0, 0, 0, 0);
138
139   test_match ("\\(\\(a*\\)b*\\)*a\\2", "aabaaa");
140   TEST_REGISTERS ("\\(\\(a*\\)b*a\\)*\\2", "aabaaa", 0, 6, 0, 3, 0, 2);
141   TEST_REGISTERS ("\\(\\(a*\\)b*a\\)*\\2", "xaabaax", 0, 0, -1, -1, -1, -1);
142
143   test_match ("\\(\\(a*\\)b*\\)*\\2a", "aabaaa");
144   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2a", "aabaaa", 0, 6, 0, 3, 0, 2);
145   TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2a", "xaabaaax", 1, 7, 1, 4, 1, 3);
146
147   test_match ("\\(\\(a*\\)b\\)*\\2\\1", "abaabaaaab");
148   TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2\\1", "abaabaaaab", 0, 10, 2, 5, 2, 4);
149   /* We are matching the empty string here.  */
150   TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2\\1", "xabaabaaaabx", 0, 0, -1, -1, -1, -1);
151
152   test_match ("\\(a*b\\)\\1", "abab");
153   test_match ("\\(a\\)\\1\\1", "aaa");
154   test_match ("\\(a\\(c\\)d\\)\\1\\2", "acdacdc");
155
156   test_match ("\\(a\\)\\1*", "aaa");
157   TEST_REGISTERS ("\\(a\\)\\1*", "aaa", 0, 3, 0, 1, -1, -1);
158   TEST_REGISTERS ("\\(a\\)\\1*", "xaaax", 1, 4, 1, 2, -1, -1);
159
160   test_match ("\\(a\\)\\{1,3\\}b\\1", "aba");
161   TEST_REGISTERS ("\\(a\\)\\{1,3\\}b\\1", "aba", 0, 3, 0, 1, -1, -1);
162   TEST_REGISTERS ("\\(a\\)\\{1,3\\}b\\1", "xabax", 1, 4, 1, 2, -1, -1);
163
164   test_match ("\\(\\(a\\)\\2\\)*", "aaaa"); /* rms? */
165   TEST_REGISTERS ("\\(\\(a*b\\)\\2\\)*", "bbabab", 0, 6, 2, 6, 2, 4); /* rms? */
166
167   test_match ("\\(\\(a\\)\\1\\)*", "a1a1");
168
169   test_match ("\\(\\(a\\)\\2\\)\\1", "aaaa");
170
171   test_match ("\\(\\(a*\\)\\2\\)\\1", "aaaa");
172   TEST_REGISTERS ("\\(\\(a*\\)\\2\\)\\1", "aaaa", 0, 4, 0, 2, 0, 1);
173   TEST_REGISTERS ("\\(\\(a*\\)\\2\\)\\1", "xaaaax", 0, 0, 0, 0, 0, 0);
174
175   test_match ("\\{1\\}", "{1}");
176   test_match ("^\\{1\\}", "{1}");
177
178   test_match ("\\(a\\)\\1\\{1,2\\}", "aaa");
179   TEST_REGISTERS ("\\(a\\)\\1\\{1,2\\}", "aaa", 0, 3, 0, 1, -1, -1);
180   TEST_REGISTERS ("\\(a\\)\\1\\{1,2\\}", "xaaax", 1, 4, 1, 2, -1, -1);
181
182
183   /* Per POSIX D11.1 p. 109, leftmost longest match.  */
184
185   test_match (PARENS_TO_OPS ("(.*).*\\1"), "abcabc");
186
187
188   /* Per POSIX D11.1, p. 125, leftmost longest match.  */
189
190   test_match (PARENS_TO_OPS ("(ac*)c*d[ac]*\\1"), "acdacaaa");
191   TEST_REGISTERS (PARENS_TO_OPS ("(ac*)c*d[ac]*\\1"), "acdacaaa",
192     0, 8, 0, 1, -1, -1);
193
194   /* Anchors become ordinary, sometimes.  */
195   MATCH_SELF ("a^");
196   MATCH_SELF ("$a");
197   MATCH_SELF ("$^");
198   test_fastmap ("$a^", "$", 0, 0);
199   test_match ("$^*", "$^^");
200   test_match ("\\($^\\)", "$^");
201   test_match ("$*", "$$");
202   /* xx -- known bug, solution pending test_match ("^^$", "^"); */
203   test_match ("$\\{0,\\}", "$$");
204   TEST_SEARCH ("^$*", "$$", 0, 2);
205   TEST_SEARCH ("^$\\{0,\\}", "$$", 0, 2);
206   MATCH_SELF ("2^10");
207   MATCH_SELF ("$HOME");
208   MATCH_SELF ("$1.35");
209
210
211   /* Basic regular expressions, continued; these don't match their strings.  */
212   test_should_match = false;
213
214   invalid_pattern (REG_EESCAPE, "\\(a\\");
215                                                 /* Invalid back references.  */
216   test_match ("\\(a\\)\\1", "ab");
217   test_match ("\\(a\\)\\1\\1", "aab");
218   test_match ("\\(a\\)\\(b\\)\\2\\1", "abab");
219   test_match ("\\(a\\(c\\)d\\)\\1\\2", "acdc");
220   test_match ("\\(a*b\\)\\1", "abaab");
221   test_match ("\\(a\\)\\1*", "aaaaaaaaaab");
222   test_match ("\\(\\(a\\)\\1\\)*", "aaa");
223   invalid_pattern (REG_ESUBREG, "\\1");
224   invalid_pattern (REG_ESUBREG, "\\(a\\)\\2");
225   test_match ("\\(\\(a\\)\\2\\)*", "abaa");
226   test_match ("\\(\\(a\\)\\1\\)*", "a");
227   test_match ("\\(\\(a\\)\\2\\)\\1", "abaa");
228   test_match ("\\(\\(a*\\)\\2\\)\\1", "abaa");
229                                                 /* Invalid intervals.  */
230   invalid_pattern (REG_EBRACE, "a\\{");
231
232   invalid_pattern (REG_BADBR, "a\\{-1");
233   invalid_pattern (REG_BADBR, concat ("a\\{", (char *)dup_max_plus_one));
234   invalid_pattern (REG_BADBR, concat (concat ("a\\{", (char *)dup_max_plus_one), ","));
235   invalid_pattern (REG_BADBR, "a\\{1,0");
236
237   invalid_pattern (REG_EBRACE, "a\\{1");
238   invalid_pattern (REG_EBRACE, "a\\{0,");
239   invalid_pattern (REG_EBRACE, "a\\{0,1");
240   invalid_pattern (REG_EBRACE, "a\\{0,1}");
241
242   printf ("\nFinished POSIX basic tests.\n");
243 }
244
245
246 \f
247 /*
248 Local variables:
249 make-backup-files: t
250 version-control: t
251 trim-versions-without-asking: nil
252 End:
253 */