Skip to content

Commit abf0439

Browse files
committed
adt: more comprehensive tests for IntervalTree.Find
Signed-off-by: redwrasse <[email protected]>
1 parent 1fa4e01 commit abf0439

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

pkg/adt/interval_tree_test.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,31 @@ func TestIntervalTreeDelete(t *testing.T) {
272272
func TestIntervalTreeFind(t *testing.T) {
273273
ivt := NewIntervalTree()
274274
ivl1 := NewInt64Interval(3, 6)
275-
assert.Nilf(t, ivt.Find(ivl1), "find expected nil on empty tree")
276-
ivt.Insert(ivl1, 123)
277-
assert.Equalf(t, ivl1, ivt.Find(ivl1).Ivl, "find expected to return exact-matched interval")
275+
val := 123
276+
assert.Nilf(t, ivt.Find(ivl1), "find for %v expected nil on empty tree", ivl1)
277+
// insert interval [3, 6) into tree
278+
ivt.Insert(ivl1, val)
279+
// check cases of expected find matches and non-matches
280+
assert.NotNilf(t, ivt.Find(ivl1), "find expected not-nil on exact-matched interval %v", ivl1)
281+
assert.Equalf(t, ivl1, ivt.Find(ivl1).Ivl, "find expected to return exact-matched interval %v", ivl1)
278282
ivl2 := NewInt64Interval(3, 7)
279-
assert.Nilf(t, ivt.Find(ivl2), "find expected nil on single-matched endpoint")
283+
assert.Nilf(t, ivt.Find(ivl2), "find expected nil on matched start, different end %v", ivl2)
284+
ivl3 := NewInt64Interval(2, 6)
285+
assert.Nilf(t, ivt.Find(ivl3), "find expected nil on different start, matched end %v", ivl3)
286+
ivl4 := NewInt64Interval(10, 20)
287+
assert.Nilf(t, ivt.Find(ivl4), "find expected nil on different start, different end %v", ivl4)
288+
// insert the additional intervals into the tree, and check they can each be found.
289+
ivls := []Interval{ivl2, ivl3, ivl4}
290+
for _, ivl := range ivls {
291+
ivt.Insert(ivl, val)
292+
assert.NotNilf(t, ivt.Find(ivl), "find expected not-nil on exact-matched interval %v", ivl)
293+
assert.Equalf(t, ivl, ivt.Find(ivl).Ivl, "find expected to return exact-matched interval %v", ivl)
294+
}
295+
// check additional intervals no longer found after deletion
296+
for _, ivl := range ivls {
297+
assert.Truef(t, ivt.Delete(ivl), "expected successful delete on %v", ivl)
298+
assert.Nilf(t, ivt.Find(ivl), "find expected nil after deleted interval %v", ivl)
299+
}
280300
}
281301

282302
func TestIntervalTreeIntersects(t *testing.T) {
@@ -357,6 +377,8 @@ func TestIntervalTreeRandom(t *testing.T) {
357377
assert.Equalf(t, ivl, iv.Ivl, "find did not get matched interval %v", ab)
358378
assert.Truef(t, ivt.Delete(ivl), "did not delete %v as expected", ab)
359379
delete(ivs, ab)
380+
ivAfterDel := ivt.Find(ivl)
381+
assert.Nilf(t, ivAfterDel, "expected find nil after deletion on %v", ab)
360382
}
361383

362384
assert.Equalf(t, 0, ivt.Len(), "got ivt.Len() = %v, expected 0", ivt.Len())

0 commit comments

Comments
 (0)