-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnamegen_test.go
146 lines (132 loc) Β· 3.47 KB
/
namegen_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package namegen
import (
"math/rand"
"strings"
"testing"
"time"
)
type getPostfixIdTest struct {
arg1 int64
arg2 PostfixIdType
arg3 int
expected string
}
var getPostfixIdTests = []getPostfixIdTest{
// 1234 -> 1234
{1234, Numeric, 0, ""},
{1234, Numeric, 4, "1234"},
{1234, Numeric, 2, "34"},
{1234, Numeric, 6, "001234"},
// 623741435 -> abcxyz
{623741435, AlphaNumeric, 0, ""},
{623741435, AlphaNumeric, 6, "abcxyz"},
{623741435, AlphaNumeric, 3, "xyz"},
{623741435, AlphaNumeric, 8, "00abcxyz"},
}
func TestGetPostfix(t *testing.T) {
for _, test := range getPostfixIdTests {
var sb strings.Builder
getPostfixId(&sb, test.arg1, test.arg2, test.arg3)
output := sb.String()
if output != test.expected {
t.Errorf("Output %q not equal to expected %q", output, test.expected)
}
}
}
var getTests = []nameGen{
*NewWithPostfixId([]DictType{Adjectives, Animals}, Numeric, 0),
*NewWithPostfixId([]DictType{Adjectives, Animals}, Numeric, 4),
*NewWithPostfixId([]DictType{Adjectives, Animals}, AlphaNumeric, 6),
*NewWithPostfixId([]DictType{Adjectives, Colors, Animals}, Numeric, 0),
*NewWithPostfixId([]DictType{Adjectives, Colors, Animals}, Numeric, 4),
*NewWithPostfixId([]DictType{Adjectives, Colors, Animals}, AlphaNumeric, 6),
}
func TestGet(t *testing.T) {
for _, test := range getTests {
output := strings.Split(test.Get(), test.delim)
// Check to make sure the correct dictionaries were used and in the right order.
for i := range test.dicts {
if !stringInSlice(output[i], dicts[test.dicts[i]]) {
t.Errorf("Dict %d doesn't contain %s", i, output[i])
}
}
// Check the postfix.
if test.pIdLen > 0 {
if want := len(test.dicts) + 1; len(output) != want {
t.Errorf("Unexpected output:%v for %d dicts", output, want)
}
if postfix := output[len(output)-1]; len(postfix) != test.pIdLen {
t.Errorf("Unexpected postfix: %s for postfixLen: %d", postfix, test.pIdLen)
}
} else {
if want := len(test.dicts); len(output) != want {
t.Errorf("Unexpected output:%v for %d dicts", output, want)
}
}
}
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func TestSetters(t *testing.T) {
// Check default values.
n := New()
if (n.dicts[0] != Adjectives) || (n.dicts[1] != Animals) {
t.Errorf("Unexpected default dictionary schema: %v", n.dicts)
}
if n.delim != "-" {
t.Errorf("Unexpected default delimiter: %s", n.delim)
}
if n.pIdLen != 0 {
t.Errorf("Unexpected default prefix length: %d", n.pIdLen)
}
// Validate the setters.
n.SetDelimiter(".")
if n.delim != "." {
t.Errorf("Unexpected delimiter: %s", n.delim)
}
n.SetPostfixIdLen(7)
if n.pIdLen != 7 {
t.Errorf("Unexpected prefix length: %d", n.pIdLen)
}
}
func BenchmarkNew(b *testing.B) {
for n := 0; n < b.N; n++ {
New()
}
}
func BenchmarkGet(b *testing.B) {
ngen := New()
for n := 0; n < b.N; n++ {
ngen.Get()
}
}
func BenchmarkGetForId(b *testing.B) {
ngen := New()
rand.Seed(time.Now().UnixNano())
rn := rand.Int63()
for n := 0; n < b.N; n++ {
ngen.GetForId(rn)
}
}
func BenchmarkGetWithPostfix(b *testing.B) {
ngen := New()
ngen.SetPostfixIdLen(8)
for n := 0; n < b.N; n++ {
ngen.Get()
}
}
func BenchmarkGetPostfixId(b *testing.B) {
rand.Seed(time.Now().UnixNano())
rn := rand.Int63()
var sb strings.Builder
for n := 0; n < b.N; n++ {
// Note: This benchmark targets the most common case of trimming the encoded number.
getPostfixId(&sb, rn, Numeric, 4)
}
}