v / vlib / builtin
Raw file | 103 loc (94 sloc) | 3.03 KB | Latest commit hash 36ec47cd2
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 'axbx?cxdxe'.match_glob('*x[?]c*')
72 assert !'axbxXcxdxe'.match_glob('*x[?]c*')
73 assert 'zaxbx*cxdxez'.match_glob('*x[Q*W]c*')
74 assert 'zaxbx*cxdxez'.match_glob('*x[QW*]c*')
75 assert 'zaxbx*cxdxez'.match_glob('*bx[*QW]c*')
76 assert 'zaxbW*cxdxez'.match_glob('*W[*nmk]c*')
77 assert 'zaxbW*cxdxez'.match_glob('*W[n*mk]c*')
78 assert 'zaxbW*cxdxez'.match_glob('*W[nm*k]c*')
79 assert 'zaxbW*cxdxez'.match_glob('*W[nmk*]c*')
80}
81
82fn test_match_glob_with_complex_patterns() {
83 assert 'axbxcxdxe'.match_glob('*xdx*')
84 assert !'axbxcxdxe'.match_glob('*xzx*')
85 assert 'axbxcxdxe'.match_glob('a*b*c*d*e*')
86 assert 'axbxcxdxexxx'.match_glob('a*b*c*d*e*')
87 assert 'abxbbxdbxebxczzx'.match_glob('a*b?c*x')
88 assert !'abxbbxdbxebxczzy'.match_glob('a*b?c*x')
89}
90
91fn test_match_glob_search_is_linear() {
92 // Note: these are pathological cases, when matches are performed
93 // using the exponential recursive approach, that can take many
94 // seconds, even minutes, but take usually only microseconds,
95 // using the linear approach from https://research.swtch.com/glob
96 // that does not backtrack.
97 long_a := 'a'.repeat(500)
98 sw := time.new_stopwatch()
99 assert !long_a.match_glob('a*a*a*a*b')
100 assert sw.elapsed().milliseconds() < 10
101 assert !long_a.match_glob('a*a*a*a*a*a*a*a*a*b')
102 assert sw.elapsed().milliseconds() < 10
103}