-
Notifications
You must be signed in to change notification settings - Fork 0
/
petname_test.go
130 lines (111 loc) · 3.05 KB
/
petname_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
package petname
import (
"strings"
"testing"
"time"
"github.com/Bios-Marcel/go-petname/long"
"github.com/Bios-Marcel/go-petname/medium"
"github.com/Bios-Marcel/go-petname/short"
"github.com/stretchr/testify/require"
)
// oldGenerate is the implementation i initially wrote for dustinkirklands
// golang-petname library. I use it as a baseline for performance comparisons.
func oldGenerate(wordCount int, separator string) string {
switch wordCount {
case 1:
return Name()
case 2:
return Adjective() + separator + Name()
case 3:
// Potentially common cases have shortcut implementations to
// reduce allocations and CPU usage, even though default: would handle
// them correctly.
return Adverb() + separator + Adjective() + separator + Name()
case 4:
return Adverb() + separator + Adverb() + separator + Adjective() + separator + Name()
default:
words := make([]string, 0, wordCount)
for i := 0; i < wordCount-2; i++ {
words = append(words, Adverb())
}
return strings.Join(append(words, Adjective(), Name()), separator)
}
}
func BenchmarkOldGenerate(b *testing.B) {
for i := 0; i < b.N; i++ {
oldGenerate(3, "-")
}
}
func BenchmarkGenerate(b *testing.B) {
b.Run("lowercase", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Generate(3, Lower, Hyphen)
}
})
b.Run("titlecase", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Generate(3, Title, Hyphen)
}
})
b.Run("uppercase", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Generate(3, Upper, Hyphen)
}
})
}
func TestGenerateDifferentWordLists(t *testing.T) {
SetNames(long.Names)
SetAdjectives(long.Adjectives)
SetAdverbs(long.Adverbs)
Seed(1)
require.NotEmpty(t, Generate(3, Lower, None))
SetNames(medium.Names)
SetAdjectives(medium.Adjectives)
SetAdverbs(medium.Adverbs)
Seed(1)
require.NotEmpty(t, Generate(3, Lower, None))
// Do short last, so everything is configured correctly for the other tests.
SetNames(short.Names)
SetAdjectives(short.Adjectives)
SetAdverbs(short.Adverbs)
Seed(1)
require.NotEmpty(t, Generate(3, Lower, None))
}
func TestGenerate(t *testing.T) {
Seed(1)
require.Equal(t, "likelygivingmagpie", Generate(3, Lower, None))
Seed(1)
require.Equal(t, "LIKELYGIVINGMAGPIE", Generate(3, Upper, None))
Seed(1)
require.Equal(t, "LikelyGivingMagpie", Generate(3, Title, None))
}
func TestGenerateDifferentLengths(t *testing.T) {
for i := 0; i < 100; i++ {
Seed(1)
require.NotEmpty(t, Generate(3, Lower, None))
Seed(1)
require.NotEmpty(t, Generate(3, Upper, None))
Seed(1)
require.NotEmpty(t, Generate(3, Title, None))
}
}
func TestGenerateZeroWords(t *testing.T) {
done := make(chan struct{})
go func() {
// Passing 0 words should not cause an "infinite" loop.
Generate(0, 0, 0)
done <- struct{}{}
}()
select {
case <-done:
// Success
case <-time.NewTimer(2 * time.Second).C:
t.Fatal("Generate(0, 0, 0) did not return")
}
}
func FuzzGenerate(f *testing.F) {
f.Add(uint(3), 1, None)
f.Fuzz(func(t *testing.T, wordCount uint, casing int, separator Separator) {
Generate(wordCount, Casing(casing), separator)
})
}