-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbimap_test.go
257 lines (179 loc) · 6.64 KB
/
bimap_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
253
254
255
256
257
package bimap
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
const key = "key"
const value = "value"
func TestNewBiMap(t *testing.T) {
actual := NewBiMap[string, string]()
expected := &BiMap[string, string]{forward: make(map[string]string), inverse: make(map[string]string)}
assert.Equal(t, expected, actual, "They should be equal")
}
func TestNewBiMapFrom(t *testing.T) {
actual := NewBiMapFromMap(map[string]string{
key: value,
})
actual.Insert(key, value)
fwdExpected := make(map[string]string)
invExpected := make(map[string]string)
fwdExpected[key] = value
invExpected[value] = key
expected := &BiMap[string, string]{forward: fwdExpected, inverse: invExpected}
assert.Equal(t, expected, actual, "They should be equal")
}
func TestBiMap_Insert(t *testing.T) {
actual := NewBiMap[string, string]()
actual.Insert(key, value)
fwdExpected := make(map[string]string)
invExpected := make(map[string]string)
fwdExpected[key] = value
invExpected[value] = key
expected := &BiMap[string, string]{forward: fwdExpected, inverse: invExpected}
assert.Equal(t, expected, actual, "They should be equal")
}
func TestBiMap_InsertTwice(t *testing.T) {
additionalValue := value + value
actual := NewBiMap[string, string]()
actual.Insert(key, value)
actual.Insert(key, additionalValue)
fwdExpected := make(map[string]string)
invExpected := make(map[string]string)
fwdExpected[key] = additionalValue
invExpected[additionalValue] = key
expected := &BiMap[string, string]{forward: fwdExpected, inverse: invExpected}
assert.Equal(t, expected, actual, "They should be equal")
}
func TestBiMap_Exists(t *testing.T) {
actual := NewBiMap[string, string]()
actual.Insert(key, value)
assert.False(t, actual.Exists("ARBITARY_KEY"), "Key should not exist")
assert.True(t, actual.Exists(key), "Inserted key should exist")
}
func TestBiMap_InverseExists(t *testing.T) {
actual := NewBiMap[string, string]()
actual.Insert(key, value)
assert.False(t, actual.ExistsInverse("ARBITARY_VALUE"), "Value should not exist")
assert.True(t, actual.ExistsInverse(value), "Inserted value should exist")
}
func TestBiMap_Get(t *testing.T) {
actual := NewBiMap[string, string]()
actual.Insert(key, value)
actualVal, ok := actual.Get(key)
assert.True(t, ok, "It should return true")
assert.Equal(t, value, actualVal, "Value and returned val should be equal")
actualVal, ok = actual.Get(value)
assert.False(t, ok, "It should return false")
assert.Empty(t, actualVal, "Actual val should be empty")
}
func TestBiMap_GetInverse(t *testing.T) {
actual := NewBiMap[string, string]()
actual.Insert(key, value)
actualKey, ok := actual.GetInverse(value)
assert.True(t, ok, "It should return true")
assert.Equal(t, key, actualKey, "Key and returned key should be equal")
actualKey, ok = actual.Get(value)
assert.False(t, ok, "It should return false")
assert.Empty(t, actualKey, "Actual key should be empty")
}
func TestBiMap_Size(t *testing.T) {
actual := NewBiMap[string, string]()
assert.Equal(t, 0, actual.Size(), "Length of empty bimap should be zero")
actual.Insert(key, value)
assert.Equal(t, 1, actual.Size(), "Length of bimap should be one")
}
func TestBiMap_Delete(t *testing.T) {
actual := NewBiMap[string, string]()
dummyKey := "DummyKey"
dummyVal := "DummyVal"
actual.Insert(key, value)
actual.Insert(dummyKey, dummyVal)
assert.Equal(t, 2, actual.Size(), "Size of bimap should be two")
actual.Delete(dummyKey)
fwdExpected := make(map[string]string)
invExpected := make(map[string]string)
fwdExpected[key] = value
invExpected[value] = key
expected := &BiMap[string, string]{forward: fwdExpected, inverse: invExpected}
assert.Equal(t, 1, actual.Size(), "Size of bimap should be two")
assert.Equal(t, expected, actual, "They should be the same")
actual.Delete(dummyKey)
assert.Equal(t, 1, actual.Size(), "Size of bimap should be two")
assert.Equal(t, expected, actual, "They should be the same")
}
func TestBiMap_InverseDelete(t *testing.T) {
actual := NewBiMap[string, string]()
dummyKey := "DummyKey"
dummyVal := "DummyVal"
actual.Insert(key, value)
actual.Insert(dummyKey, dummyVal)
assert.Equal(t, 2, actual.Size(), "Size of bimap should be two")
actual.DeleteInverse(dummyVal)
fwdExpected := make(map[string]string)
invExpected := make(map[string]string)
fwdExpected[key] = value
invExpected[value] = key
expected := &BiMap[string, string]{forward: fwdExpected, inverse: invExpected}
assert.Equal(t, 1, actual.Size(), "Size of bimap should be two")
assert.Equal(t, expected, actual, "They should be the same")
actual.DeleteInverse(dummyVal)
assert.Equal(t, 1, actual.Size(), "Size of bimap should be two")
assert.Equal(t, expected, actual, "They should be the same")
}
func TestBiMap_WithVaryingType(t *testing.T) {
actual := NewBiMap[string, int]()
dummyKey := "Dummy key"
dummyVal := 3
actual.Insert(dummyKey, dummyVal)
res, _ := actual.Get(dummyKey)
resVal, _ := actual.GetInverse(dummyVal)
assert.Equal(t, dummyVal, res, "Get by string key should return integer val")
assert.Equal(t, dummyKey, resVal, "Get by integer val should return string key")
}
func TestBiMap_MakeImmutable(t *testing.T) {
actual := NewBiMap[string, int]()
dummyKey := "Dummy key"
dummyVal := 3
actual.Insert(dummyKey, dummyVal)
actual.MakeImmutable()
assert.Panics(t, func() {
actual.Delete(dummyKey)
}, "It should panic on a mutation operation")
val, _ := actual.Get(dummyKey)
assert.Equal(t, dummyVal, val, "It should still have the value")
assert.Panics(t, func() {
actual.DeleteInverse(dummyVal)
}, "It should panic on a mutation operation")
key, _ := actual.GetInverse(dummyVal)
assert.Equal(t, dummyKey, key, "It should still have the key")
size := actual.Size()
assert.Equal(t, 1, size, "Size should be one")
assert.Panics(t, func() {
actual.Insert("New", 1)
}, "It should panic on a mutation operation")
size = actual.Size()
assert.Equal(t, 1, size, "Size should be one")
}
func TestBiMap_GetForwardMap(t *testing.T) {
actual := NewBiMap[string, int]()
dummyKey := "Dummy key"
dummyVal := 42
forwardMap := make(map[string]int)
forwardMap[dummyKey] = dummyVal
actual.Insert(dummyKey, dummyVal)
actualForwardMap := actual.GetForwardMap()
eq := reflect.DeepEqual(actualForwardMap, forwardMap)
assert.True(t, eq, "Forward maps should be equal")
}
func TestBiMap_GetInverseMap(t *testing.T) {
actual := NewBiMap[string, int]()
dummyKey := "Dummy key"
dummyVal := 42
inverseMap := make(map[int]string)
inverseMap[dummyVal] = dummyKey
actual.Insert(dummyKey, dummyVal)
actualInverseMap := actual.GetInverseMap()
eq := reflect.DeepEqual(actualInverseMap, inverseMap)
assert.True(t, eq, "Inverse maps should be equal")
}