This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict_test.go
408 lines (350 loc) · 9.97 KB
/
dict_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
// Copyright 2012 Dobrosław Żybort
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package listdict
import (
"reflect"
"testing"
)
//=============================================================================
var dictFromKeysTests = []struct {
in List
defaultVal interface{}
out Dict
}{
{List{"one", "two", "three"}, nil,
Dict{"one": nil, "two": nil, "three": nil}},
{List{1, 2}, 1, Dict{"1": 1, "2": 1}},
{List{}, nil, Dict{}},
}
func TestDictFromKeys(t *testing.T) {
for index, dfkt := range dictFromKeysTests {
dict := DictFromKeys(dfkt.in, dfkt.defaultVal)
if !reflect.DeepEqual(dict, dfkt.out) {
t.Errorf(
"%d. DictFromKeys(%v, %v) => out dict = %v, want %v",
index, dfkt.in, dfkt.defaultVal, dict, dfkt.out)
}
}
}
//=============================================================================
var dictClearTests = []struct {
in Dict
out Dict
}{
{Dict{"one": 1, "two": 2, "three": 3}, Dict{}},
{Dict{"one": List{1, 2}}, Dict{}},
{Dict{}, Dict{}},
}
func TestDictClear(t *testing.T) {
for index, dct := range dictClearTests {
dict := NewDict()
for key, val := range dct.in {
dict[key] = val
}
dict.Clear()
if len(dict) != 0 {
t.Fatalf("%d. len(%v.Clear()) => %v, should be 0",
index, dct.in, len(dict))
}
if !reflect.DeepEqual(dict, dct.out) {
t.Errorf(
"%d. %v.Clear() => out dict = %v, want %v",
index, dct.in, dict, dct.out)
}
}
}
//=============================================================================
var dictGetTests = []struct {
in Dict
key string
defaultVal interface{}
out interface{}
}{
{Dict{"one": 1, "two": 2, "three": 3}, "one", 0, 1},
{Dict{"one": 1}, "two", 0, 0},
{Dict{}, "one", 0, 0},
}
func TestDictGet(t *testing.T) {
for index, dgt := range dictGetTests {
dict := NewDict()
for key, val := range dgt.in {
dict[key] = val
}
myValue := dict.Get(dgt.key, dgt.defaultVal)
switch {
case myValue != dgt.out:
t.Errorf("%d. %v.Get(%v, %v) => %v, want 0",
index, dgt.in, dgt.key, dgt.defaultVal, myValue, dgt.out)
case dict.HasKey(dgt.key) && !dgt.in.HasKey(dgt.key):
t.Errorf("%d. %v.Get(%v, %v) => out dict should not have '%v' key",
index, dgt.in, dgt.key, dgt.defaultVal, dgt.key)
}
}
}
//=============================================================================
var dictHasKeyTests = []struct {
in Dict
key string
out bool
}{
{Dict{"one": 1, "two": 2, "three": 3}, "one", true},
{Dict{"one": 1}, "two", false},
{Dict{}, "one", false},
}
func TestDictHasKey(t *testing.T) {
for index, dhkt := range dictHasKeyTests {
var keyTest bool
if _, ok := dhkt.in[dhkt.key]; ok {
keyTest = true
} else {
keyTest = false
}
if keyTest != dhkt.out {
t.Errorf("%d. %v.HasKey(%v) => %v, want %v",
index, dhkt.in, dhkt.key, keyTest, dhkt.out)
}
}
}
//=============================================================================
var dictIsEqualTests = []struct {
in Dict
secondDict Dict
out bool
}{
{Dict{"one": 1, "two": 2}, Dict{"one": 1, "two": 2}, true},
{Dict{"one": 1, "two": 2}, Dict{"one": 1, "two": 2, "three": 3}, false},
{Dict{"one": 1, "two": 2}, Dict{"two": 2, "one": 1}, true},
{Dict{}, Dict{}, true},
}
func TestDictIsEqual(t *testing.T) {
for index, diet := range dictIsEqualTests {
val := diet.in.IsEqual(diet.secondDict)
if val != diet.out {
t.Errorf(
"%d. %v.IsEqual(%v) => %v, want %v",
index, diet.in, diet.secondDict, val, diet.out)
}
}
}
//=============================================================================
var dictItemsTests = []struct {
in Dict
out []List
}{
{Dict{"one": 1, "two": 2, "three": 3},
[]List{List{"one", 1}, List{"two", 2}, List{"three", 3}}},
{Dict{"one": 1}, []List{List{"one", 1}}},
{Dict{}, []List{}},
}
func TestDictItems(t *testing.T) {
for index, dit := range dictItemsTests {
itemList := dit.in.Items()
founded := 0
for _, listItem := range dit.out {
for _, dictItem := range itemList {
if reflect.DeepEqual(dictItem, listItem) {
founded++
}
}
}
switch {
case len(dit.out) != len(itemList):
t.Errorf("%d. len(%v.Items()) => %d, should be %d",
index, dit.in, len(itemList), len(dit.out))
case founded != len(itemList):
t.Errorf("%d. compare %v.Items() and %v => "+
"found %v same list elements, shoud found %v",
index, dit.in, dit.out, founded, len(itemList))
}
}
}
//=============================================================================
var dictKeysTests = []struct {
in Dict
out List
}{
{Dict{"one": 1, "two": 2, "three": 3}, List{"one", "two", "three"}},
{Dict{"one": 1, "two": 2, "three": 3}, List{"two", "one", "three"}},
{Dict{"one": 1}, List{"one"}},
{Dict{}, List{}},
}
func TestDictKeys(t *testing.T) {
for index, dkt := range dictKeysTests {
keyList := dkt.in.Keys()
founded := 0
for _, listItem := range dkt.out {
for _, dictItem := range keyList {
if reflect.DeepEqual(dictItem, listItem) {
founded++
}
}
}
switch {
case len(dkt.out) != len(keyList):
t.Errorf("%d. len(%v.Keys()) => %d, should be %d",
index, dkt.in, len(keyList), len(dkt.out))
case founded != len(keyList):
t.Errorf("%d. compare %v.Keys() and %v => "+
"found %v same elements, shoud found %v",
index, dkt.in, dkt.out, founded, len(keyList))
}
}
}
//=============================================================================
var dictPopTests = []struct {
in Dict
key string
defaultVal interface{}
out interface{}
outError error
outDict Dict
}{
{Dict{"one": 1, "two": 2, "three": 3},
"one", 0,
1, nil, Dict{"two": 2, "three": 3}},
{Dict{"one": 1, "two": 2, "three": 3},
"four", 0,
0, nil, Dict{"one": 1, "two": 2, "three": 3}},
{Dict{"one": 1},
"one", 0,
1, nil, Dict{}},
{Dict{},
"one", 0,
0, ErrRemoveFromEmptyDict, Dict{}},
}
func TestDictPop(t *testing.T) {
for index, dpt := range dictPopTests {
dict := NewDict()
for key, val := range dpt.in {
dict[key] = val
}
val, err := dict.Pop(dpt.key, dpt.defaultVal)
switch {
case val != dpt.out || err != dpt.outError:
t.Errorf("%d. %v.Pop(%v, %v) => %v, %v, want %v, %v",
index, dpt.in, dpt.key, dpt.defaultVal,
val, err, dpt.out, dpt.outError)
case !reflect.DeepEqual(dict, dpt.outDict):
t.Errorf("%d. %v.Pop(%v, %v) => out dict = %v, want %v",
index, dpt.in, dpt.key, dpt.defaultVal, dict, dpt.outDict)
}
}
}
//=============================================================================
var dictPopItemTests = []struct {
in Dict
out List
outError error
outDict Dict
}{
{Dict{"one": 1}, List{"one", 1}, nil, Dict{}},
{Dict{}, List{}, ErrRemoveFromEmptyDict, Dict{}},
}
func TestDictPopItem(t *testing.T) {
for index, dpit := range dictPopItemTests {
dict := NewDict()
for key, val := range dpit.in {
dict[key] = val
}
list, err := dict.PopItem()
switch {
case !reflect.DeepEqual(list, dpit.out) || err != dpit.outError:
t.Errorf("%d. %v.PopItem() => %v, %v, want %v, %v",
index, dpit.in, list, dpit.out, dpit.outError)
case !reflect.DeepEqual(dict, dpit.outDict):
t.Errorf("%d. %v.PopItem() => out dict = %v, want %v",
index, dpit.in, dict, dpit.outDict)
}
}
}
//=============================================================================
var dictSetDefaultTests = []struct {
in Dict
key string
defaultVal interface{}
out interface{}
outDict Dict
}{
{Dict{"one": 1, "two": 2}, "one", 0, 1, Dict{"one": 1, "two": 2}},
{Dict{"one": 1}, "two", 0, 0, Dict{"one": 1, "two": 0}},
{Dict{}, "one", 0, 0, Dict{"one": 0}},
}
func TestDictSetDefault(t *testing.T) {
for index, dsdt := range dictSetDefaultTests {
dict := NewDict()
for key, val := range dsdt.in {
dict[key] = val
}
myValue := dict.SetDefault(dsdt.key, dsdt.defaultVal)
switch {
case myValue != dsdt.out:
t.Errorf("%d. %v.SetDefault(%v, %v) => %v, want %v",
index, dsdt.in, dsdt.key, dsdt.defaultVal, myValue, dsdt.out)
case !reflect.DeepEqual(dict, dsdt.outDict) || !dict.HasKey(dsdt.key):
t.Errorf("%d. %v.SetDefault(%v, %v) => out dict = %v, want %v",
index, dsdt.in, dsdt.key, dsdt.defaultVal, dict, dsdt.outDict)
}
}
}
//=============================================================================
var dictUpdateTests = []struct {
in Dict
otherDict Dict
out Dict
}{
{Dict{"one": 1}, Dict{"one": 0, "two": 2}, Dict{"one": 0, "two": 2}},
{Dict{"one": 1}, Dict{"two": 2}, Dict{"one": 1, "two": 2}},
{Dict{"one": 1}, Dict{"one": 0}, Dict{"one": 0}},
{Dict{"one": 1}, Dict{}, Dict{"one": 1}},
{Dict{}, Dict{"one": 1}, Dict{"one": 1}},
{Dict{}, Dict{}, Dict{}},
}
func TestDictUpdate(t *testing.T) {
for index, dut := range dictUpdateTests {
dict := NewDict()
for key, val := range dut.in {
dict[key] = val
}
dict.Update(dut.otherDict)
switch {
case !reflect.DeepEqual(dict, dut.out):
t.Errorf("%d. %v.Update(%v) => %v, want %v",
index, dut.in, dut.otherDict, dict, dut.out)
}
}
}
//=============================================================================
var dictValuesTests = []struct {
in Dict
out List
}{
{Dict{"one": 1, "two": 2, "three": 3}, List{1, 2, 3}},
{Dict{"one": 1, "two": 2, "three": 3}, List{3, 1, 2}},
{Dict{"one": 1}, List{1}},
{Dict{}, List{}},
}
func TestDictValues(t *testing.T) {
for index, dvt := range dictValuesTests {
valueList := dvt.in.Values()
founded := 0
for _, listItem := range dvt.out {
for _, dictItem := range valueList {
if reflect.DeepEqual(dictItem, listItem) {
founded++
}
}
}
switch {
case len(dvt.out) != len(valueList):
t.Errorf("%d. len(%v.Values()) => %d, should be %d",
index, dvt.in, len(valueList), len(dvt.out))
case founded != len(valueList):
t.Errorf("%d. compare %v.Values() and %v => "+
"found %v same elements, shoud found %v",
index, dvt.in, dvt.out, founded, len(valueList))
}
}
}