-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorderedmap_test.go
350 lines (274 loc) · 7.61 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
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
package orderedmap
import (
"fmt"
"testing"
)
// Test key present in OrderedMap
func mapHasKey(t *testing.T, om *OrderedMap, key interface{}, value interface{}) bool {
if v, ok := om.Get(key); v != value || !ok {
t.Error(fmt.Sprintf("Get(%v) -> expected %v received %v", key, value, v))
return false
}
return true
}
// Test key not present in OrderedMap
func mapNotKey(t *testing.T, om *OrderedMap, key interface{}) bool {
if v, ok := om.Get(key); v != nil || ok {
t.Error(fmt.Sprintf("Get(%v) -> shouldn't have a value", key))
return true
}
return false
}
// Test map is empty
func mapIsEmpty(t *testing.T, om *OrderedMap) bool {
if om.Len() != 0 {
t.Error("Map is not empty")
return false
}
if key, value, ok := om.PopLast(); key != nil || value != nil || ok {
t.Error("Map length is 0 but Pop() returned and item")
return false
}
return true
}
func TestNewOrderedMap(t *testing.T) {
om := NewOrderedMap()
om.Set(5, 33)
om.Set(6, 44)
om.Set("key", "value")
if val, ok := om.Get(5); val != 33 || !ok {
t.Error(fmt.Sprintf("Value error, expecting 33 received %v", val))
}
if val, ok := om.Get(6); val != 44 || !ok {
t.Error(fmt.Sprintf("Value error, expecting 44 received %v", val))
}
if val, ok := om.Get("key"); val != "value" || !ok {
t.Error(fmt.Sprintf("Value error, expecting 'value' received %v", val))
}
if val, ok := om.Get("not a key"); ok || val != nil {
t.Error(fmt.Sprintf("Shouldn't have returned %v", val))
}
}
func TestGetLast(t *testing.T) {
om := NewOrderedMap()
if key, value, ok := om.GetLast(); key != nil || value != nil || ok {
t.Error(fmt.Sprintf("Expecting nil, nil, false -> Returned %v %v %v",
key, value, ok))
}
om.Set("one", 1)
om.Set("two", 2)
if key, value, ok := om.GetLast(); key != "two" || value != 2 || !ok {
t.Error(fmt.Sprintf("Expecting 'two', 2, true -> Returned %v %v %v",
key, value, ok))
}
if key, value, ok := om.GetLast(); key != "two" || value != 2 || !ok {
t.Error(fmt.Sprintf("Expecting 'two', 2, true -> Returned %v %v %v",
key, value, ok))
}
om.MoveLast("one")
if key, value, ok := om.GetLast(); key != "one" || value != 1 || !ok {
t.Error(fmt.Sprintf("Expecting 'two', 2, true -> Returned %v %v %v",
key, value, ok))
}
if om.Len() != 2 {
t.Error("Somehow popped an item ..")
}
}
func TestGetFirst(t *testing.T) {
om := NewOrderedMap()
if key, value, ok := om.GetFirst(); key != nil || value != nil || ok {
t.Error(fmt.Sprintf("Expecting nil, nil, false -> Returned %v %v %v",
key, value, ok))
}
om.Set("one", 1)
om.Set("two", 2)
if key, value, ok := om.GetFirst(); key != "one" || value != 1 || !ok {
t.Error(fmt.Sprintf("Expecting 'two', 2, true -> Returned %v %v %v",
key, value, ok))
}
if key, value, ok := om.GetFirst(); key != "one" || value != 1 || !ok {
t.Error(fmt.Sprintf("Expecting 'two', 2, true -> Returned %v %v %v",
key, value, ok))
}
om.MoveLast("one")
if key, value, ok := om.GetFirst(); key != "two" || value != 2 || !ok {
t.Error(fmt.Sprintf("Expecting 'two', 2, true -> Returned %v %v %v",
key, value, ok))
}
if om.Len() != 2 {
t.Error("Somehow popped an item ..")
}
}
func TestPop(t *testing.T) {
om := NewOrderedMap()
om.Set("one", 1)
om.Set("two", 2)
om.Set("three", 3)
// Pop last
if key, val, ok := om.Pop(true); key != "three" || val != 3 || !ok {
t.Error("Pop last item error")
}
mapHasKey(t, om, "one", 1)
mapHasKey(t, om, "two", 2)
mapNotKey(t, om, "three")
// Pop first
if key, val, ok := om.Pop(false); key != "one" || val != 1 || !ok {
t.Error("Pop first item error")
}
mapHasKey(t, om, "two", 2)
mapNotKey(t, om, "one")
mapNotKey(t, om, "three")
// Add and Pop again
om.Set("four", 4)
om.Set("two", "new 2") //This should only change the value
mapNotKey(t, om, "one")
mapNotKey(t, om, "three")
mapHasKey(t, om, "two", "new 2")
mapHasKey(t, om, "four", 4)
// Pop last
om.Pop(false)
mapNotKey(t, om, "one")
mapNotKey(t, om, "two")
mapNotKey(t, om, "three")
mapHasKey(t, om, "four", 4)
// Pop first
om.Pop(true)
mapNotKey(t, om, "one")
mapNotKey(t, om, "two")
mapNotKey(t, om, "three")
mapNotKey(t, om, "four")
// Check it is empty
mapIsEmpty(t, om)
// Add a last one and pop it
om.Set("six", 6)
mapHasKey(t, om, "six", 6)
om.Pop(true)
mapNotKey(t, om, "six")
// Try to pop an item from an empty map
mapIsEmpty(t, om)
}
func TestPopLast(t *testing.T) {
om := NewOrderedMap()
om.Set("one", 1)
om.Set("two", 2)
if key, value, ok := om.PopLast(); key != "two" || value != 2 || !ok {
t.Error("PopLast didn't pop last element")
}
}
func TestPopFirst(t *testing.T) {
om := NewOrderedMap()
om.Set("one", 1)
om.Set("two", 2)
if key, value, ok := om.PopFirst(); key != "one" || value != 1 || !ok {
t.Error("PopFirst didn't pop first element")
}
}
func TestLen(t *testing.T) {
om := NewOrderedMap()
if l := om.Len(); l != 0 {
t.Error("Expecting 0, returned ", l)
}
om.Set("one", 1)
if l := om.Len(); l != 1 {
t.Error("Expecting 1, returned ", l)
}
om.Set("two", 2)
if l := om.Len(); l != 2 {
t.Error("Expecting 2, returned ", l)
}
om.Pop(true)
if l := om.Len(); l != 1 {
t.Error("Expenting 1, returned ", l)
}
om.Pop(true)
if l := om.Len(); l != 0 {
t.Error("Expecting 0, returned ", l)
}
}
func TestDelete(t *testing.T) {
om := NewOrderedMap()
om.Set("one", 1)
om.Set("two", 2)
om.Delete("one")
mapNotKey(t, om, "one")
mapHasKey(t, om, "two", 2)
om.Delete("two")
mapNotKey(t, om, "one")
mapNotKey(t, om, "two")
// Add and delete from empty OrderedMap
om.Set("three", 3)
mapHasKey(t, om, "three", 3)
om.Delete("three")
mapNotKey(t, om, "three")
mapIsEmpty(t, om)
}
func TestMove(t *testing.T) {
// Move last
om := NewOrderedMap()
om.Set("one", 1)
om.Set("two", 2)
om.Set("three", 3)
// Moving last element to the end should leave everithing uncahnged
om.Move("three", true)
// Move "one" element to the end
om.Move("one", true)
mapHasKey(t, om, "one", 1)
mapHasKey(t, om, "two", 2)
mapHasKey(t, om, "three", 3)
if key, value, ok := om.Pop(true); key != "one" || value != 1 || !ok {
t.Error("Item was not moved to the end")
}
// Try to move unknown key
if ok := om.Move("unknown", true); ok {
t.Error("Moved a non-existent element")
}
// Move "three" to the beginning
om.Move("three", false)
mapHasKey(t, om, "three", 3)
mapHasKey(t, om, "two", 2)
if key, value, ok := om.Pop(true); key != "two" || value != 2 || !ok {
t.Error("Item was not moved to the start")
}
// Move when there is a single element
om.Move("three", false)
om.Move("three", true)
mapHasKey(t, om, "three", 3)
if key, value, ok := om.Pop(false); key != "three" || value != 3 || !ok {
t.Error("There was an error while moving the last element")
}
mapIsEmpty(t, om)
// Try to move empty map
if ok := om.Move("three", true); ok {
t.Error("Somehow moved a key in an empy map")
}
}
// Just test it MoveLast calls Move with correct option
func TestMoveLast(t *testing.T) {
om := NewOrderedMap()
om.Set("one", 1)
om.Set("two", 2)
om.MoveLast("one")
if key, value, ok := om.PopLast(); key != "one" || value != 1 || !ok {
t.Error("MoveLast didn't move to last position")
}
}
func TestMoveFirst(t *testing.T) {
om := NewOrderedMap()
om.Set("one", 1)
om.Set("two", 2)
om.MoveFirst("two")
if key, value, ok := om.PopLast(); key != "one" || value != 1 || !ok {
t.Error("MoveFirst didn't move to the beginning")
}
}
// Test string interface
func TestString(t *testing.T) {
om := NewOrderedMap()
if fmt.Sprintf("%v", om) != "OrderedMap[]" {
t.Error("Invalid empty OrderedMap representation")
}
om.Set(1, 2)
if fmt.Sprintf("%v", om) != "OrderedMap[1:2, ]" {
t.Error("Invalid OrderedMap representation")
}
}