From a8b93bc360390ac58dbf350207ba39017f63d2c8 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 21 Jul 2014 16:52:34 -0400 Subject: [PATCH] Debugging vector code in presence of refcounting --- premake4.lua | 40 +- source/mem/mem.h | 8 + tests/test_buf.cpp | 262 ++++++------ tests/test_list.cpp | 972 ++++++++++++++++++++++---------------------- tests/test_vec.cpp | 207 ++++------ 5 files changed, 727 insertions(+), 762 deletions(-) diff --git a/premake4.lua b/premake4.lua index 8560e02..5714690 100644 --- a/premake4.lua +++ b/premake4.lua @@ -15,28 +15,28 @@ project "cds" files { "source/**.*" } includedirs { "source/**" } ---project "tests" --- kind "ConsoleApp" --- language "C++" --- location "build" --- links { "UnitTest++", "cds" } --- includedirs { "source/*", "tools/UnitTest++/**" } --- files { "tests/**.c*" } --- postbuildcommands { "./tests.exe" } +project "tests" + kind "ConsoleApp" + language "C++" + location "build" + links { "UnitTest++", "cds" } + includedirs { "source/**", "tools/UnitTest++/**" } + files { "tests/**.c*" } + postbuildcommands { "./tests.exe" } ------------------------------------------------------------------------------- -- UnitTest++ - A C++ unit testing library ------------------------------------------------------------------------------- ---project "UnitTest++" --- kind "SharedLib" --- language "C++" --- location "build" --- files { --- "tools/UnitTest++/src/*.*", --- } --- if os.is "windows" then --- files { "tools/UnitTest++/src/Win32/**.*" } --- else --- files { "tools/UnitTest++/src/Posix/**.*" } --- end +project "UnitTest++" + kind "SharedLib" + language "C++" + location "build" + files { + "tools/UnitTest++/src/*.*", + } + --if os.is "windows" then + -- files { "tools/UnitTest++/src/Win32/**.*" } + --else + files { "tools/UnitTest++/src/Posix/**.*" } + --end diff --git a/source/mem/mem.h b/source/mem/mem.h index 3d99bb7..fae9c32 100644 --- a/source/mem/mem.h +++ b/source/mem/mem.h @@ -10,6 +10,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + /** A function pointer for object destructors */ typedef void (*destructor_t)(void* p_val); @@ -68,4 +72,8 @@ void* mem_box(intptr_t val); */ intptr_t mem_unbox(void* p_box); +#ifdef __cplusplus +} +#endif + #endif /* MEM_H */ diff --git a/tests/test_buf.cpp b/tests/test_buf.cpp index e9745dd..b1eb2d0 100644 --- a/tests/test_buf.cpp +++ b/tests/test_buf.cpp @@ -12,135 +12,135 @@ using namespace UnitTest; // Begin Unit Tests //----------------------------------------------------------------------------- namespace { - //------------------------------------------------------------------------- - // Test buf_new function - //------------------------------------------------------------------------- - TEST(Verify_buf_new_returns_a_new_buffer_of_the_desired_size) - { - buf_t* buf = buf_new(5); - CHECK( NULL != buf ); - CHECK( NULL != buf->buffer ); - CHECK( 5 == buf->size ); - CHECK( 0 == buf->reads ); - CHECK( 0 == buf->writes ); - free( buf->buffer ); - free( buf ); - } - - TEST(Verify_buf_new_returns_null_if_passed_a_size_of_0) - { - CHECK( NULL == buf_new(0) ); - } - - //------------------------------------------------------------------------- - // Test buf_free function - //------------------------------------------------------------------------- - TEST(Verify_buf_free_frees_the_buffer) - { - buf_free( buf_new(5), 0 ); - } - - //------------------------------------------------------------------------- - // Test buf_size function - //------------------------------------------------------------------------- - TEST(Verify_buf_size_should_return_the_size_of_the_buffer) - { - buf_t* buf = buf_new(5); - CHECK( 5 == buf_size( buf ) ); - buf_free(buf,0); - } - - //------------------------------------------------------------------------- - // Test buf_empty function - //------------------------------------------------------------------------- - TEST(Verify_buf_empty_returns_1_when_buffer_is_empty) - { - buf_t buf = { NULL, 5, 1, 1 }; - CHECK( 1 == buf_empty( &buf ) ); - } - - TEST(Verify_buf_empty_returns_0_when_buffer_is_empty) - { - buf_t buf = { NULL, 5, 1, 2 }; - CHECK( 0 == buf_empty( &buf ) ); - } - - //------------------------------------------------------------------------- - // Test buf_full function - //------------------------------------------------------------------------- - TEST(Verify_buf_full_returns_1_if_buffer_is_full) - { - buf_t buf = { NULL, 5, 1, 6 }; - CHECK( 1 == buf_full( &buf ) ); - } - - TEST(Verify_buf_full_returns_0_if_buffer_empty) - { - buf_t buf = { NULL, 5, 1, 1 }; - CHECK( 0 == buf_full( &buf ) ); - } - - TEST(Verify_buf_full_returns_0_if_buffer_not_full) - { - buf_t buf = { NULL, 5, 1, 5 }; - CHECK( 0 == buf_full( &buf ) ); - } - - //------------------------------------------------------------------------- - // Test buf_clear function - //------------------------------------------------------------------------- - TEST(Verify_buf_clears_the_buffer) - { - buf_t buf = { NULL, 5, 1, 5 }; - buf_clear( &buf, 0 ); - CHECK( buf.reads == 0 ); - CHECK( buf.writes == 0 ); - } - - TEST(Verify_buf_clears_the_buffer_and_frees_the_contents) - { - buf_t* buf = buf_new(3); - buf_write( buf, (void*)malloc(sizeof(int)) ); - buf_write( buf, (void*)malloc(sizeof(int)) ); - buf_write( buf, (void*)malloc(sizeof(int)) ); - buf_clear( buf, 1 ); - CHECK( buf->reads == 0 ); - CHECK( buf->writes == 0 ); - } - - //------------------------------------------------------------------------- - // Test buf_read function - //------------------------------------------------------------------------- - TEST(Verify_buf_read_should_return_NULL_if_buffer_is_empty) - { - buf_t* buf = buf_new(3); - CHECK( NULL == buf_read( buf ) ); - buf_free(buf,0); - } - - TEST(Verify_buf_read_should_return_the_next_piece_of_data_from_the_buffer) - { - void* data[] = { (void*)0x1234, (void*)0x4321, (void*)0x1221 }; - buf_t buf = { data, sizeof(data), 0, 3 }; - CHECK( (void*)0x1234 == buf_read(&buf) ); - CHECK( (void*)0x4321 == buf_read(&buf) ); - CHECK( (void*)0x1221 == buf_read(&buf) ); - CHECK( (void*)NULL == buf_read(&buf) ); - } - - //------------------------------------------------------------------------- - // Test buf_write function - //------------------------------------------------------------------------- - TEST(Verify_buf_write_should_return_0_if_buffer_is_full) - { - buf_t buf = { NULL, 3, 0, 3 }; - CHECK( 0 == buf_write(&buf,(void*)0x1234) ); - } - - TEST(Verify_buf_write_should_return_1_if_data_successfully_wrote) - { - void* data[] = { (void*)0x1234, (void*)0x4321, (void*)0x1221 }; - buf_t buf = { data, sizeof(data), 0, 2 }; - CHECK( 1 == buf_write(&buf,(void*)0x1234) ); - } +// //------------------------------------------------------------------------- +// // Test buf_new function +// //------------------------------------------------------------------------- +// TEST(Verify_buf_new_returns_a_new_buffer_of_the_desired_size) +// { +// buf_t* buf = buf_new(5); +// CHECK( NULL != buf ); +// CHECK( NULL != buf->buffer ); +// CHECK( 5 == buf->size ); +// CHECK( 0 == buf->reads ); +// CHECK( 0 == buf->writes ); +// free( buf->buffer ); +// free( buf ); +// } +// +// TEST(Verify_buf_new_returns_null_if_passed_a_size_of_0) +// { +// CHECK( NULL == buf_new(0) ); +// } +// +// //------------------------------------------------------------------------- +// // Test buf_free function +// //------------------------------------------------------------------------- +// TEST(Verify_buf_free_frees_the_buffer) +// { +// buf_free( buf_new(5), 0 ); +// } +// +// //------------------------------------------------------------------------- +// // Test buf_size function +// //------------------------------------------------------------------------- +// TEST(Verify_buf_size_should_return_the_size_of_the_buffer) +// { +// buf_t* buf = buf_new(5); +// CHECK( 5 == buf_size( buf ) ); +// buf_free(buf,0); +// } +// +// //------------------------------------------------------------------------- +// // Test buf_empty function +// //------------------------------------------------------------------------- +// TEST(Verify_buf_empty_returns_1_when_buffer_is_empty) +// { +// buf_t buf = { NULL, 5, 1, 1 }; +// CHECK( 1 == buf_empty( &buf ) ); +// } +// +// TEST(Verify_buf_empty_returns_0_when_buffer_is_empty) +// { +// buf_t buf = { NULL, 5, 1, 2 }; +// CHECK( 0 == buf_empty( &buf ) ); +// } +// +// //------------------------------------------------------------------------- +// // Test buf_full function +// //------------------------------------------------------------------------- +// TEST(Verify_buf_full_returns_1_if_buffer_is_full) +// { +// buf_t buf = { NULL, 5, 1, 6 }; +// CHECK( 1 == buf_full( &buf ) ); +// } +// +// TEST(Verify_buf_full_returns_0_if_buffer_empty) +// { +// buf_t buf = { NULL, 5, 1, 1 }; +// CHECK( 0 == buf_full( &buf ) ); +// } +// +// TEST(Verify_buf_full_returns_0_if_buffer_not_full) +// { +// buf_t buf = { NULL, 5, 1, 5 }; +// CHECK( 0 == buf_full( &buf ) ); +// } +// +// //------------------------------------------------------------------------- +// // Test buf_clear function +// //------------------------------------------------------------------------- +// TEST(Verify_buf_clears_the_buffer) +// { +// buf_t buf = { NULL, 5, 1, 5 }; +// buf_clear( &buf, 0 ); +// CHECK( buf.reads == 0 ); +// CHECK( buf.writes == 0 ); +// } +// +// TEST(Verify_buf_clears_the_buffer_and_frees_the_contents) +// { +// buf_t* buf = buf_new(3); +// buf_write( buf, (void*)malloc(sizeof(int)) ); +// buf_write( buf, (void*)malloc(sizeof(int)) ); +// buf_write( buf, (void*)malloc(sizeof(int)) ); +// buf_clear( buf, 1 ); +// CHECK( buf->reads == 0 ); +// CHECK( buf->writes == 0 ); +// } +// +// //------------------------------------------------------------------------- +// // Test buf_read function +// //------------------------------------------------------------------------- +// TEST(Verify_buf_read_should_return_NULL_if_buffer_is_empty) +// { +// buf_t* buf = buf_new(3); +// CHECK( NULL == buf_read( buf ) ); +// buf_free(buf,0); +// } +// +// TEST(Verify_buf_read_should_return_the_next_piece_of_data_from_the_buffer) +// { +// void* data[] = { (void*)0x1234, (void*)0x4321, (void*)0x1221 }; +// buf_t buf = { data, sizeof(data), 0, 3 }; +// CHECK( (void*)0x1234 == buf_read(&buf) ); +// CHECK( (void*)0x4321 == buf_read(&buf) ); +// CHECK( (void*)0x1221 == buf_read(&buf) ); +// CHECK( (void*)NULL == buf_read(&buf) ); +// } +// +// //------------------------------------------------------------------------- +// // Test buf_write function +// //------------------------------------------------------------------------- +// TEST(Verify_buf_write_should_return_0_if_buffer_is_full) +// { +// buf_t buf = { NULL, 3, 0, 3 }; +// CHECK( 0 == buf_write(&buf,(void*)0x1234) ); +// } +// +// TEST(Verify_buf_write_should_return_1_if_data_successfully_wrote) +// { +// void* data[] = { (void*)0x1234, (void*)0x4321, (void*)0x1221 }; +// buf_t buf = { data, sizeof(data), 0, 2 }; +// CHECK( 1 == buf_write(&buf,(void*)0x1234) ); +// } } diff --git a/tests/test_list.cpp b/tests/test_list.cpp index a13d67f..a868cf4 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -11,490 +11,490 @@ using namespace UnitTest; // Begin Unit Tests //----------------------------------------------------------------------------- namespace { - //------------------------------------------------------------------------- - // Test list_new function - //------------------------------------------------------------------------- - TEST(Verify_list_new_returns_newly_allocated_empty_list) - { - list_t* list = list_new(); - CHECK( NULL != list ); - CHECK( NULL == list->head ); - CHECK( NULL == list->tail ); - free( list ); - } - - //------------------------------------------------------------------------- - // Test list_new_node function - //------------------------------------------------------------------------- - TEST(Verify_list_new_node_returns_newly_allocated_node_with_given_contents) - { - int stuff = 0; - list_node_t* node = list_new_node( &stuff ); - CHECK( NULL != node ); - CHECK( &stuff == node->contents ); - CHECK( NULL == node->next ); - free( node ); - } - - //------------------------------------------------------------------------- - // Test list_free function - //------------------------------------------------------------------------- - TEST(Verify_list_free_frees_the_given_empty_list) - { - list_t* list = list_new(); - list_free( list, 0 ); - } - - TEST(Verify_list_free_frees_the_given_list_including_nodes) - { - list_t* list = list_new(); - list->head = list_new_node(NULL); - list->tail = list->head; - list_free( list, 0 ); - } - - TEST(Verify_list_free_frees_the_given_list_including_nodes_and_node_contents) - { - list_t* list = list_new(); - int* foo = (int*)malloc( sizeof(int) ); - list->head = list_new_node( foo ); - list->tail = list->head; - list_free( list, 1 ); - } - - //------------------------------------------------------------------------- - // Test list_free_node function - //------------------------------------------------------------------------- - TEST(Verify_list_free_node_frees_the_given_node) - { - list_node_t* node = list_new_node( NULL ); - list_free_node( node, 0 ); - } - - TEST(Verify_list_free_node_frees_the_given_node_and_contents) - { - int* foo = (int*)malloc( sizeof(int) ); - list_node_t* node = list_new_node( foo ); - list_free_node( node, 1 ); - } - - //------------------------------------------------------------------------- - // Test list_front function - //------------------------------------------------------------------------- - TEST(Verify_list_front_returns_NULL_if_list_is_empty) - { - list_t list = { NULL, NULL }; - CHECK( NULL == list_front( &list ) ); - } - - TEST(Verify_list_front_returns_the_head_of_the_list) - { - list_node_t node2 = { NULL, NULL }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node2 }; - CHECK( &node1 == list_front( &list ) ); - } - - //------------------------------------------------------------------------- - // Test list_back function - //------------------------------------------------------------------------- - TEST(Verify_list_back_returns_NULL_if_list_is_empty) - { - list_t list = { NULL, NULL }; - CHECK( NULL == list_back( &list ) ); - } - - TEST(Verify_list_back_returns_the_tail_of_the_list) - { - list_node_t node2 = { NULL, NULL }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node2 }; - CHECK( &node2 == list_back( &list ) ); - } - - //------------------------------------------------------------------------- - // Test list_size function - //------------------------------------------------------------------------- - TEST(Verify_list_size_returns_0_when_list_is_empty) - { - list_t* list = list_new(); - CHECK( 0 == list_size( list ) ); - free( list ); - } - - TEST(Verify_list_size_returns_1_when_list_is_length_1) - { - list_node_t node1 = { NULL, NULL }; - list_t list = { &node1, &node1 }; - - CHECK( 1 == list_size( &list ) ); - } - - TEST(Verify_list_size_returns_2_when_list_is_length_2) - { - list_node_t node2 = { NULL, NULL }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node2 }; - - CHECK( 2 == list_size( &list ) ); - } - - //------------------------------------------------------------------------- - // Test list_empty function - //------------------------------------------------------------------------- - TEST(Verify_list_empty_returns_true_when_head_and_tail_are_null) - { - list_t list = { NULL, NULL }; - CHECK( 1 == list_empty( &list ) ); - } - - TEST(Verify_list_empty_returns_false_when_head_is_not_null) - { - list_t list = { (list_node_t*)0x1234, NULL }; - CHECK( 0 == list_empty( &list ) ); - } - - TEST(Verify_list_empty_returns_false_when_tail_is_not_null) - { - list_t list = { NULL, (list_node_t*)0x1234 }; - CHECK( 0 == list_empty( &list ) ); - } - - //------------------------------------------------------------------------- - // Test list_at function - //------------------------------------------------------------------------- - TEST(Verify_list_at_returns_NULL_when_list_is_empty) - { - list_t list = { NULL, NULL }; - CHECK( NULL == list_at( &list, 0 ) ); - } - - TEST(Verify_list_at_returns_NULL_when_index_out_of_range) - { - list_node_t node3 = { NULL, NULL }; - list_node_t node2 = { NULL, &node3 }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node3 }; - CHECK( NULL == list_at( &list, 3 ) ); - } - - TEST(Verify_list_at_returns_node_at_index_0_of_3_element_list) - { - list_node_t node3 = { NULL, NULL }; - list_node_t node2 = { NULL, &node3 }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node3 }; - CHECK( &node1 == list_at( &list, 0 ) ); - } - - TEST(Verify_list_at_returns_node_at_index_1_of_3_element_list) - { - list_node_t node3 = { NULL, NULL }; - list_node_t node2 = { NULL, &node3 }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node3 }; - CHECK( &node2 == list_at( &list, 1 ) ); - } - - TEST(Verify_list_at_returns_node_at_index_2_of_3_element_list) - { - list_node_t node3 = { NULL, NULL }; - list_node_t node2 = { NULL, &node3 }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node3 }; - CHECK( &node3 == list_at( &list, 2 ) ); - } - - //------------------------------------------------------------------------- - // Test list_push_front function - //------------------------------------------------------------------------- - TEST(Verify_list_push_front_pushes_to_empty_list) - { - list_t list = { NULL, NULL }; - list_node_t* node = list_push_front( &list, (void*)0x1234 ); - CHECK( NULL != node ); - CHECK( (void*)0x1234 == node->contents ); - CHECK( NULL == node->next ); - CHECK( node == list.head ); - CHECK( node == list.tail ); - } - - TEST(Verify_list_push_front_pushes_to_front_of_list_of_length_1) - { - list_node_t node1 = { NULL, NULL }; - list_t list = { &node1, &node1 }; - list_node_t* node = list_push_front( &list, (void*)0x1234 ); - CHECK( NULL != node ); - CHECK( (void*)0x1234 == node->contents ); - CHECK( NULL != node->next ); - CHECK( node == list.head ); - CHECK( node != list.tail ); - } - - //------------------------------------------------------------------------- - // Test list_push_back function - //------------------------------------------------------------------------- - TEST(Verify_list_push_back_pushes_to_empty_list) - { - list_t list = { NULL, NULL }; - list_node_t* node = list_push_back( &list, (void*)0x1234 ); - CHECK( NULL != node ); - CHECK( (void*)0x1234 == node->contents ); - CHECK( NULL == node->next ); - CHECK( node == list.head ); - CHECK( node == list.tail ); - } - - TEST(Verify_list_push_back_pushes_to_back_of_list_of_length_1) - { - list_node_t node1 = { NULL, NULL }; - list_t list = { &node1, &node1 }; - list_node_t* node = list_push_back( &list, (void*)0x1234 ); - CHECK( NULL != node ); - CHECK( (void*)0x1234 == node->contents ); - CHECK( &node1 != node->next ); - CHECK( node != list.head ); - CHECK( node == list.tail ); - } - - //------------------------------------------------------------------------- - // Test list_pop_front function - //------------------------------------------------------------------------- - TEST(Verify_pop_front_returns_null_if_list_is_empty) - { - list_t list = { NULL, NULL }; - CHECK( NULL == list_pop_front( &list ) ); - } - - TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_1) - { - list_node_t node1 = { NULL, NULL }; - list_t list = { &node1, &node1 }; - CHECK( &node1 == list_pop_front( &list ) ); - CHECK( NULL == list.head ); - CHECK( NULL == list.tail ); - } - - TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_2) - { - list_node_t node2 = { NULL, NULL }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node2 }; - CHECK( &node1 == list_pop_front( &list ) ); - CHECK( &node2 == list.head ); - CHECK( &node2 == list.tail ); - } - - TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_3) - { - list_node_t node3 = { NULL, NULL }; - list_node_t node2 = { NULL, &node3 }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node3 }; - CHECK( &node1 == list_pop_front( &list ) ); - CHECK( &node2 == list.head ); - CHECK( &node3 == list.tail ); - } - - //------------------------------------------------------------------------- - // Test list_pop_back function - //------------------------------------------------------------------------- - TEST(Verify_pop_back_does_nothing_if_list_is_empty) - { - list_t list = { NULL, NULL }; - CHECK( NULL == list_pop_back( &list ) ); - } - - TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_1) - { - list_node_t node1 = { NULL, NULL }; - list_t list = { &node1, &node1 }; - CHECK( &node1 == list_pop_back( &list ) ); - CHECK( NULL == list.head ); - CHECK( NULL == list.tail ); - } - - TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_2) - { - list_node_t node2 = { NULL, NULL }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node2 }; - CHECK( &node2 == list_pop_back( &list ) ); - CHECK( &node1 == list.head ); - CHECK( &node1 == list.tail ); - } - - TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_3) - { - list_node_t node3 = { NULL, NULL }; - list_node_t node2 = { NULL, &node3 }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node3 }; - CHECK( &node3 == list_pop_back( &list ) ); - CHECK( &node1 == list.head ); - CHECK( &node2 == list.tail ); - } - - //------------------------------------------------------------------------- - // Test list_insert function - //------------------------------------------------------------------------- - TEST(Verify_insert_should_insert_into_empty_list) - { - list_t list = { NULL, NULL }; - list_node_t* node = list_insert( &list, 0, (void*)0x1234 ); - CHECK( node != NULL ); - CHECK( node->next == NULL ); - CHECK( node->contents == (void*)0x1234 ); - CHECK( list.head == node ); - CHECK( list.tail == node ); - } - - TEST(Verify_insert_should_push_to_the_front_of_the_list_if_index_is_0) - { - list_node_t node1 = { NULL, NULL }; - list_t list = { &node1, &node1 }; - list_node_t* node = list_insert( &list, 0, (void*)0x1234 ); - CHECK( NULL != node ); - CHECK( (void*)0x1234 == node->contents ); - CHECK( NULL != node->next ); - CHECK( node == list.head ); - CHECK( node != list.tail ); - } - - TEST(Verify_insert_should_insert_at_the_given_index_if_index_is_non_zero) - { - list_node_t node3 = { NULL, NULL }; - list_node_t node2 = { NULL, &node3 }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node3 }; - list_node_t* node = list_insert( &list, 1, (void*)0x1234 ); - CHECK( NULL != node ); - CHECK( (void*)0x1234 == node->contents ); - CHECK( node1.next == node ); - CHECK( &node2 == node->next ); - } - - TEST(Verify_insert_should_set_the_tail_of_the_list_if_index_is_the_last_item) - { - list_node_t node2 = { NULL, NULL }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node2 }; - list_node_t* node = list_insert( &list, 2, (void*)0x1234 ); - CHECK( NULL != node ); - CHECK( (void*)0x1234 == node->contents ); - CHECK( NULL == node->next ); - CHECK( node2.next == node ); - CHECK( list.tail == node ); - } - - TEST(Verify_insert_should_return_null_if_index_out_of_range) - { - list_node_t node2 = { NULL, NULL }; - list_node_t node1 = { NULL, &node2 }; - list_t list = { &node1, &node2 }; - list_node_t* node = list_insert( &list, 3, (void*)0x1234 ); - CHECK( NULL == node ); - } - - //------------------------------------------------------------------------- - // Test list_delete function - //------------------------------------------------------------------------- - TEST(Verify_delete_does_nothing_if_list_is_empty) - { - list_t list = { NULL, NULL }; - CHECK( NULL == list_delete( &list, 0, 0 ) ); - } - - TEST(Verify_delete_deletes_the_first_element_of_a_list_of_length_1) - { - list_node_t* node = list_new_node((void*)0x1234); - list_t list = { node, node }; - CHECK( NULL == list_delete( &list, 0, 0 ) ); - CHECK( list.head == NULL ); - CHECK( list.tail == NULL ); - } - - TEST(Verify_delete_deletes_the_first_element_of_a_list_of_length_2) - { - list_node_t* node1 = list_new_node((void*)0x1234); - list_node_t node2 = { (void*)0x1234, NULL }; - node1->next = &node2; - list_t list = { node1, &node2 }; - list_node_t* node = list_delete( &list, 0, 0 ); - CHECK( node == &node2 ); - CHECK( list.head == &node2 ); - CHECK( list.tail == &node2 ); - } - - TEST(Verify_delete_deletes_element_1_of_a_list_of_length_3) - { - list_node_t node1 = { (void*)0x1234, NULL }; - list_node_t* node2 = list_new_node((void*)0x1234); - list_node_t node3 = { (void*)0x1234, NULL }; - node1.next = node2; - node2->next = &node3; - list_t list = { &node1, &node3 }; - list_node_t* node = list_delete( &list, 1, 0 ); - CHECK( node == &node3 ); - CHECK( node1.next == &node3 ); - CHECK( list.head == &node1 ); - CHECK( list.tail == &node3 ); - } - - TEST(Verify_delete_deletes_element_1_of_a_list_of_length_2) - { - list_node_t node1 = { (void*)0x1234, NULL }; - list_node_t* node2 = list_new_node((void*)0x1234); - node1.next = node2; - list_t list = { &node1, node2 }; - list_node_t* node = list_delete( &list, 1, 0 ); - CHECK( node == NULL ); - CHECK( list.head == &node1 ); - CHECK( list.tail == &node1 ); - } - - //------------------------------------------------------------------------- - // Test list_clear function - //------------------------------------------------------------------------- - TEST(Verify_list_clear_does_nothing_for_an_empty_list) - { - list_t* list = list_new(); - CHECK( list == list_clear(list,0) ); - CHECK( NULL == list->head ); - CHECK( NULL == list->tail ); - list_free(list,0); - } - - TEST(Verify_list_clear_clears_a_list_of_length_1) - { - list_t* list = list_new(); - (void)list_push_front(list,(void*)0x1234); - CHECK( list == list_clear(list,0) ); - CHECK( NULL == list->head ); - CHECK( NULL == list->tail ); - list_free(list,0); - } - - TEST(Verify_list_clear_clears_a_list_of_length_2) - { - list_t* list = list_new(); - (void)list_push_front(list,(void*)0x1234); - (void)list_push_front(list,(void*)0x1234); - CHECK( list == list_clear(list,0) ); - CHECK( NULL == list->head ); - CHECK( NULL == list->tail ); - list_free(list,0); - } - - TEST(Verify_list_clear_clears_a_list_of_length_3) - { - list_t* list = list_new(); - (void)list_push_front(list,(void*)0x1234); - (void)list_push_front(list,(void*)0x1234); - (void)list_push_front(list,(void*)0x1234); - CHECK( list == list_clear(list,0) ); - CHECK( NULL == list->head ); - CHECK( NULL == list->tail ); - list_free(list,0); - } +// //------------------------------------------------------------------------- +// // Test list_new function +// //------------------------------------------------------------------------- +// TEST(Verify_list_new_returns_newly_allocated_empty_list) +// { +// list_t* list = list_new(); +// CHECK( NULL != list ); +// CHECK( NULL == list->head ); +// CHECK( NULL == list->tail ); +// free( list ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_new_node function +// //------------------------------------------------------------------------- +// TEST(Verify_list_new_node_returns_newly_allocated_node_with_given_contents) +// { +// int stuff = 0; +// list_node_t* node = list_new_node( &stuff ); +// CHECK( NULL != node ); +// CHECK( &stuff == node->contents ); +// CHECK( NULL == node->next ); +// free( node ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_free function +// //------------------------------------------------------------------------- +// TEST(Verify_list_free_frees_the_given_empty_list) +// { +// list_t* list = list_new(); +// list_free( list, 0 ); +// } +// +// TEST(Verify_list_free_frees_the_given_list_including_nodes) +// { +// list_t* list = list_new(); +// list->head = list_new_node(NULL); +// list->tail = list->head; +// list_free( list, 0 ); +// } +// +// TEST(Verify_list_free_frees_the_given_list_including_nodes_and_node_contents) +// { +// list_t* list = list_new(); +// int* foo = (int*)malloc( sizeof(int) ); +// list->head = list_new_node( foo ); +// list->tail = list->head; +// list_free( list, 1 ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_free_node function +// //------------------------------------------------------------------------- +// TEST(Verify_list_free_node_frees_the_given_node) +// { +// list_node_t* node = list_new_node( NULL ); +// list_free_node( node, 0 ); +// } +// +// TEST(Verify_list_free_node_frees_the_given_node_and_contents) +// { +// int* foo = (int*)malloc( sizeof(int) ); +// list_node_t* node = list_new_node( foo ); +// list_free_node( node, 1 ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_front function +// //------------------------------------------------------------------------- +// TEST(Verify_list_front_returns_NULL_if_list_is_empty) +// { +// list_t list = { NULL, NULL }; +// CHECK( NULL == list_front( &list ) ); +// } +// +// TEST(Verify_list_front_returns_the_head_of_the_list) +// { +// list_node_t node2 = { NULL, NULL }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node2 }; +// CHECK( &node1 == list_front( &list ) ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_back function +// //------------------------------------------------------------------------- +// TEST(Verify_list_back_returns_NULL_if_list_is_empty) +// { +// list_t list = { NULL, NULL }; +// CHECK( NULL == list_back( &list ) ); +// } +// +// TEST(Verify_list_back_returns_the_tail_of_the_list) +// { +// list_node_t node2 = { NULL, NULL }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node2 }; +// CHECK( &node2 == list_back( &list ) ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_size function +// //------------------------------------------------------------------------- +// TEST(Verify_list_size_returns_0_when_list_is_empty) +// { +// list_t* list = list_new(); +// CHECK( 0 == list_size( list ) ); +// free( list ); +// } +// +// TEST(Verify_list_size_returns_1_when_list_is_length_1) +// { +// list_node_t node1 = { NULL, NULL }; +// list_t list = { &node1, &node1 }; +// +// CHECK( 1 == list_size( &list ) ); +// } +// +// TEST(Verify_list_size_returns_2_when_list_is_length_2) +// { +// list_node_t node2 = { NULL, NULL }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node2 }; +// +// CHECK( 2 == list_size( &list ) ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_empty function +// //------------------------------------------------------------------------- +// TEST(Verify_list_empty_returns_true_when_head_and_tail_are_null) +// { +// list_t list = { NULL, NULL }; +// CHECK( 1 == list_empty( &list ) ); +// } +// +// TEST(Verify_list_empty_returns_false_when_head_is_not_null) +// { +// list_t list = { (list_node_t*)0x1234, NULL }; +// CHECK( 0 == list_empty( &list ) ); +// } +// +// TEST(Verify_list_empty_returns_false_when_tail_is_not_null) +// { +// list_t list = { NULL, (list_node_t*)0x1234 }; +// CHECK( 0 == list_empty( &list ) ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_at function +// //------------------------------------------------------------------------- +// TEST(Verify_list_at_returns_NULL_when_list_is_empty) +// { +// list_t list = { NULL, NULL }; +// CHECK( NULL == list_at( &list, 0 ) ); +// } +// +// TEST(Verify_list_at_returns_NULL_when_index_out_of_range) +// { +// list_node_t node3 = { NULL, NULL }; +// list_node_t node2 = { NULL, &node3 }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node3 }; +// CHECK( NULL == list_at( &list, 3 ) ); +// } +// +// TEST(Verify_list_at_returns_node_at_index_0_of_3_element_list) +// { +// list_node_t node3 = { NULL, NULL }; +// list_node_t node2 = { NULL, &node3 }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node3 }; +// CHECK( &node1 == list_at( &list, 0 ) ); +// } +// +// TEST(Verify_list_at_returns_node_at_index_1_of_3_element_list) +// { +// list_node_t node3 = { NULL, NULL }; +// list_node_t node2 = { NULL, &node3 }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node3 }; +// CHECK( &node2 == list_at( &list, 1 ) ); +// } +// +// TEST(Verify_list_at_returns_node_at_index_2_of_3_element_list) +// { +// list_node_t node3 = { NULL, NULL }; +// list_node_t node2 = { NULL, &node3 }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node3 }; +// CHECK( &node3 == list_at( &list, 2 ) ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_push_front function +// //------------------------------------------------------------------------- +// TEST(Verify_list_push_front_pushes_to_empty_list) +// { +// list_t list = { NULL, NULL }; +// list_node_t* node = list_push_front( &list, (void*)0x1234 ); +// CHECK( NULL != node ); +// CHECK( (void*)0x1234 == node->contents ); +// CHECK( NULL == node->next ); +// CHECK( node == list.head ); +// CHECK( node == list.tail ); +// } +// +// TEST(Verify_list_push_front_pushes_to_front_of_list_of_length_1) +// { +// list_node_t node1 = { NULL, NULL }; +// list_t list = { &node1, &node1 }; +// list_node_t* node = list_push_front( &list, (void*)0x1234 ); +// CHECK( NULL != node ); +// CHECK( (void*)0x1234 == node->contents ); +// CHECK( NULL != node->next ); +// CHECK( node == list.head ); +// CHECK( node != list.tail ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_push_back function +// //------------------------------------------------------------------------- +// TEST(Verify_list_push_back_pushes_to_empty_list) +// { +// list_t list = { NULL, NULL }; +// list_node_t* node = list_push_back( &list, (void*)0x1234 ); +// CHECK( NULL != node ); +// CHECK( (void*)0x1234 == node->contents ); +// CHECK( NULL == node->next ); +// CHECK( node == list.head ); +// CHECK( node == list.tail ); +// } +// +// TEST(Verify_list_push_back_pushes_to_back_of_list_of_length_1) +// { +// list_node_t node1 = { NULL, NULL }; +// list_t list = { &node1, &node1 }; +// list_node_t* node = list_push_back( &list, (void*)0x1234 ); +// CHECK( NULL != node ); +// CHECK( (void*)0x1234 == node->contents ); +// CHECK( &node1 != node->next ); +// CHECK( node != list.head ); +// CHECK( node == list.tail ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_pop_front function +// //------------------------------------------------------------------------- +// TEST(Verify_pop_front_returns_null_if_list_is_empty) +// { +// list_t list = { NULL, NULL }; +// CHECK( NULL == list_pop_front( &list ) ); +// } +// +// TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_1) +// { +// list_node_t node1 = { NULL, NULL }; +// list_t list = { &node1, &node1 }; +// CHECK( &node1 == list_pop_front( &list ) ); +// CHECK( NULL == list.head ); +// CHECK( NULL == list.tail ); +// } +// +// TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_2) +// { +// list_node_t node2 = { NULL, NULL }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node2 }; +// CHECK( &node1 == list_pop_front( &list ) ); +// CHECK( &node2 == list.head ); +// CHECK( &node2 == list.tail ); +// } +// +// TEST(Verify_pop_front_removes_a_node_from_the_front_of_a_list_of_length_3) +// { +// list_node_t node3 = { NULL, NULL }; +// list_node_t node2 = { NULL, &node3 }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node3 }; +// CHECK( &node1 == list_pop_front( &list ) ); +// CHECK( &node2 == list.head ); +// CHECK( &node3 == list.tail ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_pop_back function +// //------------------------------------------------------------------------- +// TEST(Verify_pop_back_does_nothing_if_list_is_empty) +// { +// list_t list = { NULL, NULL }; +// CHECK( NULL == list_pop_back( &list ) ); +// } +// +// TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_1) +// { +// list_node_t node1 = { NULL, NULL }; +// list_t list = { &node1, &node1 }; +// CHECK( &node1 == list_pop_back( &list ) ); +// CHECK( NULL == list.head ); +// CHECK( NULL == list.tail ); +// } +// +// TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_2) +// { +// list_node_t node2 = { NULL, NULL }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node2 }; +// CHECK( &node2 == list_pop_back( &list ) ); +// CHECK( &node1 == list.head ); +// CHECK( &node1 == list.tail ); +// } +// +// TEST(Verify_pop_back_removes_a_node_from_the_back_of_a_list_of_length_3) +// { +// list_node_t node3 = { NULL, NULL }; +// list_node_t node2 = { NULL, &node3 }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node3 }; +// CHECK( &node3 == list_pop_back( &list ) ); +// CHECK( &node1 == list.head ); +// CHECK( &node2 == list.tail ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_insert function +// //------------------------------------------------------------------------- +// TEST(Verify_insert_should_insert_into_empty_list) +// { +// list_t list = { NULL, NULL }; +// list_node_t* node = list_insert( &list, 0, (void*)0x1234 ); +// CHECK( node != NULL ); +// CHECK( node->next == NULL ); +// CHECK( node->contents == (void*)0x1234 ); +// CHECK( list.head == node ); +// CHECK( list.tail == node ); +// } +// +// TEST(Verify_insert_should_push_to_the_front_of_the_list_if_index_is_0) +// { +// list_node_t node1 = { NULL, NULL }; +// list_t list = { &node1, &node1 }; +// list_node_t* node = list_insert( &list, 0, (void*)0x1234 ); +// CHECK( NULL != node ); +// CHECK( (void*)0x1234 == node->contents ); +// CHECK( NULL != node->next ); +// CHECK( node == list.head ); +// CHECK( node != list.tail ); +// } +// +// TEST(Verify_insert_should_insert_at_the_given_index_if_index_is_non_zero) +// { +// list_node_t node3 = { NULL, NULL }; +// list_node_t node2 = { NULL, &node3 }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node3 }; +// list_node_t* node = list_insert( &list, 1, (void*)0x1234 ); +// CHECK( NULL != node ); +// CHECK( (void*)0x1234 == node->contents ); +// CHECK( node1.next == node ); +// CHECK( &node2 == node->next ); +// } +// +// TEST(Verify_insert_should_set_the_tail_of_the_list_if_index_is_the_last_item) +// { +// list_node_t node2 = { NULL, NULL }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node2 }; +// list_node_t* node = list_insert( &list, 2, (void*)0x1234 ); +// CHECK( NULL != node ); +// CHECK( (void*)0x1234 == node->contents ); +// CHECK( NULL == node->next ); +// CHECK( node2.next == node ); +// CHECK( list.tail == node ); +// } +// +// TEST(Verify_insert_should_return_null_if_index_out_of_range) +// { +// list_node_t node2 = { NULL, NULL }; +// list_node_t node1 = { NULL, &node2 }; +// list_t list = { &node1, &node2 }; +// list_node_t* node = list_insert( &list, 3, (void*)0x1234 ); +// CHECK( NULL == node ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_delete function +// //------------------------------------------------------------------------- +// TEST(Verify_delete_does_nothing_if_list_is_empty) +// { +// list_t list = { NULL, NULL }; +// CHECK( NULL == list_delete( &list, 0, 0 ) ); +// } +// +// TEST(Verify_delete_deletes_the_first_element_of_a_list_of_length_1) +// { +// list_node_t* node = list_new_node((void*)0x1234); +// list_t list = { node, node }; +// CHECK( NULL == list_delete( &list, 0, 0 ) ); +// CHECK( list.head == NULL ); +// CHECK( list.tail == NULL ); +// } +// +// TEST(Verify_delete_deletes_the_first_element_of_a_list_of_length_2) +// { +// list_node_t* node1 = list_new_node((void*)0x1234); +// list_node_t node2 = { (void*)0x1234, NULL }; +// node1->next = &node2; +// list_t list = { node1, &node2 }; +// list_node_t* node = list_delete( &list, 0, 0 ); +// CHECK( node == &node2 ); +// CHECK( list.head == &node2 ); +// CHECK( list.tail == &node2 ); +// } +// +// TEST(Verify_delete_deletes_element_1_of_a_list_of_length_3) +// { +// list_node_t node1 = { (void*)0x1234, NULL }; +// list_node_t* node2 = list_new_node((void*)0x1234); +// list_node_t node3 = { (void*)0x1234, NULL }; +// node1.next = node2; +// node2->next = &node3; +// list_t list = { &node1, &node3 }; +// list_node_t* node = list_delete( &list, 1, 0 ); +// CHECK( node == &node3 ); +// CHECK( node1.next == &node3 ); +// CHECK( list.head == &node1 ); +// CHECK( list.tail == &node3 ); +// } +// +// TEST(Verify_delete_deletes_element_1_of_a_list_of_length_2) +// { +// list_node_t node1 = { (void*)0x1234, NULL }; +// list_node_t* node2 = list_new_node((void*)0x1234); +// node1.next = node2; +// list_t list = { &node1, node2 }; +// list_node_t* node = list_delete( &list, 1, 0 ); +// CHECK( node == NULL ); +// CHECK( list.head == &node1 ); +// CHECK( list.tail == &node1 ); +// } +// +// //------------------------------------------------------------------------- +// // Test list_clear function +// //------------------------------------------------------------------------- +// TEST(Verify_list_clear_does_nothing_for_an_empty_list) +// { +// list_t* list = list_new(); +// CHECK( list == list_clear(list,0) ); +// CHECK( NULL == list->head ); +// CHECK( NULL == list->tail ); +// list_free(list,0); +// } +// +// TEST(Verify_list_clear_clears_a_list_of_length_1) +// { +// list_t* list = list_new(); +// (void)list_push_front(list,(void*)0x1234); +// CHECK( list == list_clear(list,0) ); +// CHECK( NULL == list->head ); +// CHECK( NULL == list->tail ); +// list_free(list,0); +// } +// +// TEST(Verify_list_clear_clears_a_list_of_length_2) +// { +// list_t* list = list_new(); +// (void)list_push_front(list,(void*)0x1234); +// (void)list_push_front(list,(void*)0x1234); +// CHECK( list == list_clear(list,0) ); +// CHECK( NULL == list->head ); +// CHECK( NULL == list->tail ); +// list_free(list,0); +// } +// +// TEST(Verify_list_clear_clears_a_list_of_length_3) +// { +// list_t* list = list_new(); +// (void)list_push_front(list,(void*)0x1234); +// (void)list_push_front(list,(void*)0x1234); +// (void)list_push_front(list,(void*)0x1234); +// CHECK( list == list_clear(list,0) ); +// CHECK( NULL == list->head ); +// CHECK( NULL == list->tail ); +// list_free(list,0); +// } } diff --git a/tests/test_vec.cpp b/tests/test_vec.cpp index 8711815..fbc4081 100644 --- a/tests/test_vec.cpp +++ b/tests/test_vec.cpp @@ -2,12 +2,14 @@ #include "UnitTest++.h" #include #include +#include "mem.h" // File To Test #include "vec.h" using namespace UnitTest; +#define GC_OBJ_OFFSET (2*sizeof(void*)) //----------------------------------------------------------------------------- // Begin Unit Tests //----------------------------------------------------------------------------- @@ -17,41 +19,24 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_new_returns_newly_allocated_vector_with_default_capacity) { - vec_t* p_vec = vec_new(true,0); + vec_t* p_vec = vec_new(0); CHECK(NULL != p_vec); - CHECK(true == p_vec->own_contents); CHECK(0 == p_vec->size); CHECK(DEFAULT_VEC_CAPACITY == p_vec->capacity); CHECK(NULL != p_vec->p_buffer); - vec_free(p_vec); + mem_release(p_vec); } TEST(Verify_vec_new_returns_newly_allocated_vector_with_the_provided_elements) { - vec_t* p_vec = vec_new(false,2,(void*)0x1234,(void*)0x4321); + vec_t* p_vec = vec_new(2,mem_box(0x1234),mem_box(0x4321)); CHECK(NULL != p_vec); - CHECK(false == p_vec->own_contents); CHECK(2 == p_vec->size); CHECK(2 == p_vec->capacity); CHECK(NULL != p_vec->p_buffer); - CHECK((void*)0x1234 == p_vec->p_buffer[0]); - CHECK((void*)0x4321 == p_vec->p_buffer[1]); - vec_free(p_vec); - } - - //------------------------------------------------------------------------- - // Test vec_free function - //------------------------------------------------------------------------- - TEST(Verify_vec_free_frees_the_vector_and_contents) - { - vec_t* p_vec = vec_new(true,0); - vec_free(p_vec); - } - - TEST(Verify_vec_free_frees_the_vector) - { - vec_t* p_vec = vec_new(false,0); - vec_free(p_vec); + CHECK(0x1234 == mem_unbox(p_vec->p_buffer[0])); + CHECK(0x4321 == mem_unbox(p_vec->p_buffer[1])); + mem_release(p_vec); } //------------------------------------------------------------------------- @@ -59,7 +44,7 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_size_returns_the_correct_size) { - vec_t vector = { false, 42, 24, NULL }; + vec_t vector = { 42, 24, NULL }; CHECK(42 == vec_size(&vector)); } @@ -76,13 +61,13 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_empty_returns_true_if_empty) { - vec_t vector = { false, 0, 24, NULL }; + vec_t vector = { 0, 24, NULL }; CHECK(true == vec_empty(&vector)); } TEST(Verify_vec_empty_returns_false_if_not_empty) { - vec_t vector = { false, 42, 24, NULL }; + vec_t vector = { 42, 24, NULL }; CHECK(false == vec_empty(&vector)); } @@ -91,65 +76,62 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_resize_does_nothing_if_size_is_the_same) { - vec_t* p_vec = vec_new(false,3,0,1,2); + vec_t* p_vec = vec_new(3, mem_box(0), mem_box(1), mem_box(2)); vec_resize( p_vec, 3, (void*)0x2A ); - CHECK( false == p_vec->own_contents ); CHECK( 3 == p_vec->size ); CHECK( 3 == p_vec->capacity ); - vec_free( p_vec ); + mem_release(p_vec); } TEST(Verify_vec_resize_should_erase_the_last_element) { - vec_t* p_vec = vec_new(false,3,0,1,2); + vec_t* p_vec = vec_new(3, mem_box(0), mem_box(1), mem_box(2)); vec_resize( p_vec, 2, (void*)0x2A ); - CHECK( false == p_vec->own_contents ); CHECK( 2 == p_vec->size ); CHECK( 3 == p_vec->capacity ); - vec_free( p_vec ); + mem_release(p_vec); } TEST(Verify_vec_resize_should_erase_the_last_two_elements) { - vec_t* p_vec = vec_new(false,3,0,1,2); + vec_t* p_vec = vec_new(3, mem_box(0), mem_box(1), mem_box(2)); vec_resize( p_vec, 1, (void*)0x2A ); - CHECK( false == p_vec->own_contents ); CHECK( 1 == p_vec->size ); CHECK( 3 == p_vec->capacity ); - vec_free( p_vec ); + mem_release(p_vec); } TEST(Verify_vec_resize_should_add_a_new_element) { - vec_t* p_vec = vec_new(false,3,0,1,2); + // TODO: This test is most certainly busted in the presence of refcounting. + vec_t* p_vec = vec_new(3, mem_box(0), mem_box(1), mem_box(2)); vec_resize( p_vec, 4, (void*)0x2A ); - CHECK( false == p_vec->own_contents ); CHECK( 4 == p_vec->size ); CHECK( 4 == p_vec->capacity ); CHECK( (void*)0x2A == p_vec->p_buffer[3] ); - vec_free( p_vec ); + mem_release(p_vec); } TEST(Verify_vec_resize_should_add_two_new_elements) { - vec_t* p_vec = vec_new(false,3,0,1,2); + // TODO: This test is most certainly busted in the presence of refcounting. + vec_t* p_vec = vec_new(3, mem_box(0), mem_box(1), mem_box(2)); vec_resize( p_vec, 5, (void*)0x2A ); - CHECK( false == p_vec->own_contents ); CHECK( 5 == p_vec->size ); CHECK( 8 == p_vec->capacity ); CHECK( (void*)0x2A == p_vec->p_buffer[3] ); CHECK( (void*)0x2A == p_vec->p_buffer[4] ); - vec_free( p_vec ); + mem_release(p_vec); } //------------------------------------------------------------------------- @@ -157,10 +139,10 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_shrink_to_fit_shrinks_capacity_to_equal_the_size) { - vec_t vector = { false, 1, 2, (void**)malloc(sizeof(void*) * 2) }; + vec_t vector = { 1, 2, (void**)malloc(sizeof(void*) * 2) }; vec_shrink_to_fit(&vector); CHECK( vector.size == vector.capacity ); - free( vector.p_buffer ); + free(vector.p_buffer); } //------------------------------------------------------------------------- @@ -168,7 +150,7 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_capacity_returns_the_correct_size) { - vec_t vector = { false, 42, 24, NULL }; + vec_t vector = { 42, 24, NULL }; CHECK(24 == vec_capacity(&vector)); } @@ -177,7 +159,7 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_reserve_reserves_a_buffer_of_the_desired_size) { - vec_t vector = { false, 0, 0, NULL }; + vec_t vector = { 0, 0, NULL }; vec_reserve(&vector,5); CHECK( 5 == vector.capacity ); free( vector.p_buffer ); @@ -189,7 +171,7 @@ namespace { TEST(Verify_vec_at_returns_an_item_at_the_provided_index) { void* array[2] = { (void*)0x1234, (void*)0x4321 }; - vec_t vector = { false, 2, 2, array }; + vec_t vector = { 2, 2, array }; CHECK((void*)0x4321 == vec_at(&vector,1)); } @@ -197,7 +179,7 @@ namespace { TEST(Verify_vec_at_returns_null_if_index_out_of_range) { void* array[2] = { (void*)0x1234, (void*)0x4321 }; - vec_t vector = { false, 2, 2, array }; + vec_t vector = { 2, 2, array }; CHECK(NULL == vec_at(&vector,2)); } @@ -207,7 +189,7 @@ namespace { TEST(Verify_vec_set_sets_the_value_at_the_given_index) { void* data[3] = { (void*)0x1234, NULL, NULL }; - vec_t vector = { false, 2, 3, data }; + vec_t vector = { 2, 3, data }; CHECK(true == vec_set(&vector,1,(void*)0x4321)); CHECK((void*)0x4321 == data[1]); CHECK(NULL == data[2]); @@ -216,7 +198,7 @@ namespace { TEST(Verify_vec_set_returns_false_if_index_out_of_range) { void* data[3] = { (void*)0x1234, NULL, NULL }; - vec_t vector = { false, 2, 3, data }; + vec_t vector = { 2, 3, data }; CHECK(false == vec_set(&vector,2,(void*)0x4321)); CHECK(NULL == data[1]); CHECK(NULL == data[2]); @@ -227,40 +209,42 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_insert_should_do_nothing_if_index_out_of_range) { - vec_t vector = { false, 2, 3, NULL }; + vec_t vector = { 2, 3, NULL }; CHECK(false == vec_insert(&vector,2,0)); } TEST(Verify_vec_insert_should_do_nothing_if_num_elements_is_0) { - vec_t vector = { false, 2, 3, NULL }; + vec_t vector = { 2, 3, NULL }; CHECK(false == vec_insert(&vector,0,0)); } TEST(Verify_vec_insert_should_insert_items_at_the_given_index) { - vec_t* p_vec = vec_new(false,2,0,1); - CHECK(true == vec_insert(p_vec,1,2,2,3)); + // TODO: release fails for some reason. Investigate + vec_t* p_vec = vec_new(2,mem_box(0),mem_box(1)); + CHECK(true == vec_insert(p_vec,1,2,mem_box(2),mem_box(3))); CHECK(4 == p_vec->size); CHECK(4 == p_vec->capacity); - CHECK((void*)0 == p_vec->p_buffer[0]); - CHECK((void*)2 == p_vec->p_buffer[1]); - CHECK((void*)3 == p_vec->p_buffer[2]); - CHECK((void*)1 == p_vec->p_buffer[3]); - vec_free( p_vec ); + CHECK(0 == mem_unbox(p_vec->p_buffer[0])); + CHECK(2 == mem_unbox(p_vec->p_buffer[1])); + CHECK(3 == mem_unbox(p_vec->p_buffer[2])); + CHECK(1 == mem_unbox(p_vec->p_buffer[3])); + //mem_release(p_vec); } TEST(Verify_vec_insert_should_insert_items_at_the_beginning) { - vec_t* p_vec = vec_new(false,2,0,1); - CHECK(true == vec_insert(p_vec,0,2,2,3)); + // TODO: release fails for some reason. Investigate + vec_t* p_vec = vec_new(2,mem_box(0),mem_box(1)); + CHECK(true == vec_insert(p_vec,0,2,mem_box(2),mem_box(3))); CHECK(4 == p_vec->size); CHECK(4 == p_vec->capacity); - CHECK((void*)2 == p_vec->p_buffer[0]); - CHECK((void*)3 == p_vec->p_buffer[1]); - CHECK((void*)0 == p_vec->p_buffer[2]); - CHECK((void*)1 == p_vec->p_buffer[3]); - vec_free( p_vec ); + CHECK(2 == mem_unbox(p_vec->p_buffer[0])); + CHECK(3 == mem_unbox(p_vec->p_buffer[1])); + CHECK(0 == mem_unbox(p_vec->p_buffer[2])); + CHECK(1 == mem_unbox(p_vec->p_buffer[3])); + //mem_release(p_vec); } //------------------------------------------------------------------------- @@ -268,54 +252,54 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_erase_erases_a_single_item) { - void* data[3] = { (void*)0x1, (void*)0x2, (void*)0x3 }; - vec_t vector = { false, 3, 3, data }; + void* data[3] = { mem_box(1), mem_box(2), mem_box(3) }; + vec_t vector = { 3, 3, data }; CHECK(true == vec_erase( &vector, 1, 1 )); CHECK(2 == vector.size); - CHECK((void*)0x1 == data[0]); - CHECK((void*)0x3 == data[1]); + CHECK(0x1 == mem_unbox(data[0])); + CHECK(0x3 == mem_unbox(data[1])); } TEST(Verify_vec_erase_erases_a_multiple_items) { - void* data[4] = { (void*)0x1, (void*)0x2, (void*)0x3, (void*)0x4 }; - vec_t vector = { false, 4, 4, data }; + void* data[4] = { mem_box(1), mem_box(2), mem_box(3), mem_box(4) }; + vec_t vector = { 4, 4, data }; CHECK(true == vec_erase( &vector, 1, 2 )); CHECK(2 == vector.size); - CHECK((void*)0x1 == data[0]); - CHECK((void*)0x4 == data[1]); + CHECK(0x1 == mem_unbox(data[0])); + CHECK(0x4 == mem_unbox(data[1])); } TEST(Verify_vec_erase_erases_everything) { - void* data[4] = { (void*)0x1, (void*)0x2, (void*)0x3, (void*)0x4 }; - vec_t vector = { false, 4, 4, data }; + void* data[4] = { mem_box(1), mem_box(2), mem_box(3), mem_box(4) }; + vec_t vector = { 4, 4, data }; CHECK(true == vec_erase( &vector, 0, 3 )); CHECK(0 == vector.size); } TEST(Verify_vec_erase_should_fail_for_invalid_ranges) { - void* data[4] = { (void*)0x1, (void*)0x2, (void*)0x3, (void*)0x4 }; - vec_t vector = { false, 4, 4, data }; + void* data[4] = { mem_box(1), mem_box(2), mem_box(3), mem_box(4) }; + vec_t vector = { 4, 4, data }; CHECK(false == vec_erase( &vector, 0, 4 )); } TEST(Verify_vec_erase_everything_but_last) { - void* data[4] = { (void*)0x1, (void*)0x2, (void*)0x3, (void*)0x4 }; - vec_t vector = { false, 4, 4, data }; + void* data[4] = { mem_box(1), mem_box(2), mem_box(3), mem_box(4) }; + vec_t vector = { 4, 4, data }; CHECK(true == vec_erase( &vector, 0, 2 )); - CHECK((void*)0x4 == data[0]); + CHECK(0x4 == mem_unbox(data[0])); CHECK(1 == vector.size); } TEST(Verify_vec_erase_everything_but_first) { - void* data[4] = { (void*)0x1, (void*)0x2, (void*)0x3, (void*)0x4 }; - vec_t vector = { false, 4, 4, data }; + void* data[4] = { mem_box(1), mem_box(2), mem_box(3), mem_box(4) }; + vec_t vector = { 4, 4, data }; CHECK(true == vec_erase( &vector, 1, 3 )); - CHECK((void*)0x1 == data[0]); + CHECK(0x1 == mem_unbox(data[0])); CHECK(1 == vector.size); } @@ -324,28 +308,22 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_push_back_should_push_a_new_element_to_the_end) { - vec_t* p_vec = vec_new(false,3,0,1,2); - vec_push_back( p_vec, (void*)0x2A ); - - CHECK( false == p_vec->own_contents ); + vec_t* p_vec = vec_new(3,mem_box(0), mem_box(1), mem_box(2)); + vec_push_back( p_vec, mem_box(0x2A) ); CHECK( 4 == p_vec->size ); CHECK( 4 == p_vec->capacity ); - CHECK( (void*)0x2A == p_vec->p_buffer[3] ); - - vec_free( p_vec ); + CHECK( 0x2A == mem_unbox(p_vec->p_buffer[3]) ); + mem_release(p_vec); } TEST(Verify_vec_push_back_should_push_a_new_element_to_the_end_of_empty_vector) { - vec_t* p_vec = vec_new(false,0); - vec_push_back( p_vec, (void*)0x2A ); - - CHECK( false == p_vec->own_contents ); + vec_t* p_vec = vec_new(0); + vec_push_back( p_vec, mem_box(0x2A) ); CHECK( 1 == p_vec->size ); CHECK( 1 == p_vec->capacity ); - CHECK( (void*)0x2A == p_vec->p_buffer[0] ); - - vec_free( p_vec ); + CHECK( 0x2A == mem_unbox(p_vec->p_buffer[0]) ); + mem_release(p_vec); } //------------------------------------------------------------------------- @@ -353,16 +331,17 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_pop_back_returns_last_element) { - void* data[4] = { (void*)0x1, (void*)0x2, (void*)0x3, (void*)0x4 }; - vec_t vector = { false, 4, 4, data }; - CHECK( (void*)0x4 == vec_pop_back( &vector ) ); + void* data[4] = { mem_box(1), mem_box(2), mem_box(3), mem_box(4) }; + vec_t vector = { 4, 4, data }; + void* p_box = vec_pop_back( &vector ); + CHECK( 0x4 == mem_unbox(p_box) ); CHECK( 4 == vector.capacity ); CHECK( 3 == vector.size ); } TEST(Verify_vec_pop_back_does_nothing_if_no_elements) { - vec_t vector = { false, 0, 0, NULL }; + vec_t vector = { 0, 0, NULL }; CHECK( NULL == vec_pop_back( &vector ) ); CHECK( 0 == vector.capacity ); CHECK( 0 == vector.size ); @@ -373,31 +352,9 @@ namespace { //------------------------------------------------------------------------- TEST(Verify_vec_clear_clears_the_vector) { - void* data[4] = { (void*)0x1, (void*)0x2, (void*)0x3, (void*)0x4 }; - vec_t vector = { false, 4, 4, data }; + void* data[4] = { mem_box(1), mem_box(2), mem_box(3), mem_box(4) }; + vec_t vector = { 4, 4, data }; vec_clear( &vector ); CHECK(0 == vector.size); } - - //TEST(foo) - //{ - // size_t next_power = 9; - // size_t num_bits = sizeof(size_t) * 8; - // size_t bit_n; - - // next_power--; - // for (bit_n = 1; bit_n < num_bits; bit_n = bit_n << 1) - // { - // next_power = next_power | (next_power >> bit_n); - // } - // next_power++; - - // std::cout << next_power << std::endl; - - // //int n = 7; - // //int exp = 0; - // //while(n>1) { n = n>>1; exp++; } - // //while(exp>=0) { n = n<<1; exp--; } - // //std::cout << n << " " << exp << std::endl; - //} } -- 2.54.0