-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_test.go
More file actions
206 lines (168 loc) · 4.1 KB
/
map_test.go
File metadata and controls
206 lines (168 loc) · 4.1 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
package genericmap
import (
"fmt"
"sort"
"sync"
"testing"
)
func TestNew(t *testing.T) {
// Test empty construction
m := New[string, int]()
if m.Len() != 0 {
t.Errorf("Expected empty map, got length %d", m.Len())
}
// Test construction with initial data
initial := map[string]int{"a": 1, "b": 2, "c": 1}
m = New[string, int](initial)
if m.Len() != 3 {
t.Errorf("Expected 3 items, got %d", m.Len())
}
if keys := m.GetKeys(1); len(keys) != 2 {
t.Errorf("Expected 2 keys for value 1, got %d: %v", len(keys), keys)
}
}
func TestSetAndGet(t *testing.T) {
m := New[string, int]()
m.Set("key1", 100)
if val, ok := m.Get("key1"); !ok || val != 100 {
t.Errorf("Get failed: expected 100, got %v, exists: %v", val, ok)
}
// Test setting same value twice
m.Set("key1", 100)
if val, ok := m.Get("key1"); !ok || val != 100 {
t.Errorf("Set same value failed: expected 100, got %v", val)
}
// Test updating value
m.Set("key1", 200)
if val, ok := m.Get("key1"); !ok || val != 200 {
t.Errorf("Update failed: expected 200, got %v", val)
}
}
func TestReverseLookup(t *testing.T) {
m := New[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 1)
keys := m.GetKeys(1)
if len(keys) != 2 {
t.Errorf("Expected 2 keys for value 1, got %d: %v", len(keys), keys)
}
// Ensure keys contain both "a" and "c"
keySet := make(map[string]bool)
for _, k := range keys {
keySet[k] = true
}
if !keySet["a"] || !keySet["c"] {
t.Errorf("Expected keys [a c] for value 1, got %v", keys)
}
}
func TestRemove(t *testing.T) {
m := New[string, int]()
m.Set("a", 1)
m.Set("b", 2)
m.Set("c", 1)
// Test successful removal
if !m.Remove("a") {
t.Errorf("Remove failed: expected true, got false")
}
if m.Len() != 2 {
t.Errorf("Expected length 2 after removal, got %d", m.Len())
}
// Test removal of non-existent key
if m.Remove("nonexistent") {
t.Errorf("Remove of nonexistent key returned true")
}
}
func TestListAndValues(t *testing.T) {
m := New[string, int]()
m.Set("x", 10)
m.Set("y", 20)
m.Set("z", 10)
keys := m.List()
if len(keys) != 3 {
t.Errorf("Expected 3 keys, got %d: %v", len(keys), keys)
}
values := m.Values()
if len(values) != 3 {
t.Errorf("Expected 3 values, got %d: %v", len(values), values)
}
}
func TestLen(t *testing.T) {
m := New[string, int]()
if m.Len() != 0 {
t.Errorf("Expected 0 entries, got %d", m.Len())
}
m.Set("a", 1)
if m.Len() != 1 {
t.Errorf("Expected 1 entries, got %d", m.Len())
}
m.Set("b", 2)
m.Set("c", 3)
if m.Len() != 3 {
t.Errorf("Expected 3 entries, got %d", m.Len())
}
m.Remove("b")
if m.Len() != 2 {
t.Errorf("Expected 2 entries after removal, got %d", m.Len())
}
}
func TestConcurrentAccess(t *testing.T) {
m := New[int, string]()
const goroutines = 10
const itemsPerGoroutine = 100
var wg sync.WaitGroup
// Parallel writes
wg.Add(goroutines)
for i := 0; i < goroutines; i++ {
go func(id int) {
defer wg.Done()
for j := 0; j < itemsPerGoroutine; j++ {
key := id*itemsPerGoroutine + j
value := fmt.Sprintf("value-%d-%d", id, j)
m.Set(key, value)
}
}(i)
}
// Parallel reads
wg.Add(goroutines)
for i := 0; i < goroutines; i++ {
go func(id int) {
defer wg.Done()
for j := 0; j < 50; j++ {
_ = m.Len()
_ = m.List()
_ = m.Values()
}
}(i)
}
wg.Wait()
totalItems := goroutines * itemsPerGoroutine
if m.Len() != totalItems {
t.Errorf("Expected %d items after concurrent writes, got %d", totalItems, m.Len())
}
}
func TestString(t *testing.T) {
m := New[string, int]()
m.Set("a", 1)
m.Set("b", 2)
str := m.String()
// Skip string test due to map iteration order
_ = str
}
func Example() {
// Create empty map
m := New[string, int]()
fmt.Printf("Empty map length: %d\n", m.Len())
// Create with initial data
initial := map[string]int{"apple": 5, "banana": 2, "cherry": 5}
m = New[string, int](initial)
fmt.Printf("Initialized map length: %d\n", m.Len())
// Sort keys for consistent output
keys := m.GetKeys(5)
sort.Strings(keys)
fmt.Printf("Keys for value 5: %v\n", keys)
// Output:
// Empty map length: 0
// Initialized map length: 3
// Keys for value 5: [apple cherry]
}