github.com/apernet/quic-go@v0.43.1-0.20240515053213-5e9e635fd9f0/internal/utils/tree/tree_test.go (about) 1 package tree 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 type IntVal int 9 10 func (i IntVal) Comp(v IntVal) int8 { 11 if i > v { 12 return 1 13 } else if i < v { 14 return -1 15 } else { 16 return 0 17 } 18 } 19 20 func (i IntVal) Match(v IntVal) int8 { 21 // Unused 22 return 0 23 } 24 25 type StringVal string 26 27 func (i StringVal) Comp(v StringVal) int8 { 28 if i > v { 29 return 1 30 } else if i < v { 31 return -1 32 } else { 33 return 0 34 } 35 } 36 37 func (i StringVal) Match(v StringVal) int8 { 38 // Unused 39 return 0 40 } 41 42 func btreeInOrder(n int) *Btree[IntVal] { 43 btree := New[IntVal]() 44 for i := 1; i <= n; i++ { 45 btree.Insert(IntVal(i)) 46 } 47 return btree 48 } 49 50 func btreeFixed[T Val[T]](values []T) *Btree[T] { 51 btree := New[T]() 52 btree.InsertAll(values) 53 return btree 54 } 55 56 func TestBtree_Get(t *testing.T) { 57 values := []IntVal{9, 4, 2, 6, 8, 0, 3, 1, 7, 5} 58 btree := btreeFixed[IntVal](values).InsertAll(values) 59 60 expect, actual := len(values), btree.Len() 61 if actual != expect { 62 t.Error("length should equal", expect, "actual", actual) 63 } 64 65 expect2 := IntVal(2) 66 if btree.Get(expect2) == nil || *btree.Get(expect2) != expect2 { 67 t.Error("value should equal", expect2) 68 } 69 } 70 71 func TestBtreeString_Get(t *testing.T) { 72 tree := New[StringVal]() 73 tree.Insert("Oreto").Insert("Michael").Insert("Ross") 74 75 expect := StringVal("Ross") 76 if tree.Get(expect) == nil || *tree.Get(expect) != expect { 77 t.Error("value should equal", expect) 78 } 79 } 80 81 func TestBtree_Contains(t *testing.T) { 82 btree := btreeInOrder(1000) 83 84 test := IntVal(1) 85 if !btree.Contains(test) { 86 t.Error("tree should contain", test) 87 } 88 89 test2 := []IntVal{1, 2, 3, 4} 90 if !btree.ContainsAll(test2) { 91 t.Error("tree should contain", test2) 92 } 93 94 test2 = []IntVal{5} 95 if !btree.ContainsAny(test2) { 96 t.Error("tree should contain", test2) 97 } 98 99 test2 = []IntVal{5000, 2000} 100 if btree.ContainsAny(test2) { 101 t.Error("tree should not contain any", test2) 102 } 103 } 104 105 func TestBtree_String(t *testing.T) { 106 btree := btreeFixed[IntVal]([]IntVal{1, 2, 3, 4, 5, 6}) 107 s1 := btree.String() 108 s2 := "[1 2 3 4 5 6]" 109 if s1 != s2 { 110 t.Error(s1, "tree string representation should equal", s2) 111 } 112 } 113 114 func TestBtree_Values(t *testing.T) { 115 const capacity = 3 116 btree := btreeFixed[IntVal]([]IntVal{1, 2}) 117 118 b := btree.Values() 119 c := []IntVal{1, 2} 120 if !reflect.DeepEqual(c, b) { 121 t.Error(c, "should equal", b) 122 } 123 btree.Insert(IntVal(3)) 124 125 desc := [capacity]IntVal{} 126 btree.Descend(func(n *Node[IntVal], i int) bool { 127 desc[i] = n.Value 128 return true 129 }) 130 d := [capacity]IntVal{3, 2, 1} 131 if !reflect.DeepEqual(desc, d) { 132 t.Error(desc, "should equal", d) 133 } 134 135 e := []IntVal{1, 2, 3} 136 for i, v := range btree.Values() { 137 if e[i] != v { 138 t.Error(e[i], "should equal", v) 139 } 140 } 141 } 142 143 func TestBtree_Delete(t *testing.T) { 144 test := []IntVal{1, 2, 3} 145 btree := btreeFixed(test) 146 147 btree.DeleteAll(test) 148 149 if !btree.Empty() { 150 t.Error("tree should be empty") 151 } 152 153 btree = btreeFixed(test) 154 pop := btree.Pop() 155 if pop == nil || *pop != IntVal(3) { 156 t.Error(pop, "should be 3") 157 } 158 pull := btree.Pull() 159 if pull == nil || *pull != IntVal(1) { 160 t.Error(pop, "should be 3") 161 } 162 if !btree.Delete(*btree.Pop()).Empty() { 163 t.Error("tree should be empty") 164 } 165 btree.Pop() 166 btree.Pull() 167 } 168 169 func TestBtree_HeadTail(t *testing.T) { 170 btree := btreeFixed[IntVal]([]IntVal{1, 2, 3}) 171 if btree.Head() == nil || *btree.Head() != IntVal(1) { 172 t.Error("head element should be 1") 173 } 174 if btree.Tail() == nil || *btree.Tail() != IntVal(3) { 175 t.Error("head element should be 3") 176 } 177 btree.Init() 178 if btree.Head() != nil { 179 t.Error("head element should be nil") 180 } 181 } 182 183 type TestKey1 struct { 184 Name string 185 } 186 187 func (testkey TestKey1) Comp(tk TestKey1) int8 { 188 var c int8 189 if testkey.Name > tk.Name { 190 c = 1 191 } else if testkey.Name < tk.Name { 192 c = -1 193 } 194 return c 195 } 196 197 func (testkey TestKey1) Match(tk TestKey1) int8 { 198 // Unused 199 return 0 200 } 201 202 func TestBtree_CustomKey(t *testing.T) { 203 btree := New[TestKey1]() 204 btree.InsertAll([]TestKey1{ 205 {Name: "Ross"}, 206 {Name: "Michael"}, 207 {Name: "Angelo"}, 208 {Name: "Jason"}, 209 }) 210 211 rootName := btree.root.Value.Name 212 if btree.root.Value.Name != "Michael" { 213 t.Error(rootName, "should equal Michael") 214 } 215 btree.Init() 216 btree.InsertAll([]TestKey1{ 217 {Name: "Ross"}, 218 {Name: "Michael"}, 219 {Name: "Angelo"}, 220 {Name: "Jason"}, 221 }) 222 btree.Debug() 223 s := btree.String() 224 test := "[{Angelo} {Jason} {Michael} {Ross}]" 225 if s != test { 226 t.Error(s, "should equal", test) 227 } 228 229 btree.Delete(TestKey1{Name: "Michael"}) 230 if btree.Len() != 3 { 231 t.Error("tree length should be 3") 232 } 233 test = "Jason" 234 if btree.root.Value.Name != test { 235 t.Error(btree.root.Value, "root of the tree should be", test) 236 } 237 for !btree.Empty() { 238 btree.Delete(btree.root.Value) 239 } 240 btree.Debug() 241 } 242 243 func TestBtree_Duplicates(t *testing.T) { 244 btree := New[IntVal]() 245 btree.InsertAll([]IntVal{ 246 0, 2, 5, 10, 15, 20, 12, 14, 247 13, 25, 0, 2, 5, 10, 15, 20, 12, 14, 13, 25, 248 }) 249 test := 10 250 length := btree.Len() 251 if length != test { 252 t.Error(length, "tree length should be", test) 253 } 254 }