1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
|
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "memory/resourceArea.hpp"
#include "runtime/os.hpp"
#include "testutils.hpp"
#include "unittest.hpp"
#include "utilities/growableArray.hpp"
#include "utilities/rbTree.hpp"
#include "utilities/rbTree.inline.hpp"
class RBTreeTest : public testing::Test {
public:
using RBTreeIntNode = RBNode<int, int>;
struct Cmp {
static int cmp(int a, int b) {
return a - b;
}
static bool cmp(const RBTreeIntNode* a, const RBTreeIntNode* b) {
return a->key() < b->key();
}
};
struct CmpInverse {
static int cmp(int a, int b) {
return b - a;
}
};
struct FCmp {
static int cmp(float a, float b) {
if (a < b) return -1;
if (a == b) return 0;
return 1;
}
};
// Bump-pointer style allocator that can't free
template <size_t AreaSize>
struct ArrayAllocator {
uint8_t area[AreaSize];
size_t offset = 0;
void* allocate(size_t sz) {
if (offset + sz > AreaSize) {
vm_exit_out_of_memory(sz, OOM_MALLOC_ERROR,
"red-black tree failed allocation");
}
void* place = &area[offset];
offset += sz;
return place;
}
void free(void* ptr) { }
};
using RBTreeInt = RBTreeCHeap<int, int, Cmp, mtTest>;
using IntrusiveTreeNode = IntrusiveRBNode;
struct IntrusiveHolder {
IntrusiveTreeNode node;
int key;
int data;
IntrusiveTreeNode* get_node() { return &node; }
IntrusiveHolder() {}
IntrusiveHolder(int key, int data) : key(key), data(data) {}
static IntrusiveHolder* cast_to_self(const IntrusiveTreeNode* node) { return (IntrusiveHolder*)node; }
};
struct IntrusiveCmp {
static int cmp(int a, const IntrusiveTreeNode* b) {
return a - IntrusiveHolder::cast_to_self(b)->key;
}
static int cmp(int a, int b) {
return a - b;
}
// true if a < b
static bool cmp(const IntrusiveTreeNode* a, const IntrusiveTreeNode* b) {
return (IntrusiveHolder::cast_to_self(a)->key -
IntrusiveHolder::cast_to_self(b)->key) < 0;
}
};
using IntrusiveTreeInt = IntrusiveRBTree<int, IntrusiveCmp>;
using IntrusiveCursor = IntrusiveTreeInt::Cursor;
public:
void inserting_duplicates_results_in_one_value() {
constexpr int up_to = 10;
GrowableArrayCHeap<int, mtTest> nums_seen(up_to, up_to, 0);
RBTreeInt rbtree;
const RBTreeInt& rbtree_const = rbtree;
for (int i = 0; i < up_to; i++) {
rbtree.upsert(i, i);
rbtree.upsert(i, i);
rbtree.upsert(i, i);
rbtree.upsert(i, i);
rbtree.upsert(i, i);
}
rbtree_const.visit_in_order([&](const RBTreeIntNode* node) {
nums_seen.at(node->key())++;
});
for (int i = 0; i < up_to; i++) {
EXPECT_EQ(1, nums_seen.at(i));
}
}
void rbtree_ought_not_leak() {
struct LeakCheckedAllocator {
int allocations;
LeakCheckedAllocator()
: allocations(0) {
}
void* allocate(size_t sz) {
void* allocation = os::malloc(sz, mtTest);
if (allocation == nullptr) {
vm_exit_out_of_memory(sz, OOM_MALLOC_ERROR, "rbtree failed allocation");
}
++allocations;
return allocation;
}
void free(void* ptr) {
--allocations;
os::free(ptr);
}
};
constexpr int up_to = 10;
{
RBTree<int, int, Cmp, LeakCheckedAllocator> rbtree;
for (int i = 0; i < up_to; i++) {
rbtree.upsert(i, i);
}
EXPECT_EQ(up_to, rbtree._allocator.allocations);
for (int i = 0; i < up_to; i++) {
rbtree.remove(i);
}
EXPECT_EQ(0, rbtree._allocator.allocations);
EXPECT_EQ(nullptr, rbtree._root);
}
{
RBTree<int, int, Cmp, LeakCheckedAllocator> rbtree;
for (int i = 0; i < up_to; i++) {
rbtree.upsert(i, i);
}
rbtree.remove_all();
EXPECT_EQ(0, rbtree._allocator.allocations);
EXPECT_EQ(nullptr, rbtree._root);
}
}
void test_find() {
struct Empty {};
RBTreeCHeap<float, Empty, FCmp, mtOther> rbtree;
using Node = RBNode<float, Empty>;
Node* n = nullptr;
auto test = [&](float f) {
EXPECT_EQ(nullptr, rbtree.find(f));
rbtree.upsert(f, Empty{});
const Node* n = rbtree.find_node(f);
EXPECT_NE(nullptr, n);
EXPECT_EQ(f, n->key());
};
test(1.0f);
test(5.0f);
test(0.0f);
}
void test_visitors() {
{ // Tests with 'default' ordering (ascending)
RBTreeInt rbtree;
const RBTreeInt& rbtree_const = rbtree;
using Node = RBTreeIntNode;
rbtree_const.visit_range_in_order(0, 100, [&](const Node* x) {
EXPECT_TRUE(false) << "Empty rbtree has no nodes to visit";
});
// Single-element set
rbtree.upsert(1, 0);
int count = 0;
rbtree_const.visit_range_in_order(0, 100, [&](const Node* x) {
count++;
});
EXPECT_EQ(1, count);
count = 0;
rbtree_const.visit_in_order([&](const Node* x) {
count++;
});
EXPECT_EQ(1, count);
// Add an element outside of the range that should not be visited on the right side and
// one on the left side.
rbtree.upsert(101, 0);
rbtree.upsert(-1, 0);
count = 0;
rbtree_const.visit_range_in_order(0, 100, [&](const Node* x) {
count++;
});
EXPECT_EQ(1, count);
count = 0;
rbtree_const.visit_in_order([&](const Node* x) {
count++;
});
EXPECT_EQ(3, count);
count = 0;
rbtree.upsert(0, 0);
rbtree_const.visit_range_in_order(0, 0, [&](const Node* x) {
count++;
});
EXPECT_EQ(1, count);
rbtree.remove_all();
for (int i = 0; i < 11; i++) {
rbtree.upsert(i, 0);
}
ResourceMark rm;
GrowableArray<int> seen;
rbtree_const.visit_range_in_order(0, 9, [&](const Node* x) {
seen.push(x->key());
});
EXPECT_EQ(10, seen.length());
for (int i = 0; i < 10; i++) {
EXPECT_EQ(i, seen.at(i));
}
seen.clear();
rbtree_const.visit_in_order([&](const Node* x) {
seen.push(x->key());
});
EXPECT_EQ(11, seen.length());
for (int i = 0; i < 10; i++) {
EXPECT_EQ(i, seen.at(i));
}
seen.clear();
rbtree_const.visit_range_in_order(10, 12, [&](const Node* x) {
seen.push(x->key());
});
EXPECT_EQ(1, seen.length());
EXPECT_EQ(10, seen.at(0));
}
{ // Test with descending ordering
RBTreeCHeap<int, int, CmpInverse, mtOther> rbtree;
const RBTreeCHeap<int, int, CmpInverse, mtOther>& rbtree_const = rbtree;
using Node = RBNode<int, int>;
for (int i = 0; i < 10; i++) {
rbtree.upsert(i, 0);
}
ResourceMark rm;
GrowableArray<int> seen;
rbtree_const.visit_range_in_order(9, -1, [&](const Node* x) {
seen.push(x->key());
});
EXPECT_EQ(10, seen.length());
for (int i = 0; i < 10; i++) {
EXPECT_EQ(10-i-1, seen.at(i));
}
seen.clear();
rbtree_const.visit_in_order([&](const Node* x) {
seen.push(x->key());
});
EXPECT_EQ(10, seen.length());
for (int i = 0; i < 10; i++) {
EXPECT_EQ(10 - i - 1, seen.at(i));
}
}
}
void test_visit_outside_range() {
RBTreeInt rbtree;
using Node = RBTreeIntNode;
rbtree.upsert(2, 0);
rbtree.upsert(5, 0);
constexpr int test_cases[9][2] = {{0, 0}, {0, 1}, {1, 1}, {3, 3}, {3, 4},
{4, 4}, {6, 6}, {6, 7}, {7, 7}};
for (const int (&test_case)[2] : test_cases) {
rbtree.visit_range_in_order(test_case[0], test_case[1], [&](const Node* x) {
FAIL() << "Range should not visit nodes";
});
}
}
void test_closest_leq() {
using Node = RBTreeIntNode;
{
RBTreeInt rbtree;
const RBTreeInt& rbtree_const = rbtree;
const Node* n = rbtree_const.closest_leq(0);
EXPECT_EQ(nullptr, n);
rbtree.upsert(0, 0);
n = rbtree_const.closest_leq(0);
EXPECT_EQ(0, n->key());
rbtree.upsert(-1, -1);
n = rbtree_const.closest_leq(0);
EXPECT_EQ(0, n->key());
rbtree.upsert(6, 0);
n = rbtree_const.closest_leq(6);
EXPECT_EQ(6, n->key());
n = rbtree_const.closest_leq(-2);
EXPECT_EQ(nullptr, n);
}
}
void test_closest_gt() {
using Node = RBTreeIntNode;
{
RBTreeInt rbtree;
Node* n = rbtree.closest_gt(0);
EXPECT_EQ(nullptr, n);
rbtree.upsert(0, 0);
n = rbtree.closest_gt(-1);
EXPECT_EQ(0, n->key());
rbtree.upsert(-5, -5);
n = rbtree.closest_gt(-1);
EXPECT_EQ(0, n->key());
n = rbtree.closest_gt(-5);
EXPECT_EQ(0, n->key());
n = rbtree.closest_gt(-10);
EXPECT_EQ(-5, n->key());
rbtree.upsert(10, 10);
n = rbtree.closest_gt(5);
EXPECT_EQ(10, n->key());
n = rbtree.closest_gt(10);
EXPECT_EQ(nullptr, n);
}
}
void test_leftmost() {
using Node = RBTreeIntNode;
RBTreeInt rbtree;
Node* n = rbtree.leftmost();
EXPECT_EQ(nullptr, n);
rbtree.upsert(0, 0);
n = rbtree.leftmost();
EXPECT_EQ(0, n->key());
rbtree.upsert(2, 2);
n = rbtree.leftmost();
EXPECT_EQ(0, n->key());
rbtree.upsert(1, 1);
n = rbtree.leftmost();
EXPECT_EQ(0, n->key());
rbtree.upsert(-1, -1);
n = rbtree.leftmost();
EXPECT_EQ(-1, n->key());
rbtree.remove(-1);
n = rbtree.leftmost();
EXPECT_EQ(0, n->key());
rbtree.remove(1);
n = rbtree.leftmost();
EXPECT_EQ(0, n->key());
rbtree.remove(0);
n = rbtree.leftmost();
EXPECT_EQ(2, n->key());
rbtree.remove(2);
n = rbtree.leftmost();
EXPECT_EQ(nullptr, n);
}
void test_node_prev() {
RBTreeInt rbtree;
const RBTreeInt& rbtree_const = rbtree;
using Node = RBTreeIntNode;
constexpr int num_nodes = 100;
for (int i = num_nodes; i > 0; i--) {
rbtree.upsert(i, i);
}
const Node* node = rbtree_const.find_node(num_nodes);
int count = num_nodes;
while (node != nullptr) {
EXPECT_EQ(count, node->val());
node = node->prev();
count--;
}
EXPECT_EQ(count, 0);
}
void test_node_next() {
RBTreeInt rbtree;
const RBTreeInt& rbtree_const = rbtree;
using Node = RBTreeIntNode;
constexpr int num_nodes = 100;
for (int i = 0; i < num_nodes; i++) {
rbtree.upsert(i, i);
}
const Node* node = rbtree_const.find_node(0);
int count = 0;
while (node != nullptr) {
EXPECT_EQ(count, node->val());
node = node->next();
count++;
}
EXPECT_EQ(count, num_nodes);
}
void test_stable_nodes() {
RBTreeInt rbtree;
const RBTreeInt& rbtree_const = rbtree;
using Node = RBTreeIntNode;
ResourceMark rm;
GrowableArray<Node*> a(10000);
for (int i = 0; i < 10000; i++) {
rbtree.upsert(i, i);
a.push(rbtree.find_node(i));
}
for (int i = 0; i < 2000; i++) {
int r = os::random() % 10000;
Node* to_delete = rbtree.find_node(r);
if (to_delete != nullptr && to_delete->_left != nullptr &&
to_delete->_right != nullptr) {
rbtree.remove(to_delete);
}
}
// After deleting, nodes should have been moved around but kept their values
for (int i = 0; i < 10000; i++) {
const Node* n = rbtree_const.find_node(i);
if (n != nullptr) {
EXPECT_EQ(a.at(i), n);
}
}
}
void test_stable_nodes_addresses() {
using Tree = RBTreeCHeap<int, void*, Cmp, mtOther>;
using Node = RBNode<int, void*>;
Tree rbtree;
for (int i = 0; i < 10000; i++) {
rbtree.upsert(i, (void*)nullptr);
Node* inserted_node = rbtree.find_node(i);
inserted_node->val() = inserted_node;
}
for (int i = 0; i < 2000; i++) {
int r = os::random() % 10000;
Node* to_delete = rbtree.find_node(r);
if (to_delete != nullptr && to_delete->_left != nullptr &&
to_delete->_right != nullptr) {
rbtree.remove(to_delete);
}
}
// After deleting, values should have remained consistant
rbtree.visit_in_order([&](const Node* node) {
EXPECT_EQ(node, node->val());
});
}
void test_node_hints() {
constexpr int num_nodes = 100;
RBTreeInt tree;
RBTreeIntNode* nodes[num_nodes];
RBTreeIntNode* prev_node = nullptr;
for (int i = 0; i < num_nodes; i++) {
RBTreeIntNode* node = tree.allocate_node(i, i);
nodes[i] = node;
tree.insert(i, node, prev_node);
prev_node = node;
}
for (int i = 0; i < num_nodes; i++) {
RBTreeIntNode* target_node = nodes[i];
for (int j = 0; j < num_nodes; j++) {
if (i == j) continue;
RBTreeIntNode* hint_node = nodes[j];
RBTreeIntNode* find_node = tree.find_node(i);
RBTreeIntNode* hint_find_node = tree.find_node(i, hint_node);
ASSERT_EQ(find_node, hint_find_node);
ASSERT_EQ(target_node, hint_find_node);
}
}
}
void test_cursor() {
constexpr int num_nodes = 10;
RBTreeInt tree;
for (int n = 0; n <= num_nodes; n++) {
RBTreeInt::Cursor find_cursor = tree.cursor(n);
EXPECT_FALSE(find_cursor.found());
}
for (int n = 0; n <= num_nodes; n++) {
tree.upsert(n, n);
}
for (int n = 0; n <= num_nodes; n++) {
RBTreeInt::Cursor find_cursor = tree.cursor(n);
EXPECT_TRUE(find_cursor.found());
}
EXPECT_FALSE(tree.cursor(-1).found());
EXPECT_FALSE(tree.cursor(101).found());
}
void test_get_cursor() {
constexpr int num_nodes = 10;
IntrusiveTreeInt tree;
GrowableArrayCHeap<IntrusiveHolder*, mtTest> nodes(num_nodes);
for (int n = 0; n <= num_nodes; n++) {
IntrusiveHolder* place = (IntrusiveHolder*)os::malloc(sizeof(IntrusiveHolder), mtTest);
new (place) IntrusiveHolder(n, n);
tree.insert_at_cursor(place->get_node(), tree.cursor(n));
nodes.push(place);
}
for (int n = 0; n <= num_nodes; n++) {
IntrusiveTreeNode* node = nodes.at(n)->get_node();
IntrusiveCursor cursor = tree.cursor(node);
IntrusiveCursor find_cursor = tree.cursor(n);
EXPECT_TRUE(cursor.found());
EXPECT_TRUE(cursor.valid());
EXPECT_TRUE(find_cursor.found());
EXPECT_TRUE(find_cursor.valid());
EXPECT_EQ(cursor.node(), find_cursor.node());
}
}
void test_cursor_empty_tree() {
RBTreeInt tree;
RBTreeInt::Cursor cursor = tree.cursor(tree.leftmost());
EXPECT_FALSE(cursor.valid());
cursor = tree.cursor(0);
EXPECT_TRUE(cursor.valid());
EXPECT_FALSE(cursor.found());
EXPECT_FALSE(tree.next(cursor).valid());
}
void test_cursor_iterate() {
constexpr int num_nodes = 100;
RBTreeInt tree;
for (int n = 0; n <= num_nodes; n++) {
tree.upsert(n, n);
}
RBTreeInt::Cursor cursor = tree.cursor(0);
for (int n = 0; n <= num_nodes; n++) {
EXPECT_TRUE(cursor.valid());
EXPECT_EQ(cursor.node()->val(), n);
cursor = tree.next(cursor);
}
EXPECT_FALSE(cursor.valid());
cursor = tree.cursor(num_nodes);
for (int n = num_nodes; n >= 0; n--) {
EXPECT_TRUE(cursor.valid());
EXPECT_EQ(cursor.node()->val(), n);
cursor = tree.prev(cursor);
}
EXPECT_FALSE(cursor.valid());
}
void test_leftmost_rightmost() {
using Node = RBTreeIntNode;
for (int i = 0; i < 10; i++) {
RBTreeInt rbtree;
const RBTreeInt& rbtree_const = rbtree;
int max = 0, min = INT_MAX;
for (int j = 0; j < 10; j++) {
if (j == 0) {
ASSERT_EQ(rbtree_const.leftmost(), (const Node*)nullptr);
ASSERT_EQ(rbtree_const.rightmost(), (const Node*)nullptr);
} else {
ASSERT_EQ(rbtree_const.rightmost()->key(), max);
ASSERT_EQ(rbtree_const.rightmost()->val(), max);
ASSERT_EQ(rbtree_const.leftmost()->key(), min);
ASSERT_EQ(rbtree_const.leftmost()->val(), min);
ASSERT_EQ(rbtree_const.rightmost(), rbtree.rightmost());
ASSERT_EQ(rbtree_const.leftmost(), rbtree.leftmost());
}
const int r = os::random();
rbtree.upsert(r, r);
min = MIN2(min, r);
max = MAX2(max, r);
}
// Explicitly test non-const variants
Node* n = rbtree.rightmost();
ASSERT_EQ(n->key(), max);
n->set_val(1);
n = rbtree.leftmost();
ASSERT_EQ(n->key(), min);
n->set_val(1);
}
}
void test_fill_verify() {
RBTreeInt rbtree;
const RBTreeInt& rbtree_const = rbtree;
ResourceMark rm;
GrowableArray<int> allocations;
int size = 10000;
// Create random values
for (int i = 0; i < size; i++) {
int r = os::random() % size;
allocations.append(r);
}
// Insert ~half of the values
for (int i = 0; i < size; i++) {
int r = os::random();
if (r % 2 == 0) {
rbtree.upsert(allocations.at(i), allocations.at(i));
}
if (i % 100 == 0) {
rbtree_const.verify_self();
}
}
// Insert and remove randomly
for (int i = 0; i < size; i++) {
int r = os::random();
if (r % 2 == 0) {
rbtree.upsert(allocations.at(i), allocations.at(i));
} else {
rbtree.remove(allocations.at(i));
}
if (i % 100 == 0) {
rbtree_const.verify_self();
}
}
// Remove all elements
for (int i = 0; i < size; i++) {
rbtree.remove(allocations.at(i));
}
rbtree.verify_self();
EXPECT_EQ(rbtree_const.size(), 0UL);
}
void test_cursor_replace() {
constexpr int num_nodes = 100;
RBTreeInt tree;
for (int i = 0; i < num_nodes * 10; i += 10) {
tree.upsert(i, i);
}
for (int i = 0; i < num_nodes * 10; i += 10) {
RBTreeInt::Cursor cursor = tree.cursor(tree.find_node(i));
RBTreeIntNode* new_node = tree.allocate_node(i + 1, i + 1);
tree.replace_at_cursor(new_node, cursor);
}
for (int i = 0; i < num_nodes * 10; i += 10) {
RBTreeIntNode* node = tree.find_node(i);
EXPECT_NULL(node);
node = tree.find_node(i + 1);
EXPECT_NOT_NULL(node);
}
tree.verify_self();
}
void test_intrusive() {
IntrusiveTreeInt intrusive_tree;
int num_iterations = 100;
// Insert values
for (int n = 0; n < num_iterations; n++) {
IntrusiveCursor cursor = intrusive_tree.cursor(n);
EXPECT_NULL(cursor.node());
// Custom allocation here is just malloc
IntrusiveHolder* place = (IntrusiveHolder*)os::malloc(sizeof(IntrusiveHolder), mtTest);
new (place) IntrusiveHolder(n, n);
intrusive_tree.insert_at_cursor(place->get_node(), cursor);
IntrusiveCursor cursor2 = intrusive_tree.cursor(n);
EXPECT_NOT_NULL(cursor2.node());
intrusive_tree.verify_self();
}
// Check inserted values
for (int n = 0; n < num_iterations; n++) {
IntrusiveCursor cursor = intrusive_tree.cursor(n);
EXPECT_NOT_NULL(cursor.node());
EXPECT_EQ(n, IntrusiveHolder::cast_to_self(cursor.node())->data);
}
// Remove all values
for (int n = 0; n < num_iterations; n++) {
IntrusiveCursor cursor = intrusive_tree.cursor(n);
EXPECT_NOT_NULL(cursor.node());
intrusive_tree.remove_at_cursor(cursor);
IntrusiveCursor cursor2 = intrusive_tree.cursor(n);
EXPECT_NULL(cursor2.node());
intrusive_tree.verify_self();
}
// Check removed values
for (int n = 0; n < num_iterations; n++) {
IntrusiveCursor cursor = intrusive_tree.cursor(n);
EXPECT_NULL(cursor.node());
}
}
#ifdef ASSERT
void test_nodes_visited_once() {
constexpr size_t memory_size = 65536;
using Tree = RBTree<int, int, Cmp, ArrayAllocator<memory_size>>;
using Node = RBNode<int, int>;
Tree tree;
int num_nodes = memory_size / sizeof(Node);
for (int i = 0; i < num_nodes; i++) {
tree.upsert(i, i);
}
Node* start = tree.find_node(0);
Node* node = start;
for (int i = 0; i < num_nodes; i++) {
EXPECT_EQ(tree._expected_visited, node->_visited);
node += 1;
}
tree.verify_self();
node = start;
for (int i = 0; i < num_nodes; i++) {
EXPECT_EQ(tree._expected_visited, node->_visited);
node += 1;
}
}
#endif // ASSERT
};
TEST_VM_F(RBTreeTest, InsertingDuplicatesResultsInOneValue) {
this->inserting_duplicates_results_in_one_value();
}
TEST_VM_F(RBTreeTest, RBTreeOughtNotLeak) {
this->rbtree_ought_not_leak();
}
TEST_VM_F(RBTreeTest, TestFind) {
this->test_find();
}
TEST_VM_F(RBTreeTest, TestVisitors) {
this->test_visitors();
}
TEST_VM_F(RBTreeTest, TestVisitOutsideRange) {
this->test_visit_outside_range();
}
TEST_VM_F(RBTreeTest, TestClosestLeq) {
this->test_closest_leq();
}
TEST_VM_F(RBTreeTest, TestClosestGt) {
this->test_closest_gt();
}
TEST_VM_F(RBTreeTest, TestFirst) {
this->test_leftmost();
}
TEST_VM_F(RBTreeTest, NodePrev) {
this->test_node_prev();
}
TEST_VM_F(RBTreeTest, NodeNext) {
this->test_node_next();
}
TEST_VM_F(RBTreeTest, NodeStableTest) {
this->test_stable_nodes();
}
TEST_VM_F(RBTreeTest, NodeStableAddressTest) {
this->test_stable_nodes_addresses();
}
TEST_VM_F(RBTreeTest, NodeHints) {
this->test_node_hints();
}
TEST_VM_F(RBTreeTest, CursorFind) {
this->test_cursor();
}
TEST_VM_F(RBTreeTest, CursorGet) {
this->test_cursor();
}
TEST_VM_F(RBTreeTest, CursorEmptyTreeTest) {
this->test_cursor_empty_tree();
}
TEST_VM_F(RBTreeTest, CursorIterateTest) {
this->test_cursor_iterate();
}
TEST_VM_F(RBTreeTest, LeftMostRightMost) {
this->test_leftmost_rightmost();
}
struct PtrCmp {
static int cmp(const void* a, const void* b) {
const uintptr_t ai = p2u(a);
const uintptr_t bi = p2u(b);
return ai == bi ? 0 : (ai > bi ? 1 : -1);
}
};
TEST_VM(RBTreeTestNonFixture, TestPrintPointerTree) {
typedef RBTreeCHeap<const void*, unsigned, PtrCmp, mtTest> TreeType;
TreeType tree;
#ifdef _LP64
const void* const p1 = (const void*) 0x800000000ULL;
const char* const s1 = "[0x0000000800000000] = 1";
const void* const p2 = (const void*) 0xDEADBEEF0ULL;
const char* const s2 = "[0x0000000deadbeef0] = 2";
const void* const p3 = (const void*) 0x7f223fba0ULL;
const char* const s3 = "[0x00000007f223fba0] = 3";
#else
const void* const p1 = (const void*) 0x80000000ULL;
const char* const s1 = "[0x80000000] = 1";
const void* const p2 = (const void*) 0xDEADBEEFLL;
const char* const s2 = "[0xdeadbeef] = 2";
const void* const p3 = (const void*) 0x7f223fbaULL;
const char* const s3 = "[0x7f223fba] = 3";
#endif
tree.upsert(p1, 1U);
tree.upsert(p2, 2U);
tree.upsert(p3, 3U);
stringStream ss;
tree.print_on(&ss);
const char* const N = nullptr;
ASSERT_NE(strstr(ss.base(), s1), N);
ASSERT_NE(strstr(ss.base(), s2), N);
ASSERT_NE(strstr(ss.base(), s3), N);
}
struct IntCmp {
static int cmp(int a, int b) { return a == b ? 0 : (a > b ? 1 : -1); }
};
TEST_VM(RBTreeTestNonFixture, TestPrintIntegerTree) {
typedef RBTree<int, unsigned, IntCmp, RBTreeCHeapAllocator<mtTest> > TreeType;
TreeType tree;
const int i1 = 82924;
const char* const s1 = "[82924] = 1";
const int i2 = -13591;
const char* const s2 = "[-13591] = 2";
const int i3 = 0;
const char* const s3 = "[0] = 3";
tree.upsert(i1, 1U);
tree.upsert(i2, 2U);
tree.upsert(i3, 3U);
stringStream ss;
tree.print_on(&ss);
const char* const N = nullptr;
ASSERT_NE(strstr(ss.base(), s1), N);
ASSERT_NE(strstr(ss.base(), s2), N);
ASSERT_NE(strstr(ss.base(), s3), N);
}
TEST_VM_F(RBTreeTest, IntrusiveTest) {
this->test_intrusive();
}
TEST_VM_F(RBTreeTest, FillAndVerify) {
this->test_fill_verify();
}
TEST_VM_F(RBTreeTest, CursorReplace) {
this->test_cursor_replace();
}
#ifdef ASSERT
TEST_VM_F(RBTreeTest, NodesVisitedOnce) {
this->test_nodes_visited_once();
}
#endif // ASSERT
TEST_VM_F(RBTreeTest, InsertRemoveVerify) {
constexpr int num_nodes = 100;
for (int n_t1 = 0; n_t1 < num_nodes; n_t1++) {
for (int n_t2 = 0; n_t2 < n_t1; n_t2++) {
RBTreeInt tree;
for (int i = 0; i < n_t1; i++) {
tree.upsert(i, i);
}
for (int i = 0; i < n_t2; i++) {
tree.remove(i);
}
tree.verify_self();
}
}
}
TEST_VM_F(RBTreeTest, VerifyItThroughStressTest) {
{ // Repeatedly verify a tree of moderate size
RBTreeInt rbtree;
constexpr int ten_thousand = 10000;
for (int i = 0; i < ten_thousand; i++) {
int r = os::random();
if (r % 2 == 0) {
rbtree.upsert(i, i);
} else {
rbtree.remove(i);
}
if (i % 100 == 0) {
rbtree.verify_self();
}
}
RBTreeInt::Cursor cursor = rbtree.cursor(10);
RBTreeInt::Cursor cursor2 = rbtree.next(cursor);
for (int i = 0; i < ten_thousand; i++) {
int r = os::random();
if (r % 2 == 0) {
rbtree.upsert(i, i);
} else {
rbtree.remove(i);
}
if (i % 100 == 0) {
rbtree.verify_self();
}
}
}
{ // Make a very large tree and verify at the end
RBTreeCHeap<int, int, Cmp, mtOther> rbtree;
constexpr int one_hundred_thousand = 100000;
for (int i = 0; i < one_hundred_thousand; i++) {
rbtree.upsert(i, i);
}
EXPECT_EQ((size_t)one_hundred_thousand, rbtree.size());
rbtree.verify_self();
}
}
|