summaryrefslogtreecommitdiffstats
path: root/library/core/tests/pattern.rs
blob: d4bec996d89a1e279a1ea1cf89d8f5cb53b9b833 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use std::str::pattern::*;

// This macro makes it easier to write
// tests that do a series of iterations
macro_rules! search_asserts {
    ($haystack:expr, $needle:expr, $testname:expr, [$($func:ident),*], $result:expr) => {
        let mut searcher = $needle.into_searcher($haystack);
        let arr = [$( Step::from(searcher.$func()) ),*];
        assert_eq!(&arr[..], &$result, $testname);
    }
}

/// Combined enum for the results of next() and next_match()/next_reject()
#[derive(Debug, PartialEq, Eq)]
enum Step {
    // variant names purposely chosen to
    // be the same length for easy alignment
    Matches(usize, usize),
    Rejects(usize, usize),
    InRange(usize, usize),
    Done,
}

use self::Step::*;

impl From<SearchStep> for Step {
    fn from(x: SearchStep) -> Self {
        match x {
            SearchStep::Match(a, b) => Matches(a, b),
            SearchStep::Reject(a, b) => Rejects(a, b),
            SearchStep::Done => Done,
        }
    }
}

impl From<Option<(usize, usize)>> for Step {
    fn from(x: Option<(usize, usize)>) -> Self {
        match x {
            Some((a, b)) => InRange(a, b),
            None => Done,
        }
    }
}

// FIXME(Manishearth) these tests focus on single-character searching  (CharSearcher)
// and on next()/next_match(), not next_reject(). This is because
// the memchr changes make next_match() for single chars complex, but next_reject()
// continues to use next() under the hood. We should add more test cases for all
// of these, as well as tests for StrSearcher and higher level tests for str::find() (etc)

#[test]
fn test_simple_iteration() {
    search_asserts!(
        "abcdeabcd",
        'a',
        "forward iteration for ASCII string",
        // a            b              c              d              e              a              b              c              d              EOF
        [next, next, next, next, next, next, next, next, next, next],
        [
            Matches(0, 1),
            Rejects(1, 2),
            Rejects(2, 3),
            Rejects(3, 4),
            Rejects(4, 5),
            Matches(5, 6),
            Rejects(6, 7),
            Rejects(7, 8),
            Rejects(8, 9),
            Done
        ]
    );

    search_asserts!(
        "abcdeabcd",
        'a',
        "reverse iteration for ASCII string",
        // d            c              b              a            e                d              c              b              a             EOF
        [
            next_back, next_back, next_back, next_back, next_back, next_back, next_back, next_back,
            next_back, next_back
        ],
        [
            Rejects(8, 9),
            Rejects(7, 8),
            Rejects(6, 7),
            Matches(5, 6),
            Rejects(4, 5),
            Rejects(3, 4),
            Rejects(2, 3),
            Rejects(1, 2),
            Matches(0, 1),
            Done
        ]
    );

    search_asserts!(
        "我爱我的猫",
        '我',
        "forward iteration for Chinese string",
        // 我           愛             我             的              貓               EOF
        [next, next, next, next, next, next],
        [Matches(0, 3), Rejects(3, 6), Matches(6, 9), Rejects(9, 12), Rejects(12, 15), Done]
    );

    search_asserts!(
        "我的猫说meow",
        'm',
        "forward iteration for mixed string",
        // 我           的             猫             说              m                e                o                w                EOF
        [next, next, next, next, next, next, next, next, next],
        [
            Rejects(0, 3),
            Rejects(3, 6),
            Rejects(6, 9),
            Rejects(9, 12),
            Matches(12, 13),
            Rejects(13, 14),
            Rejects(14, 15),
            Rejects(15, 16),
            Done
        ]
    );

    search_asserts!(
        "我的猫说meow",
        '猫',
        "reverse iteration for mixed string",
        // w             o                 e                m                说              猫             的             我             EOF
        [
            next_back, next_back, next_back, next_back, next_back, next_back, next_back, next_back,
            next_back
        ],
        [
            Rejects(15, 16),
            Rejects(14, 15),
            Rejects(13, 14),
            Rejects(12, 13),
            Rejects(9, 12),
            Matches(6, 9),
            Rejects(3, 6),
            Rejects(0, 3),
            Done
        ]
    );
}

#[test]
fn test_simple_search() {
    search_asserts!(
        "abcdeabcdeabcde",
        'a',
        "next_match for ASCII string",
        [next_match, next_match, next_match, next_match],
        [InRange(0, 1), InRange(5, 6), InRange(10, 11), Done]
    );

    search_asserts!(
        "abcdeabcdeabcde",
        'a',
        "next_match_back for ASCII string",
        [next_match_back, next_match_back, next_match_back, next_match_back],
        [InRange(10, 11), InRange(5, 6), InRange(0, 1), Done]
    );

    search_asserts!(
        "abcdeab",
        'a',
        "next_reject for ASCII string",
        [next_reject, next_reject, next_match, next_reject, next_reject],
        [InRange(1, 2), InRange(2, 3), InRange(5, 6), InRange(6, 7), Done]
    );

    search_asserts!(
        "abcdeabcdeabcde",
        'a',
        "next_reject_back for ASCII string",
        [
            next_reject_back,
            next_reject_back,
            next_match_back,
            next_reject_back,
            next_reject_back,
            next_reject_back
        ],
        [
            InRange(14, 15),
            InRange(13, 14),
            InRange(10, 11),
            InRange(9, 10),
            InRange(8, 9),
            InRange(7, 8)
        ]
    );
}

// Á, 각, ก, 😀 all end in 0x81
// 🁀, ᘀ do not end in 0x81 but contain the byte
// ꁁ has 0x81 as its second and third bytes.
//
// The memchr-using implementation of next_match
// and next_match_back temporarily violate
// the property that the search is always on a unicode boundary,
// which is fine as long as this never reaches next() or next_back().
// So we test if next() is correct after each next_match() as well.
const STRESS: &str = "Áa🁀bÁꁁfg😁각กᘀ각aÁ각ꁁก😁a";

#[test]
fn test_stress_indices() {
    // this isn't really a test, more of documentation on the indices of each character in the stresstest string

    search_asserts!(
        STRESS,
        'x',
        "Indices of characters in stress test",
        [
            next, next, next, next, next, next, next, next, next, next, next, next, next, next,
            next, next, next, next, next, next, next
        ],
        [
            Rejects(0, 2),   // Á
            Rejects(2, 3),   // a
            Rejects(3, 7),   // 🁀
            Rejects(7, 8),   // b
            Rejects(8, 10),  // Á
            Rejects(10, 13), // ꁁ
            Rejects(13, 14), // f
            Rejects(14, 15), // g
            Rejects(15, 19), // 😀
            Rejects(19, 22), // 각
            Rejects(22, 25), // ก
            Rejects(25, 28), // ᘀ
            Rejects(28, 31), // 각
            Rejects(31, 32), // a
            Rejects(32, 34), // Á
            Rejects(34, 37), // 각
            Rejects(37, 40), // ꁁ
            Rejects(40, 43), // ก
            Rejects(43, 47), // 😀
            Rejects(47, 48), // a
            Done
        ]
    );
}

#[test]
fn test_forward_search_shared_bytes() {
    search_asserts!(
        STRESS,
        'Á',
        "Forward search for two-byte Latin character",
        [next_match, next_match, next_match, next_match],
        [InRange(0, 2), InRange(8, 10), InRange(32, 34), Done]
    );

    search_asserts!(
        STRESS,
        'Á',
        "Forward search for two-byte Latin character; check if next() still works",
        [next_match, next, next_match, next, next_match, next, next_match],
        [
            InRange(0, 2),
            Rejects(2, 3),
            InRange(8, 10),
            Rejects(10, 13),
            InRange(32, 34),
            Rejects(34, 37),
            Done
        ]
    );

    search_asserts!(
        STRESS,
        '각',
        "Forward search for three-byte Hangul character",
        [next_match, next, next_match, next_match, next_match],
        [InRange(19, 22), Rejects(22, 25), InRange(28, 31), InRange(34, 37), Done]
    );

    search_asserts!(
        STRESS,
        '각',
        "Forward search for three-byte Hangul character; check if next() still works",
        [next_match, next, next_match, next, next_match, next, next_match],
        [
            InRange(19, 22),
            Rejects(22, 25),
            InRange(28, 31),
            Rejects(31, 32),
            InRange(34, 37),
            Rejects(37, 40),
            Done
        ]
    );

    search_asserts!(
        STRESS,
        'ก',
        "Forward search for three-byte Thai character",
        [next_match, next, next_match, next, next_match],
        [InRange(22, 25), Rejects(25, 28), InRange(40, 43), Rejects(43, 47), Done]
    );

    search_asserts!(
        STRESS,
        'ก',
        "Forward search for three-byte Thai character; check if next() still works",
        [next_match, next, next_match, next, next_match],
        [InRange(22, 25), Rejects(25, 28), InRange(40, 43), Rejects(43, 47), Done]
    );

    search_asserts!(
        STRESS,
        '😁',
        "Forward search for four-byte emoji",
        [next_match, next, next_match, next, next_match],
        [InRange(15, 19), Rejects(19, 22), InRange(43, 47), Rejects(47, 48), Done]
    );

    search_asserts!(
        STRESS,
        '😁',
        "Forward search for four-byte emoji; check if next() still works",
        [next_match, next, next_match, next, next_match],
        [InRange(15, 19), Rejects(19, 22), InRange(43, 47), Rejects(47, 48), Done]
    );

    search_asserts!(
        STRESS,
        'ꁁ',
        "Forward search for three-byte Yi character with repeated bytes",
        [next_match, next, next_match, next, next_match],
        [InRange(10, 13), Rejects(13, 14), InRange(37, 40), Rejects(40, 43), Done]
    );

    search_asserts!(
        STRESS,
        'ꁁ',
        "Forward search for three-byte Yi character with repeated bytes; check if next() still works",
        [next_match, next, next_match, next, next_match],
        [InRange(10, 13), Rejects(13, 14), InRange(37, 40), Rejects(40, 43), Done]
    );
}

#[test]
fn test_reverse_search_shared_bytes() {
    search_asserts!(
        STRESS,
        'Á',
        "Reverse search for two-byte Latin character",
        [next_match_back, next_match_back, next_match_back, next_match_back],
        [InRange(32, 34), InRange(8, 10), InRange(0, 2), Done]
    );

    search_asserts!(
        STRESS,
        'Á',
        "Reverse search for two-byte Latin character; check if next_back() still works",
        [next_match_back, next_back, next_match_back, next_back, next_match_back, next_back],
        [InRange(32, 34), Rejects(31, 32), InRange(8, 10), Rejects(7, 8), InRange(0, 2), Done]
    );

    search_asserts!(
        STRESS,
        '각',
        "Reverse search for three-byte Hangul character",
        [next_match_back, next_back, next_match_back, next_match_back, next_match_back],
        [InRange(34, 37), Rejects(32, 34), InRange(28, 31), InRange(19, 22), Done]
    );

    search_asserts!(
        STRESS,
        '각',
        "Reverse search for three-byte Hangul character; check if next_back() still works",
        [
            next_match_back,
            next_back,
            next_match_back,
            next_back,
            next_match_back,
            next_back,
            next_match_back
        ],
        [
            InRange(34, 37),
            Rejects(32, 34),
            InRange(28, 31),
            Rejects(25, 28),
            InRange(19, 22),
            Rejects(15, 19),
            Done
        ]
    );

    search_asserts!(
        STRESS,
        'ก',
        "Reverse search for three-byte Thai character",
        [next_match_back, next_back, next_match_back, next_back, next_match_back],
        [InRange(40, 43), Rejects(37, 40), InRange(22, 25), Rejects(19, 22), Done]
    );

    search_asserts!(
        STRESS,
        'ก',
        "Reverse search for three-byte Thai character; check if next_back() still works",
        [next_match_back, next_back, next_match_back, next_back, next_match_back],
        [InRange(40, 43), Rejects(37, 40), InRange(22, 25), Rejects(19, 22), Done]
    );

    search_asserts!(
        STRESS,
        '😁',
        "Reverse search for four-byte emoji",
        [next_match_back, next_back, next_match_back, next_back, next_match_back],
        [InRange(43, 47), Rejects(40, 43), InRange(15, 19), Rejects(14, 15), Done]
    );

    search_asserts!(
        STRESS,
        '😁',
        "Reverse search for four-byte emoji; check if next_back() still works",
        [next_match_back, next_back, next_match_back, next_back, next_match_back],
        [InRange(43, 47), Rejects(40, 43), InRange(15, 19), Rejects(14, 15), Done]
    );

    search_asserts!(
        STRESS,
        'ꁁ',
        "Reverse search for three-byte Yi character with repeated bytes",
        [next_match_back, next_back, next_match_back, next_back, next_match_back],
        [InRange(37, 40), Rejects(34, 37), InRange(10, 13), Rejects(8, 10), Done]
    );

    search_asserts!(
        STRESS,
        'ꁁ',
        "Reverse search for three-byte Yi character with repeated bytes; check if next_back() still works",
        [next_match_back, next_back, next_match_back, next_back, next_match_back],
        [InRange(37, 40), Rejects(34, 37), InRange(10, 13), Rejects(8, 10), Done]
    );
}

#[test]
fn double_ended_regression_test() {
    // https://github.com/rust-lang/rust/issues/47175
    // Ensures that double ended searching comes to a convergence
    search_asserts!(
        "abcdeabcdeabcde",
        'a',
        "alternating double ended search",
        [next_match, next_match_back, next_match, next_match_back],
        [InRange(0, 1), InRange(10, 11), InRange(5, 6), Done]
    );
    search_asserts!(
        "abcdeabcdeabcde",
        'a',
        "triple double ended search for a",
        [next_match, next_match_back, next_match_back, next_match_back],
        [InRange(0, 1), InRange(10, 11), InRange(5, 6), Done]
    );
    search_asserts!(
        "abcdeabcdeabcde",
        'd',
        "triple double ended search for d",
        [next_match, next_match_back, next_match_back, next_match_back],
        [InRange(3, 4), InRange(13, 14), InRange(8, 9), Done]
    );
    search_asserts!(
        STRESS,
        'Á',
        "Double ended search for two-byte Latin character",
        [next_match, next_match_back, next_match, next_match_back],
        [InRange(0, 2), InRange(32, 34), InRange(8, 10), Done]
    );
    search_asserts!(
        STRESS,
        '각',
        "Reverse double ended search for three-byte Hangul character",
        [next_match_back, next_back, next_match, next, next_match_back, next_match],
        [InRange(34, 37), Rejects(32, 34), InRange(19, 22), Rejects(22, 25), InRange(28, 31), Done]
    );
    search_asserts!(
        STRESS,
        'ก',
        "Double ended search for three-byte Thai character",
        [next_match, next_back, next, next_match_back, next_match],
        [InRange(22, 25), Rejects(47, 48), Rejects(25, 28), InRange(40, 43), Done]
    );
    search_asserts!(
        STRESS,
        '😁',
        "Double ended search for four-byte emoji",
        [next_match_back, next, next_match, next_back, next_match],
        [InRange(43, 47), Rejects(0, 2), InRange(15, 19), Rejects(40, 43), Done]
    );
    search_asserts!(
        STRESS,
        'ꁁ',
        "Double ended search for three-byte Yi character with repeated bytes",
        [next_match, next, next_match_back, next_back, next_match],
        [InRange(10, 13), Rejects(13, 14), InRange(37, 40), Rejects(34, 37), Done]
    );
}