/
z_test.go
450 lines (364 loc) · 10.9 KB
/
z_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package codex
// Tests.
import (
"fmt"
"reflect"
"sort"
"testing"
)
/********************************** Globals **********************************/
var testWords = []string{
"go", "nebula", "aurora", "theron", "thorax", "deity", "quasar",
}
var testManyWords = []string{
"go", "nebula", "aurora", "theron", "thorax", "deity",
"quasar", "graphene", "nanite", "orchestra", "eridium",
}
// Words with the following traits:
// * 5-6 sounds;
// * 2 vowels;
// * 1 max consequtive vowel;
// * 1-2 max consequtive consonants.
var testLimitedWords = []string{
"theron", "thorax", "rocket", "proton", "filler", "absurd", "paper",
}
var testDefWords = testWords
const testDefCount = 12
/*********************************** Tests ***********************************/
// NewTraits()
func Test_NewTraits(t *testing.T) {
// t.SkipNow()
traits, err := NewTraits(testLimitedWords)
tmust(t, err)
if traits == nil {
t.Fatal("missing traits object")
}
// MinNSounds
if traits.MinNSounds != 5 {
t.Fatalf("MinNSounds mismatch: expected %v, got %v", 5, traits.MinNSounds)
}
// MaxNSounds
if traits.MaxNSounds != 6 {
t.Fatalf("MaxNSounds mismatch: expected %v, got %v", 6, traits.MaxNSounds)
}
// MinNVowels
if traits.MinNVowels != 2 {
t.Fatalf("MinNVowels mismatch: expected %v, got %v", 2, traits.MinNVowels)
}
// MaxNVowels
if traits.MaxNVowels != 2 {
t.Fatalf("MaxNVowels mismatch: expected %v, got %v", 2, traits.MaxNVowels)
}
// MaxConseqVow
if traits.MaxConseqVow != 1 {
t.Fatalf("MaxConseqVow mismatch: expected %v, got %v", 1, traits.MaxConseqVow)
}
// MaxConseqCons
if traits.MaxConseqCons != 2 {
t.Fatalf("MaxConseqCons mismatch: expected %v, got %v", 2, traits.MaxConseqCons)
}
// SoundSet
sounds := Set{}
for _, word := range testLimitedWords {
sequence, err := getSounds(word, traits.knownSounds())
tmust(t, err)
for _, sound := range sequence {
sounds.Add(sound)
}
}
if !reflect.DeepEqual(traits.SoundSet, sounds) {
t.Fatalf("SoundSet mismatch")
}
// PairSet
pairs := PairSet{}
for _, word := range testLimitedWords {
sequence, err := getSounds(word, traits.knownSounds())
tmust(t, err)
for pair := range getPairs(sequence) {
pairs.Add(pair)
}
}
if !reflect.DeepEqual(traits.PairSet, pairs) {
t.Fatalf("PairSet mismatch")
}
}
// Traits.Generator()
func Test_Traits_Generator(t *testing.T) {
// t.SkipNow()
traits, err := NewTraits(testDefWords)
tmust(t, err)
gen := traits.Generator()
if gen == nil {
t.Fatal("missing generator function")
}
if gen() == "" {
t.Fatal("no output received from generator")
}
}
// Checks a generator's output. Also verifies that a generator eventually
// exhausts its word set (will enter an infinite loop otherwise).
func Test_Generator(t *testing.T) {
// t.SkipNow()
Test_Traits_Generator(t)
traits, _ := NewTraits(testDefWords)
gen := traits.Generator()
words := Set{}
// Collect the total output, check each word's validity and uniqueness.
for word := gen(); word != ""; word = gen() {
sounds, err := getSounds(word, traits.knownSounds())
tmust(t, err)
if !traits.validComplete(sounds...) {
t.Fatal("invalid output from generator:", word)
}
if words.Has(word) {
t.Fatal("repeated output from generator:", word)
}
words.Add(word)
}
// The output for a dozen source words can easily reach tens of thousands of
// results. We're being very conservative here.
if len(words) < 100 {
t.Fatal("unexpectedly small number of words:", len(words))
}
// t.Log("total words in sample:", len(words))
// t.Log("words in sample:", words)
}
// Verifies that the words returned from a generator match its source traits.
func Test_Generator_Words_Match_Traits(t *testing.T) {
// t.SkipNow()
Test_Generator(t)
traits, _ := NewTraits(testLimitedWords)
words := collectAll(traits)
test_Words_Match_Traits(t, traits, words)
}
// Verifies that NewTraits() produces an error with invalid input.
func Test_Invalid_Input(t *testing.T) {
// t.SkipNow()
Test_NewTraits(t)
invalids := []string{
"", "a", "CAPITALS", "Capitalised", "with space",
"numbers134125", "łàtîñôñè", "кириллица",
}
for _, invalid := range invalids {
traits, err := NewTraits([]string{invalid})
if traits != nil || err == nil {
t.Fatalf("expected nil traits and non-nil error, got %v and %v", traits, err)
}
}
}
// Verifies that Traits.Examine() and NewTraits() are equivalent.
func Test_Traits_Examine(t *testing.T) {
// t.SkipNow()
Test_NewTraits(t)
traits := new(Traits)
tmust(t, traits.Examine(testDefWords))
other, _ := NewTraits(testDefWords)
if !reflect.DeepEqual(traits, other) {
t.Fatal("expected new(Traits) + Traits.Examine() to be equivalent to NewTraits()")
}
if !reflect.DeepEqual(collectAll(traits), collectAll(other)) {
t.Fatal("expected resulting word sets to be equivalent")
}
}
// Verifies that a Traits object uses internal known sounds, if available.
func Test_Traits_KnownSounds(t *testing.T) {
// t.SkipNow()
Test_Traits_Examine(t)
traits := new(Traits)
traits.KnownSounds = Set.New(nil,
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
)
if reflect.DeepEqual(traits.knownSounds(), knownSounds) {
t.Fatal("expected Traits.knownSounds() to return the internal sound set")
}
if traits.Examine(testDefWords) == nil {
t.Fatal("expected Traits.Examine() to fail when used with a limited sound set")
}
}
// Verifies that a Traits object uses internal known vowels, if available.
func Test_Traits_KnownVowels(t *testing.T) {
// t.SkipNow()
Test_NewTraits(t)
Test_Traits_Examine(t)
traits := new(Traits)
traits.KnownVowels = Set.New(nil,
"a", "e", "i",
)
if reflect.DeepEqual(traits.knownVowels(), knownVowels) {
t.Fatal("expected Traits.knownVowels() to return the internal vowel set")
}
if traits.Examine(testDefWords) != nil {
t.Fatal("expected Traits.Examine() to complete successfully when used with a limited vowel set")
}
other, _ := NewTraits(testDefWords)
if reflect.DeepEqual(traits, other) {
t.Fatalf("expected Traits.Examine() with custom vowels to produce different traits")
}
if reflect.DeepEqual(collectAll(traits), collectAll(other)) {
t.Fatal("expected resulting word sets to be different")
}
}
// Verifies that words from a generator are randomly distributed. Rudimental and
// naive, todo remember some math and use a real probability function.
func Test_Generator_Random_Distribution(t *testing.T) {
// t.SkipNow()
Test_Generator(t)
traits, _ := NewTraits(testDefWords)
// Make a sorted list of words.
gen := traits.Generator()
unordered := Set{}
for word := gen(); word != ""; word = gen() {
unordered.Add(word)
}
words := make([]string, 0, len(unordered))
for word := range unordered {
words = append(words, word)
}
sort.Strings(words)
// Limit of how many tight groups to permit.
maxTightGroups := len(words) / testDefCount / 10
if maxTightGroups == 0 {
maxTightGroups = 1
}
// Counter of tight group occurrences.
count := 0
// Prepare a generator that makes words in chunks.
wordsN := generatorN(traits)
// Loop over generator results and count how many times all indices from
// a sample fall within a tight range (let's say 1/5th the length).
for sample := wordsN(testDefCount); len(sample) > 0; sample = wordsN(testDefCount) {
indices := make([]int, 0, len(sample))
for word := range sample {
indices = append(indices, findIndex(words, word))
}
if maximum(indices)-minimum(indices) < len(words)/5 {
count++
}
}
if count > maxTightGroups {
t.Fatalf("for %v sorted words, %v out of %v samples were tightly grouped", len(words), count, len(words)/testDefCount+1)
}
}
/********************************** Helpers **********************************/
// Words_Match_Traits helper.
func test_Words_Match_Traits(t *testing.T, traits *Traits, words Set) {
for word := range words {
// MinNSounds
sounds, err := getSounds(word, traits.knownSounds())
tmust(t, err)
if len(sounds) < traits.MinNSounds {
t.Fatalf("\"%v\" MinNSounds mismatch: expected >=%v, got %v", word, traits.MinNSounds, len(sounds))
}
// MaxNSounds
if len(sounds) > traits.MaxNSounds {
t.Fatalf("\"%v\" MaxNSounds mismatch: expected <=%v, got %v", word, traits.MaxNSounds, len(sounds))
}
// MinNVowels
if n := traits.countVowels(sounds); n < traits.MinNVowels {
t.Fatalf("\"%v\" MinNVowels mismatch: expected >=%v, got %v", word, traits.MinNVowels, n)
}
// MaxNVowels
if n := traits.countVowels(sounds); n > traits.MaxNVowels {
t.Fatalf("\"%v\" MaxNVowels mismatch: expected <=%v, got %v", word, traits.MaxNVowels, n)
}
// MaxConseqVow
if n := traits.maxConsequtiveVowels(sounds); n > traits.MaxConseqVow {
t.Fatalf("\"%v\" MaxConseqVow mismatch: expected <=%v, got %v", word, traits.MaxConseqVow, n)
}
// MaxConseqCons
if n := traits.maxConsequtiveConsonants(sounds); n > traits.MaxConseqCons {
t.Fatalf("\"%v\" MaxConseqCons mismatch: expected <=%v, got %v", word, traits.MaxConseqCons, n)
}
// SoundSet
for sound := range Set.New(nil, sounds...) {
if !traits.SoundSet.Has(sound) {
t.Fatalf("\"%v\" SoundSet mismatch, unexpected sound: %v", word, sound)
}
}
// PairSet
for pair := range getPairs(sounds) {
if !traits.PairSet.Has(pair) {
t.Fatalf("\"%v\" PairSet mismatch, unexpected pair: %v", word, pair)
}
}
}
}
/*********************************** Utils ***********************************/
// Prints expanded values.
func prn(values ...interface{}) {
var result string
for i := 0; i < len(values); i++ {
if reflect.ValueOf(values[i]).Kind() == reflect.String {
result += fmt.Sprintf("%v", values[i])
} else {
result += fmt.Sprintf("%#v", values[i])
}
if i < len(values)-1 {
result += " "
}
}
fmt.Println(result)
}
// Prints simple values.
func log(values ...interface{}) {
fmt.Println(values...)
}
func tmust(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}
// Finds the position of the given value in the given slice.
func findIndex(group []string, value string) int {
for index, val := range group {
if val == value {
return index
}
}
return -1
}
func minimum(values []int) int {
if len(values) == 0 {
return 0
}
result := values[0]
for _, value := range values {
if value < result {
result = value
}
}
return result
}
func maximum(values []int) int {
if len(values) == 0 {
return 0
}
result := values[0]
for _, value := range values {
if value > result {
result = value
}
}
return result
}
// Creates a function that generates words in chunks until its inner generator
// is exhausted.
func generatorN(traits *Traits) func(int) Set {
gen := traits.Generator()
return func(num int) Set {
words := Set{}
for word := gen(); word != "" && len(words) < num; word = gen() {
words.Add(word)
}
return words
}
}
// Collects all words from the given traits.
func collectAll(traits *Traits) Set {
words := Set{}
gen := traits.Generator()
for word := gen(); word != ""; word = gen() {
words.Add(word)
}
return words
}