forked from cevaris/ordered_map
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered_map_test.go
More file actions
128 lines (106 loc) · 2.25 KB
/
Copy pathordered_map_test.go
File metadata and controls
128 lines (106 loc) · 2.25 KB
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
package ordered_map
import (
"testing"
)
type MyStruct struct {
a float64
b bool
}
func testStringInt() []*KVPair {
var data []*KVPair = make([]*KVPair, 5)
data[0] = &KVPair{"test0", 0}
data[1] = &KVPair{"test1", 1}
data[2] = &KVPair{"test2", 2}
data[3] = &KVPair{"test3", 3}
data[4] = &KVPair{"test4", 4}
return data
}
func TestSetData(t *testing.T) {
expected := testStringInt()
om := NewOrderedMap()
if om == nil {
t.Error("Failed to create OrderedMap")
}
for _, kvp := range expected {
om.Set(kvp.Key, kvp.Value)
}
if len(om.store) != len(expected) {
t.Error("Failed insert of args:", om.store, expected)
}
}
func TestGetData(t *testing.T) {
data := testStringInt()
om := NewOrderedMapWithArgs(data)
for _, kvp := range data {
val, ok := om.Get(kvp.Key)
if ok && kvp.Value != val {
t.Error(kvp.Value, val)
}
}
_, ok := om.Get("invlalid-key")
if ok {
t.Error("Invalid key was found in OrderedMap")
}
}
func TestDeleteData(t *testing.T) {
data := testStringInt()
om := NewOrderedMapWithArgs(data)
testKey := data[2].Key
// First check to see if exists
_, ok := om.Get(testKey)
if !ok {
t.Error("Key/Value not found in OrderedMap")
}
// Delete key
om.Delete(testKey)
// Test to see if removed
_, ok2 := om.Get(testKey)
if ok2 {
t.Error("Key/Value was not deleted")
}
}
func TestIterator(t *testing.T) {
sample := testStringInt()
om := NewOrderedMapWithArgs(sample)
iter := om.UnsafeIter()
if iter == nil {
t.Error("Failed to create OrderedMap")
}
var index int = 0
for k := range iter {
expected := sample[index]
if !k.Compare(expected) {
t.Error(expected, k)
}
index++
}
}
func TestIteratorFunc(t *testing.T) {
sample := testStringInt()
om := NewOrderedMapWithArgs(sample)
iter := om.IterFunc()
if iter == nil {
t.Error("Failed to create OrderedMap")
}
var index int = 0
for k, ok := iter(); ok; k, ok = iter() {
expected := sample[index]
if !k.Compare(expected) {
t.Error(expected, k)
}
index++
}
}
func TestLenNonEmpty(t *testing.T) {
data := testStringInt()
om := NewOrderedMapWithArgs(data)
if om.Len() != len(data) {
t.Fatal("Unexpected length")
}
}
func TestLenEmpty(t *testing.T) {
om := NewOrderedMap()
if om.Len() != 0 {
t.Fatal("Unexpected length")
}
}