#include #include #include "testmeaputils.h" #include "../meaputils.h" void test_Parent_index_of_children_of_node_zero_is_zero() { assert( MeapUtils::get_parent_index( 1 ) == 0 ); assert( MeapUtils::get_parent_index( 2 ) == 0 ); } void test_Parent_index_of_children_of_node_one_is_one() { assert( MeapUtils::get_parent_index( 3 ) == 1 ); assert( MeapUtils::get_parent_index( 4 ) == 1 ); } void test_Parent_index_of_children_of_node_two_is_two() { assert( MeapUtils::get_parent_index( 5 ) == 2 ); assert( MeapUtils::get_parent_index( 6 ) == 2 ); } void test_Parent_index_of_children_of_node_three_is_three() { assert( MeapUtils::get_parent_index( 7 ) == 3 ); assert( MeapUtils::get_parent_index( 8 ) == 3 ); } void test_Left_child_index_of_node_zero_is_one() { assert( MeapUtils::get_left_child_index( 0 ) == 1 ); } void test_Left_child_index_of_node_one_is_three() { assert( MeapUtils::get_left_child_index( 1 ) == 3 ); } void test_Left_child_index_of_node_two_is_five() { assert( MeapUtils::get_left_child_index( 2 ) == 5 ); } void test_Left_child_index_of_node_three_is_seven() { assert( MeapUtils::get_left_child_index( 3 ) == 7 ); } void test_Right_sibling_index_of_node_one_is_two() { assert( MeapUtils::get_right_sibling_index( 1 ) == 2 ); } void test_Right_sibling_index_of_node_three_is_four() { assert( MeapUtils::get_right_sibling_index( 3 ) == 4 ); } void test_Can_swap_adjacent_nodes() { IntMeap::container_type c; c.push_back( 0 ); c.push_back( 1 ); c.push_back( 2 ); c.push_back( 3 ); c.push_back( 4 ); MeapUtils::swap_values( c, 1, 2 ); assert( c[0] == 0 ); assert( c[1] == 2 ); assert( c[2] == 1 ); assert( c[3] == 3 ); assert( c[4] == 4 ); } void test_Can_swap_nonadjacent_nodes() { IntMeap::container_type c; c.push_back( 0 ); c.push_back( 1 ); c.push_back( 2 ); c.push_back( 3 ); c.push_back( 4 ); MeapUtils::swap_values( c, 2, 4 ); assert( c[0] == 0 ); assert( c[1] == 1 ); assert( c[2] == 4 ); assert( c[3] == 3 ); assert( c[4] == 2 ); } void test_Index_of_begin_is_zero() { IntMeap meap; meap.insert( 0 ); meap.insert( 1 ); assert( MeapUtils::it_to_index( meap, meap.begin() ) == 0 ); } void test_Index_of_begin_plus_3_is_3() { IntMeap meap; meap.insert( 0 ); meap.insert( 1 ); meap.insert( 2 ); meap.insert( 3 ); meap.insert( 4 ); IntMeap::const_iterator it = meap.begin(); std::advance( it, 3 ); assert( MeapUtils::it_to_index( meap, it ) == 3 ); } void test_Index_of_end_minus_one_is_size_minus_one() { IntMeap meap; meap.insert( 0 ); meap.insert( 1 ); meap.insert( 2 ); meap.insert( 3 ); meap.insert( 4 ); IntMeap::const_iterator it = meap.end(); std::advance( it, -1 ); assert( MeapUtils::it_to_index( meap, it ) == ( meap.size() - 1 ) ); } void test_Iterator_from_zero_index_is_begin() { IntMeap meap; meap.insert( 0 ); meap.insert( 1 ); assert( MeapUtils::index_to_it( meap, 0 ) == meap.begin() ); } void test_Iterator_from_index_3_is_begin_plus_three() { IntMeap meap; meap.insert( 0 ); meap.insert( 1 ); meap.insert( 2 ); meap.insert( 3 ); meap.insert( 4 ); IntMeap::const_iterator it = meap.begin(); std::advance( it, 3 ); assert( MeapUtils::index_to_it( meap, 3 ) == it ); } void test_Iterator_from_last_index_is_end_minus_one() { IntMeap meap; meap.insert( 0 ); meap.insert( 1 ); meap.insert( 2 ); meap.insert( 3 ); meap.insert( 4 ); IntMeap::const_iterator it = meap.end(); std::advance( it, -1 ); assert( MeapUtils::index_to_it( meap, 4 ) == it ); } void test_Iterator_from_too_large_index_is_end() { IntMeap meap; meap.insert( 0 ); meap.insert( 1 ); meap.insert( 2 ); meap.insert( 3 ); meap.insert( 4 ); assert( MeapUtils::index_to_it( meap, 5 ) == meap.end() ); assert( MeapUtils::index_to_it( meap, 6 ) == meap.end() ); assert( MeapUtils::index_to_it( meap, 20 ) == meap.end() ); } void test_meap_utils() { test_Parent_index_of_children_of_node_zero_is_zero(); test_Parent_index_of_children_of_node_one_is_one(); test_Parent_index_of_children_of_node_two_is_two(); test_Left_child_index_of_node_zero_is_one(); test_Left_child_index_of_node_one_is_three(); test_Left_child_index_of_node_two_is_five(); test_Left_child_index_of_node_three_is_seven(); test_Right_sibling_index_of_node_one_is_two(); test_Right_sibling_index_of_node_three_is_four(); test_Can_swap_nonadjacent_nodes(); test_Can_swap_nonadjacent_nodes(); test_Index_of_begin_is_zero(); test_Index_of_begin_plus_3_is_3(); test_Index_of_end_minus_one_is_size_minus_one(); test_Iterator_from_zero_index_is_begin(); test_Iterator_from_index_3_is_begin_plus_three(); test_Iterator_from_last_index_is_end_minus_one(); test_Iterator_from_too_large_index_is_end(); }