forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_test.go
217 lines (180 loc) · 4.98 KB
/
set_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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ids
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSet(t *testing.T) {
id1 := ID{1}
ids := Set{}
ids.Add(id1)
if !ids.Contains(id1) {
t.Fatalf("Initial value not set correctly")
}
ids.Remove(id1)
if ids.Contains(id1) {
t.Fatalf("Value not removed correctly")
}
ids.Add(id1)
if !ids.Contains(id1) {
t.Fatalf("Initial value not set correctly")
} else if ids.Len() != 1 {
t.Fatalf("Bad set size")
} else if list := ids.List(); len(list) != 1 {
t.Fatalf("Bad list size")
} else if list[0] != id1 {
t.Fatalf("Set value not correct")
}
ids.Clear()
if ids.Contains(id1) {
t.Fatalf("Value not removed correctly")
}
ids.Add(id1)
ids2 := Set{}
if ids.Overlaps(ids2) {
t.Fatalf("Empty set shouldn't overlap")
}
ids2.Union(ids)
if !ids2.Contains(id1) {
t.Fatalf("Value not union added correctly")
}
if !ids.Overlaps(ids2) {
t.Fatalf("Sets overlap")
}
ids2.Difference(ids)
if ids2.Contains(id1) {
t.Fatalf("Value not difference removed correctly")
}
if ids.Overlaps(ids2) {
t.Fatalf("Sets don't overlap")
}
}
func TestSetCappedList(t *testing.T) {
set := Set{}
id := Empty
if list := set.CappedList(0); len(list) != 0 {
t.Fatalf("List should have been empty but was %v", list)
}
set.Add(id)
if list := set.CappedList(0); len(list) != 0 {
t.Fatalf("List should have been empty but was %v", list)
} else if list := set.CappedList(1); len(list) != 1 {
t.Fatalf("List should have had length %d but had %d", 1, len(list))
} else if returnedID := list[0]; id != returnedID {
t.Fatalf("List should have been %s but was %s", id, returnedID)
} else if list := set.CappedList(2); len(list) != 1 {
t.Fatalf("List should have had length %d but had %d", 1, len(list))
} else if returnedID := list[0]; id != returnedID {
t.Fatalf("List should have been %s but was %s", id, returnedID)
}
id2 := ID{1}
set.Add(id2)
if list := set.CappedList(0); len(list) != 0 {
t.Fatalf("List should have been empty but was %v", list)
} else if list := set.CappedList(1); len(list) != 1 {
t.Fatalf("List should have had length %d but had %d", 1, len(list))
} else if returnedID := list[0]; id != returnedID && id2 != returnedID {
t.Fatalf("List should have been %s but was %s", id, returnedID)
} else if list := set.CappedList(2); len(list) != 2 {
t.Fatalf("List should have had length %d but had %d", 2, len(list))
} else if list := set.CappedList(3); len(list) != 2 {
t.Fatalf("List should have had length %d but had %d", 2, len(list))
} else if returnedID := list[0]; id != returnedID && id2 != returnedID {
t.Fatalf("list contains unexpected element %s", returnedID)
} else if returnedID := list[1]; id != returnedID && id2 != returnedID {
t.Fatalf("list contains unexpected element %s", returnedID)
}
}
// Test that Clear() works with both the iterative and set-to-nil path
func TestSetClearLarge(t *testing.T) {
// Using iterative clear path
set := Set{}
for i := 0; i < clearSizeThreshold; i++ {
set.Add(GenerateTestID())
}
set.Clear()
if set.Len() != 0 {
t.Fatal("length should be 0")
}
set.Add(GenerateTestID())
if set.Len() != 1 {
t.Fatal("length should be 1")
}
// Using bulk (set map to nil) path
set = Set{}
for i := 0; i < clearSizeThreshold+1; i++ {
set.Add(GenerateTestID())
}
set.Clear()
if set.Len() != 0 {
t.Fatal("length should be 0")
}
set.Add(GenerateTestID())
if set.Len() != 1 {
t.Fatal("length should be 1")
}
}
func TestSetPop(t *testing.T) {
var s Set
_, ok := s.Pop()
assert.False(t, ok)
s = make(Set)
_, ok = s.Pop()
assert.False(t, ok)
id1, id2 := GenerateTestID(), GenerateTestID()
s.Add(id1, id2)
got, ok := s.Pop()
assert.True(t, ok)
assert.True(t, got == id1 || got == id2)
assert.EqualValues(t, 1, s.Len())
got, ok = s.Pop()
assert.True(t, ok)
assert.True(t, got == id1 || got == id2)
assert.EqualValues(t, 0, s.Len())
_, ok = s.Pop()
assert.False(t, ok)
}
func TestSetMarshalJSON(t *testing.T) {
assert := assert.New(t)
set := Set{}
{
asJSON, err := set.MarshalJSON()
assert.NoError(err)
assert.Equal("[]", string(asJSON))
}
id1, id2 := GenerateTestID(), GenerateTestID()
set.Add(id1)
{
asJSON, err := set.MarshalJSON()
assert.NoError(err)
assert.Equal(fmt.Sprintf("[\"%s\"]", id1), string(asJSON))
}
set.Add(id2)
{
asJSON, err := set.MarshalJSON()
assert.NoError(err)
assert.Equal(fmt.Sprintf("[\"%s\",\"%s\"]", id1, id2), string(asJSON))
}
}
func TestSortedList(t *testing.T) {
assert := assert.New(t)
set := Set{}
assert.Len(set.SortedList(), 0)
set.Add(ID{0})
sorted := set.SortedList()
assert.Len(sorted, 1)
assert.Equal(ID{0}, sorted[0])
set.Add(ID{1})
sorted = set.SortedList()
assert.Len(sorted, 2)
assert.Equal(ID{0}, sorted[0])
assert.Equal(ID{1}, sorted[1])
set.Add(ID{2})
sorted = set.SortedList()
assert.Len(sorted, 3)
assert.Equal(ID{0}, sorted[0])
assert.Equal(ID{1}, sorted[1])
assert.Equal(ID{2}, sorted[2])
}