test/rbtree/test_rbtree_insert_fixup.cpp
TEST(fixup_single_node)
{
    allocator* alloc = nullptr;
    rbtree* tree = nullptr;

    /* we should be able to create a malloc allocator. */
    TEST_ASSERT(
        STATUS_SUCCESS == malloc_allocator_create(&alloc));

    /* we should be able to create an rbtree instance. */
    TEST_ASSERT(
        STATUS_SUCCESS ==
            rbtree_create(&tree, alloc, &dummy_compare, &dummy_key, nullptr));

    rbtree_node z;

    /* PRECONDITIONS. */
    tree->root = &z;
    z.parent = tree->nil;
    z.left = tree->nil;
    z.right = tree->nil;
    z.color = RBTREE_RED;

    /* perform the insert fixup. */
    rbtree_insert_fixup(tree, &z);

    /* POSTCONDITIONS. */
    TEST_EXPECT(&z == tree->root);
    TEST_EXPECT(tree->nil == z.left);
    TEST_EXPECT(tree->nil == z.right);
    TEST_EXPECT(RBTREE_BLACK == z.color);

    /* before cleaning up rbtree, set the root to nil. */
    tree->root = tree->nil;

    /* clean up. */
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(rbtree_resource_handle(tree)));
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(allocator_resource_handle(alloc)));
}
/**
 * Create an rbtree, sub in a single node, and verify that rbtree_insert_fixup
 * works correctly.
 */
Create an rbtree, sub in a single node, and verify that rbtree_insert_fixup
works correctly.
Create an rbtree sub in a single node and verify that rbtree_insert_fixup works correctly .
TEST(fixup_single_node)
{
    allocator* alloc = nullptr;
    rbtree* tree = nullptr;
    TEST_ASSERT(
        STATUS_SUCCESS == malloc_allocator_create(&alloc));
    TEST_ASSERT(
        STATUS_SUCCESS ==
            rbtree_create(&tree, alloc, &dummy_compare, &dummy_key, nullptr));
    rbtree_node z;
    tree->root = &z;
    z.parent = tree->nil;
    z.left = tree->nil;
    z.right = tree->nil;
    z.color = RBTREE_RED;
    rbtree_insert_fixup(tree, &z);
    TEST_EXPECT(&z == tree->root);
    TEST_EXPECT(tree->nil == z.left);
    TEST_EXPECT(tree->nil == z.right);
    TEST_EXPECT(RBTREE_BLACK == z.color);
    tree->root = tree->nil;
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(rbtree_resource_handle(tree)));
    TEST_ASSERT(
        STATUS_SUCCESS == resource_release(allocator_resource_handle(alloc)));
}
TEST ( fixup_single_node ) { allocator * alloc = nullptr ; rbtree * tree = nullptr ; TEST_ASSERT ( STATUS_SUCCESS == malloc_allocator_create ( & alloc ) ) ; TEST_ASSERT ( STATUS_SUCCESS == rbtree_create ( & tree , alloc , & dummy_compare , & dummy_key , nullptr ) ) ; rbtree_node z ; tree -> root = & z ; z . parent = tree -> nil ; z . left = tree -> nil ; z . right = tree -> nil ; z . color = RBTREE_RED ; rbtree_insert_fixup ( tree , & z ) ; TEST_EXPECT ( & z == tree -> root ) ; TEST_EXPECT ( tree -> nil == z . left ) ; TEST_EXPECT ( tree -> nil == z . right ) ; TEST_EXPECT ( RBTREE_BLACK == z . color ) ; tree -> root = tree -> nil ; TEST_ASSERT ( STATUS_SUCCESS == resource_release ( rbtree_resource_handle ( tree ) ) ) ; TEST_ASSERT ( STATUS_SUCCESS == resource_release ( allocator_resource_handle ( alloc ) ) ) ; }
Create an rbtree, sub in a single node, and verify that rbtree_insert_fixup
 works correctly.
Create an rbtree sub in a single node and verify that rbtree_insert_fixup works correctly .
/* we should be able to create a malloc allocator. */ /* we should be able to create an rbtree instance. */ /* PRECONDITIONS. */ /* perform the insert fixup. */ /* POSTCONDITIONS. */ /* before cleaning up rbtree, set the root to nil. */ /* clean up. */