-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsafe_slice_map_test.go
171 lines (141 loc) · 3.79 KB
/
safe_slice_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
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
package maps
import (
"encoding/gob"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSafeSliceMap_Mapi(t *testing.T) {
runMapiTests[SafeSliceMap[string, int]](t, makeMapi[SafeSliceMap[string, int]])
}
func init() {
gob.Register(new(SafeSliceMap[string, int]))
}
func TestSafeSliceMap_MapiWithSortFunction(t *testing.T) {
runMapiTests[SafeSliceMap[string, int]](t,
func(sources ...mapT) MapI[string, int] {
m := new(SafeSliceMap[string, int])
for _, s := range sources {
m.Merge(s)
}
m.SetSortFunc(func(k1, k2 string, v1, v2 int) bool {
return k1 < k2 // mimic natural sort for test result comparisons
})
return m
},
)
}
func ExampleSafeSliceMap_SetSortFunc() {
m := new(SafeSliceMap[string, int])
m.Set("b", 2)
m.Set("a", 1)
// This will print in the order items were assigned
fmt.Println(m)
// sort by keys
m.SetSortFunc(func(k1, k2 string, v1, v2 int) bool {
return k1 < k2
})
fmt.Println(m)
// Output: {"b":2,"a":1}
// {"a":1,"b":2}
}
func ExampleSafeSliceMap_SetAt() {
m := new(SafeSliceMap[string, int])
m.Set("b", 2)
m.Set("a", 1)
m.SetAt(1, "c", 3)
fmt.Println(m)
// Output: {"b":2,"c":3,"a":1}
}
func ExampleSafeSliceMap_GetAt() {
m := new(SafeSliceMap[string, int])
m.Set("b", 2)
m.Set("c", 3)
m.Set("a", 1)
v := m.GetAt(1)
fmt.Print(v)
// Output: 3
}
func ExampleSafeSliceMap_GetKeyAt() {
m := new(SafeSliceMap[string, int])
m.Set("b", 2)
m.Set("c", 3)
m.Set("a", 1)
v := m.GetKeyAt(1)
fmt.Print(v)
// Output: c
}
func TestSafeSliceMap_SetAt(t *testing.T) {
m := new(SafeSliceMap[string, int])
m.Set("b", 2)
m.Set("a", 1)
m.SetAt(5, "c", 3)
assert.Equal(t, 3, m.GetAt(2))
// backwards from end
m.SetAt(-1, "d", 4)
assert.Equal(t, 4, m.GetAt(2))
// past start, so at beginning
m.SetAt(-7, "e", 5)
assert.Equal(t, 5, m.GetAt(0))
// set same item does not cause reshuffle
m.Set("e", 6)
assert.Equal(t, 6, m.GetAt(0))
// delete and set will put to end
m.Delete("e")
m.Set("e", 6)
assert.Equal(t, 6, m.GetAt(4))
// Or force it to new location
m.SetAt(3, "e", 6)
assert.Equal(t, 6, m.GetAt(3))
// can't call SetAt when there is a sort function
m.SetSortFunc(func(k1, k2 string, v1, v2 int) bool {
return k1 < k2
})
assert.Panics(t, func() {
m.SetAt(3, "f", 4)
})
}
func TestSafeSliceMap_GetAt(t *testing.T) {
// Just need to check operation of empty maps. Other GetAt uses are checked elsewhere.
m := new(SafeSliceMap[string, int])
assert.Equal(t, 0, m.GetAt(0))
assert.Equal(t, "", m.GetKeyAt(0))
}
func TestSafeSliceMap_Clone(t *testing.T) {
// Create a new SafeSliceMap and populate it
originalMap := NewSafeSliceMap[string, int]()
originalMap.Set("b", 2)
originalMap.Set("a", 1)
originalMap.Set("c", 3)
// Clone the original map
clonedMap := originalMap.Clone()
assert.True(t, clonedMap.Equal(originalMap))
// Verify that modifying the cloned map does not affect the original map
clonedMap.Set("a", 100)
if originalMap.Get("a") == 100 {
t.Error("Modification in cloned map affected the original map")
}
// Verify that the original map remains unchanged
if originalMap.Get("a") != 1 {
t.Errorf("Expected value for key 'a' in original map to be %d, got %d", 1, originalMap.Get("a"))
}
// Verify order is the same
values := clonedMap.Values()
expectedValues := []int{2, 100, 3}
assert.Equal(t, expectedValues, values)
}
func TestCollectSafeSliceMap(t *testing.T) {
// Create a sequence of key-value pairs
s := NewSafeSliceMap[string, int]()
s.Set("b", 2)
s.Set("a", 1)
s.Set("c", 3)
seq := s.All()
// Use CollectSafeSliceMap to create a new SafeSliceMap
collectedMap := CollectSliceMap(seq)
assert.True(t, s.Equal(collectedMap))
// Ensure the order of keys follows the insertion order
keys := collectedMap.Keys()
expectedKeys := []string{"b", "a", "c"}
assert.Equal(t, keys, expectedKeys)
}