-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmightymap_test.go
95 lines (82 loc) · 2.02 KB
/
mightymap_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
package mightymap_test
import (
"testing"
"github.com/thisisdevelopment/mightymap"
)
func TestMightyMap_DefaultStorage(t *testing.T) {
// Testing MightyMap with default storage (map[K]V)
cm := mightymap.New[int, string](true)
t.Run("Store and Load", func(t *testing.T) {
cm.Store(ctx, 1, "one")
value, ok := cm.Load(ctx, 1)
if !ok || value != "one" {
t.Errorf("Expected to load 'one', got '%v'", value)
}
})
t.Run("Has", func(t *testing.T) {
if !cm.Has(ctx, 1) {
t.Errorf("Expected key 1 to exist")
}
if cm.Has(ctx, 2) {
t.Errorf("Did not expect key 2 to exist")
}
})
t.Run("Delete", func(t *testing.T) {
cm.Delete(ctx, 1)
if cm.Has(ctx, 1) {
t.Errorf("Expected key 1 to be deleted")
}
})
t.Run("Range", func(t *testing.T) {
cm.Store(ctx, 2, "two")
cm.Store(ctx, 3, "three")
keys := make(map[int]bool)
cm.Range(ctx, func(key int, value string) bool {
keys[key] = true
return true
})
if len(keys) != 2 {
t.Errorf("Expected to range over 2 keys, got %d", len(keys))
}
})
t.Run("Pop", func(t *testing.T) {
value, ok := cm.Pop(ctx, 2)
if !ok || value != "two" {
t.Errorf("Expected to pop 'two', got '%v'", value)
}
if cm.Has(ctx, 2) {
t.Errorf("Expected key 2 to be deleted after Pop")
}
})
t.Run("Pop Nonexistent Key", func(t *testing.T) {
value, ok := cm.Pop(ctx, 42)
if ok {
t.Errorf("Expected to pop 'nil', got '%v'", value)
}
})
t.Run("Next", func(t *testing.T) {
cm.Store(ctx, 4, "four")
value, key, ok := cm.Next(ctx)
if !ok {
t.Errorf("Expected to get next value")
}
if value != "three" && value != "four" {
t.Errorf("Unexpected value '%v'", value)
}
if cm.Has(ctx, key) {
t.Errorf("Expected key %d to be deleted after Next", key)
}
})
t.Run("Len", func(t *testing.T) {
if cm.Len(ctx) != 1 {
t.Errorf("Expected map to have 1 item, got %d", cm.Len(ctx))
}
})
t.Run("Clear", func(t *testing.T) {
cm.Store(ctx, 5, "five")
cm.Clear(ctx)
if cm.Len(ctx) != 0 {
t.Errorf("Expected map to be cleared")
}
})
}