-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderedmap_test.go
276 lines (230 loc) · 5.8 KB
/
orderedmap_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
// Package orderedmap provides a map where the order of items is maintained.
// Furthermore, access to contained data is done in a way that is protected and
// concurrent.
package orderedmap
import (
"fmt"
"reflect"
"strconv"
"testing"
)
type TestData struct {
ID int
Name string
}
func TestNewOrderedMap(t *testing.T) {
om := New()
if reflect.TypeOf(om).Name() != "OrderedMap" {
t.Error("Map is not the correct type")
}
if om.Count() != 0 {
t.Error("New map is not empty")
}
}
func TestAdd(t *testing.T) {
om := New()
one := TestData{ID: 1, Name: "one"}
two := TestData{ID: 2, Name: "two"}
om.Add("one", one)
om.Add("two", two)
if om.Count() != 2 {
t.Error("Map does not contain two items")
}
}
func TestInsert(t *testing.T) {
om := New()
om.Add("one", TestData{ID: 1, Name: "one"})
om.Add("three", TestData{ID: 3, Name: "three"})
om.Add("four", TestData{ID: 4, Name: "four"})
om.Add("five", TestData{ID: 5, Name: "five"})
err := om.Insert(1, "two", TestData{ID: 2, Name: "two"})
if err != nil {
t.Error("Error trying to insert into ordered map: " + err.Error())
}
if om.Count() != 5 {
t.Error("Map does not contain correct number of items")
}
tmp := om.GetOrder()
if tmp[1] != "two" {
t.Logf("Order: %v\n", tmp)
t.Error("Index two is not the correct object name")
}
err = om.Insert(30, "six", TestData{ID: 6, Name: "six"})
if err == nil {
t.Error("No error was received when trying to insert above the range.")
}
err = om.Insert(-1, "six", TestData{ID: 6, Name: "six"})
if err == nil {
t.Error("No error was received when trying to insert negative value.")
}
}
func TestGetKey(t *testing.T) {
om := New()
om.Add("one", TestData{ID: 1, Name: "one"})
om.Add("two", TestData{ID: 2, Name: "two"})
om.Add("three", TestData{ID: 3, Name: "three"})
test, ok := om.GetKey("two")
gotten := test.(TestData)
if !ok {
t.Error("Unable to get item from map by key")
}
if gotten.ID != 2 || gotten.Name != "two" {
t.Error("Wrong item was returned from map")
}
}
func TestGetIndex(t *testing.T) {
om := New()
om.Add("one", TestData{ID: 1, Name: "one"})
om.Add("two", TestData{ID: 2, Name: "two"})
om.Add("three", TestData{ID: 3, Name: "three"})
key, val, ok := om.GetIndex(1)
gotten := val.(TestData)
if !ok {
t.Error("Unable to get item from map by index")
}
if key != "two" || gotten.ID != 2 || gotten.Name != "two" {
t.Error("Wrong item was returned from map")
}
}
func TestGetOrder(t *testing.T) {
om := New()
om.Add("one", TestData{ID: 1, Name: "one"})
om.Add("two", TestData{ID: 2, Name: "two"})
om.Add("three", TestData{ID: 3, Name: "three"})
ord := om.GetOrder()
if ord[0] != "one" {
t.Error("First item was wrong")
} else if ord[1] != "two" {
t.Error("Second item was wrong")
} else if ord[2] != "three" {
t.Error("Third item was wrong")
}
}
func TestSetOrder(t *testing.T) {
om := New()
om.Add("one", TestData{ID: 1, Name: "one"})
om.Add("two", TestData{ID: 2, Name: "two"})
om.Add("three", TestData{ID: 3, Name: "three"})
err := om.SetOrder([]string{"three", "one", "two"})
if err != nil {
t.Error("An error occured setting order: " + err.Error())
}
ord := om.GetOrder()
if ord[0] != "three" {
t.Error("First item was wrong")
} else if ord[1] != "one" {
t.Error("Second item was wrong")
} else if ord[2] != "two" {
t.Error("Third item was wrong")
}
err = om.SetOrder([]string{"three", "one", "two", "five", "eleventy"})
if err == nil {
t.Error("No error occured when trying to use an order that was too large")
}
err = om.SetOrder([]string{"three", "one", "five"})
if err == nil {
t.Error("No error occured when trying to use an order the right size, but with the wrong items")
}
}
func TestIndexOf(t *testing.T) {
om := New()
om.Add("one", TestData{ID: 1, Name: "one"})
om.Add("two", TestData{ID: 2, Name: "two"})
om.Add("three", TestData{ID: 3, Name: "three"})
idx := om.IndexOf("one")
if idx != 0 {
t.Error("Index of one was not 0")
}
idx = om.IndexOf("three")
if idx != 2 {
t.Error("Index of three was not 2")
}
}
func TestDelete(t *testing.T) {
om := New()
om.Add("one", TestData{ID: 1, Name: "one"})
om.Add("two", TestData{ID: 2, Name: "two"})
om.Add("three", TestData{ID: 3, Name: "three"})
om.Delete("two")
_, ok := om.GetKey("two")
if ok {
t.Error("Deleted key still exists")
}
if om.Count() != 2 {
t.Error("Size of ordered map was wrong")
}
}
func TestCount(t *testing.T) {
om := New()
om.Add("one", TestData{ID: 1, Name: "one"})
if om.Count() != 1 {
t.Error("First count was wrong")
}
om.Add("two", TestData{ID: 2, Name: "two"})
if om.Count() != 2 {
t.Error("Second count was wrong")
}
om.Add("three", TestData{ID: 3, Name: "three"})
if om.Count() != 3 {
t.Error("Third count was wrong")
}
}
func TestIterator(t *testing.T) {
om := New()
for i := 0; i < 100; i++ {
str := strconv.Itoa(i)
om.Add(str, TestData{ID: i, Name: str})
}
itr := om.Iterator()
j := 0
for item := range itr.Loop() {
if item.Key != strconv.Itoa(j) {
t.Errorf("Index %v did not match", j)
}
j++
}
}
func TestIteratorBreak(t *testing.T) {
om := New()
for i := 0; i < 1000; i++ {
str := strconv.Itoa(i)
om.Add(str, TestData{ID: i, Name: str})
}
itr := om.Iterator()
j := 0
for _ = range itr.Loop() {
if j == 60 {
itr.Break()
break
}
j++
}
}
func ExampleIterator_full() {
om := New()
om.Add("1", "one")
om.Add("2", "two")
om.Add("3", "three")
om.Add("4", "four")
om.Add("5", "five")
iter := om.Iterator()
for data := range iter.Loop() {
fmt.Printf("%s > %v\n", data.Key, data.Val)
}
}
func ExampleIterator_break() {
om := New()
om.Add("1", "one")
om.Add("2", "two")
om.Add("3", "three")
om.Add("4", "four")
om.Add("5", "five")
iter := om.Iterator()
for data := range iter.Loop() {
if data.Key == "3" {
iter.Break()
break
}
fmt.Printf("%s > %v\n", data.Key, data.Val)
}
}