-
Notifications
You must be signed in to change notification settings - Fork 1
/
gocha_test.go
132 lines (108 loc) · 2.11 KB
/
gocha_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gocha
import (
"math/rand"
"regexp"
"regexp/syntax"
"testing"
)
func TestNew(t *testing.T) {
t.Run("New (error)", func(t *testing.T) {
pattern := `[a-z`
err, _ := New(pattern)
_, want := syntax.Parse(pattern, syntax.Perl)
if err.Error() != want.Error() {
t.Errorf("%v", err.Error())
}
})
t.Run("New with options", func(t *testing.T) {
pattern := `[a-z]`
r := rand.New(rand.NewSource(1))
err, _ := New(pattern, Rand(r))
if err != nil {
t.Errorf("%v", err.Error())
}
})
}
func TestGen(t *testing.T) {
patterns := []string{
`a`,
`ab`,
`a|b`,
`a*`,
`a?`,
`a+`,
`a{1,3}`,
`a{3}`,
`a{3,}`,
`[xyz]`,
`[^xyz]`,
`[[:alpha:]]`,
`[[:^alpha:]]`,
`\pN`,
`\p{Greek}`,
`\PN`,
`\P{Greek}`,
`x*?`,
`x+?`,
`x??`,
`x{n,m}?`,
`x{n,}?`,
`x{n}?`,
`(re)`,
`\d`,
`\D`,
`.`,
`[カコヵか][ッー]{1,3}?[フヒふひ]{1,3}[ィェー]{1,3}[ズス][ドクグュ][リイ][プブぷぶ]{1,3}[トドォ]{1,2}`,
`[あ-お]{10}`,
`(?i:[a-z]{10})`,
`$`,
`(?i)[^\W]`,
`[[:alpha:]]`,
}
for _, pattern := range patterns {
_, g := New(pattern)
s := g.Gen()
if m, _ := regexp.MatchString(pattern, s); !m {
t.Errorf("%v does not match to %v", s, pattern)
}
}
_, g := New(``)
s := g.Gen()
if m, _ := regexp.MatchString(`\A\z`, s); !m {
t.Errorf("null regexp")
}
t.Run("fixed seed", func(t *testing.T) {
r := rand.New(rand.NewSource(1))
_, g := New(`[a-z]{10}`, Rand(r))
if s := g.Gen(); s != "vbgacrjwtc" {
t.Errorf("%v must be vbgacrjwtc", s)
}
if s := g.Gen(); s != "cahkfapsfc" {
t.Errorf("%v must be cahkfapsfc", s)
}
})
}
var RandFromRange = randFromRange
func TestRandFromRange(t *testing.T) {
r := rand.New(rand.NewSource(1))
rs := []intRange{}
r1 := intRange{
a: 1,
b: 2,
}
r2 := intRange{
a: 10,
b: 11,
}
rs = append(rs, r1)
rs = append(rs, r2)
if result := randFromRange(rs, r); (result != 1) && (result != 2) && (result != 10) && (result != 11) {
t.Errorf("result:%v must 1 or 2", result)
}
}
func BenchmarkGen(b *testing.B) {
for i := 0; i < b.N; i++ {
_, g := New(`.{1000}`)
g.Gen()
}
}