@@ -269,6 +269,36 @@ func TestIntervalTreeDelete(t *testing.T) {
269
269
require .Truef (t , reflect .DeepEqual (expectedAfterDelete11 , visitsAfterDelete11 ), "level order after deleting '11' expected %v, got %v" , expectedAfterDelete11 , visitsAfterDelete11 )
270
270
}
271
271
272
+ func TestIntervalTreeFind (t * testing.T ) {
273
+ ivt := NewIntervalTree ()
274
+ ivl1 := NewInt64Interval (3 , 6 )
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 )
282
+ ivl2 := NewInt64Interval (3 , 7 )
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
+ }
300
+ }
301
+
272
302
func TestIntervalTreeIntersects (t * testing.T ) {
273
303
ivt := NewIntervalTree ()
274
304
ivt .Insert (NewStringInterval ("1" , "3" ), 123 )
@@ -341,8 +371,14 @@ func TestIntervalTreeRandom(t *testing.T) {
341
371
require .NotEmptyf (t , ivt .Stab (NewInt64Point (v )), "expected %v stab non-zero for [%+v)" , v , xy )
342
372
require .Truef (t , ivt .Intersects (NewInt64Point (v )), "did not get %d as expected for [%+v)" , v , xy )
343
373
}
344
- assert .Truef (t , ivt .Delete (NewInt64Interval (ab .x , ab .y )), "did not delete %v as expected" , ab )
374
+ ivl := NewInt64Interval (ab .x , ab .y )
375
+ iv := ivt .Find (ivl )
376
+ assert .NotNilf (t , iv , "expected find non-nil on %v" , ab )
377
+ assert .Equalf (t , ivl , iv .Ivl , "find did not get matched interval %v" , ab )
378
+ assert .Truef (t , ivt .Delete (ivl ), "did not delete %v as expected" , ab )
345
379
delete (ivs , ab )
380
+ ivAfterDel := ivt .Find (ivl )
381
+ assert .Nilf (t , ivAfterDel , "expected find nil after deletion on %v" , ab )
346
382
}
347
383
348
384
assert .Equalf (t , 0 , ivt .Len (), "got ivt.Len() = %v, expected 0" , ivt .Len ())
0 commit comments