-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinkedhashmap_test.go
252 lines (214 loc) · 5.06 KB
/
linkedhashmap_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
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
248
249
250
251
252
package tomtp
import (
"github.com/stretchr/testify/suite"
"sync"
"testing"
)
type LinkedHashMapTestSuite struct {
suite.Suite
lhm *LinkedHashMap[string, int]
}
func (s *LinkedHashMapTestSuite) SetupTest() {
s.lhm = NewLinkedHashMap[string, int]()
}
func TestLinkedHashMapSuite(t *testing.T) {
suite.Run(t, new(LinkedHashMapTestSuite))
}
func (s *LinkedHashMapTestSuite) TestPutAndGet() {
// Test putting and getting values
s.True(s.lhm.Put("one", 1))
result := s.lhm.Get("one")
s.Equal(1, result.Value)
// Test getting non-existent value
result = s.lhm.Get("doesnotexist")
s.Nil(result) // zero value for int
// Test updating existing value
s.True(s.lhm.Put("one", 100))
result = s.lhm.Get("one")
s.Equal(100, result.Value)
}
func (s *LinkedHashMapTestSuite) TestSizeAndClear() {
s.Equal(0, s.lhm.Size())
s.lhm.Put("one", 1)
s.lhm.Put("two", 2)
s.Equal(2, s.lhm.Size())
}
func (s *LinkedHashMapTestSuite) TestRemove() {
s.lhm.Put("one", 1)
s.lhm.Put("two", 2)
// Test removing existing item
result := s.lhm.Remove("one")
s.Equal(1, result.Value)
result = s.lhm.Get("one")
s.Nil(result) // zero value for int
s.Equal(1, s.lhm.Size())
// Test removing non-existent item
result = s.lhm.Remove("doesnotexist")
s.Nil(result) // zero value for int
}
func (s *LinkedHashMapTestSuite) TestInsertionOrder() {
values := []int{1, 2, 3, 4}
expectedOrder := []string{"one", "two", "three", "four"}
for i, key := range expectedOrder {
s.lhm.Put(key, values[i])
}
current := s.lhm.head
for i := 0; current != nil; i++ {
s.Equal(expectedOrder[i], current.Key)
s.Equal(values[i], current.Value)
current = current.next
}
}
func (s *LinkedHashMapTestSuite) TestOldestAndNewest() {
// Test empty map
pair := s.lhm.Oldest()
s.Nil(pair)
// Add items
s.lhm.Put("first", 1)
s.lhm.Put("second", 2)
s.lhm.Put("third", 3)
// Check oldest
pair = s.lhm.Oldest()
s.Equal("first", pair.Key)
s.Equal(1, pair.Value)
// Remove oldest and check new oldest
s.lhm.Remove("first")
pair = s.lhm.Oldest()
s.Equal("second", pair.Key)
s.Equal(2, pair.Value)
}
func (s *LinkedHashMapTestSuite) TestConcurrency() {
var wg sync.WaitGroup
numGoroutines := 10
numOperations := 100
// Concurrent writes
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(base int) {
defer wg.Done()
for j := 0; j < numOperations; j++ {
val := base*numOperations + j
s.lhm.Put("key", val)
}
}(i)
}
// Concurrent reads
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < numOperations; j++ {
s.lhm.Get("key")
}
}()
}
wg.Wait()
}
// Benchmark tests
func BenchmarkLinkedHashMap(b *testing.B) {
b.Run("Put", func(b *testing.B) {
lhm := NewLinkedHashMap[int, int]()
b.ResetTimer()
for i := 0; i < b.N; i++ {
lhm.Put(i, i)
}
})
b.Run("Get", func(b *testing.B) {
lhm := NewLinkedHashMap[int, int]()
for i := 0; i < 1000; i++ {
lhm.Put(i, i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
lhm.Get(i % 1000)
}
})
}
func TestFrontEmpty(t *testing.T) {
m := NewLinkedHashMap[string, int]()
if pair := m.Oldest(); pair != nil {
t.Errorf("Expected nil for empty map, got %v", pair)
}
}
func TestFrontSingle(t *testing.T) {
m := NewLinkedHashMap[string, int]()
m.Put("a", 1)
pair := m.Oldest()
if pair == nil {
t.Fatal("Expected non-nil pair")
}
if pair.Key != "a" || pair.Value != 1 {
t.Errorf("Expected {a,1}, got {%v,%v}", pair.Key, pair.Value)
}
}
func TestFrontMultiple(t *testing.T) {
m := NewLinkedHashMap[string, int]()
m.Put("a", 1)
m.Put("b", 2)
pair := m.Oldest()
if pair == nil {
t.Fatal("Expected non-nil pair")
}
if pair.Key != "a" || pair.Value != 1 {
t.Errorf("Expected {a,1}, got {%v,%v}", pair.Key, pair.Value)
}
}
func TestNextNil(t *testing.T) {
var pair *LhmPair[string, int]
if next := pair.Next(); next != nil {
t.Errorf("Expected nil for nil pair, got %v", next)
}
}
func TestNextEmpty(t *testing.T) {
m := NewLinkedHashMap[string, int]()
pair := &LhmPair[string, int]{m: m}
if next := pair.Next(); next != nil {
t.Errorf("Expected nil for empty map, got %v", next)
}
}
func TestNextSequence(t *testing.T) {
m := NewLinkedHashMap[string, int]()
m.Put("a", 1)
m.Put("b", 2)
m.Put("c", 3)
expected := []struct {
key string
val int
}{
{"a", 1},
{"b", 2},
{"c", 3},
}
pair := m.Oldest()
for i, exp := range expected {
if pair == nil {
t.Fatalf("Step %d: Unexpected nil pair", i)
}
if pair.Key != exp.key || pair.Value != exp.val {
t.Errorf("Step %d: Expected {%v,%v}, got {%v,%v}",
i, exp.key, exp.val, pair.Key, pair.Value)
}
pair = pair.Next()
}
if pair != nil {
t.Error("Expected nil after last element")
}
}
func TestNextAfterRemoval(t *testing.T) {
m := NewLinkedHashMap[string, int]()
m.Put("a", 1)
m.Put("b", 2)
m.Put("c", 3)
pair := m.Oldest()
if pair == nil {
t.Fatal("Expected non-nil front")
}
m.Remove("b")
next := pair.Next()
if next == nil {
t.Fatal("Expected non-nil next after removal")
}
if next.Key != "c" || next.Value != 3 {
t.Errorf("Expected {c,3}, got {%v,%v}", next.Key, next.Value)
}
}