-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcas_map_test.go
More file actions
247 lines (204 loc) · 5.01 KB
/
cas_map_test.go
File metadata and controls
247 lines (204 loc) · 5.01 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package mapx
import (
"sync"
"testing"
)
func TestCASMap_BasicOperations(t *testing.T) {
m := NewCASMap[string, int]()
// Test Set and Get
m.Set("key1", 100)
if val, ok := m.Get("key1"); !ok || val != 100 {
t.Errorf("Expected (100, true), got (%d, %v)", val, ok)
}
// Test Get non-existent key
if val, ok := m.Get("key2"); ok {
t.Errorf("Expected (0, false), got (%d, true)", val)
}
// Test Has
if !m.Has("key1") {
t.Error("Expected key1 to exist")
}
if m.Has("key2") {
t.Error("Expected key2 to not exist")
}
// Test Len
if m.Len() != 1 {
t.Errorf("Expected length 1, got %d", m.Len())
}
// Test Delete
m.Delete("key1")
if m.Has("key1") {
t.Error("Expected key1 to be deleted")
}
if m.Len() != 0 {
t.Errorf("Expected length 0, got %d", m.Len())
}
// Test Delete non-existent key (should not panic)
m.Delete("nonexistent")
}
func TestCASMap_GetOrSet(t *testing.T) {
m := NewCASMap[string, int]()
// First call should set the value
val, existed := m.GetOrSet("key1", 100)
if existed || val != 100 {
t.Errorf("Expected (100, false), got (%d, %v)", val, existed)
}
// Second call should return existing value
val, existed = m.GetOrSet("key1", 200)
if !existed || val != 100 {
t.Errorf("Expected (100, true), got (%d, %v)", val, existed)
}
}
func TestCASMap_SetIfAbsent(t *testing.T) {
m := NewCASMap[string, int]()
// Should set successfully
if !m.SetIfAbsent("key1", 100) {
t.Error("Expected SetIfAbsent to succeed")
}
// Should fail on second attempt
if m.SetIfAbsent("key1", 200) {
t.Error("Expected SetIfAbsent to fail")
}
// Value should remain unchanged
if val, _ := m.Get("key1"); val != 100 {
t.Errorf("Expected value 100, got %d", val)
}
}
func TestCASMap_CompareAndSwap(t *testing.T) {
m := NewCASMap[string, int]()
// CAS on non-existent key should fail
if m.CompareAndSwap("key1", 100, 200) {
t.Error("Expected CAS to fail on non-existent key")
}
m.Set("key1", 100)
// CAS with wrong old value should fail
if m.CompareAndSwap("key1", 999, 200) {
t.Error("Expected CAS to fail with wrong old value")
}
// CAS with correct old value should succeed
if !m.CompareAndSwap("key1", 100, 200) {
t.Error("Expected CAS to succeed")
}
// Verify new value
if val, _ := m.Get("key1"); val != 200 {
t.Errorf("Expected value 200, got %d", val)
}
}
func TestCASMap_Clear(t *testing.T) {
m := NewCASMap[string, int]()
m.Set("key1", 100)
m.Set("key2", 200)
m.Set("key3", 300)
m.Clear()
if m.Len() != 0 {
t.Errorf("Expected length 0 after clear, got %d", m.Len())
}
if m.Has("key1") {
t.Error("Expected all keys to be cleared")
}
}
func TestCASMap_Keys(t *testing.T) {
m := NewCASMap[string, int]()
m.Set("key1", 100)
m.Set("key2", 200)
m.Set("key3", 300)
keys := m.Keys()
if len(keys) != 3 {
t.Errorf("Expected 3 keys, got %d", len(keys))
}
keyMap := make(map[string]bool)
for _, k := range keys {
keyMap[k] = true
}
if !keyMap["key1"] || !keyMap["key2"] || !keyMap["key3"] {
t.Error("Keys not returned correctly")
}
}
func TestCASMap_Values(t *testing.T) {
m := NewCASMap[string, int]()
m.Set("key1", 100)
m.Set("key2", 200)
m.Set("key3", 300)
values := m.Values()
if len(values) != 3 {
t.Errorf("Expected 3 values, got %d", len(values))
}
valueMap := make(map[int]bool)
for _, v := range values {
valueMap[v] = true
}
if !valueMap[100] || !valueMap[200] || !valueMap[300] {
t.Error("Values not returned correctly")
}
}
func TestCASMap_Range(t *testing.T) {
m := NewCASMap[string, int]()
m.Set("key1", 100)
m.Set("key2", 200)
m.Set("key3", 300)
count := 0
sum := 0
m.Range(func(key string, value int) bool {
count++
sum += value
return true
})
if count != 3 {
t.Errorf("Expected to visit 3 entries, visited %d", count)
}
if sum != 600 {
t.Errorf("Expected sum 600, got %d", sum)
}
// Test early termination
count = 0
m.Range(func(key string, value int) bool {
count++
return false // stop after first entry
})
if count != 1 {
t.Errorf("Expected to visit 1 entry, visited %d", count)
}
}
func TestCASMap_Concurrent(t *testing.T) {
if testing.Short() {
t.Skip("Skipping long-running concurrent test in short mode")
}
m := NewCASMap[int, int]()
const goroutines = 10
const iterations = 100
var wg sync.WaitGroup
wg.Add(goroutines * 2)
// Concurrent writes
for i := 0; i < goroutines; i++ {
go func(id int) {
defer wg.Done()
for j := 0; j < iterations; j++ {
key := id*iterations + j
m.Set(key, key*2)
}
}(i)
}
// Concurrent reads
for i := 0; i < goroutines; i++ {
go func(id int) {
defer wg.Done()
for j := 0; j < iterations; j++ {
key := id*iterations + j
m.Get(key)
}
}(i)
}
wg.Wait()
// Verify final count
expectedLen := goroutines * iterations
if m.Len() != expectedLen {
t.Errorf("Expected length %d, got %d", expectedLen, m.Len())
}
}
func TestCASMap_WithCapacity(t *testing.T) {
m := NewCASMapWithCapacity[string, int](100)
m.Set("key1", 100)
if val, ok := m.Get("key1"); !ok || val != 100 {
t.Errorf("Expected (100, true), got (%d, %v)", val, ok)
}
}