| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package petname |
| 23 |
|
| 24 |
import ( |
| 25 |
"math/rand" |
| 26 |
"sync" |
| 27 |
"testing" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 105 |
func TestDeterministicBehavior(t *testing.T) { |
| 106 |
seed := int64(12345) |
| 107 |
|
| 108 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 173 |
func TestNilRandUsesGlobalRand(t *testing.T) { |
| 174 |
gen := New(nil) |
| 175 |
|
| 176 |
|
| 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 |
|
| 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 |
|
| 212 |
func BenchmarkGenerateOldAPI(b *testing.B) { |
| 213 |
for i := 0; i < b.N; i++ { |
| 214 |
Generate(2, "-") |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 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 |
|
| 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 |
} |