| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package avltree |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"slices" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestAVLTreeGet(t *testing.T) { |
| 14 |
tree := New[int, string]() |
| 15 |
|
| 16 |
if actualValue := tree.Size(); actualValue != 0 { |
| 17 |
t.Errorf("Got %v expected %v", actualValue, 0) |
| 18 |
} |
| 19 |
|
| 20 |
if actualValue := tree.GetNode(2).Size(); actualValue != 0 { |
| 21 |
t.Errorf("Got %v expected %v", actualValue, 0) |
| 22 |
} |
| 23 |
|
| 24 |
tree.Put(1, "x") |
| 25 |
tree.Put(2, "b") |
| 26 |
tree.Put(1, "a") |
| 27 |
tree.Put(3, "c") |
| 28 |
tree.Put(4, "d") |
| 29 |
tree.Put(5, "e") |
| 30 |
tree.Put(6, "f") |
| 31 |
|
| 32 |
|
| 33 |
── 6 |
| 34 |
── 5 |
| 35 |
── 4 |
| 36 |
── 3 |
| 37 |
── 2 |
| 38 |
── 1 |
| 39 |
|
| 40 |
if actualValue := tree.Size(); actualValue != 6 { |
| 41 |
t.Errorf("Got %v expected %v", actualValue, 6) |
| 42 |
} |
| 43 |
|
| 44 |
if actualValue := tree.GetNode(2).Size(); actualValue != 3 { |
| 45 |
t.Errorf("Got %v expected %v", actualValue, 3) |
| 46 |
} |
| 47 |
|
| 48 |
if actualValue := tree.GetNode(4).Size(); actualValue != 6 { |
| 49 |
t.Errorf("Got %v expected %v", actualValue, 6) |
| 50 |
} |
| 51 |
|
| 52 |
if actualValue := tree.GetNode(7).Size(); actualValue != 0 { |
| 53 |
t.Errorf("Got %v expected %v", actualValue, 0) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestAVLTreePut(t *testing.T) { |
| 58 |
tree := New[int, string]() |
| 59 |
tree.Put(5, "e") |
| 60 |
tree.Put(6, "f") |
| 61 |
tree.Put(7, "g") |
| 62 |
tree.Put(3, "c") |
| 63 |
tree.Put(4, "d") |
| 64 |
tree.Put(1, "x") |
| 65 |
tree.Put(2, "b") |
| 66 |
tree.Put(1, "a") |
| 67 |
|
| 68 |
if actualValue := tree.Size(); actualValue != 7 { |
| 69 |
t.Errorf("Got %v expected %v", actualValue, 7) |
| 70 |
} |
| 71 |
if actualValue, expectedValue := tree.Keys(), []int{1, 2, 3, 4, 5, 6, 7}; !slices.Equal(actualValue, expectedValue) { |
| 72 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 73 |
} |
| 74 |
if actualValue, expectedValue := tree.Values(), []string{"a", "b", "c", "d", "e", "f", "g"}; !slices.Equal(actualValue, expectedValue) { |
| 75 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 76 |
} |
| 77 |
|
| 78 |
tests1 := [][]interface{}{ |
| 79 |
{1, "a", true}, |
| 80 |
{2, "b", true}, |
| 81 |
{3, "c", true}, |
| 82 |
{4, "d", true}, |
| 83 |
{5, "e", true}, |
| 84 |
{6, "f", true}, |
| 85 |
{7, "g", true}, |
| 86 |
{8, "", false}, |
| 87 |
} |
| 88 |
|
| 89 |
for _, test := range tests1 { |
| 90 |
|
| 91 |
actualValue, actualFound := tree.Get(test[0].(int)) |
| 92 |
if actualValue != test[1] || actualFound != test[2] { |
| 93 |
t.Errorf("Got %v expected %v", actualValue, test[1]) |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func TestAVLTreeRemove(t *testing.T) { |
| 99 |
tree := New[int, string]() |
| 100 |
tree.Put(5, "e") |
| 101 |
tree.Put(6, "f") |
| 102 |
tree.Put(7, "g") |
| 103 |
tree.Put(3, "c") |
| 104 |
tree.Put(4, "d") |
| 105 |
tree.Put(1, "x") |
| 106 |
tree.Put(2, "b") |
| 107 |
tree.Put(1, "a") |
| 108 |
|
| 109 |
tree.Remove(5) |
| 110 |
tree.Remove(6) |
| 111 |
tree.Remove(7) |
| 112 |
tree.Remove(8) |
| 113 |
tree.Remove(5) |
| 114 |
|
| 115 |
if actualValue, expectedValue := tree.Keys(), []int{1, 2, 3, 4}; !slices.Equal(actualValue, expectedValue) { |
| 116 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 117 |
} |
| 118 |
if actualValue, expectedValue := tree.Values(), []string{"a", "b", "c", "d"}; !slices.Equal(actualValue, expectedValue) { |
| 119 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 120 |
} |
| 121 |
if actualValue := tree.Size(); actualValue != 4 { |
| 122 |
t.Errorf("Got %v expected %v", actualValue, 7) |
| 123 |
} |
| 124 |
|
| 125 |
tests2 := [][]interface{}{ |
| 126 |
{1, "a", true}, |
| 127 |
{2, "b", true}, |
| 128 |
{3, "c", true}, |
| 129 |
{4, "d", true}, |
| 130 |
{5, "", false}, |
| 131 |
{6, "", false}, |
| 132 |
{7, "", false}, |
| 133 |
{8, "", false}, |
| 134 |
} |
| 135 |
|
| 136 |
for _, test := range tests2 { |
| 137 |
actualValue, actualFound := tree.Get(test[0].(int)) |
| 138 |
if actualValue != test[1] || actualFound != test[2] { |
| 139 |
t.Errorf("Got %v expected %v", actualValue, test[1]) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
tree.Remove(1) |
| 144 |
tree.Remove(4) |
| 145 |
tree.Remove(2) |
| 146 |
tree.Remove(3) |
| 147 |
tree.Remove(2) |
| 148 |
tree.Remove(2) |
| 149 |
|
| 150 |
if actualValue, expectedValue := tree.Keys(), []int{}; !slices.Equal(actualValue, expectedValue) { |
| 151 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 152 |
} |
| 153 |
if actualValue, expectedValue := tree.Values(), []string{}; !slices.Equal(actualValue, expectedValue) { |
| 154 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 155 |
} |
| 156 |
if empty, size := tree.Empty(), tree.Size(); empty != true || size != -0 { |
| 157 |
t.Errorf("Got %v expected %v", empty, true) |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
func TestAVLTreeLeftAndRight(t *testing.T) { |
| 163 |
tree := New[int, string]() |
| 164 |
|
| 165 |
if actualValue := tree.Left(); actualValue != nil { |
| 166 |
t.Errorf("Got %v expected %v", actualValue, nil) |
| 167 |
} |
| 168 |
if actualValue := tree.Right(); actualValue != nil { |
| 169 |
t.Errorf("Got %v expected %v", actualValue, nil) |
| 170 |
} |
| 171 |
|
| 172 |
tree.Put(1, "a") |
| 173 |
tree.Put(5, "e") |
| 174 |
tree.Put(6, "f") |
| 175 |
tree.Put(7, "g") |
| 176 |
tree.Put(3, "c") |
| 177 |
tree.Put(4, "d") |
| 178 |
tree.Put(1, "x") |
| 179 |
tree.Put(2, "b") |
| 180 |
|
| 181 |
if actualValue, expectedValue := tree.Left().Key, 1; actualValue != expectedValue { |
| 182 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 183 |
} |
| 184 |
if actualValue, expectedValue := tree.Left().Value, "x"; actualValue != expectedValue { |
| 185 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 186 |
} |
| 187 |
|
| 188 |
if actualValue, expectedValue := tree.Right().Key, 7; actualValue != expectedValue { |
| 189 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 190 |
} |
| 191 |
if actualValue, expectedValue := tree.Right().Value, "g"; actualValue != expectedValue { |
| 192 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
func TestAVLTreeCeilingAndFloor(t *testing.T) { |
| 197 |
tree := New[int, string]() |
| 198 |
|
| 199 |
if node, found := tree.Floor(0); node != nil || found { |
| 200 |
t.Errorf("Got %v expected %v", node, "<nil>") |
| 201 |
} |
| 202 |
if node, found := tree.Ceiling(0); node != nil || found { |
| 203 |
t.Errorf("Got %v expected %v", node, "<nil>") |
| 204 |
} |
| 205 |
|
| 206 |
tree.Put(5, "e") |
| 207 |
tree.Put(6, "f") |
| 208 |
tree.Put(7, "g") |
| 209 |
tree.Put(3, "c") |
| 210 |
tree.Put(4, "d") |
| 211 |
tree.Put(1, "x") |
| 212 |
tree.Put(2, "b") |
| 213 |
|
| 214 |
if node, found := tree.Floor(4); node.Key != 4 || !found { |
| 215 |
t.Errorf("Got %v expected %v", node.Key, 4) |
| 216 |
} |
| 217 |
if node, found := tree.Floor(0); node != nil || found { |
| 218 |
t.Errorf("Got %v expected %v", node, "<nil>") |
| 219 |
} |
| 220 |
|
| 221 |
if node, found := tree.Ceiling(4); node.Key != 4 || !found { |
| 222 |
t.Errorf("Got %v expected %v", node.Key, 4) |
| 223 |
} |
| 224 |
if node, found := tree.Ceiling(8); node != nil || found { |
| 225 |
t.Errorf("Got %v expected %v", node, "<nil>") |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
func TestAVLTreeIteratorNextOnEmpty(t *testing.T) { |
| 230 |
tree := New[int, string]() |
| 231 |
it := tree.Iterator() |
| 232 |
for it.Next() { |
| 233 |
t.Errorf("Shouldn't iterate on empty tree") |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
func TestAVLTreeIteratorPrevOnEmpty(t *testing.T) { |
| 238 |
tree := New[int, string]() |
| 239 |
it := tree.Iterator() |
| 240 |
for it.Prev() { |
| 241 |
t.Errorf("Shouldn't iterate on empty tree") |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
func TestAVLTreeIterator1Next(t *testing.T) { |
| 246 |
tree := New[int, string]() |
| 247 |
tree.Put(5, "e") |
| 248 |
tree.Put(6, "f") |
| 249 |
tree.Put(7, "g") |
| 250 |
tree.Put(3, "c") |
| 251 |
tree.Put(4, "d") |
| 252 |
tree.Put(1, "x") |
| 253 |
tree.Put(2, "b") |
| 254 |
tree.Put(1, "a") |
| 255 |
── 7 |
| 256 |
── 6 |
| 257 |
── 5 |
| 258 |
── 4 |
| 259 |
── 3 |
| 260 |
── 2 |
| 261 |
── 1 |
| 262 |
it := tree.Iterator() |
| 263 |
|
| 264 |
count := 0 |
| 265 |
for it.Next() { |
| 266 |
count++ |
| 267 |
key := it.Key() |
| 268 |
if actualValue, expectedValue := key, count; actualValue != expectedValue { |
| 269 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 270 |
} |
| 271 |
} |
| 272 |
if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue { |
| 273 |
t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
func TestAVLTreeIterator1Prev(t *testing.T) { |
| 278 |
tree := New[int, string]() |
| 279 |
tree.Put(5, "e") |
| 280 |
tree.Put(6, "f") |
| 281 |
tree.Put(7, "g") |
| 282 |
tree.Put(3, "c") |
| 283 |
tree.Put(4, "d") |
| 284 |
tree.Put(1, "x") |
| 285 |
tree.Put(2, "b") |
| 286 |
tree.Put(1, "a") |
| 287 |
── 7 |
| 288 |
── 6 |
| 289 |
── 5 |
| 290 |
── 4 |
| 291 |
── 3 |
| 292 |
── 2 |
| 293 |
── 1 |
| 294 |
it := tree.Iterator() |
| 295 |
for it.Next() { |
| 296 |
} |
| 297 |
countDown := tree.size |
| 298 |
for it.Prev() { |
| 299 |
key := it.Key() |
| 300 |
if actualValue, expectedValue := key, countDown; actualValue != expectedValue { |
| 301 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 302 |
} |
| 303 |
countDown-- |
| 304 |
} |
| 305 |
if actualValue, expectedValue := countDown, 0; actualValue != expectedValue { |
| 306 |
t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
func TestAVLTreeIterator2Next(t *testing.T) { |
| 311 |
tree := New[int, string]() |
| 312 |
tree.Put(3, "c") |
| 313 |
tree.Put(1, "a") |
| 314 |
tree.Put(2, "b") |
| 315 |
it := tree.Iterator() |
| 316 |
count := 0 |
| 317 |
for it.Next() { |
| 318 |
count++ |
| 319 |
key := it.Key() |
| 320 |
if actualValue, expectedValue := key, count; actualValue != expectedValue { |
| 321 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 322 |
} |
| 323 |
} |
| 324 |
if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue { |
| 325 |
t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
func TestAVLTreeIterator2Prev(t *testing.T) { |
| 330 |
tree := New[int, string]() |
| 331 |
tree.Put(3, "c") |
| 332 |
tree.Put(1, "a") |
| 333 |
tree.Put(2, "b") |
| 334 |
it := tree.Iterator() |
| 335 |
for it.Next() { |
| 336 |
} |
| 337 |
countDown := tree.size |
| 338 |
for it.Prev() { |
| 339 |
key := it.Key() |
| 340 |
if actualValue, expectedValue := key, countDown; actualValue != expectedValue { |
| 341 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 342 |
} |
| 343 |
countDown-- |
| 344 |
} |
| 345 |
if actualValue, expectedValue := countDown, 0; actualValue != expectedValue { |
| 346 |
t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
func TestAVLTreeIterator3Next(t *testing.T) { |
| 351 |
tree := New[int, string]() |
| 352 |
tree.Put(1, "a") |
| 353 |
it := tree.Iterator() |
| 354 |
count := 0 |
| 355 |
for it.Next() { |
| 356 |
count++ |
| 357 |
key := it.Key() |
| 358 |
if actualValue, expectedValue := key, count; actualValue != expectedValue { |
| 359 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 360 |
} |
| 361 |
} |
| 362 |
if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue { |
| 363 |
t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
func TestAVLTreeIterator3Prev(t *testing.T) { |
| 368 |
tree := New[int, string]() |
| 369 |
tree.Put(1, "a") |
| 370 |
it := tree.Iterator() |
| 371 |
for it.Next() { |
| 372 |
} |
| 373 |
countDown := tree.size |
| 374 |
for it.Prev() { |
| 375 |
key := it.Key() |
| 376 |
if actualValue, expectedValue := key, countDown; actualValue != expectedValue { |
| 377 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 378 |
} |
| 379 |
countDown-- |
| 380 |
} |
| 381 |
if actualValue, expectedValue := countDown, 0; actualValue != expectedValue { |
| 382 |
t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
func TestAVLTreeIterator4Next(t *testing.T) { |
| 387 |
tree := New[int, int]() |
| 388 |
tree.Put(13, 5) |
| 389 |
tree.Put(8, 3) |
| 390 |
tree.Put(17, 7) |
| 391 |
tree.Put(1, 1) |
| 392 |
tree.Put(11, 4) |
| 393 |
tree.Put(15, 6) |
| 394 |
tree.Put(25, 9) |
| 395 |
tree.Put(6, 2) |
| 396 |
tree.Put(22, 8) |
| 397 |
tree.Put(27, 10) |
| 398 |
── 27 |
| 399 |
── 25 |
| 400 |
└── 22 |
| 401 |
── 17 |
| 402 |
└── 15 |
| 403 |
─ 13 |
| 404 |
── 11 |
| 405 |
── 8 |
| 406 |
── 6 |
| 407 |
── 1 |
| 408 |
it := tree.Iterator() |
| 409 |
count := 0 |
| 410 |
for it.Next() { |
| 411 |
count++ |
| 412 |
value := it.Value() |
| 413 |
if actualValue, expectedValue := value, count; actualValue != expectedValue { |
| 414 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 415 |
} |
| 416 |
} |
| 417 |
if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue { |
| 418 |
t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
func TestAVLTreeIterator4Prev(t *testing.T) { |
| 423 |
tree := New[int, int]() |
| 424 |
tree.Put(13, 5) |
| 425 |
tree.Put(8, 3) |
| 426 |
tree.Put(17, 7) |
| 427 |
tree.Put(1, 1) |
| 428 |
tree.Put(11, 4) |
| 429 |
tree.Put(15, 6) |
| 430 |
tree.Put(25, 9) |
| 431 |
tree.Put(6, 2) |
| 432 |
tree.Put(22, 8) |
| 433 |
tree.Put(27, 10) |
| 434 |
── 27 |
| 435 |
── 25 |
| 436 |
└── 22 |
| 437 |
── 17 |
| 438 |
└── 15 |
| 439 |
─ 13 |
| 440 |
── 11 |
| 441 |
── 8 |
| 442 |
── 6 |
| 443 |
── 1 |
| 444 |
it := tree.Iterator() |
| 445 |
count := tree.Size() |
| 446 |
for it.Next() { |
| 447 |
} |
| 448 |
for it.Prev() { |
| 449 |
value := it.Value() |
| 450 |
if actualValue, expectedValue := value, count; actualValue != expectedValue { |
| 451 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 452 |
} |
| 453 |
count-- |
| 454 |
} |
| 455 |
if actualValue, expectedValue := count, 0; actualValue != expectedValue { |
| 456 |
t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue) |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
func TestAVLTreeIteratorBegin(t *testing.T) { |
| 461 |
tree := New[int, string]() |
| 462 |
tree.Put(3, "c") |
| 463 |
tree.Put(1, "a") |
| 464 |
tree.Put(2, "b") |
| 465 |
it := tree.Iterator() |
| 466 |
|
| 467 |
if it.Key() != 0 { |
| 468 |
t.Errorf("Got %v expected %v", it.Key(), 0) |
| 469 |
} |
| 470 |
|
| 471 |
it.Begin() |
| 472 |
|
| 473 |
if it.Key() != 0 { |
| 474 |
t.Errorf("Got %v expected %v", it.Key(), 0) |
| 475 |
} |
| 476 |
|
| 477 |
for it.Next() { |
| 478 |
} |
| 479 |
|
| 480 |
it.Begin() |
| 481 |
|
| 482 |
if it.Key() != 0 { |
| 483 |
t.Errorf("Got %v expected %v", it.Key(), 0) |
| 484 |
} |
| 485 |
|
| 486 |
it.Next() |
| 487 |
if key, value := it.Key(), it.Value(); key != 1 || value != "a" { |
| 488 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
func TestAVLTreeIteratorEnd(t *testing.T) { |
| 493 |
tree := New[int, string]() |
| 494 |
it := tree.Iterator() |
| 495 |
|
| 496 |
if it.Key() != 0 { |
| 497 |
t.Errorf("Got %v expected %v", it.Key(), 0) |
| 498 |
} |
| 499 |
|
| 500 |
it.End() |
| 501 |
if it.Key() != 0 { |
| 502 |
t.Errorf("Got %v expected %v", it.Key(), 0) |
| 503 |
} |
| 504 |
|
| 505 |
tree.Put(3, "c") |
| 506 |
tree.Put(1, "a") |
| 507 |
tree.Put(2, "b") |
| 508 |
it.End() |
| 509 |
if it.Key() != 0 { |
| 510 |
t.Errorf("Got %v expected %v", it.Key(), 0) |
| 511 |
} |
| 512 |
|
| 513 |
it.Prev() |
| 514 |
if key, value := it.Key(), it.Value(); key != 3 || value != "c" { |
| 515 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
func TestAVLTreeIteratorFirst(t *testing.T) { |
| 520 |
tree := New[int, string]() |
| 521 |
tree.Put(3, "c") |
| 522 |
tree.Put(1, "a") |
| 523 |
tree.Put(2, "b") |
| 524 |
it := tree.Iterator() |
| 525 |
if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { |
| 526 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 527 |
} |
| 528 |
if key, value := it.Key(), it.Value(); key != 1 || value != "a" { |
| 529 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
func TestAVLTreeIteratorLast(t *testing.T) { |
| 534 |
tree := New[int, string]() |
| 535 |
tree.Put(3, "c") |
| 536 |
tree.Put(1, "a") |
| 537 |
tree.Put(2, "b") |
| 538 |
it := tree.Iterator() |
| 539 |
if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { |
| 540 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 541 |
} |
| 542 |
if key, value := it.Key(), it.Value(); key != 3 || value != "c" { |
| 543 |
t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
func TestAVLTreeIteratorNextTo(t *testing.T) { |
| 548 |
|
| 549 |
seek := func(index int, value string) bool { |
| 550 |
return strings.HasSuffix(value, "b") |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
{ |
| 555 |
tree := New[int, string]() |
| 556 |
it := tree.Iterator() |
| 557 |
for it.NextTo(seek) { |
| 558 |
t.Errorf("Shouldn't iterate on empty tree") |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
{ |
| 564 |
tree := New[int, string]() |
| 565 |
tree.Put(0, "xx") |
| 566 |
tree.Put(1, "yy") |
| 567 |
it := tree.Iterator() |
| 568 |
for it.NextTo(seek) { |
| 569 |
t.Errorf("Shouldn't iterate on empty tree") |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
{ |
| 575 |
tree := New[int, string]() |
| 576 |
tree.Put(2, "cc") |
| 577 |
tree.Put(0, "aa") |
| 578 |
tree.Put(1, "bb") |
| 579 |
it := tree.Iterator() |
| 580 |
it.Begin() |
| 581 |
if !it.NextTo(seek) { |
| 582 |
t.Errorf("Shouldn't iterate on empty tree") |
| 583 |
} |
| 584 |
if index, value := it.Key(), it.Value(); index != 1 || value != "bb" { |
| 585 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") |
| 586 |
} |
| 587 |
if !it.Next() { |
| 588 |
t.Errorf("Should go to first element") |
| 589 |
} |
| 590 |
if index, value := it.Key(), it.Value(); index != 2 || value != "cc" { |
| 591 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc") |
| 592 |
} |
| 593 |
if it.Next() { |
| 594 |
t.Errorf("Should not go past last element") |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
func TestAVLTreeIteratorPrevTo(t *testing.T) { |
| 600 |
|
| 601 |
seek := func(index int, value string) bool { |
| 602 |
return strings.HasSuffix(value, "b") |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
{ |
| 607 |
tree := New[int, string]() |
| 608 |
it := tree.Iterator() |
| 609 |
it.End() |
| 610 |
for it.PrevTo(seek) { |
| 611 |
t.Errorf("Shouldn't iterate on empty tree") |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
{ |
| 617 |
tree := New[int, string]() |
| 618 |
tree.Put(0, "xx") |
| 619 |
tree.Put(1, "yy") |
| 620 |
it := tree.Iterator() |
| 621 |
it.End() |
| 622 |
for it.PrevTo(seek) { |
| 623 |
t.Errorf("Shouldn't iterate on empty tree") |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
{ |
| 629 |
tree := New[int, string]() |
| 630 |
tree.Put(2, "cc") |
| 631 |
tree.Put(0, "aa") |
| 632 |
tree.Put(1, "bb") |
| 633 |
it := tree.Iterator() |
| 634 |
it.End() |
| 635 |
if !it.PrevTo(seek) { |
| 636 |
t.Errorf("Shouldn't iterate on empty tree") |
| 637 |
} |
| 638 |
if index, value := it.Key(), it.Value(); index != 1 || value != "bb" { |
| 639 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") |
| 640 |
} |
| 641 |
if !it.Prev() { |
| 642 |
t.Errorf("Should go to first element") |
| 643 |
} |
| 644 |
if index, value := it.Key(), it.Value(); index != 0 || value != "aa" { |
| 645 |
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa") |
| 646 |
} |
| 647 |
if it.Prev() { |
| 648 |
t.Errorf("Should not go before first element") |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
func TestAVLTreeSerialization(t *testing.T) { |
| 654 |
tree := New[string, string]() |
| 655 |
tree.Put("c", "3") |
| 656 |
tree.Put("b", "2") |
| 657 |
tree.Put("a", "1") |
| 658 |
|
| 659 |
var err error |
| 660 |
assert := func() { |
| 661 |
if actualValue, expectedValue := tree.Size(), 3; actualValue != expectedValue { |
| 662 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 663 |
} |
| 664 |
if actualValue, expectedValue := tree.Keys(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) { |
| 665 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 666 |
} |
| 667 |
if actualValue, expectedValue := tree.Values(), []string{"1", "2", "3"}; !slices.Equal(actualValue, expectedValue) { |
| 668 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 669 |
} |
| 670 |
if err != nil { |
| 671 |
t.Errorf("Got error %v", err) |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
assert() |
| 676 |
|
| 677 |
bytes, err := tree.ToJSON() |
| 678 |
assert() |
| 679 |
|
| 680 |
err = tree.FromJSON(bytes) |
| 681 |
assert() |
| 682 |
|
| 683 |
bytes, err = json.Marshal([]interface{}{"a", "b", "c", tree}) |
| 684 |
if err != nil { |
| 685 |
t.Errorf("Got error %v", err) |
| 686 |
} |
| 687 |
|
| 688 |
intTree := New[string, int]() |
| 689 |
err = json.Unmarshal([]byte(`{"a":1,"b":2}`), intTree) |
| 690 |
if err != nil { |
| 691 |
t.Errorf("Got error %v", err) |
| 692 |
} |
| 693 |
if actualValue, expectedValue := intTree.Size(), 2; actualValue != expectedValue { |
| 694 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 695 |
} |
| 696 |
if actualValue, expectedValue := intTree.Keys(), []string{"a", "b"}; !slices.Equal(actualValue, expectedValue) { |
| 697 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 698 |
} |
| 699 |
if actualValue, expectedValue := intTree.Values(), []int{1, 2}; !slices.Equal(actualValue, expectedValue) { |
| 700 |
t.Errorf("Got %v expected %v", actualValue, expectedValue) |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
func TestAVLTreeString(t *testing.T) { |
| 705 |
c := New[int, int]() |
| 706 |
c.Put(1, 1) |
| 707 |
c.Put(2, 1) |
| 708 |
c.Put(3, 1) |
| 709 |
c.Put(4, 1) |
| 710 |
c.Put(5, 1) |
| 711 |
c.Put(6, 1) |
| 712 |
c.Put(7, 1) |
| 713 |
c.Put(8, 1) |
| 714 |
|
| 715 |
if !strings.HasPrefix(c.String(), "AVLTree") { |
| 716 |
t.Errorf("String should start with container name") |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
func benchmarkGet(b *testing.B, tree *Tree[int, struct{}], size int) { |
| 721 |
for i := 0; i < b.N; i++ { |
| 722 |
for n := 0; n < size; n++ { |
| 723 |
tree.Get(n) |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
func benchmarkPut(b *testing.B, tree *Tree[int, struct{}], size int) { |
| 729 |
for i := 0; i < b.N; i++ { |
| 730 |
for n := 0; n < size; n++ { |
| 731 |
tree.Put(n, struct{}{}) |
| 732 |
} |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
func benchmarkRemove(b *testing.B, tree *Tree[int, struct{}], size int) { |
| 737 |
for i := 0; i < b.N; i++ { |
| 738 |
for n := 0; n < size; n++ { |
| 739 |
tree.Remove(n) |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
func BenchmarkAVLTreeGet100(b *testing.B) { |
| 745 |
b.StopTimer() |
| 746 |
size := 100 |
| 747 |
tree := New[int, struct{}]() |
| 748 |
for n := 0; n < size; n++ { |
| 749 |
tree.Put(n, struct{}{}) |
| 750 |
} |
| 751 |
b.StartTimer() |
| 752 |
benchmarkGet(b, tree, size) |
| 753 |
} |
| 754 |
|
| 755 |
func BenchmarkAVLTreeGet1000(b *testing.B) { |
| 756 |
b.StopTimer() |
| 757 |
size := 1000 |
| 758 |
tree := New[int, struct{}]() |
| 759 |
for n := 0; n < size; n++ { |
| 760 |
tree.Put(n, struct{}{}) |
| 761 |
} |
| 762 |
b.StartTimer() |
| 763 |
benchmarkGet(b, tree, size) |
| 764 |
} |
| 765 |
|
| 766 |
func BenchmarkAVLTreeGet10000(b *testing.B) { |
| 767 |
b.StopTimer() |
| 768 |
size := 10000 |
| 769 |
tree := New[int, struct{}]() |
| 770 |
for n := 0; n < size; n++ { |
| 771 |
tree.Put(n, struct{}{}) |
| 772 |
} |
| 773 |
b.StartTimer() |
| 774 |
benchmarkGet(b, tree, size) |
| 775 |
} |
| 776 |
|
| 777 |
func BenchmarkAVLTreeGet100000(b *testing.B) { |
| 778 |
b.StopTimer() |
| 779 |
size := 100000 |
| 780 |
tree := New[int, struct{}]() |
| 781 |
for n := 0; n < size; n++ { |
| 782 |
tree.Put(n, struct{}{}) |
| 783 |
} |
| 784 |
b.StartTimer() |
| 785 |
benchmarkGet(b, tree, size) |
| 786 |
} |
| 787 |
|
| 788 |
func BenchmarkAVLTreePut100(b *testing.B) { |
| 789 |
b.StopTimer() |
| 790 |
size := 100 |
| 791 |
tree := New[int, struct{}]() |
| 792 |
b.StartTimer() |
| 793 |
benchmarkPut(b, tree, size) |
| 794 |
} |
| 795 |
|
| 796 |
func BenchmarkAVLTreePut1000(b *testing.B) { |
| 797 |
b.StopTimer() |
| 798 |
size := 1000 |
| 799 |
tree := New[int, struct{}]() |
| 800 |
for n := 0; n < size; n++ { |
| 801 |
tree.Put(n, struct{}{}) |
| 802 |
} |
| 803 |
b.StartTimer() |
| 804 |
benchmarkPut(b, tree, size) |
| 805 |
} |
| 806 |
|
| 807 |
func BenchmarkAVLTreePut10000(b *testing.B) { |
| 808 |
b.StopTimer() |
| 809 |
size := 10000 |
| 810 |
tree := New[int, struct{}]() |
| 811 |
for n := 0; n < size; n++ { |
| 812 |
tree.Put(n, struct{}{}) |
| 813 |
} |
| 814 |
b.StartTimer() |
| 815 |
benchmarkPut(b, tree, size) |
| 816 |
} |
| 817 |
|
| 818 |
func BenchmarkAVLTreePut100000(b *testing.B) { |
| 819 |
b.StopTimer() |
| 820 |
size := 100000 |
| 821 |
tree := New[int, struct{}]() |
| 822 |
for n := 0; n < size; n++ { |
| 823 |
tree.Put(n, struct{}{}) |
| 824 |
} |
| 825 |
b.StartTimer() |
| 826 |
benchmarkPut(b, tree, size) |
| 827 |
} |
| 828 |
|
| 829 |
func BenchmarkAVLTreeRemove100(b *testing.B) { |
| 830 |
b.StopTimer() |
| 831 |
size := 100 |
| 832 |
tree := New[int, struct{}]() |
| 833 |
for n := 0; n < size; n++ { |
| 834 |
tree.Put(n, struct{}{}) |
| 835 |
} |
| 836 |
b.StartTimer() |
| 837 |
benchmarkRemove(b, tree, size) |
| 838 |
} |
| 839 |
|
| 840 |
func BenchmarkAVLTreeRemove1000(b *testing.B) { |
| 841 |
b.StopTimer() |
| 842 |
size := 1000 |
| 843 |
tree := New[int, struct{}]() |
| 844 |
for n := 0; n < size; n++ { |
| 845 |
tree.Put(n, struct{}{}) |
| 846 |
} |
| 847 |
b.StartTimer() |
| 848 |
benchmarkRemove(b, tree, size) |
| 849 |
} |
| 850 |
|
| 851 |
func BenchmarkAVLTreeRemove10000(b *testing.B) { |
| 852 |
b.StopTimer() |
| 853 |
size := 10000 |
| 854 |
tree := New[int, struct{}]() |
| 855 |
for n := 0; n < size; n++ { |
| 856 |
tree.Put(n, struct{}{}) |
| 857 |
} |
| 858 |
b.StartTimer() |
| 859 |
benchmarkRemove(b, tree, size) |
| 860 |
} |
| 861 |
|
| 862 |
func BenchmarkAVLTreeRemove100000(b *testing.B) { |
| 863 |
b.StopTimer() |
| 864 |
size := 100000 |
| 865 |
tree := New[int, struct{}]() |
| 866 |
for n := 0; n < size; n++ { |
| 867 |
tree.Put(n, struct{}{}) |
| 868 |
} |
| 869 |
b.StartTimer() |
| 870 |
benchmarkRemove(b, tree, size) |
| 871 |
} |