-
Notifications
You must be signed in to change notification settings - Fork 0
/
bimultimap_test.go
166 lines (125 loc) · 6.63 KB
/
bimultimap_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
package bimultimap
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewBiMultiMap(t *testing.T) {
sut := New[string, string]()
expected := &BiMultiMap[string, string]{
forward: make(map[string][]string),
inverse: make(map[string][]string),
}
assert.Equal(t, expected, sut, "a new BiMultiMap should be empty")
}
func TestBiMultiMapPut(t *testing.T) {
sut := New[string, string]()
sut.Add("key", "value")
assert.True(t, sut.KeyExists("key"), "the key should exist")
assert.ElementsMatch(t, []string{"value"}, sut.LookupKey("key"), "the value associated with the key should be the correct one")
assert.True(t, sut.ValueExists("value"), "the value should exist")
assert.ElementsMatch(t, []string{"key"}, sut.LookupValue("value"), "the key associated with the value should be the correct one")
}
func TestBiMultiMapPutDup(t *testing.T) {
sut := New[string, string]()
sut.Add("key", "value")
sut.Add("key", "value")
assert.True(t, sut.KeyExists("key"), "the key should exist")
assert.ElementsMatch(t, []string{"value"}, sut.LookupKey("key"), "the value associated with the key should not be duplicated")
assert.True(t, sut.ValueExists("value"), "the value should exist")
assert.ElementsMatch(t, []string{"key"}, sut.LookupValue("value"), "the key associated with the value should not be duplicated")
}
func TestBiMultiMapMultiPut(t *testing.T) {
sut := biMultiMapWithMultipleKeysValues()
assert.True(t, sut.KeyExists("key1"), "key1 should exist")
assert.True(t, sut.KeyExists("key2"), "key2 should exist")
assert.ElementsMatch(t, []string{"value1", "value2"}, sut.LookupKey("key1"), "the values associated with the key should be the correct one")
assert.True(t, sut.ValueExists("value1"), "value1 should exist")
assert.True(t, sut.ValueExists("value2"), "value2 should exist")
assert.ElementsMatch(t, []string{"key1", "key2"}, sut.LookupValue("value1"), "the keys associated with the value should be the correct one")
}
func TestBiMultiMapGetEmpty(t *testing.T) {
sut := New[string, string]()
assert.ElementsMatch(t, []string{}, sut.LookupValue("foo"), "a nonexistent key should return an empty slice")
assert.ElementsMatch(t, []string{}, sut.LookupKey("foo"), "a nonexistent value should return an empty slice")
sut.Add("key", "value")
assert.ElementsMatch(t, []string{}, sut.LookupValue("foo"), "a nonexistent key should return an empty slice")
assert.ElementsMatch(t, []string{}, sut.LookupKey("foo"), "a nonexistent value should return an empty slice")
}
func TestBiMultiMapDeleteKey(t *testing.T) {
sut := New[string, string]()
sut.Add("key", "value")
value := sut.DeleteKey("key")
assert.ElementsMatch(t, []string{}, sut.LookupKey("key"), "deleting a key should delete it")
assert.ElementsMatch(t, []string{}, sut.LookupValue("value"), "a deleted key should delete its values")
assert.ElementsMatch(t, []string{"value"}, value, "deleting a key should return its associated values")
}
func TestBiMultiMapDeleteValue(t *testing.T) {
sut := New[string, string]()
sut.Add("key", "value")
value := sut.DeleteValue("value")
assert.ElementsMatch(t, []string{}, sut.LookupKey("key"), "deleting a value should delete it")
assert.ElementsMatch(t, []string{}, sut.LookupValue("value"), "a deleted value should delete its keys")
assert.ElementsMatch(t, []string{"key"}, value, "deleting a value should return its associated keys")
}
func TestBiMultiMapDeleteMultiKey(t *testing.T) {
sut := biMultiMapWithMultipleKeysValues()
sut.DeleteKey("key1")
assert.ElementsMatch(t, []string{}, sut.LookupKey("key1"), "deleting a key should delete all its values")
assert.ElementsMatch(t, []string{"value1", "value2"}, sut.LookupKey("key2"), "deleting a key should only delete its own values")
assert.ElementsMatch(t, []string{"key2"}, sut.LookupValue("value1"), "deleting a key should delete the inverse")
}
func TestBiMultiMapDeleteMultiValue(t *testing.T) {
sut := biMultiMapWithMultipleKeysValues()
sut.DeleteValue("value1")
assert.ElementsMatch(t, []string{}, sut.LookupValue("value1"), "deleting a value should delete all its keys")
assert.ElementsMatch(t, []string{"key1", "key2"}, sut.LookupValue("value2"), "deleting a value should only delete its own keys")
assert.ElementsMatch(t, []string{"value2"}, sut.LookupKey("key1"), "deleting a value should delete the inverse")
}
func TestMultiMapDeleteKeyValue(t *testing.T) {
sut := biMultiMapWithMultipleKeysValues()
sut.DeleteKeyValue("key1", "value1")
assert.ElementsMatch(t, []string{"value2"}, sut.LookupKey("key1"), "deleting a key/value pair should delete only that key/value pair from its key")
assert.ElementsMatch(t, []string{"value1", "value2"}, sut.LookupKey("key2"), "deleting a key/value pair should not affect other keys")
assert.ElementsMatch(t, []string{"key2"}, sut.LookupValue("value1"), "deleting a key/value pair delete only that key/value pair from its value")
assert.ElementsMatch(t, []string{"key1", "key2"}, sut.LookupValue("value2"), "deleting a key/value pair should not affect other values")
}
func TestBiMultiMapKeysValues(t *testing.T) {
sut := biMultiMapWithMultipleKeysValues()
sut.Add("key3", "value3")
keys := sut.Keys()
values := sut.Values()
assert.ElementsMatch(t, []string{"key1", "key2", "key3"}, keys, "Keys() should return a slice containing the keys")
assert.ElementsMatch(t, []string{"value1", "value2", "value3"}, values, "Values() should return a slice containing the keys")
}
func TestBiMultiMapClear(t *testing.T) {
sut := biMultiMapWithMultipleKeysValues()
sut.Clear()
assert.Equal(t, []string{}, sut.Keys())
assert.Equal(t, []string{}, sut.Values())
}
func TestBiMultiMapMerge(t *testing.T) {
map1 := biMultiMapWithMultipleKeysValues()
map2 := New[string, string]()
map2.Add("key1", "value3")
map2.Add("key3", "value3")
map2.Add("key1", "value1")
map2.Add("key3", "value1")
map2.Add("key4", "value4")
sut := map1.Merge(map2)
assert.ElementsMatch(t, []string{"key1", "key2", "key3", "key4"}, sut.Keys())
assert.ElementsMatch(t, []string{"value1", "value2", "value3"}, sut.LookupKey("key1"))
assert.ElementsMatch(t, []string{"value1", "value3"}, sut.LookupKey("key3"))
assert.ElementsMatch(t, []string{"value4"}, sut.LookupKey("key4"))
assert.ElementsMatch(t, []string{"value1", "value2", "value3", "value4"}, sut.Values())
assert.ElementsMatch(t, []string{"key1", "key2", "key3"}, sut.LookupValue("value1"))
assert.ElementsMatch(t, []string{"key1", "key3"}, sut.LookupValue("value3"))
assert.ElementsMatch(t, []string{"key4"}, sut.LookupValue("value4"))
}
func biMultiMapWithMultipleKeysValues() *BiMultiMap[string, string] {
m := New[string, string]()
m.Add("key1", "value1")
m.Add("key1", "value2")
m.Add("key2", "value1")
m.Add("key2", "value2")
return m
}