-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_test.go
124 lines (101 loc) · 2.94 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
package ordered_sync_map_test
import (
mp "github.com/m-murad/ordered-sync-map"
"testing"
)
func initMap() *mp.Map {
return mp.New()
}
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 := mp.New()
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)
m = mp.New()
m.OrderedRange(rangeFunc)
}