-
Notifications
You must be signed in to change notification settings - Fork 0
/
smac_lino_test.go
579 lines (485 loc) · 18.5 KB
/
smac_lino_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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// Copyright Piero de Salvia.
// All Rights Reserved
package smac
import (
"encoding/gob"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"sort"
"testing"
)
var (
words []string
prefix0 map[string]string
prefix1 map[string]string
prefix2 map[string]string
prefix3 map[string]string
)
func initTestVals() {
words = []string{"aaaa", "aabc", "abc", "bbb", "v", "vvv", "vvvaaa"}
prefix0 = make(map[string]string)
prefix1 = make(map[string]string)
prefix2 = make(map[string]string)
prefix3 = make(map[string]string)
prefix1["a"] = "aaaa"
prefix1["b"] = "bbb"
prefix1["v"] = "v"
for k, v := range prefix1 {
prefix2[k] = v
}
prefix2["aa"] = "aaaa"
prefix2["ab"] = "abc"
prefix2["bb"] = "bbb"
prefix2["vv"] = "vvv"
for k, v := range prefix2 {
prefix3[k] = v
}
prefix3["aaa"] = "aaaa"
prefix3["aab"] = "aabc"
prefix3["abc"] = "abc"
prefix3["bbb"] = "bbb"
prefix3["vvv"] = "vvv"
sort.Strings(words)
}
func TestLinoConstruction(t *testing.T) {
initTestVals()
t.Log("Given the need to test the prefix map creation")
{
prefixes := makePrefixMap(words, 0)
if !reflect.DeepEqual(prefixes, prefix0) {
t.Log("Should be able to create a 0 len prefix map", ballotX)
}
t.Log("Should be able to create a 0 len prefix map", checkMark)
prefixes = makePrefixMap(words, 1)
if !reflect.DeepEqual(prefixes, prefix1) {
t.Fatal("Should be able to create a 1 len prefix map", ballotX)
}
t.Log("Should be able to create a 1 len prefix map", checkMark)
prefixes = makePrefixMap(words, 2)
if !reflect.DeepEqual(prefixes, prefix2) {
t.Fatal("Should be able to create a 2 len prefix map", ballotX)
}
t.Log("Should be able to create a 2 len prefix map", checkMark)
prefixes = makePrefixMap(words, 3)
if !reflect.DeepEqual(prefixes, prefix3) {
t.Fatal("Should be able to create a 3 len prefix map", ballotX)
}
t.Log("Should be able to create a 3 len prefix map", checkMark)
}
t.Log("Given the need to test the lino creation")
{
words := []string{"aaaa", "aabc", "abc", "bbb", "v", "vvv", "vvvaaa"}
autoComplete, _ := NewAutoCompleteLinoS(words, 0, 0, 0)
for i, word := range words[:len(words)-1] {
nextWord := autoComplete.wordMap[word].next
if nextWord != words[i+1] {
t.Fatal("Should be able to build a linked list of dictionary words", ballotX)
}
}
t.Log("Should be able to build a linked list of dictionary words", checkMark)
if autoComplete.head != "aaaa" {
t.Fatal("Should be able to correctly initialize the head of an autocompleter", ballotX)
}
t.Log("Should be able to correctly initialize the head of an autocompleter", checkMark)
if autoComplete.tail != "vvvaaa" {
t.Fatal("Should be able to correctly initialize the tail of an autocompleter", ballotX)
}
t.Log("Should be able to correctly initialize the tail of an autocompleter", checkMark)
}
}
func TestLinoFindPreviousWord(t *testing.T) {
initTestVals()
t.Log("Given the need to test the lino navigation function")
{
autoComplete, _ := NewAutoCompleteLinoS(words, 2, 0, 0)
if autoComplete.findPreviousWord("vvv") != "v" {
t.Fatal("Should be able to correctly navigate a lino on a prefix word", ballotX)
}
t.Log("Should be able to correctly navigate a lino on a prefix word", checkMark)
if autoComplete.findPreviousWord("bba") != "abc" {
t.Fatal("Should be able to correctly navigate a lino on a pre-prefix word", ballotX)
}
t.Log("Should be able to correctly navigate a lino on a pre-prefix word", checkMark)
if autoComplete.findPreviousWord("bbc") != "bbb" {
t.Fatal("Should be able to correctly navigate a lino on a post-prefix word", ballotX)
}
t.Log("Should be able to correctly navigate a lino on a post-prefix word", checkMark)
if autoComplete.findPreviousWord("t") != "bbb" {
t.Fatal("Should be able to correctly navigate a lino on a non-prefix word", ballotX)
}
t.Log("Should be able to correctly navigate a lino on a non-prefix word", checkMark)
if autoComplete.findPreviousWord("a") != autoComplete.head {
t.Fatal("Should be able to correctly navigate a lino on a pre-head word", ballotX)
}
t.Log("Should be able to correctly navigate a lino on a pre-head word", checkMark)
if autoComplete.findPreviousWord("z") != autoComplete.tail {
t.Fatal("Should be able to correctly navigate a lino on a post-tail word", ballotX)
}
t.Log("Should be able to correctly navigate a lino on a post-tail word", checkMark)
}
}
func TestLinoLearnCoherence(t *testing.T) {
initTestVals()
t.Log("Given the need to test the lino learn coherence")
{
autoComplete, _ := NewAutoCompleteLinoS(words, 2, 0, 0)
autoComplete.Learn("bba")
newWords := []string(words)
newWords = append(newWords, "bba")
sort.Strings(newWords)
for i, word := range newWords[:len(newWords)-1] {
nextWord := autoComplete.wordMap[word].next
if nextWord != newWords[i+1] {
t.Fatal("Should be able to rebuild a linked list of dictionary words", ballotX)
}
}
t.Log("Should be able to rebuild a linked list of dictionary words", checkMark)
prefix2["bb"] = "bba"
prefix2["b"] = "bba"
if !reflect.DeepEqual(autoComplete.prefixMap, prefix2) {
t.Fatal("Should be able to rebuild the prefix map of an autocompleter", ballotX)
}
t.Log("Should be able to rebuild the prefix map of an autocompleter", checkMark)
autoComplete.Learn("zzz")
newWords = append(newWords, "zzz")
sort.Strings(newWords)
for i, word := range newWords[:len(newWords)-1] {
nextWord := autoComplete.wordMap[word].next
if nextWord != newWords[i+1] {
t.Fatal("Should be able to rebuild a linked list of dictionary words on a tail word", ballotX)
}
}
t.Log("Should be able to rebuild a linked list of dictionary words on a tail word", checkMark)
if autoComplete.tail != "zzz" {
t.Fatal("Should be able to correctly re-initialize the tail of an autocompleter", ballotX)
}
t.Log("Should be able to correctly re-initialize the tail of an autocompleter", checkMark)
prefix2["zz"] = "zzz"
prefix2["z"] = "zzz"
if !reflect.DeepEqual(autoComplete.prefixMap, prefix2) {
t.Fatal("Should be able to rebuild the prefix map of an autocompleter on a tail insert", ballotX)
}
t.Log("Should be able to rebuild the prefix map of an autocompleter on a tail insert", checkMark)
autoComplete.Learn("a")
newWords = append(newWords, "a")
sort.Strings(newWords)
for i, word := range newWords[:len(newWords)-1] {
nextWord := autoComplete.wordMap[word].next
if nextWord != newWords[i+1] {
t.Fatal("Should be able to rebuild a linked list of dictionary words on a head word", ballotX)
}
}
t.Log("Should be able to rebuild a linked list of dictionary words on a tail word", checkMark)
if autoComplete.head != "a" {
t.Fatal("Should be able to correctly initialize the head of an autocompleter", ballotX)
}
t.Log("Should be able to correctly re-initialize the head of an autocompleter", checkMark)
prefix2["a"] = "a"
if !reflect.DeepEqual(autoComplete.prefixMap, prefix2) {
t.Fatal("Should be able to rebuild the prefix map of an autocompleter on a head insert", ballotX)
}
t.Log("Should be able to rebuild the prefix map of an autocompleter on a head insert", checkMark)
}
}
func TestLinoUnLearnCoherence(t *testing.T) {
initTestVals()
t.Log("Given the need to test the lino un learn coherence")
{
autoComplete, _ := NewAutoCompleteLinoS(words, 2, 0, 0)
autoComplete.UnLearn("aaaa")
newWords := []string{"aabc", "abc", "bbb", "v", "vvv", "vvvaaa"}
for i, word := range newWords[:len(newWords)-1] {
nextWord := autoComplete.wordMap[word].next
if nextWord != newWords[i+1] {
t.Fatal("Should be able to rebuild a linked list of dictionary words", ballotX)
}
}
t.Log("Should be able to rebuild a linked list of dictionary words", checkMark)
if autoComplete.head != "aabc" {
t.Fatal("Should be able to correctly reassign the head of an autocompleter", ballotX)
}
t.Log("Should be able to correctly reassign the head of an autocompleter", checkMark)
prefix2["aa"] = "aabc"
prefix2["a"] = "aabc"
if !reflect.DeepEqual(autoComplete.prefixMap, prefix2) {
t.Log(autoComplete.prefixMap)
t.Fatal("Should be able to rebuild the prefix map of an autocompleter on a head removal", ballotX)
}
t.Log("Should be able to rebuild the prefix map of an autocompleter on a head removal", checkMark)
autoComplete.UnLearn("vvvaaa")
newWords = []string{"aabc", "abc", "bbb", "v", "vvv"}
for i, word := range newWords[:len(newWords)-1] {
nextWord := autoComplete.wordMap[word].next
if nextWord != newWords[i+1] {
t.Fatal("Should be able to rebuild a linked list of dictionary words", ballotX)
}
}
t.Log("Should be able to rebuild a linked list of dictionary words", checkMark)
if autoComplete.tail != "vvv" {
t.Fatal("Should be able to correctly reassign the tail of an autocompleter", ballotX)
}
t.Log("Should be able to correctly reassign the tail of an autocompleter", checkMark)
prefix2["vv"] = "vvv"
if !reflect.DeepEqual(autoComplete.prefixMap, prefix2) {
t.Fatal("Should be able to rebuild the prefix map of an autocompleter on a tail removal", ballotX)
}
t.Log("Should be able to rebuild the prefix map of an autocompleter on a tail removal", checkMark)
autoComplete.UnLearn("bbb")
newWords = []string{"aabc", "abc", "v", "vvv"}
for i, word := range newWords[:len(newWords)-1] {
nextWord := autoComplete.wordMap[word].next
if nextWord != newWords[i+1] {
t.Fatal("Should be able to rebuild a linked list of dictionary words", ballotX)
}
}
t.Log("Should be able to rebuild a linked list of dictionary words", checkMark)
delete(prefix2, "b")
delete(prefix2, "bb")
if !reflect.DeepEqual(autoComplete.prefixMap, prefix2) {
t.Log(autoComplete.prefixMap)
t.Log(prefix2)
t.Fatal("Should be able to rebuild the prefix map of an autocompleter on prefix word removal", ballotX)
}
t.Log("Should be able to rebuild the prefix map of an autocompleter on prefix word removal", checkMark)
initTestVals()
autoComplete, _ = NewAutoCompleteLinoS(words, 2, 0, 0)
autoComplete.UnLearn("aabc")
newWords = []string{"aaaa", "abc", "bbb", "v", "vvv", "vvvaaa"}
for i, word := range newWords[:len(newWords)-1] {
nextWord := autoComplete.wordMap[word].next
if nextWord != newWords[i+1] {
t.Fatal("Should be able to rebuild a linked list of dictionary words", ballotX)
}
}
t.Log("Should be able to rebuild a linked list of dictionary words", checkMark)
if !reflect.DeepEqual(autoComplete.prefixMap, prefix2) {
t.Fatal("Should be able to maintain prefix map of an autocompleter on a non-prefix word removal", ballotX)
}
t.Log("Should be able to maintain the prefix map of an autocompleter on a non-prefix word removal", checkMark)
}
}
func TestLinoSaveAndRetrieve(t *testing.T) {
tempDir := os.TempDir()
tempFile, err := ioutil.TempFile(tempDir, "smac")
if err != nil {
t.Fatal(err)
}
fName := tempFile.Name()
initTestVals()
autoComplete, _ := NewAutoCompleteLinoS(words, 2, 0, 0)
autoComplete.Accept("aabc")
autoComplete.Learn("ddd")
autoComplete.Learn("eee")
autoComplete.Accept("eee")
autoComplete.UnLearn("vvv")
t.Log("Given the need to test the save/retrieve feature")
{
err = autoComplete.Save(tempFile.Name())
if err != nil {
t.Fatal("Should be able to save words to a file", ballotX)
}
t.Log("Should be able to save words to a file", checkMark)
f, err := os.Open(fName)
defer f.Close()
if err != nil {
t.Fatal(err)
}
dec := gob.NewDecoder(f)
readWords := make(map[string]int)
for {
var wA wordAccepts
if err = dec.Decode(&wA); err == io.EOF {
break
} else if err != nil {
t.Fatal("Should be able to read a smac file")
}
readWords[wA.Word] = wA.Accepts
}
accepts, exists := readWords["aabc"]
if !exists {
t.Fatal("Should be able to persist an accepted word", ballotX)
}
t.Log("Should be able to perstist an accepted word", checkMark)
if accepts != 1 {
t.Fatal("Should be able to persist an accepted word", ballotX)
}
t.Log("Should be able to perstist an accepted word", checkMark)
accepts, exists = readWords["ddd"]
if !exists {
t.Fatal("Should be able to persist a learnt word", ballotX)
}
t.Log("Should be able to persist a learnt word", checkMark)
accepts, exists = readWords["eee"]
if !exists {
t.Fatal("Should be able to persist a learnt word", ballotX)
}
t.Log("Should be able to persist a learnt word", checkMark)
if accepts != 1 {
t.Fatal("Should be able to persist a learnt and accepted word", ballotX)
}
t.Log("Should be able to persist a learnt and accepted word", checkMark)
accepts, exists = readWords["vvv"]
if !exists {
t.Fatal("Should be able to persist an unlearnt word", ballotX)
}
t.Log("Should be able to persist an unlearnt word", checkMark)
if accepts != -1 {
t.Fatal("Should be able to persist a learnt and accepted word", ballotX)
}
t.Log("Should be able to persist a learnt and accepted word", checkMark)
autoComplete, _ = NewAutoCompleteLinoS(words, 2, 0, 0)
err = autoComplete.Retrieve(fName)
if err != nil {
t.Fatal(err)
}
}
}
func TestLinoAccept(t *testing.T) {
initTestVals()
autoComplete, _ := NewAutoCompleteLinoS(words, 2, 0, 0)
autoComplete.Accept("abc")
ac, _ := autoComplete.Complete("a")
if !reflect.DeepEqual(ac, []string{"abc", "aaaa", "aabc"}) {
fmt.Println(ac)
t.Fatal("Should be able to prioritize an accepted word", ballotX)
}
t.Log("Should be able to prioritize an accepted word", checkMark)
autoComplete.Accept("abc")
autoComplete.Accept("aaaa")
ac, _ = autoComplete.Complete("a")
if !reflect.DeepEqual(ac, []string{"abc", "aaaa", "aabc"}) {
t.Fatal("Should be able to prioritize priorities", ballotX)
}
t.Log("Should be able to prioritize priorities", checkMark)
}
func TestLinoComplete(t *testing.T) {
words = []string{"aaa", "aaa1", "aaa2", "aaa3", "aaa4", "aaa5", "aaa6", "aaa7", "bbb"}
autoComplete, _ := NewAutoCompleteLinoS(words, 2, 0, 0)
ac, _ := autoComplete.Complete("a")
if !reflect.DeepEqual(ac, []string{"aaa", "aaa1", "aaa2", "aaa3", "aaa4", "aaa5", "aaa6", "aaa7"}) {
t.Fatal("Should be able to correctly complete a prefix", ballotX)
}
t.Log("Should be able to correctly complete a prefix", checkMark)
autoComplete, _ = NewAutoCompleteLinoS(words, 2, 3, 5)
ac, _ = autoComplete.Complete("a")
if !reflect.DeepEqual(ac, []string{"aaa", "aaa1", "aaa2"}) {
t.Fatal("Should be able to limit completion to result size", ballotX)
}
t.Log("Should be able to limit completion to result size", checkMark)
autoComplete.Accept("aaa4")
ac, _ = autoComplete.Complete("a")
if !reflect.DeepEqual(ac, []string{"aaa4", "aaa", "aaa1"}) {
t.Log(ac)
t.Fatal("Should be able to extend completion to radius", ballotX)
}
t.Log("Should be able to limit completion to radius", checkMark)
autoComplete, err := NewAutoCompleteLinoS(words, 2, 5, 3)
if err == nil {
t.Fatal("Should be able to reject resultSize > radius", ballotX)
}
t.Log("Should be able to reject resultSize > radius", checkMark)
}
func TestLinoE(t *testing.T) {
autoComplete, _ := NewAutoCompleteLinoE(2, 0, 0)
ac, _ := autoComplete.Complete("chair")
if !reflect.DeepEqual(ac, []string{}) {
t.Fatal("Should be able to initialize an empty autocompleter", ballotX)
}
t.Log("Should be able to initialize an empty autocompleter", checkMark)
autoComplete.Learn("vvvaaa")
ac, _ = autoComplete.Complete("chair")
if !reflect.DeepEqual(ac, []string{}) {
t.Fatal("Should be able to learn the first word", ballotX)
}
t.Log("Should be able to learn the first word", checkMark)
ac, _ = autoComplete.Complete("v")
if !reflect.DeepEqual(ac, []string{"vvvaaa"}) {
t.Fatal("Should be able to learn the first word", ballotX)
}
t.Log("Should be able to learn the first word", checkMark)
ac, _ = autoComplete.Complete("vv")
if !reflect.DeepEqual(ac, []string{"vvvaaa"}) {
t.Fatal("Should be able to learn the first word", ballotX)
}
t.Log("Should be able to learn the first word", checkMark)
ac, _ = autoComplete.Complete("vvva")
if !reflect.DeepEqual(ac, []string{"vvvaaa"}) {
t.Fatal("Should be able to learn the first word", ballotX)
}
t.Log("Should be able to learn the first word", checkMark)
autoComplete.Learn("aabc")
autoComplete.Learn("aaaa")
autoComplete.Learn("bbb")
autoComplete.Learn("abc")
autoComplete.Learn("vvv")
autoComplete.Learn("v")
ac, _ = autoComplete.Complete("aa")
if !reflect.DeepEqual(ac, []string{"aaaa", "aabc"}) {
t.Fatal("Should be able to learn words after the first", ballotX)
}
t.Log("Should be able to learn words after the first", checkMark)
ac, _ = autoComplete.Complete("v")
if !reflect.DeepEqual(ac, []string{"v", "vvv", "vvvaaa"}) {
t.Fatal("Should be able to learn words after the first", ballotX)
}
t.Log("Should be able to learn words after the first", checkMark)
ac, _ = autoComplete.Complete("vv")
if !reflect.DeepEqual(ac, []string{"vvv", "vvvaaa"}) {
t.Fatal("Should be able to learn words after the first", ballotX)
}
t.Log("Should be able to learn words after the first", checkMark)
ac, _ = autoComplete.Complete("b")
if !reflect.DeepEqual(ac, []string{"bbb"}) {
t.Fatal("Should be able to learn words after the first", ballotX)
}
t.Log("Should be able to learn words after the first", checkMark)
ac, _ = autoComplete.Complete("bb")
if !reflect.DeepEqual(ac, []string{"bbb"}) {
t.Fatal("Should be able to learn words after the first", ballotX)
}
t.Log("Should be able to learn words after the first", checkMark)
autoComplete, _ = NewAutoCompleteLinoE(2, 0, 0)
autoComplete.Learn("3333")
autoComplete.Learn("3344")
ac, _ = autoComplete.Complete("33")
if !reflect.DeepEqual(ac, []string{"3333", "3344"}) {
t.Fatal("Should be able to learn words when building dictionary from head", ballotX)
}
t.Log("Should be able to learn words when building dictionary from head", checkMark)
}
func ExampleNewAutoCompleteLinoS() {
words := []string{"chair", "chairman", "chairperson", "chairwoman", "chairmaker", "chairmaking"}
autoComplete, err := NewAutoCompleteLinoS(words, 2, 0, 0)
if err != nil {
fmt.Println(err)
return
}
completes, err := autoComplete.Complete("chairm")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(completes)
// Output: [chairmaker chairmaking chairman]
}
func ExampleNewAutoCompleteLinoE() {
words := []string{"chair", "chairman", "chairperson", "chairwoman", "chairmaker", "chairmaking"}
autoComplete, err := NewAutoCompleteLinoE(2, 0, 0)
if err != nil {
fmt.Println(err)
return
}
for _, w := range words {
autoComplete.Learn(w)
}
completes, err := autoComplete.Complete("chairm")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(completes)
// Output: [chairmaker chairmaking chairman]
}