forked from distributedio/titan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_test.go
115 lines (104 loc) · 2.24 KB
/
command_test.go
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
package command
import (
"testing"
"github.com/stretchr/testify/assert"
)
func matchPrefixCase() map[string]string {
cs := map[string]string{
"abc?[a-z]": "abc",
"?abc": "",
"\\*[^abc]?": "*",
}
return cs
}
type patternMap map[string]bool
func matchCase(nocase bool) map[string]*patternMap {
var cs map[string]*patternMap
if !nocase {
cs = map[string]*patternMap{
"*": &patternMap{
"": true,
"abcd": true,
"*[*]": true,
},
"******a": &patternMap{
"a": true,
"***a": true,
"bcdea": true,
"abcd": false,
},
"\\*?aaa": &patternMap{
"*caaa": true,
"abc": false,
},
"[a-z][^0-9][z-a]?[a-z": &patternMap{
"abz.a": true,
"a1z.*": false,
"abz.e": true,
},
"[a-z]*cat*[h][^b]*eyes*": &patternMap{
"my cat has very bright eyes": true,
"my dog has very bright eyes": false,
},
"h?llo": &patternMap{
"hello": true,
"healo": false,
},
"h??lo": &patternMap{
"hello": true,
},
"h*o": &patternMap{
"hello": true,
"ho": true,
},
}
} else {
cs = map[string]*patternMap{
"[A-Z][0-9]*": &patternMap{
"B1": true,
"B2000": true,
"b2000": false,
},
"*A": &patternMap{
"abcdA": true,
"abcda": false,
"Ae": false,
},
"?A*C": &patternMap{
"1AbcdC": true,
"cA12344C": true,
"1abcdc": false,
},
}
}
return cs
}
func TestGlobMatchPrefix(t *testing.T) {
list := matchPrefixCase()
for match, exptected := range list {
val := globMatchPrefix([]byte(match))
assert.Equal(t, exptected, string(val))
}
}
func TestPatternMatch(t *testing.T) {
cs := matchCase(false)
for pattern, vals := range cs {
for val, expected := range map[string]bool(*vals) {
actual := globMatch([]byte(pattern), []byte(val), false)
assert.Equal(t, expected, actual, "err log:", pattern, val)
}
}
// check upper string
cs = matchCase(true)
for pattern, vals := range cs {
for val, expected := range map[string]bool(*vals) {
actual := globMatch([]byte(pattern), []byte(val), true)
assert.Equal(t, expected, actual, "err log:", pattern, val)
}
}
}
func BenchmarkGlobMatch(b *testing.B) {
for i := 0; i < b.N; i++ {
globMatch([]byte("hellabcdlo"), []byte("h*lo"), false)
}
}