-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintervals_test.go
More file actions
258 lines (222 loc) · 7.17 KB
/
Copy pathintervals_test.go
File metadata and controls
258 lines (222 loc) · 7.17 KB
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
258
package intervals
import (
"math"
"math/rand/v2"
"slices"
"strconv"
"testing"
)
func TestEmpty(t *testing.T) {
tests := []struct {
interval Interval[float64]
empty bool
}{
{interval: Interval[float64]{0.0, 0.0}, empty: true},
{interval: Interval[float64]{math.Copysign(0, -1), +0.0}, empty: true},
{interval: Interval[float64]{0.0, math.SmallestNonzeroFloat64}, empty: false},
{interval: Interval[float64]{0.0, 1.0}, empty: false},
{interval: Interval[float64]{-1.0, -1.0}, empty: true},
{interval: Interval[float64]{-1.0, 0.0}, empty: false},
{interval: Interval[float64]{math.Inf(-1), -1}, empty: false},
{interval: Interval[float64]{1, math.Inf(1)}, empty: false},
// NaN can't be compared to anything
{interval: Interval[float64]{math.NaN(), 1}, empty: true},
{interval: Interval[float64]{1, math.NaN()}, empty: true},
{interval: Interval[float64]{math.NaN(), math.NaN()}, empty: true},
}
for _, test := range tests {
if empty := test.interval.Empty(); empty != test.empty {
t.Errorf("Empty(%v) = %v, want %v", test.interval, empty, test.empty)
}
}
}
func TestSearchExpected(t *testing.T) {
tests := []struct {
intervals Intervals[int64]
offset int64
expected Interval[int64]
ok bool
}{
{intervals: Intervals[int64]{{0, 2}, {4, 5}}, offset: -1, expected: Interval[int64]{0, 0}, ok: false},
{intervals: Intervals[int64]{{0, 2}, {4, 5}}, offset: 0, expected: Interval[int64]{0, 2}, ok: true},
{intervals: Intervals[int64]{{0, 2}, {4, 5}}, offset: 1, expected: Interval[int64]{0, 2}, ok: true},
{intervals: Intervals[int64]{{0, 2}, {4, 5}}, offset: 2, expected: Interval[int64]{0, 0}, ok: false},
{intervals: Intervals[int64]{{0, 2}, {4, 5}}, offset: 3, expected: Interval[int64]{0, 0}, ok: false},
{intervals: Intervals[int64]{{0, 2}, {4, 5}}, offset: 4, expected: Interval[int64]{4, 5}, ok: true},
{intervals: Intervals[int64]{{0, 2}, {4, 5}}, offset: 5, expected: Interval[int64]{0, 0}, ok: false},
{intervals: Intervals[int64]{{0, 2}, {4, 5}}, offset: 6, expected: Interval[int64]{0, 0}, ok: false},
}
for _, test := range tests {
v, ok := test.intervals.Search(test.offset)
if v != test.expected || ok != test.ok {
t.Errorf("%v.Search(%d) = (%v, %v), want (%v, %v)", test.intervals, test.offset, v, ok, test.expected, test.ok)
}
}
}
func TestInsertExpected(t *testing.T) {
tests := []struct {
inserts Intervals[int64]
expected Intervals[int64]
}{
// Merge two adjacent things
{inserts: Intervals[int64]{{0, 2}, {2, 3}}, expected: Intervals[int64]{{0, 3}}},
// Same thing, reversed
{inserts: Intervals[int64]{{2, 3}, {0, 2}}, expected: Intervals[int64]{{0, 3}}},
// A gap shouldn't be merged
{inserts: Intervals[int64]{{0, 4}, {5, 8}, {10, 12}}, expected: Intervals[int64]{{0, 4}, {5, 8}, {10, 12}}},
// Filling in the gap should work fine
{inserts: Intervals[int64]{{2, 6}, {9, 11}, {6, 9}}, expected: Intervals[int64]{{2, 11}}},
// Replacing some or all of them is fine
{inserts: Intervals[int64]{{1, 4}, {10, 12}, {4, 16}}, expected: Intervals[int64]{{1, 16}}},
{inserts: Intervals[int64]{{0, 3}, {10, 12}, {2, 12}}, expected: Intervals[int64]{{0, 12}}},
{inserts: Intervals[int64]{{1, 4}, {10, 12}, {0, 7}}, expected: Intervals[int64]{{0, 7}, {10, 12}}},
// Inserting nothing gets nothing
{inserts: Intervals[int64]{{12, 12}}, expected: Intervals[int64]{}},
}
for _, test := range tests {
vs := Intervals[int64]{}
for _, v := range test.inserts {
vs = vs.Insert(Interval[int64]{v.Start, v.End})
}
if !slices.Equal(vs, test.expected) {
t.Errorf("inserting %v = %v, want %v", test.inserts, vs, test.expected)
}
}
}
func TestInsertRandom(t *testing.T) {
for range 1000 {
vs := Intervals[int64]{}
count := rand.IntN(10) + 1 //nolint:gosec // Not security sensitive.
start := int64(10000)
end := int64(-1)
for range count {
s := rand.Int64N(1024) //nolint:gosec // Not security sensitive.
e := s + rand.Int64N(128) //nolint:gosec // Not security sensitive.
v := Interval[int64]{s, e}
vs = vs.Insert(v)
if s == e {
continue
}
// Track our own idea of the first start and verify it.
if s < start {
start = s
}
if vs[0].Start != start {
t.Fatalf("vs[0].Start = %d, want %d", vs[0].Start, start)
}
// Track our own idea of the last end and verify it.
if e > end {
end = e
}
if vs[len(vs)-1].End != end {
t.Fatalf("vs[%d].End = %d, want %d", len(vs)-1, vs[len(vs)-1].End, end)
}
}
if len(vs) > count {
t.Fatalf("len(vs) = %d, want <= %d", len(vs), count)
}
for i := range vs {
// Make sure each of the records looks legit.
if vs[i].Start >= vs[i].End {
t.Fatalf("vs[%d] = %v, want Start < End", i, vs[i])
}
// And there's a gap to the next one.
if i < len(vs)-1 && vs[i].End >= vs[i+1].Start {
t.Fatalf("vs[%d].End = %d >= vs[%d].Start = %d, want a gap", i, vs[i].End, i+1, vs[i+1].Start)
}
}
}
}
func TestInsertPanic(t *testing.T) {
// End before start should panic.
assertPanics(t, func() { Intervals[int64]{}.Insert(Interval[int64]{2, 1}) })
}
// assertPanics fails the test if fn does not panic when called.
func assertPanics(t *testing.T, fn func()) {
t.Helper()
defer func() {
if recover() == nil {
t.Error("expected panic, but function returned normally")
}
}()
fn()
}
func BenchmarkInsertDisjoint(b *testing.B) {
benchInsert(b, 1024, 1, 0)
}
func BenchmarkInsertMergeable(b *testing.B) {
benchInsert(b, 1024, 0, 0)
}
func BenchmarkInsertOverlapping(b *testing.B) {
benchInsert(b, 1024, 0, 10240)
}
func benchInsert(b *testing.B, step, gap, overlap int64) {
b.ReportAllocs()
for _, num := range []int{1, 10, 100, 1000, 10000} {
b.Run(strconv.Itoa(num), func(sb *testing.B) {
sb.SetBytes(1)
sb.RunParallel(func(pb *testing.PB) {
ovs := make(Intervals[int64], 0, num)
ivs := make(Intervals[int64], num)
n := len(ivs)
for pb.Next() {
n++
if n >= len(ivs) {
ovs = ovs[:0]
randIntervals(ivs, step, gap, overlap)
n = 0
}
ovs = ovs.Insert(ivs[n])
}
})
})
}
}
func randIntervals(vs Intervals[int64], step, gap, overlap int64) {
s := int64(0)
for i := range vs {
l := rand.Int64N(step) //nolint:gosec // Not security sensitive.
e := s + l
v := Interval[int64]{s, e}
if overlap > 0 {
v.End += rand.Int64N(overlap) //nolint:gosec // Not security sensitive.
}
vs[i] = v
s = e + gap
}
rand.Shuffle(len(vs), func(i, j int) {
vs[i], vs[j] = vs[j], vs[i]
})
}
func BenchmarkSearchDisjoint(b *testing.B) {
benchSearch(b, 1024, 1, 0)
}
func BenchmarkSearchMerged(b *testing.B) {
benchSearch(b, 1024, 0, 0)
}
func benchSearch(b *testing.B, step, gap, overlap int64) {
b.ReportAllocs()
for _, num := range []int{1, 10, 100, 1000, 10000} {
b.Run(strconv.Itoa(num), func(sb *testing.B) {
sb.SetBytes(1)
sb.RunParallel(func(pb *testing.PB) {
ivs := make(Intervals[int64], num)
randIntervals(ivs, step, gap, overlap)
ovs := make(Intervals[int64], 0, len(ivs))
for _, iv := range ivs {
ovs = ovs.Insert(iv)
}
start := ovs[0].Start
end := ovs[len(ovs)-1].End
n := end
for pb.Next() {
n++
if n >= end {
n = start
}
_, _ = ovs.Search(n)
}
})
})
}
}