-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
100 lines (83 loc) · 2.05 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
package odmap_test
import (
odmap "github.com/RealFax/order-map"
"strconv"
"testing"
)
var (
m = odmap.New[string, string]()
empty = struct{}{}
)
func TestOrderedMap_Load(t *testing.T) {
m.Store("Hello", "123")
m.Store("Hello1", "Bonjour")
t.Log(m.Load("Hello"))
t.Log(m.Load("Hello1"))
}
func TestOrderedMap_LoadOrStore(t *testing.T) {
m.Store("Key1", "Value1")
t.Log(m.LoadOrStore("Key1", "Value2"))
t.Log(m.LoadOrStore("Key2", "Value3"))
t.Log(m.Load("Key1"))
t.Log(m.Load("Key2"))
}
func TestOrderedMap_LoadAndDelete(t *testing.T) {
m.Store("Key10", "Value10")
t.Log(m.LoadAndDelete("Key10"))
t.Log(m.Load("Key10"))
}
func TestOrderedMap_Delete(t *testing.T) {
m.Store("Key20", "Value20")
m.Delete("Key20")
t.Log(m.Load("Key20"))
}
func TestOrderedMap_Swap(t *testing.T) {
m.Store("Key30", "Value30")
t.Log(m.Load("Key30"))
t.Log(m.Swap("Key30", "Value31"))
t.Log(m.Swap("Key31", "Value32"))
t.Log(m.Load("Key30"))
t.Log(m.Load("Key31"))
}
func TestOrderedMap_CompareAndSwap(t *testing.T) {
m.Store("Key40", "Value40")
t.Log(m.CompareAndSwap("Key40", "Value40_", "Value41"))
t.Log(m.Load("Key40"))
t.Log(m.CompareAndSwap("Key40", "Value40", "Value41"))
t.Log(m.Load("Key40"))
t.Log(m.CompareAndSwap("Key41", "", "Value42"))
t.Log(m.Load("Key41"))
}
func TestOrderedMap_CompareAndDelete(t *testing.T) {
m.Store("Key50", "Value50")
m.CompareAndDelete("Key50", "Value50_")
t.Log(m.Load("Key50"))
m.CompareAndDelete("Key50", "Value50")
t.Log(m.Load("Key50"))
}
func TestOrderedMap_Range(t *testing.T) {
nm := odmap.New[int, string]()
for i := 0; i < 100; i++ {
nm.Store(i, "VALUE_"+strconv.Itoa(i))
}
nm.Range(func(key int, value string) bool {
// t.Log(key, value)
return true
})
}
func BenchmarkOmap_Store(b *testing.B) {
internal := odmap.New[int, struct{}]()
for i := 0; i < b.N; i++ {
internal.Store(i, empty)
}
}
func ExampleNew() {
m := odmap.New[int, string]()
m.Store(0, "Hello")
m.Store(1, "World")
m.Store(2, "😄😄😄")
m.Range(func(key int, value string) bool {
print(value, " ")
return true
})
}