github.com/MerlinKodo/quic-go@v0.39.2/internal/utils/tree/tree_match_test.go (about) 1 package tree 2 3 import ( 4 "testing" 5 ) 6 7 type interval struct { 8 start, end int 9 } 10 11 func (i interval) Comp(ot interval) int8 { 12 if i.start < ot.start { 13 return -1 14 } 15 if i.start > ot.start { 16 return 1 17 } 18 if i.end < ot.end { 19 return -1 20 } 21 if i.end > ot.end { 22 return 1 23 } 24 return 0 25 } 26 27 func (i interval) Match(ot interval) int8 { 28 // Check for overlap 29 if i.start <= ot.end && i.end >= ot.start { 30 return 0 31 } 32 if i.start > ot.end { 33 return 1 34 } else { 35 return -1 36 } 37 } 38 39 func TestBtree(t *testing.T) { 40 values := []interval{ 41 {start: 9, end: 10}, 42 {start: 3, end: 4}, 43 {start: 1, end: 2}, 44 {start: 5, end: 6}, 45 {start: 7, end: 8}, 46 {start: 20, end: 100}, 47 {start: 11, end: 12}, 48 } 49 btree := New[interval]() 50 btree.InsertAll(values) 51 52 expect, actual := len(values), btree.Len() 53 if actual != expect { 54 t.Error("length should equal", expect, "actual", actual) 55 } 56 57 rs := btree.Match(interval{start: 1, end: 6}) 58 if len(rs) != 3 { 59 t.Errorf("expected 3 results, got %d", len(rs)) 60 } 61 if rs[0].start != 1 || rs[0].end != 2 { 62 t.Errorf("expected result 1 to be [1, 2], got %v", rs[0]) 63 } 64 if rs[1].start != 3 || rs[1].end != 4 { 65 t.Errorf("expected result 2 to be [3, 4], got %v", rs[1]) 66 } 67 if rs[2].start != 5 || rs[2].end != 6 { 68 t.Errorf("expected result 3 to be [5, 6], got %v", rs[2]) 69 } 70 71 btree.Delete(interval{start: 5, end: 6}) 72 73 rs = btree.Match(interval{start: 1, end: 6}) 74 if len(rs) != 2 { 75 t.Errorf("expected 2 results, got %d", len(rs)) 76 } 77 if rs[0].start != 1 || rs[0].end != 2 { 78 t.Errorf("expected result 1 to be [1, 2], got %v", rs[0]) 79 } 80 if rs[1].start != 3 || rs[1].end != 4 { 81 t.Errorf("expected result 2 to be [3, 4], got %v", rs[1]) 82 } 83 84 btree.Delete(interval{start: 11, end: 12}) 85 86 rs = btree.Match(interval{start: 12, end: 19}) 87 if len(rs) != 0 { 88 t.Errorf("expected 0 results, got %d", len(rs)) 89 } 90 91 expect, actual = len(values)-2, btree.Len() 92 if actual != expect { 93 t.Error("length should equal", expect, "actual", actual) 94 } 95 }