-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
171 lines (137 loc) · 3.65 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
165
166
167
168
169
170
171
package ordered_sync_map_test
import (
"testing"
mp "github.com/m-murad/ordered-sync-map"
)
func initMap() *mp.Map[any, any] {
return mp.New[any, any]()
}
func TestGetPutDelete(t *testing.T) {
m := initMap()
if _, ok := m.Get("some-key"); ok {
t.Fatal(`Get for key "some-key" should return nil and false`)
}
if _, ok := m.Get(123); ok {
t.Fatal(`Get for key 123 should return nil and false`)
}
m.Put("key", 123)
m.Put(123, "key")
if _, ok := m.Get("some-key"); ok {
t.Fatal(`Get for key "some-key" should still return nil and false`)
}
if val, ok := m.Get("key"); !ok {
t.Fatal(`2nd value returned by Get for key "key" should be true`)
} else if val == nil {
t.Fatal(`1st value returned by Get for key "key" should be non nil`)
} else {
intVal, ok := val.(int)
if !ok || intVal != 123 {
t.Fatal(`1st value returned by Get for key "key" should int 123`)
}
}
m.Put("key", 456)
if val, ok := m.Get(123); !ok {
t.Fatal("2nd value returned by Get for key 123 should be true")
} else if val == nil {
t.Fatal("1st value returned by Get for key 123 should be non nil")
} else {
strVal, ok := val.(string)
if !ok || strVal != "key" {
t.Fatal(`1st value returned by Get for key "key" should int 123`)
}
}
if exists := m.Delete(123); !exists {
t.Fatal("Delete for key 123 should return true")
}
if exists := m.Delete(123); exists {
t.Fatal("Delete for key 123 on second time should return false")
}
if val, ok := m.Get(123); val != nil || ok {
t.Fatal("Get for key 123 after calling delete should return nil and false")
}
}
func TestUnorderedRange(t *testing.T) {
m := initMap()
kvs := map[interface{}]interface{}{
"key": 123,
123: "key",
"some-key": "val 1",
"some-other-key": "val_2",
56.11: true,
}
var insertCount int
for k, v := range kvs {
insertCount++
m.Put(k, v)
}
var rangeCount int
rangeFunc := func(key interface{}, val interface{}) {
rangeCount++
if kvs[key] != val {
t.Fatalf("Value mismatch for key %s. In standard map: %v, In ordered_sync_map %v.", key, kvs[key], val)
}
}
m.UnorderedRange(rangeFunc)
if insertCount != rangeCount {
t.Fatalf("Range count mismatch. Expected %d got %d", insertCount, rangeCount)
}
}
func TestOrderedRange(t *testing.T) {
kvs := [][]interface{}{
{"key", 123, "some-key", "some-other-key", 56.11}, //keys
{123, "key", "val 1", "val_2", true}, //values
}
m := initMap()
for i := range kvs[0] {
m.Put(kvs[0][i], kvs[1][i])
}
var rangeCount int
rangeFunc := func(key interface{}, val interface{}) {
if kvs[0][rangeCount] != key {
t.Fatalf("Key sequesnce mismatic at position %d. Extected %v, received %v.", rangeCount+1, kvs[0][rangeCount], key)
}
if kvs[1][rangeCount] != val {
t.Fatalf("Value sequesnce mismatic at position %d. Extected %v, received %v.", rangeCount+1, kvs[1][rangeCount], val)
}
rangeCount++
}
m.OrderedRange(rangeFunc)
}
func TestLength(t *testing.T) {
m := initMap()
if m.Length() != 0 {
t.FailNow()
}
m.Put("a", 1)
m.Put("b", 2)
if m.Length() != 2 {
t.FailNow()
}
m.Delete("a")
if m.Length() != 1 {
t.FailNow()
}
m.Delete("does_not_exist")
if m.Length() != 1 {
t.FailNow()
}
}
func TestGetOrPut(t *testing.T) {
m := initMap()
if finalValue, updated := m.GetOrPut("a", 5); finalValue != 5 || updated {
t.Fail()
}
if finalValue, updated := m.GetOrPut("a", 4); finalValue != 5 || !updated {
t.Fail()
}
}
func TestGetAndDelete(t *testing.T) {
m := initMap()
if value, deleted := m.GetAndDelete("a"); value != nil || deleted {
t.Fail()
}
m.Put("a", 2)
if value, deleted := m.GetAndDelete("a"); value != 2 || !deleted {
t.Fail()
}
}