-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlge_test.go
488 lines (446 loc) · 12.2 KB
/
lge_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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// This file provides unit tests for the LGE data type.
package intern_test
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"math/rand"
"runtime"
"testing"
"github.com/spakin/intern"
)
// TestPreLGEDups tests if we can create a large number of symbols for which
// duplicates are certain to occur.
func TestPreLGEDups(t *testing.T) {
intern.ForgetAllLGEs()
const sLen = 3 // Symbol length in characters
const nSymbols = 1000000 // Must be greater than len(charSet) choose sLen
prng := rand.New(rand.NewSource(910)) // Constant for reproducibility
for i := 0; i < nSymbols; i++ {
intern.PreLGE(randomString(prng, sLen))
}
_, err := intern.NewLGE("Yet another string") // Force tree construction.
if err != nil {
t.Fatal(err)
}
}
// TestPreLGENoDups tests if we can create a large number of symbols for which
// duplicates are extremely unlikely to occur.
func TestPreLGENoDups(t *testing.T) {
intern.ForgetAllLGEs()
const sLen = 50 // Symbol length in characters
const nSymbols = 100000 // Number of symbols to generate
prng := rand.New(rand.NewSource(1112)) // Constant for reproducibility
for i := 0; i < nSymbols; i++ {
intern.PreLGE(randomString(prng, sLen))
}
_, err := intern.NewLGE("Yet another string") // Force tree construction.
if err != nil {
t.Fatal(err)
}
}
// TestNewLGEFull tests that the tree does fill up and return an error if we
// don't use PreLGE.
func TestNewLGEFull(t *testing.T) {
// Creating 64 symbols in alphabetical order should work.
intern.ForgetAllLGEs()
var i int
for i = 0; i < 64; i++ {
str := fmt.Sprintf("This is symbol #%03d.", i+1)
_, err := intern.NewLGE(str)
if err != nil {
t.Fatal(err)
}
}
// Creating 65 symbols in alphabetical order should fail.
str := fmt.Sprintf("This is symbol #%03d.", i)
_, err := intern.NewLGE(str)
if err == nil {
t.Fatal("NewLGE failed to return an error when its symbol table filled up")
}
}
// TestLGEOrder ensures that LGE symbol comparisons match the corresponding
// string comparisons.
func TestLGEOrder(t *testing.T) {
// Create a bunch of random strings.
intern.ForgetAllLGEs()
const sLen = 10 // Symbol length in characters
const nSymbols = 100 // Number of symbols to generate
prng := rand.New(rand.NewSource(1314)) // Constant for reproducibility
strList := make([]string, nSymbols)
for i := range strList {
strList[i] = randomString(prng, sLen)
}
// Convert all of the strings to LGE symbols.
for _, str := range strList {
intern.PreLGE(str)
}
symList := make([]intern.LGE, nSymbols)
for i, str := range strList {
var err error
symList[i], err = intern.NewLGE(str)
if err != nil {
t.Fatal(err)
}
}
// Compare all symbols.
for i, sym1 := range symList {
str1 := strList[i]
for j, sym2 := range symList {
str2 := strList[j]
switch {
case sym1 < sym2 && str1 < str2:
case sym1 == sym2 && str1 == str2:
case sym1 > sym2 && str1 > str2:
default:
t.Fatalf("Strings %q and %q mapped incorrectly to LGEs %d and %d", str1, str2, sym1, sym2)
}
}
}
}
// TestLGEString tests if we can convert strings to LGEs and back to strings.
func TestLGEString(t *testing.T) {
// Prepare the test.
const ns = 10000 // Number of strings to generate
strs := make([]string, ns) // Original strings
syms := make([]intern.LGE, ns) // Interned strings
prng := rand.New(rand.NewSource(1516)) // Constant for reproducibility
// Generate a bunch of strings.
for i := range strs {
nc := prng.Intn(20) + 1 // Number of characters
strs[i] = randomString(prng, nc)
}
// Intern each string to an LGE.
for _, s := range strs {
intern.PreLGE(s)
}
var err error
for i, s := range strs {
syms[i], err = intern.NewLGE(s)
if err != nil {
t.Fatal(err)
}
}
// Ensure that converting an LGE back to a string is a lossless
// operation. We use fmt.Sprintf as this represents a typical way an
// LGE might be converted to a string.
for i, str := range strs {
sym := syms[i]
sStr := fmt.Sprintf("%s", sym)
if str != sStr {
t.Fatalf("Expected %q but saw %q", str, sStr)
}
}
}
// TestLGEStringMulti tests if we can convert strings to LGEs and back to
// strings. Unlike TestLGEString, it uses PreLGEMulti and NewLGEMulti.
func TestLGEStringMulti(t *testing.T) {
// Prepare the test.
const ns = 10000 // Number of strings to generate
strs := make([]string, ns) // Original strings
prng := rand.New(rand.NewSource(1516)) // Constant for reproducibility
// Generate a bunch of strings.
for i := range strs {
nc := prng.Intn(20) + 1 // Number of characters
strs[i] = randomString(prng, nc)
}
// Intern each string to an LGE.
intern.PreLGEMulti(strs)
syms, err := intern.NewLGEMulti(strs)
if err != nil {
t.Fatal(err)
}
// Ensure that converting an LGE back to a string is a lossless
// operation. We use fmt.Sprintf as this represents a typical way an
// LGE might be converted to a string.
for i, str := range strs {
sym := syms[i]
sStr := fmt.Sprintf("%s", sym)
if str != sStr {
t.Fatalf("Expected %q but saw %q", str, sStr)
}
}
}
// TestLGEStringMultiEmpty tests if PreLGEMulti and NewLGEMulti can handle
// empty slices.
func TestLGEStringMultiEmpty(t *testing.T) {
strs := make([]string, 0)
intern.PreLGEMulti(strs)
_, err := intern.NewLGEMulti(strs)
if err != nil {
t.Fatal(err)
}
}
// TestBadLGE ensures we panic when converting an invalid LGE to a string.
func TestBadLGE(t *testing.T) {
defer func() { _ = recover() }()
var bad intern.LGE
_ = bad.String() // Should panic
t.Fatalf("Failed to catch invalid intern.LGE %d", bad)
}
// TestForgetAllLGEs ensures we panic when converting a forgotten LGE to a
// string.
func TestForgetAllLGEs(t *testing.T) {
defer func() { _ = recover() }()
sym, err := intern.NewLGE("old string")
if err != nil {
t.Fatal(err)
}
str := sym.String()
intern.ForgetAllLGEs()
str = sym.String() // Should panic
t.Fatalf("Failed to catch invalid intern.LGE %d (%q)", sym, str)
}
// TestLGECase ensures that symbol comparisons are case-sensitive.
func TestLGECase(t *testing.T) {
// Convert a set of strings to LGEs.
strs := []string{
"roadrunner",
"Roadrunner",
"roadRunner",
"ROADRUNNER",
"rOaDrUnNeR",
"ROADrunner",
"roadRUNNER",
}
syms := make([]intern.LGE, len(strs))
var err error
for i, s := range strs {
syms[i], err = intern.NewLGE(s)
if err != nil {
t.Fatal(err)
}
}
// Ensure that each symbol is equal only to itself.
numLGE := 0
for _, s1 := range syms {
for _, s2 := range syms {
if s1 == s2 {
numLGE++
}
}
}
if numLGE != len(syms) {
t.Fatalf("Expected %d case-sensitive comparisons but saw %d",
len(syms), numLGE)
}
}
// TestLGEConcurrent performs a bunch of accesses in parallel in an attempt to
// expose race conditions.
func TestLGEConcurrent(t *testing.T) {
const symsPerThread = 1000
nThreads := runtime.NumCPU() * 2 // Oversubscribe CPUs by a factor of 2.
// Spawn a number of goroutines.
begin := make(chan bool, nThreads)
done := make(chan bool, nThreads)
for j := 0; j < nThreads; j++ {
go func() {
_ = <-begin
prng := rand.New(rand.NewSource(2021)) // Constant for reproducibility and to invite conflicts
for i := 0; i < symsPerThread; i++ {
nc := prng.Intn(20) + 1 // Number of characters
intern.PreLGE(randomString(prng, nc))
}
prng = rand.New(rand.NewSource(2021)) // Restart from the same seed.
for i := 0; i < symsPerThread; i++ {
nc := prng.Intn(20) + 1 // Number of characters
_, err := intern.NewLGE(randomString(prng, nc))
if err != nil {
t.Fatal(err)
}
}
done <- true
}()
}
// Tell all goroutines to begin then wait for them all to finish.
for j := 0; j < nThreads; j++ {
begin <- true
}
for j := 0; j < nThreads; j++ {
_ = <-done
}
}
// TestRemapAllLGEs tests that old strings are remapped and pending strings are
// added.
func TestRemapAllLGEs(t *testing.T) {
// Define a list of strings and allocate space for two lists of
// associated LGEs.
strs := []string{
"David J. Thouless",
"F. Duncan M. Haldane",
"J. Michael Kosterlitz",
"Jean-Pierre Sauvage",
"Sir J. Fraser Stoddart",
"Bernard L. Feringa",
"Yoshinori Ohsumi",
"Bob Dylan",
"Juan Manuel Santos",
"Oliver Hart",
"Bengt Holmström",
}
nStrs := len(strs)
syms0 := make([]intern.LGE, nStrs/2)
syms1 := make([]intern.LGE, nStrs)
// Generate symbols for half the names.
for i := 0; i < nStrs/2; i++ {
intern.PreLGE(strs[i])
}
var err error
for i := 0; i < nStrs/2; i++ {
syms0[i], err = intern.NewLGE(strs[i])
if err != nil {
t.Fatal(err)
}
}
// Preallocate the remaining symbols (with a bit of overlap to test
// that, too), but don't actually allocate them.
for i := nStrs/2 - 1; i < nStrs; i++ {
intern.PreLGE(strs[i])
}
// Remap all old and pending symbols.
m, err := intern.RemapAllLGEs()
if err != nil {
t.Fatal(err)
}
for i, s := range strs {
syms1[i], err = intern.NewLGE(s)
if err != nil {
t.Fatal(err)
}
}
// Confirm that the map is accurate.
for i, s := range strs[:nStrs/2] {
s0 := syms0[i]
s1 := syms1[i]
if m[s0] != s1 {
t.Fatalf("For %q, expected %d to map to %d but it instead mapped to %d",
s, s0, s1, m[s0])
}
}
// Confirm that all new LGEs compare with each other as expected.
for i, a0 := range strs {
b0 := syms1[i]
for j, a1 := range strs {
b1 := syms1[j]
switch {
case a0 == a1 && b0 == b1:
case a0 < a1 && b0 < b1:
case a0 > a1 && b0 > b1:
default:
t.Fatalf("Comparison of strings %q and %q does not match comparison of LGEs %d and %d",
a0, a1, b0, b1)
}
}
}
}
// TestLGEMarshalJSON marshals LGEs to JSON and back and checks that the outputs
// match the input.
func TestLGEMarshalJSON(t *testing.T) {
for r, rStr := range []string{
"NoForget",
"Forget",
} {
t.Run(rStr, func(t *testing.T) {
// Create a long slice of LGEs.
intern.ForgetAllLGEs()
iSyms, err := intern.NewLGEMulti(ozChars)
if err != nil {
t.Fatal(err)
}
// Encode the LGEs as JSON.
b, err := json.MarshalIndent(iSyms, "", " ")
if err != nil {
t.Fatal(err)
}
// On our second iteration, forget our entire mapping.
if r == 1 {
intern.ForgetAllLGEs()
}
// Convert the JSON back to a slice of LGEs. If we
// fail, remap all of our LGEs and try again.
var oSyms []intern.LGE
KeepTrying:
for {
err = json.Unmarshal(b, &oSyms)
switch e := err.(type) {
case nil:
break KeepTrying
case *intern.PkgError:
if e.Code != intern.ErrTableFull {
t.Fatal(err)
}
_, err = intern.RemapAllLGEs()
if err != nil {
t.Fatal(err)
}
default:
t.Fatal(err)
}
}
// Ensure that the outputs match the original strings.
for i, s := range ozChars {
if s != oSyms[i].String() {
t.Fatalf("Expected %q but saw input symbol %q", s, oSyms[i])
}
}
})
}
}
// TestLGEMarshalGob marshals LGEs to a gob and back and checks that the
// outputs match the input.
func TestLGEMarshalGob(t *testing.T) {
for r, rStr := range []string{
"NoForget",
"Forget",
} {
t.Run(rStr, func(t *testing.T) {
// Create a long slice of LGEs.
intern.ForgetAllLGEs()
iSyms, err := intern.NewLGEMulti(ozChars)
if err != nil {
t.Fatal(err)
}
// Encode the LGEs as a gob.
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err = enc.Encode(&iSyms)
if err != nil {
t.Fatal(err)
}
// On our second iteration, forget our entire mapping.
if r == 1 {
intern.ForgetAllLGEs()
}
// Convert the gob back to a slice of LGEs. If we
// fail, remap all of our LGEs and try again.
var oSyms []intern.LGE
b := buf.Bytes()
KeepTrying:
for {
dec := gob.NewDecoder(bytes.NewBuffer(b))
err = dec.Decode(&oSyms)
switch e := err.(type) {
case nil:
break KeepTrying
case *intern.PkgError:
if e.Code != intern.ErrTableFull {
t.Fatal(err)
}
_, err = intern.RemapAllLGEs()
if err != nil {
t.Fatal(err)
}
default:
t.Fatal(err)
}
}
// Ensure that the outputs match the original strings.
for i, s := range ozChars {
if s != oSyms[i].String() {
t.Fatalf("Expected %q but saw input symbol %q", s, oSyms[i])
}
}
})
}
}