-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_test.go
157 lines (118 loc) · 2.79 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
package gg_test
import (
"testing"
"github.com/mitranim/gg"
"github.com/mitranim/gg/gtest"
)
func TestMapInit(t *testing.T) {
defer gtest.Catch(t)
var tar IntMap
gtest.Equal(gg.MapInit(&tar), tar)
gtest.NotZero(tar)
tar[10] = 20
gtest.Equal(gg.MapInit(&tar), tar)
gtest.Equal(tar, IntMap{10: 20})
}
func TestMapClone(t *testing.T) {
defer gtest.Catch(t)
gtest.Equal(gg.MapClone(IntMap(nil)), IntMap(nil))
src := IntMap{10: 20, 30: 40}
out := gg.MapClone(src)
gtest.Equal(out, src)
src[10] = 50
gtest.Equal(src, IntMap{10: 50, 30: 40})
gtest.Equal(out, IntMap{10: 20, 30: 40})
}
func TestMapKeys(t *testing.T) {
defer gtest.Catch(t)
test := func(src IntMap, exp []int) {
gtest.Equal(gg.SortedPrim(gg.MapKeys(src)), exp)
}
test(IntMap(nil), []int(nil))
test(IntMap{}, []int{})
test(IntMap{10: 20}, []int{10})
test(IntMap{10: 20, 30: 40}, []int{10, 30})
}
func TestMapVals(t *testing.T) {
defer gtest.Catch(t)
test := func(src IntMap, exp []int) {
gtest.Equal(gg.SortedPrim(gg.MapVals(src)), exp)
}
test(IntMap(nil), []int(nil))
test(IntMap{}, []int{})
test(IntMap{10: 20}, []int{20})
test(IntMap{10: 20, 30: 40}, []int{20, 40})
}
func TestMapHas(t *testing.T) {
defer gtest.Catch(t)
gtest.False(gg.MapHas(IntMap(nil), 10))
gtest.False(gg.MapHas(IntMap{10: 20}, 20))
gtest.True(gg.MapHas(IntMap{10: 20}, 10))
}
func TestMapGot(t *testing.T) {
defer gtest.Catch(t)
{
val, ok := gg.MapGot(IntMap(nil), 10)
gtest.Zero(val)
gtest.False(ok)
}
{
val, ok := gg.MapGot(IntMap{10: 20}, 20)
gtest.Zero(val)
gtest.False(ok)
}
{
val, ok := gg.MapGot(IntMap{10: 20}, 10)
gtest.Eq(val, 20)
gtest.True(ok)
}
}
func TestMapGet(t *testing.T) {
defer gtest.Catch(t)
gtest.Zero(gg.MapGet(IntMap(nil), 10))
gtest.Zero(gg.MapGet(IntMap{10: 20}, 20))
gtest.Eq(gg.MapGet(IntMap{10: 20}, 10), 20)
}
func TestMapSet(t *testing.T) {
defer gtest.Catch(t)
tar := IntMap{}
gg.MapSet(tar, 10, 20)
gtest.Equal(tar, IntMap{10: 20})
gg.MapSet(tar, 10, 30)
gtest.Equal(tar, IntMap{10: 30})
}
func TestMapSetOpt(t *testing.T) {
defer gtest.Catch(t)
tar := IntMap{}
gg.MapSetOpt(tar, 0, 20)
gtest.Equal(tar, IntMap{})
gg.MapSetOpt(tar, 10, 0)
gtest.Equal(tar, IntMap{})
gg.MapSetOpt(tar, 10, 20)
gtest.Equal(tar, IntMap{10: 20})
gg.MapSetOpt(tar, 10, 30)
gtest.Equal(tar, IntMap{10: 30})
}
func TestMapClear(t *testing.T) {
defer gtest.Catch(t)
var tar IntMap
gg.MapClear(tar)
gtest.Equal(tar, IntMap(nil))
tar = IntMap{10: 20, 30: 40}
gg.MapClear(tar)
gtest.Equal(tar, IntMap{})
}
/*
func Benchmark_map_iteration(b *testing.B) {
tar := make(map[string]float64)
for ind := range gg.Iter(1024) {
tar[gg.String(ind)] = float64((ind % 2) * ind)
}
b.ResetTimer()
for ind := 0; ind < b.N; ind++ {
for key, val := range tar {
gg.Nop2(key, val)
}
}
}
*/