]> git.proxmox.com Git - rustc.git/blob - src/vendor/regex-0.2.10/tests/crazy.rs
New upstream version 1.28.0~beta.14+dfsg1
[rustc.git] / src / vendor / regex-0.2.10 / tests / crazy.rs
1 mat!(ascii_literal, r"a", "a", Some((0, 1)));
2
3 // Some crazy expressions from regular-expressions.info.
4 mat!(match_ranges,
5 r"\b(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b",
6 "num: 255", Some((5, 8)));
7 mat!(match_ranges_not,
8 r"\b(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b",
9 "num: 256", None);
10 mat!(match_float1, r"[-+]?[0-9]*\.?[0-9]+", "0.1", Some((0, 3)));
11 mat!(match_float2, r"[-+]?[0-9]*\.?[0-9]+", "0.1.2", Some((0, 3)));
12 mat!(match_float3, r"[-+]?[0-9]*\.?[0-9]+", "a1.2", Some((1, 4)));
13 mat!(match_float4, r"^[-+]?[0-9]*\.?[0-9]+$", "1.a", None);
14 mat!(match_email, r"(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b",
15 "mine is jam.slam@gmail.com ", Some((8, 26)));
16 mat!(match_email_not, r"(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b",
17 "mine is jam.slam@gmail ", None);
18 mat!(match_email_big, r"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
19 "mine is jam.slam@gmail.com ", Some((8, 26)));
20 mat!(match_date1,
21 r"^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$",
22 "1900-01-01", Some((0, 10)));
23 mat!(match_date2,
24 r"^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$",
25 "1900-00-01", None);
26 mat!(match_date3,
27 r"^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$",
28 "1900-13-01", None);
29
30 // Do some crazy dancing with the start/end assertions.
31 matiter!(match_start_end_empty, r"^$", "", (0, 0));
32 matiter!(match_start_end_empty_many_1, r"^$^$^$", "", (0, 0));
33 matiter!(match_start_end_empty_many_2, r"^^^$$$", "", (0, 0));
34 matiter!(match_start_end_empty_rev, r"$^", "", (0, 0));
35 matiter!(match_start_end_empty_rep, r"(?:^$)*", "a\nb\nc",
36 (0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5));
37 matiter!(match_start_end_empty_rep_rev, r"(?:$^)*", "a\nb\nc",
38 (0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5));
39
40 // Test negated character classes.
41 mat!(negclass_letters, r"[^ac]", "acx", Some((2, 3)));
42 mat!(negclass_letter_comma, r"[^a,]", "a,x", Some((2, 3)));
43 mat!(negclass_letter_space, r"[^a\s]", "a x", Some((2, 3)));
44 mat!(negclass_comma, r"[^,]", ",,x", Some((2, 3)));
45 mat!(negclass_space, r"[^\s]", " a", Some((1, 2)));
46 mat!(negclass_space_comma, r"[^,\s]", ", a", Some((2, 3)));
47 mat!(negclass_comma_space, r"[^\s,]", " ,a", Some((2, 3)));
48 mat!(negclass_ascii, r"[^[:alpha:]Z]", "A1", Some((1, 2)));
49
50 // Test that repeated empty expressions don't loop forever.
51 mat!(lazy_many_many, r"((?:.*)*?)=", "a=b", Some((0, 2)));
52 mat!(lazy_many_optional, r"((?:.?)*?)=", "a=b", Some((0, 2)));
53 mat!(lazy_one_many_many, r"((?:.*)+?)=", "a=b", Some((0, 2)));
54 mat!(lazy_one_many_optional, r"((?:.?)+?)=", "a=b", Some((0, 2)));
55 mat!(lazy_range_min_many, r"((?:.*){1,}?)=", "a=b", Some((0, 2)));
56 mat!(lazy_range_many, r"((?:.*){1,2}?)=", "a=b", Some((0, 2)));
57 mat!(greedy_many_many, r"((?:.*)*)=", "a=b", Some((0, 2)));
58 mat!(greedy_many_optional, r"((?:.?)*)=", "a=b", Some((0, 2)));
59 mat!(greedy_one_many_many, r"((?:.*)+)=", "a=b", Some((0, 2)));
60 mat!(greedy_one_many_optional, r"((?:.?)+)=", "a=b", Some((0, 2)));
61 mat!(greedy_range_min_many, r"((?:.*){1,})=", "a=b", Some((0, 2)));
62 mat!(greedy_range_many, r"((?:.*){1,2})=", "a=b", Some((0, 2)));
63
64 // Test that we handle various flavors of empty expressions.
65 matiter!(match_empty1, r"", "", (0, 0));
66 matiter!(match_empty2, r"", "abc", (0, 0), (1, 1), (2, 2), (3, 3));
67 matiter!(match_empty3, r"()", "abc", (0, 0), (1, 1), (2, 2), (3, 3));
68 matiter!(match_empty4, r"()*", "abc", (0, 0), (1, 1), (2, 2), (3, 3));
69 matiter!(match_empty5, r"()+", "abc", (0, 0), (1, 1), (2, 2), (3, 3));
70 matiter!(match_empty6, r"()?", "abc", (0, 0), (1, 1), (2, 2), (3, 3));
71 matiter!(match_empty7, r"()()", "abc", (0, 0), (1, 1), (2, 2), (3, 3));
72 matiter!(match_empty8, r"()+|z", "abc", (0, 0), (1, 1), (2, 2), (3, 3));
73 matiter!(match_empty9, r"z|()+", "abc", (0, 0), (1, 1), (2, 2), (3, 3));
74 matiter!(match_empty10, r"()+|b", "abc", (0, 0), (1, 1), (2, 2), (3, 3));
75 matiter!(match_empty11, r"b|()+", "abc", (0, 0), (1, 2), (3, 3));
76
77 // Test that the DFA can handle pathological cases.
78 // (This should result in the DFA's cache being flushed too frequently, which
79 // should cause it to quit and fall back to the NFA algorithm.)
80 #[test]
81 fn dfa_handles_pathological_case() {
82 fn ones_and_zeroes(count: usize) -> String {
83 use rand::{Rng, thread_rng};
84
85 let mut rng = thread_rng();
86 let mut s = String::new();
87 for _ in 0..count {
88 if rng.gen() {
89 s.push('1');
90 } else {
91 s.push('0');
92 }
93 }
94 s
95 }
96
97 let re = regex!(r"[01]*1[01]{20}$");
98 let text = {
99 let mut pieces = ones_and_zeroes(100_000);
100 pieces.push('1');
101 pieces.push_str(&ones_and_zeroes(20));
102 pieces
103 };
104 assert!(re.is_match(text!(&*text)));
105 }
106
107 #[test]
108 fn nest_limit_makes_it_parse() {
109 use regex::RegexBuilder;
110
111 RegexBuilder::new(
112 r#"
113 2(?:
114 [45]\d{3}|
115 7(?:
116 1[0-267]|
117 2[0-289]|
118 3[0-29]|
119 4[01]|
120 5[1-3]|
121 6[013]|
122 7[0178]|
123 91
124 )|
125 8(?:
126 0[125]|
127 [139][1-6]|
128 2[0157-9]|
129 41|
130 6[1-35]|
131 7[1-5]|
132 8[1-8]|
133 90
134 )|
135 9(?:
136 0[0-2]|
137 1[0-4]|
138 2[568]|
139 3[3-6]|
140 5[5-7]|
141 6[0167]|
142 7[15]|
143 8[0146-9]
144 )
145 )\d{4}|
146 3(?:
147 12?[5-7]\d{2}|
148 0(?:
149 2(?:
150 [025-79]\d|
151 [348]\d{1,2}
152 )|
153 3(?:
154 [2-4]\d|
155 [56]\d?
156 )
157 )|
158 2(?:
159 1\d{2}|
160 2(?:
161 [12]\d|
162 [35]\d{1,2}|
163 4\d?
164 )
165 )|
166 3(?:
167 1\d{2}|
168 2(?:
169 [2356]\d|
170 4\d{1,2}
171 )
172 )|
173 4(?:
174 1\d{2}|
175 2(?:
176 2\d{1,2}|
177 [47]|
178 5\d{2}
179 )
180 )|
181 5(?:
182 1\d{2}|
183 29
184 )|
185 [67]1\d{2}|
186 8(?:
187 1\d{2}|
188 2(?:
189 2\d{2}|
190 3|
191 4\d
192 )
193 )
194 )\d{3}|
195 4(?:
196 0(?:
197 2(?:
198 [09]\d|
199 7
200 )|
201 33\d{2}
202 )|
203 1\d{3}|
204 2(?:
205 1\d{2}|
206 2(?:
207 [25]\d?|
208 [348]\d|
209 [67]\d{1,2}
210 )
211 )|
212 3(?:
213 1\d{2}(?:
214 \d{2}
215 )?|
216 2(?:
217 [045]\d|
218 [236-9]\d{1,2}
219 )|
220 32\d{2}
221 )|
222 4(?:
223 [18]\d{2}|
224 2(?:
225 [2-46]\d{2}|
226 3
227 )|
228 5[25]\d{2}
229 )|
230 5(?:
231 1\d{2}|
232 2(?:
233 3\d|
234 5
235 )
236 )|
237 6(?:
238 [18]\d{2}|
239 2(?:
240 3(?:
241 \d{2}
242 )?|
243 [46]\d{1,2}|
244 5\d{2}|
245 7\d
246 )|
247 5(?:
248 3\d?|
249 4\d|
250 [57]\d{1,2}|
251 6\d{2}|
252 8
253 )
254 )|
255 71\d{2}|
256 8(?:
257 [18]\d{2}|
258 23\d{2}|
259 54\d{2}
260 )|
261 9(?:
262 [18]\d{2}|
263 2[2-5]\d{2}|
264 53\d{1,2}
265 )
266 )\d{3}|
267 5(?:
268 02[03489]\d{2}|
269 1\d{2}|
270 2(?:
271 1\d{2}|
272 2(?:
273 2(?:
274 \d{2}
275 )?|
276 [457]\d{2}
277 )
278 )|
279 3(?:
280 1\d{2}|
281 2(?:
282 [37](?:
283 \d{2}
284 )?|
285 [569]\d{2}
286 )
287 )|
288 4(?:
289 1\d{2}|
290 2[46]\d{2}
291 )|
292 5(?:
293 1\d{2}|
294 26\d{1,2}
295 )|
296 6(?:
297 [18]\d{2}|
298 2|
299 53\d{2}
300 )|
301 7(?:
302 1|
303 24
304 )\d{2}|
305 8(?:
306 1|
307 26
308 )\d{2}|
309 91\d{2}
310 )\d{3}|
311 6(?:
312 0(?:
313 1\d{2}|
314 2(?:
315 3\d{2}|
316 4\d{1,2}
317 )
318 )|
319 2(?:
320 2[2-5]\d{2}|
321 5(?:
322 [3-5]\d{2}|
323 7
324 )|
325 8\d{2}
326 )|
327 3(?:
328 1|
329 2[3478]
330 )\d{2}|
331 4(?:
332 1|
333 2[34]
334 )\d{2}|
335 5(?:
336 1|
337 2[47]
338 )\d{2}|
339 6(?:
340 [18]\d{2}|
341 6(?:
342 2(?:
343 2\d|
344 [34]\d{2}
345 )|
346 5(?:
347 [24]\d{2}|
348 3\d|
349 5\d{1,2}
350 )
351 )
352 )|
353 72[2-5]\d{2}|
354 8(?:
355 1\d{2}|
356 2[2-5]\d{2}
357 )|
358 9(?:
359 1\d{2}|
360 2[2-6]\d{2}
361 )
362 )\d{3}|
363 7(?:
364 (?:
365 02|
366 [3-589]1|
367 6[12]|
368 72[24]
369 )\d{2}|
370 21\d{3}|
371 32
372 )\d{3}|
373 8(?:
374 (?:
375 4[12]|
376 [5-7]2|
377 1\d?
378 )|
379 (?:
380 0|
381 3[12]|
382 [5-7]1|
383 217
384 )\d
385 )\d{4}|
386 9(?:
387 [35]1|
388 (?:
389 [024]2|
390 81
391 )\d|
392 (?:
393 1|
394 [24]1
395 )\d{2}
396 )\d{3}
397 "#
398 )
399 .build()
400 .unwrap();
401 }