summaryrefslogtreecommitdiffstats
path: root/gnu/lib/libregex/test/psx-basic.c
blob: 52535b6b30761f4c6089dc77357f6a66e503beeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* psx-basic.c: Test POSIX basic regular expressions.  */

#include "test.h"


void
test_posix_basic ()
{
  /* Intervals can only match up to RE_DUP_MAX occurences of anything.  */
  char dup_max_plus_one[6];
  sprintf (dup_max_plus_one, "%d", RE_DUP_MAX + 1);
  
  printf ("\nStarting POSIX basic tests.\n");
  t = posix_basic_test;

  re_set_syntax (RE_SYNTAX_POSIX_MINIMAL_BASIC);  
  
  test_posix_generic ();

  printf ("\nContinuing POSIX basic tests.\n");

/* Grouping tests that are not the same.  */
  
  test_should_match = false;
  invalid_pattern (REG_EPAREN, PARENS_TO_OPS ("a)"));

  test_should_match = true;
						/* Special characters.  */
  MATCH_SELF ("*");
  test_match ("\\(*\\)", "*");
  test_match ("\\(^*\\)", "*");
  test_match ("**", "***");
  test_match ("***", "****");
  
  MATCH_SELF ("{");					/* of extended...  */
  MATCH_SELF ("()");					/* also non-Posix.  */
  MATCH_SELF ("a+");
  MATCH_SELF ("a?");
  MATCH_SELF ("a|b");
  MATCH_SELF ("a|");					/* No alternations, */
  MATCH_SELF ("|a");					/* so OK if empty.  */
  MATCH_SELF ("a||");
  test_match ("\\(|a\\)", "|a");
  test_match ("\\(a|\\)", "a|");
  test_match ("a\\+", "a+");
  test_match ("a\\?", "a?");
  test_match ("a\\|b", "a|b");
  test_match ("^*", "*");
  test_match ("^+", "+");
  test_match ("^?", "?");
  test_match ("^{", "{");
                                              /* Valid subexpressions
                                                 (empty) in basic only.  */
  test_match ("\\(\\)", "");

  test_match ("a\\(\\)", "a");
  test_match ("\\(\\)b", "b");
  test_match ("a\\(\\)b", "ab");
  TEST_REGISTERS ("a\\(\\)b", "ab", 0, 2, 1, 1, -1, -1);

  test_match ("\\(\\)*", "");
  test_match ("\\(\\(\\)\\)*", "");
                                                /* Valid back references.  */

  /* N.B.: back references to subexpressions that include a * are
     undefined in the spec.  The tests are in here to see if we handle
     the situation consistently, but if it fails any of them, it doesn't
     matter.  */

  test_match ("\\(\\)\\1", "");
  TEST_REGISTERS ("\\(\\)\\1", "", 0, 0, 0, 0, -1, -1);

  test_match ("\\(\\(\\)\\)\\(\\)\\2", "");

  test_match ("\\(a\\)\\1", "aa");
  TEST_REGISTERS ("\\(a\\)\\1", "aa", 0, 2, 0, 1, -1, -1);
  TEST_REGISTERS ("\\(a\\)\\1", "xaax", 1, 3, 1, 2, -1, -1);

  test_match ("\\(\\(a\\)\\)\\1", "aa");
  test_match ("\\(a\\)\\(b\\)\\2\\1", "abba");

  test_match ("\\(a\\)*\\1", "aa");
  TEST_REGISTERS ("\\(a\\)*\\1", "aa", 0, 2, 0, 1, -1, -1);
  TEST_REGISTERS ("\\(a\\)*\\1", "xaax", 0, 0, -1, -1, -1, -1);
  
  test_match ("\\(\\(a\\)\\2b\\)*", "aab");
  TEST_REGISTERS ("\\(\\(a\\)\\2b\\)*", "aab", 0, 3, 0, 3, 0, 1);
  TEST_REGISTERS ("\\(\\(a\\)\\2b\\)*", "xaabx", 0, 0, -1, -1, -1, -1);
  
  test_match ("\\(a*\\)*\\1", "");
  test_match ("\\(a*\\)*\\1", "aa");
  TEST_REGISTERS ("\\(a*\\)*\\1", "aa", 0, 2, 0, 1, -1, -1);
  TEST_REGISTERS ("\\(a*\\)*\\1", "xaax", 0, 0, 0, 0, -1, -1);
  
  test_match ("\\(a*\\)*\\1", ""); 
  test_match ("\\(a*\\)*\\1", "aa"); 
  test_match ("\\(\\(a*\\)*\\)*\\1", "aa"); 
  test_match ("\\(ab*\\)*\\1", "abab");
  TEST_REGISTERS ("\\(ab*\\)*\\1", "abab", 0, 4, 0, 2, -1, -1);
  TEST_REGISTERS ("\\(ab*\\)*\\1", "xababx", 0, 0, -1, -1, -1, -1);

  test_match ("\\(a*\\)ab\\1", "aaba"); 
  TEST_REGISTERS ("\\(a*\\)ab\\1", "aaba", 0, 4, 0, 1, -1, -1); 
  TEST_REGISTERS ("\\(a*\\)ab\\1", "xaabax", 1, 5, 1, 2, -1, -1); 

  test_match ("\\(a*\\)*ab\\1", "aaba"); 
  TEST_REGISTERS ("\\(a*\\)*ab\\1", "aaba", 0, 4, 0, 1, -1, -1); 
  TEST_REGISTERS ("\\(a*\\)*ab\\1", "xaabax", 1, 5, 1, 2, -1, -1); 

  test_match ("\\(\\(a*\\)b\\)*\\2", "abb"); 
  TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2", "abb", 0, 3, 2, 3, 2, 2);
  TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2", "xabbx", 0, 0, -1, -1, -1, -1); 

  /* Different from above.  */
  test_match ("\\(\\(a*\\)b*\\)*\\2", "aa"); 
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "aa", 0, 2, 0, 1, 0, 1);
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "xaax", 0, 0, 0, 0, 0, 0); 

  test_match ("\\(\\(a*\\)b*\\)*\\2", "aba"); 
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "aba", 0, 3, 0, 2, 0, 1);
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "xabax", 0, 0, 0, 0, 0, 0); 

  test_match ("\\(\\(a*\\)b\\)*\\2", "aababa");
  TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2", "aababa", 0, 6, 3, 5, 3, 4); 
  TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2", "xaababax", 0, 0, -1, -1, -1, -1); 

  test_match ("\\(\\(a*\\)b*\\)*\\2", "aabaa"); 
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "aabaa", 0, 5, 0, 3, 0, 2);
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "xaabaax", 0, 0, 0, 0, 0, 0); 

  test_match ("\\(\\(a*\\)b*\\)*\\2", "aabbaa"); 
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "aabbaa", 0, 6, 0, 4, 0, 2);
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "xaabbaax", 0, 0, 0, 0, 0, 0); 

  test_match ("\\(\\(a*\\)b*\\)*\\2", "abaabaa"); 
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "abaabaa", 0, 7, 2, 5, 2, 4);
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2", "xaababaax", 0, 0, 0, 0, 0, 0); 

  test_match ("\\(\\(a*\\)b*\\)*a\\2", "aabaaa"); 
  TEST_REGISTERS ("\\(\\(a*\\)b*a\\)*\\2", "aabaaa", 0, 6, 0, 3, 0, 2);
  TEST_REGISTERS ("\\(\\(a*\\)b*a\\)*\\2", "xaabaax", 0, 0, -1, -1, -1, -1); 

  test_match ("\\(\\(a*\\)b*\\)*\\2a", "aabaaa"); 
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2a", "aabaaa", 0, 6, 0, 3, 0, 2);
  TEST_REGISTERS ("\\(\\(a*\\)b*\\)*\\2a", "xaabaaax", 1, 7, 1, 4, 1, 3); 

  test_match ("\\(\\(a*\\)b\\)*\\2\\1", "abaabaaaab");
  TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2\\1", "abaabaaaab", 0, 10, 2, 5, 2, 4);
  /* We are matching the empty string here.  */
  TEST_REGISTERS ("\\(\\(a*\\)b\\)*\\2\\1", "xabaabaaaabx", 0, 0, -1, -1, -1, -1);

  test_match ("\\(a*b\\)\\1", "abab");
  test_match ("\\(a\\)\\1\\1", "aaa");
  test_match ("\\(a\\(c\\)d\\)\\1\\2", "acdacdc");
  
  test_match ("\\(a\\)\\1*", "aaa");
  TEST_REGISTERS ("\\(a\\)\\1*", "aaa", 0, 3, 0, 1, -1, -1);
  TEST_REGISTERS ("\\(a\\)\\1*", "xaaax", 1, 4, 1, 2, -1, -1);

  test_match ("\\(a\\)\\{1,3\\}b\\1", "aba");
  TEST_REGISTERS ("\\(a\\)\\{1,3\\}b\\1", "aba", 0, 3, 0, 1, -1, -1);
  TEST_REGISTERS ("\\(a\\)\\{1,3\\}b\\1", "xabax", 1, 4, 1, 2, -1, -1);

  test_match ("\\(\\(a\\)\\2\\)*", "aaaa"); /* rms? */
  TEST_REGISTERS ("\\(\\(a*b\\)\\2\\)*", "bbabab", 0, 6, 2, 6, 2, 4); /* rms? */

  test_match ("\\(\\(a\\)\\1\\)*", "a1a1");

  test_match ("\\(\\(a\\)\\2\\)\\1", "aaaa");

  test_match ("\\(\\(a*\\)\\2\\)\\1", "aaaa");
  TEST_REGISTERS ("\\(\\(a*\\)\\2\\)\\1", "aaaa", 0, 4, 0, 2, 0, 1);
  TEST_REGISTERS ("\\(\\(a*\\)\\2\\)\\1", "xaaaax", 0, 0, 0, 0, 0, 0);

  test_match ("\\{1\\}", "{1}");
  test_match ("^\\{1\\}", "{1}");

  test_match ("\\(a\\)\\1\\{1,2\\}", "aaa");
  TEST_REGISTERS ("\\(a\\)\\1\\{1,2\\}", "aaa", 0, 3, 0, 1, -1, -1);
  TEST_REGISTERS ("\\(a\\)\\1\\{1,2\\}", "xaaax", 1, 4, 1, 2, -1, -1);


  /* Per POSIX D11.1 p. 109, leftmost longest match.  */

  test_match (PARENS_TO_OPS ("(.*).*\\1"), "abcabc");


  /* Per POSIX D11.1, p. 125, leftmost longest match.  */
  
  test_match (PARENS_TO_OPS ("(ac*)c*d[ac]*\\1"), "acdacaaa");
  TEST_REGISTERS (PARENS_TO_OPS ("(ac*)c*d[ac]*\\1"), "acdacaaa", 	
    0, 8, 0, 1, -1, -1);

  /* Anchors become ordinary, sometimes.  */
  MATCH_SELF ("a^");
  MATCH_SELF ("$a");		
  MATCH_SELF ("$^");		
  test_fastmap ("$a^", "$", 0, 0);
  test_match ("$^*", "$^^");
  test_match ("\\($^\\)", "$^");
  test_match ("$*", "$$");
  /* xx -- known bug, solution pending test_match ("^^$", "^"); */
  test_match ("$\\{0,\\}", "$$");
  TEST_SEARCH ("^$*", "$$", 0, 2);
  TEST_SEARCH ("^$\\{0,\\}", "$$", 0, 2);
  MATCH_SELF ("2^10");
  MATCH_SELF ("$HOME");
  MATCH_SELF ("$1.35");


  /* Basic regular expressions, continued; these don't match their strings.  */
  test_should_match = false;

  invalid_pattern (REG_EESCAPE, "\\(a\\");
                                                /* Invalid back references.  */
  test_match ("\\(a\\)\\1", "ab");
  test_match ("\\(a\\)\\1\\1", "aab");
  test_match ("\\(a\\)\\(b\\)\\2\\1", "abab");
  test_match ("\\(a\\(c\\)d\\)\\1\\2", "acdc");
  test_match ("\\(a*b\\)\\1", "abaab");
  test_match ("\\(a\\)\\1*", "aaaaaaaaaab");
  test_match ("\\(\\(a\\)\\1\\)*", "aaa");
  invalid_pattern (REG_ESUBREG, "\\1");
  invalid_pattern (REG_ESUBREG, "\\(a\\)\\2");
  test_match ("\\(\\(a\\)\\2\\)*", "abaa");
  test_match ("\\(\\(a\\)\\1\\)*", "a");
  test_match ("\\(\\(a\\)\\2\\)\\1", "abaa");
  test_match ("\\(\\(a*\\)\\2\\)\\1", "abaa");
  						/* Invalid intervals.  */
  invalid_pattern (REG_EBRACE, "a\\{");

  invalid_pattern (REG_BADBR, "a\\{-1");
  invalid_pattern (REG_BADBR, concat ("a\\{", (char *)dup_max_plus_one));
  invalid_pattern (REG_BADBR, concat (concat ("a\\{", (char *)dup_max_plus_one), ","));
  invalid_pattern (REG_BADBR, "a\\{1,0");

  invalid_pattern (REG_EBRACE, "a\\{1");
  invalid_pattern (REG_EBRACE, "a\\{0,");
  invalid_pattern (REG_EBRACE, "a\\{0,1");
  invalid_pattern (REG_EBRACE, "a\\{0,1}");

  printf ("\nFinished POSIX basic tests.\n");
}



/*
Local variables:
make-backup-files: t
version-control: t
trim-versions-without-asking: nil
End:
*/
OpenPOWER on IntegriCloud