-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmap_test.go
164 lines (146 loc) · 3.76 KB
/
map_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
package critbitgo_test
import (
"strings"
"testing"
"github.com/k-sone/critbitgo"
)
func buildSortedMap(keys []string) *critbitgo.SortedMap {
m := critbitgo.NewSortedMap()
for _, key := range keys {
m.Set(key, key)
}
return m
}
func TestSortedMapContains(t *testing.T) {
keys := []string{"", "a", "aa", "b", "bb", "ab", "ba", "aba", "bab"}
m := buildSortedMap(keys)
for _, key := range keys {
if !m.Contains(key) {
t.Errorf("Contains() - not found - [%s]", key)
}
}
if m.Contains("aaa") {
t.Error("Contains() - phantom found")
}
}
func TestSortedMapGet(t *testing.T) {
keys := []string{"", "a", "aa", "b", "bb", "ab", "ba", "aba", "bab"}
m := buildSortedMap(keys)
for _, key := range keys {
if value, ok := m.Get(key); !ok || value != key {
t.Errorf("Get() - not found - [%s]", key)
}
}
if value, ok := m.Get("aaa"); ok || value != nil {
t.Error("Get() - phantom found")
}
}
func TestSortedMapDelete(t *testing.T) {
keys := []string{"", "a", "aa", "b", "bb", "ab", "ba", "aba", "bab"}
m := buildSortedMap(keys)
for i, key := range keys {
if !m.Contains(key) {
t.Errorf("Delete() - not exists - [%s]", key)
}
if value, ok := m.Delete(key); !ok || value != key {
t.Errorf("Delete() - failed - [%s]", key)
}
if m.Contains(key) {
t.Errorf("Delete() - exists - [%s]", key)
}
if value, ok := m.Delete(key); ok || value != nil {
t.Errorf("Delete() - phantom found - [%s]", key)
}
if i != len(keys) {
for _, key2 := range keys[i+1:] {
if !m.Contains(key2) {
t.Errorf("Delete() - other not exists - [%s](%s)", key2, key)
}
}
}
}
}
func TestSortedMapSize(t *testing.T) {
keys := []string{"", "a", "aa", "b", "bb", "ab", "ba", "aba", "bab"}
m := buildSortedMap(keys)
klen := len(keys)
if s := m.Size(); s != klen {
t.Errorf("Size() - expected [%d], actual [%d]", klen, s)
}
for i, key := range keys {
m.Delete(key)
if s := m.Size(); s != klen-(i+1) {
t.Errorf("Size() - expected [%d], actual [%d]", klen, s)
}
}
}
func TestSortedMapKeys(t *testing.T) {
keys := []string{"", "a", "aa", "b", "bb", "ab", "ba", "aba", "bab"}
m := buildSortedMap(keys)
skeys := m.Keys()
for _, key := range keys {
match := false
for _, skey := range skeys {
if key == skey {
match = true
break
}
}
if !match {
t.Errorf("Keys() - not found [%s]", key)
}
}
}
func TestSortedMapEach(t *testing.T) {
keys := []string{"", "a", "aa", "b", "bb", "ab", "ba", "aba", "bab"}
m := buildSortedMap(keys)
elems := make(map[string]interface{})
handle := func(key string, value interface{}) bool {
elems[key] = value
return true
}
if !m.Each("", handle) {
t.Error("Each() - invalid result")
}
for _, key := range keys {
if _, ok := elems[key]; !ok {
t.Errorf("Each() - not found [%s]", key)
} else if value, ok := elems[key].(string); !ok || value != key {
t.Errorf("Each() - invalid value [%s](%s)", value, key)
}
}
elems = make(map[string]interface{})
handle = func(key string, value interface{}) bool {
elems[key] = value
return true
}
if !m.Each("b", handle) {
t.Error("Each() - invalid result")
}
for _, key := range keys {
if strings.Index(key, "b") == 0 {
if _, ok := elems[key]; !ok {
t.Errorf("Each() - not found [%s]", key)
} else if value, ok := elems[key].(string); !ok || value != key {
t.Errorf("Each() - invalid value [%s](%s)", value, key)
}
} else {
if _, ok := elems[key]; ok {
t.Errorf("Each() - phantom found [%s]", key)
}
}
}
elems = make(map[string]interface{})
handle = func(key string, value interface{}) bool {
elems[key] = value
return true
}
if !m.Each("c", handle) {
t.Error("Each() - invalid result")
}
for _, key := range keys {
if _, ok := elems[key]; ok {
t.Errorf("Each() - phantom found [%s]", key)
}
}
}