-
Notifications
You must be signed in to change notification settings - Fork 2
/
dictionary_test.go
169 lines (149 loc) · 3.39 KB
/
dictionary_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
package collection
import (
"context"
"strings"
"testing"
)
func TestDictionary_Enumerate(t *testing.T) {
dictSets := [][]string{
{"alpha", "beta", "charlie"},
{"also", "always"},
{"canned", "beans"},
{"duplicated", "duplicated", "after"},
}
for _, ds := range dictSets {
t.Run("", func(t *testing.T) {
subject := Dictionary{}
expected := make(map[string]bool)
added := 0
for _, entry := range ds {
if subject.Add(entry) {
added++
}
expected[entry] = false
}
expectedSize := len(expected)
if added != expectedSize {
t.Logf("`Add` returned true %d times, expected %d times", added, expectedSize)
t.Fail()
}
if subjectSize := CountAll[string](subject); subjectSize != expectedSize {
t.Logf("`CountAll` returned %d elements, expected %d", subjectSize, expectedSize)
t.Fail()
}
prev := ""
for result := range subject.Enumerate(context.Background()) {
t.Logf(result)
if alreadySeen, ok := expected[result]; !ok {
t.Logf("An unadded value was returned")
t.Fail()
} else if alreadySeen {
t.Logf("\"%s\" was duplicated", result)
t.Fail()
}
if stringle(result, prev) {
t.Logf("Results \"%s\" and \"%s\" were not alphabetized.", prev, result)
t.Fail()
}
prev = result
expected[result] = true
}
})
}
}
func TestDictionary_Add(t *testing.T) {
subject := Dictionary{}
subject.Add("word")
if rootChildrenCount := len(subject.root.Children); rootChildrenCount != 1 {
t.Logf("The root should only have one child, got %d instead.", rootChildrenCount)
t.Fail()
}
if retreived, ok := subject.root.Children['w']; ok {
leaf := retreived.Navigate("ord")
if leaf == nil {
t.Log("Unable to navigate from `w`")
t.Fail()
} else if !leaf.IsWord {
t.Log("leaf should have been a word")
t.Fail()
}
} else {
t.Log("Root doesn't have child for `w`")
t.Fail()
}
}
func TestTrieNode_Navigate(t *testing.T) {
leaf := trieNode{
IsWord: true,
}
subject := trieNode{
Children: map[rune]*trieNode{
'a': &trieNode{
Children: map[rune]*trieNode{
'b': &trieNode{
Children: map[rune]*trieNode{
'c': &leaf,
},
},
},
},
},
}
testCases := []struct {
address string
expected *trieNode
}{
{"abc", &leaf},
{"abd", nil},
{"", &subject},
{"a", subject.Children['a']},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
if result := subject.Navigate(tc.address); result != tc.expected {
t.Logf("got: %v want: %v", result, tc.expected)
t.Fail()
}
})
}
}
func Test_stringle(t *testing.T) {
testCases := []struct {
left string
right string
expected bool
}{
{"a", "b", true},
{"b", "a", false},
{"a", "a", true},
{"alpha", "b", true},
{"a", "beta", true},
{"alpha", "alpha", true},
{"alpha", "alphabet", true},
{"alphabet", "alpha", false},
{"", "a", true},
{"", "", true},
}
for _, tc := range testCases {
t.Run(strings.Join([]string{tc.left, tc.right}, ","), func(t *testing.T) {
if got := stringle(tc.left, tc.right); got != tc.expected {
t.Logf("got: %v want: %v", got, tc.expected)
t.Fail()
}
})
}
}
func stringle(left, right string) bool {
other := []byte(right)
for i, letter := range []byte(left) {
if i >= len(other) {
return false
}
if letter > other[i] {
return false
} else if letter < other[i] {
break
}
}
return true
}