-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_test.go
71 lines (65 loc) · 1.65 KB
/
generator_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
package randstr
import (
"fmt"
"testing"
)
func TestGenerators(t *testing.T) {
// rely on dynamic programming to check if str can be composed of candidates
validate := func(str string, candidates []string) {
match := make([]bool, len(str))
for j := range str {
for _, candidate := range candidates {
if j-len(candidate) >= 0 && match[j-len(candidate)] {
if str[j+1-len(candidate):j+1] == candidate {
match[j] = true
break
}
} else if j == 0 {
if string(str[0:1]) == candidate {
match[j] = true
break
}
} else if j-len(candidate) == -1 && str[0:j+1] == candidate {
match[j] = true
break
}
}
}
if !match[len(match)-1] {
t.Fatalf("no match for random str %s candidates(%v) match(%v)", str, candidates, match)
}
}
t.Run("test Gen()", func(t *testing.T) {
candidates := []string{"a", "bc", "def"}
randStr := NewRandStr(candidates, true, 4)
for i := 0; i < 10; i++ {
str := randStr.Gen()
fmt.Println(str)
validate(str, candidates)
}
})
t.Run("test Alnums()", func(t *testing.T) {
randStr := NewRandStr([]string{}, true, 4)
for i := 0; i < 10; i++ {
str := randStr.Alnums()
fmt.Println(str)
validate(str, append(numberSet, alphabetSet...))
}
})
t.Run("test Alphabets()", func(t *testing.T) {
randStr := NewRandStr([]string{}, false, 4)
for i := 0; i < 10; i++ {
str := randStr.Alphabets()
fmt.Println(str)
validate(str, alphabetSet)
}
})
t.Run("test Numbers()", func(t *testing.T) {
randStr := NewRandStr([]string{}, true, 4)
for i := 0; i < 10; i++ {
str := randStr.Numbers()
fmt.Println(str)
validate(str, numberSet)
}
})
}