v2 / vlib / builtin / string_match_glob_test.v
106 lines · 90 sloc · 3.11 KB · d951737f2eff664d128ba7a1c893d5256f24d55c
Raw
1import time
2
3fn test_match_glob_on_empty_string() {
4 assert ''.match_glob('')
5 assert !''.match_glob('x')
6}
7
8fn test_match_glob_on_x() {
9 assert !'x'.match_glob('')
10 assert 'x'.match_glob('x')
11 assert 'xxx'.match_glob('*x')
12 assert 'xxx'.match_glob('x*')
13}
14
15fn test_match_glob_on_abc() {
16 assert !'abc'.match_glob('')
17 assert 'abc'.match_glob('*')
18
19 assert !'abc'.match_glob('ab')
20 assert 'abc'.match_glob('abc')
21 assert 'abc'.match_glob('abc*')
22
23 assert 'abc'.match_glob('*c')
24 assert !'abc'.match_glob('*b')
25 assert 'abc'.match_glob('*bc')
26 assert 'abc'.match_glob('*abc')
27
28 assert 'abc'.match_glob('a*')
29 assert !'abc'.match_glob('b*')
30 assert 'abc'.match_glob('a*c')
31
32 assert 'abc'.match_glob('ab?')
33 assert 'abc'.match_glob('a??')
34 assert 'abc'.match_glob('???')
35 assert !'abc'.match_glob('??')
36 assert !'abc'.match_glob('?')
37}
38
39fn test_match_glob_on_a() {
40 assert 'a'.match_glob('a')
41 assert 'a'.match_glob('?')
42 assert !'a'.match_glob('??')
43 assert 'a'.match_glob('*')
44 assert 'a'.match_glob('a*')
45 assert 'a'.match_glob('*a')
46}
47
48fn test_match_glob_with_any_charset_patterns() {
49 assert 'axbxcxdxe'.match_glob('*c[xyz]d*')
50 assert 'axbxcxdxe'.match_glob('*c[yxz]d*')
51 assert 'axbxcxdxe'.match_glob('*c[zyx]d*')
52
53 assert 'axbxcxdxe'.match_glob('*dx[QeW]')
54 assert 'axbxcxdxe'.match_glob('*dx[QeW]*')
55
56 assert !'axbxcxdxe'.match_glob('*bx[QcW]')
57 assert 'axbxcxdxe'.match_glob('*bx[QcW]*')
58
59 assert !'axbxcxdxe'.match_glob('*zx[QeW]')
60 assert !'axbxcxdxe'.match_glob('*zx[QeW]*')
61}
62
63fn test_match_glob_with_none_of_charset_patterns() {
64 assert 'axbxcxdxe'.match_glob('*c[^XYZ]d*')
65 assert !'axbxcxdxe'.match_glob('*c[^xYZ]d*')
66 assert !'axbxcxdxe'.match_glob('*c[^YxZ]d*')
67 assert !'axbxcxdxe'.match_glob('*c[^YZx]d*')
68}
69
70fn test_match_glob_with_escaped_metachars() {
71 assert '10?x'.match_glob('*[?]x')
72 assert '10*x'.match_glob('*[*]x')
73 assert !'10+x'.match_glob('*[*]x')
74 assert 'axbx?cxdxe'.match_glob('*x[?]c*')
75 assert !'axbxXcxdxe'.match_glob('*x[?]c*')
76 assert 'zaxbx*cxdxez'.match_glob('*x[Q*W]c*')
77 assert 'zaxbx*cxdxez'.match_glob('*x[QW*]c*')
78 assert 'zaxbx*cxdxez'.match_glob('*bx[*QW]c*')
79 assert 'zaxbW*cxdxez'.match_glob('*W[*nmk]c*')
80 assert 'zaxbW*cxdxez'.match_glob('*W[n*mk]c*')
81 assert 'zaxbW*cxdxez'.match_glob('*W[nm*k]c*')
82 assert 'zaxbW*cxdxez'.match_glob('*W[nmk*]c*')
83}
84
85fn test_match_glob_with_complex_patterns() {
86 assert 'axbxcxdxe'.match_glob('*xdx*')
87 assert !'axbxcxdxe'.match_glob('*xzx*')
88 assert 'axbxcxdxe'.match_glob('a*b*c*d*e*')
89 assert 'axbxcxdxexxx'.match_glob('a*b*c*d*e*')
90 assert 'abxbbxdbxebxczzx'.match_glob('a*b?c*x')
91 assert !'abxbbxdbxebxczzy'.match_glob('a*b?c*x')
92}
93
94fn test_match_glob_search_is_linear() {
95 // Note: these are pathological cases, when matches are performed
96 // using the exponential recursive approach, that can take many
97 // seconds, even minutes, but take usually only microseconds,
98 // using the linear approach from https://research.swtch.com/glob
99 // that does not backtrack.
100 long_a := 'a'.repeat(500)
101 sw := time.new_stopwatch()
102 assert !long_a.match_glob('a*a*a*a*b')
103 assert sw.elapsed().milliseconds() < 10
104 assert !long_a.match_glob('a*a*a*a*a*a*a*a*a*b')
105 assert sw.elapsed().milliseconds() < 10
106}
107