-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmightymap_test.go
More file actions
128 lines (112 loc) · 2.77 KB
/
mightymap_test.go
File metadata and controls
128 lines (112 loc) · 2.77 KB
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
package mightymap_test
import (
"context"
"testing"
"github.com/thisisdevelopment/mightymap"
)
func TestMightyMap_DefaultStorage(t *testing.T) {
// Testing MightyMap with default storage (map[K]V)
ctx := context.Background()
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")
}
})
t.Run("Keys", func(t *testing.T) {
cm.Clear(ctx)
cm.Store(ctx, 10, "ten")
cm.Store(ctx, 20, "twenty")
cm.Store(ctx, 30, "thirty")
keys := cm.Keys(ctx)
if len(keys) != 3 {
t.Errorf("Expected 3 keys, got %d", len(keys))
}
// Verify all expected keys are present
keyMap := make(map[int]bool)
for _, key := range keys {
keyMap[key] = true
}
expectedKeys := []int{10, 20, 30}
for _, expected := range expectedKeys {
if !keyMap[expected] {
t.Errorf("Expected key %d not found in Keys() result", expected)
}
}
})
t.Run("Keys empty map", func(t *testing.T) {
cm.Clear(ctx)
keys := cm.Keys(ctx)
if len(keys) != 0 {
t.Errorf("Expected empty keys list, got %d keys", len(keys))
}
})
}