-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncmap_test.go
63 lines (50 loc) · 1.16 KB
/
syncmap_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
package gwrap_test
import (
"testing"
"github.com/muir/gwrap"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSyncMap(t *testing.T) {
var m gwrap.SyncMap[string, int]
total := func() int {
var total int
m.Range(func(_ string, v int) bool {
total += v
return true
})
return total
}
m.Store("foo", 7)
require.Equal(t, 7, total())
m.Store("bar", 10)
require.Equal(t, 17, total())
m.Store("baz", -5)
require.Equal(t, 12, total())
v, ok := m.LoadOrStore("baz", 5)
assert.True(t, ok)
assert.Equal(t, -5, v)
require.Equal(t, 12, total())
v, ok = m.LoadOrStore("bloop", 10)
assert.False(t, ok)
assert.Equal(t, 10, v)
require.Equal(t, 22, total())
v, ok = m.LoadAndDelete("bloop")
assert.True(t, ok)
assert.Equal(t, 10, v)
require.Equal(t, 12, total())
v, ok = m.LoadAndDelete("blarp")
assert.False(t, ok)
assert.Equal(t, 0, v)
require.Equal(t, 12, total())
v, ok = m.Load("blarp")
assert.False(t, ok)
assert.Equal(t, 0, v)
require.Equal(t, 12, total())
v, ok = m.Load("foo")
assert.True(t, ok)
assert.Equal(t, 7, v)
require.Equal(t, 12, total())
m.Delete("foo")
require.Equal(t, 5, total())
}