-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_test.go
66 lines (53 loc) · 1.25 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
package main
import (
"testing"
)
func TestSafeMap_SetAndGet(t *testing.T) {
safeMap := NewSafeMap[string, int]()
safeMap.Set("key1", 1)
value, ok := safeMap.Get("key1")
if !ok || value != 1 {
t.Errorf("Expected value 1 for key 'key1', got %d", value)
}
}
func TestSafeMap_SetAndDelete(t *testing.T) {
safeMap := NewSafeMap[string, int]()
safeMap.Set("key1", 1)
safeMap.Delete("key1")
_, ok := safeMap.Get("key1")
if ok {
t.Errorf("Expected key 'key1' to be deleted")
}
}
func TestSafeMap_Has(t *testing.T) {
safeMap := NewSafeMap[string, int]()
safeMap.Set("key1", 1)
if !safeMap.Has("key1") {
t.Errorf("Expected key 'key1' to exist")
}
}
func TestSafeMap_Keys(t *testing.T) {
safeMap := NewSafeMap[string, int]()
safeMap.Set("key1", 1)
safeMap.Set("key2", 2)
keys := safeMap.Keys()
if len(keys) != 2 {
t.Errorf("Expected 2 keys, got %d", len(keys))
}
}
func TestSafeMap_Range(t *testing.T) {
safeMap := NewSafeMap[string, int]()
safeMap.Set("key1", 1)
safeMap.Set("key2", 2)
var keys []string
err := safeMap.Range(func(k string, v int) error {
keys = append(keys, k)
return nil
})
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(keys) != 2 {
t.Errorf("Expected 2 keys, got %d", len(keys))
}
}