Debian Codegraph @

petname_test.go

raw symbols SCIP
1 /*
2 petname: test of library for generating human-readable, random names
3 for objects (e.g. hostnames, containers, blobs)
4
5 Copyright 2014 Dustin Kirkland <[email protected]>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 */
19
20 // Package petname is a library for generating human-readable, random
21 // names for objects (e.g. hostnames, containers, blobs).
22 package petname
23
24 import (
25 "math/rand"
26 "sync"
27 "testing"
28 )
29
30 // Make sure the generated names exist
31 func TestPetName(t *testing.T) {
32 for i:=0; i<10; i++ {
33 name := Generate(i, "-")
34 if name == "" {
35 t.Fatalf("Did not generate a %d-word name, '%s'", i, name)
36 }
37 }
38 }
39
40 // Test backward compatibility - existing API still works
41 func TestBackwardCompatibility(t *testing.T) {
42 tests := []struct {
43 name string
44 fn func() string
45 }{
46 {"Adverb", Adverb},
47 {"Adjective", Adjective},
48 {"Name", Name},
49 }
50
51 for _, tt := range tests {
52 t.Run(tt.name, func(t *testing.T) {
53 result := tt.fn()
54 if result == "" {
55 t.Errorf("%s() returned empty string", tt.name)
56 }
57 })
58 }
59
60 // Test Generate with various word counts
61 for words := 1; words <= 5; words++ {
62 t.Run("Generate", func(t *testing.T) {
63 result := Generate(words, "-")
64 if result == "" {
65 t.Errorf("Generate(%d, '-') returned empty string", words)
66 }
67 })
68 }
69 }
70
71 // Test new Generator API works
72 func TestGeneratorAPI(t *testing.T) {
73 gen := New(rand.New(rand.NewSource(42)))
74
75 tests := []struct {
76 name string
77 fn func() string
78 }{
79 {"Adverb", gen.Adverb},
80 {"Adjective", gen.Adjective},
81 {"Name", gen.Name},
82 }
83
84 for _, tt := range tests {
85 t.Run(tt.name, func(t *testing.T) {
86 result := tt.fn()
87 if result == "" {
88 t.Errorf("%s() returned empty string", tt.name)
89 }
90 })
91 }
92
93 // Test Generate with various word counts
94 for words := 1; words <= 5; words++ {
95 t.Run("Generate", func(t *testing.T) {
96 result := gen.Generate(words, "-")
97 if result == "" {
98 t.Errorf("Generate(%d, '-') returned empty string", words)
99 }
100 })
101 }
102 }
103
104 // Test deterministic behavior with seeded rand
105 func TestDeterministicBehavior(t *testing.T) {
106 seed := int64(12345)
107
108 // Generate with first generator
109 gen1 := New(rand.New(rand.NewSource(seed)))
110 results1 := []string{
111 gen1.Adverb(),
112 gen1.Adjective(),
113 gen1.Name(),
114 gen1.Generate(2, "-"),
115 gen1.Generate(3, "_"),
116 }
117
118 // Generate with second generator using same seed
119 gen2 := New(rand.New(rand.NewSource(seed)))
120 results2 := []string{
121 gen2.Adverb(),
122 gen2.Adjective(),
123 gen2.Name(),
124 gen2.Generate(2, "-"),
125 gen2.Generate(3, "_"),
126 }
127
128 // Results should be identical
129 for i := range results1 {
130 if results1[i] != results2[i] {
131 t.Errorf("Deterministic mismatch at index %d: got %q, want %q", i, results2[i], results1[i])
132 }
133 }
134 }
135
136 // Test thread-safe concurrent use with separate generators
137 func TestConcurrentGenerators(t *testing.T) {
138 const numGoroutines = 100
139 const numGenerations = 10
140
141 var wg sync.WaitGroup
142 results := make([][]string, numGoroutines)
143
144 for i := 0; i < numGoroutines; i++ {
145 wg.Add(1)
146 go func(id int) {
147 defer wg.Done()
148 // Each goroutine gets its own generator with unique seed
149 gen := New(rand.New(rand.NewSource(int64(id))))
150 results[id] = make([]string, numGenerations)
151 for j := 0; j < numGenerations; j++ {
152 results[id][j] = gen.Generate(2, "-")
153 }
154 }(i)
155 }
156
157 wg.Wait()
158
159 // Verify all goroutines completed and generated names
160 for i, res := range results {
161 if len(res) != numGenerations {
162 t.Errorf("Goroutine %d: expected %d results, got %d", i, numGenerations, len(res))
163 }
164 for j, name := range res {
165 if name == "" {
166 t.Errorf("Goroutine %d, generation %d: got empty name", i, j)
167 }
168 }
169 }
170 }
171
172 // Test that nil rand uses global rand (backward compatibility)
173 func TestNilRandUsesGlobalRand(t *testing.T) {
174 gen := New(nil)
175
176 // Should not panic and should generate names
177 result := gen.Generate(2, "-")
178 if result == "" {
179 t.Error("Generator with nil rand returned empty string")
180 }
181
182 adverb := gen.Adverb()
183 if adverb == "" {
184 t.Error("Generator with nil rand Adverb() returned empty string")
185 }
186
187 adjective := gen.Adjective()
188 if adjective == "" {
189 t.Error("Generator with nil rand Adjective() returned empty string")
190 }
191
192 name := gen.Name()
193 if name == "" {
194 t.Error("Generator with nil rand Name() returned empty string")
195 }
196 }
197
198 // Test separator handling
199 func TestSeparators(t *testing.T) {
200 gen := New(rand.New(rand.NewSource(42)))
201
202 separators := []string{"-", "_", ".", "", " ", "::", "---"}
203 for _, sep := range separators {
204 result := gen.Generate(3, sep)
205 if result == "" {
206 t.Errorf("Generate(3, %q) returned empty string", sep)
207 }
208 }
209 }
210
211 // Benchmark old API
212 func BenchmarkGenerateOldAPI(b *testing.B) {
213 for i := 0; i < b.N; i++ {
214 Generate(2, "-")
215 }
216 }
217
218 // Benchmark new API with global rand (nil)
219 func BenchmarkGenerateNewAPIGlobalRand(b *testing.B) {
220 gen := New(nil)
221 b.ResetTimer()
222 for i := 0; i < b.N; i++ {
223 gen.Generate(2, "-")
224 }
225 }
226
227 // Benchmark new API with dedicated rand
228 func BenchmarkGenerateNewAPIDedicatedRand(b *testing.B) {
229 gen := New(rand.New(rand.NewSource(42)))
230 b.ResetTimer()
231 for i := 0; i < b.N; i++ {
232 gen.Generate(2, "-")
233 }
234 }