From eeaf160892a977848e9d2bab654875901d879af5 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 24 Jul 2014 16:33:02 -0400 Subject: [PATCH] fixed some moar bugs --- Rakefile | 4 + source/list/list.c | 7 + source/vector/vec.c | 18 +- tests/test_list.c | 892 ++++++++++++++++++++++---------------------- tests/test_vec.c | 40 +- 5 files changed, 490 insertions(+), 471 deletions(-) diff --git a/Rakefile b/Rakefile index d097686..ac83890 100644 --- a/Rakefile +++ b/Rakefile @@ -19,12 +19,15 @@ task(:posix){ is_windows = false } # Define the compiler environment Env = Rscons::Environment.new do |env| + env.build_dir('source/','build/obj/source') env["CFLAGS"] += ['-Wall', '-Werror'] env['CPPPATH'] += Dir['source/**/'] end # Define the test environment TestEnv = Env.clone do |env| + env.build_dir('source','build/obj/test_source') + env.build_dir('tests','build/obj/tests/source') env['CPPPATH'] += Dir['tests/'] end @@ -39,6 +42,7 @@ task :default => [:test, :build] desc "Build the C Data Structures static library" task :build do Env.Library('build/libcds.a', Dir['source/**/*.c']) + Env.process end #------------------------------------------------------------------------------ diff --git a/source/list/list.c b/source/list/list.c index f910a09..8f6b12b 100644 --- a/source/list/list.c +++ b/source/list/list.c @@ -67,15 +67,22 @@ list_node_t* list_at(list_t* list, size_t index) return node; } +#include + list_node_t* list_push_front( list_t* list, void* contents ) { list_node_t* node = list_new_node( contents ); + puts("1"); node->next = list->head; + puts("2"); list->head = node; + puts("3"); if( NULL == list->tail ) { + puts("4"); list->tail = node; } + puts("5"); return node; } diff --git a/source/vector/vec.c b/source/vector/vec.c index 7209797..6f9f78c 100644 --- a/source/vector/vec.c +++ b/source/vector/vec.c @@ -8,6 +8,7 @@ #include "mem.h" #include #include +#include static void vec_free(void* p_vec); @@ -21,9 +22,11 @@ vec_t* vec_new(size_t num_elements, ...) /* Allocate and construct the vector object */ p_vec = (vec_t*)mem_allocate(sizeof(vec_t), vec_free); + assert(p_vec != NULL); p_vec->size = num_elements; p_vec->capacity = (0 == num_elements) ? DEFAULT_VEC_CAPACITY : num_elements; - p_vec->p_buffer = (void**)malloc( sizeof(void*) * p_vec->capacity ); + p_vec->p_buffer = (void**)calloc( sizeof(void*), p_vec->capacity ); + assert(p_vec->p_buffer != NULL); /* Populate the array with the elements list */ va_start(elements, num_elements); @@ -59,7 +62,8 @@ void vec_resize(vec_t* p_vec, size_t size, void* data) for (; p_vec->size < size; p_vec->size++) { p_vec->p_buffer[ p_vec->size ] = data; - if((size-p_vec->size) > 1) mem_retain(data); + if((NULL != data) && ((size - p_vec->size) > 1)) + mem_retain(data); } } else if (size < p_vec->size) @@ -88,7 +92,9 @@ size_t vec_next_capacity(size_t req_size) void vec_shrink_to_fit(vec_t* p_vec) { + assert(p_vec != NULL); p_vec->p_buffer = realloc( p_vec->p_buffer, sizeof(void*) * p_vec->size ); + assert(p_vec->p_buffer != NULL); p_vec->capacity = p_vec->size; } @@ -99,7 +105,9 @@ size_t vec_capacity(vec_t* p_vec) void vec_reserve(vec_t* p_vec, size_t size) { + assert(p_vec != NULL); p_vec->p_buffer = realloc( p_vec->p_buffer, sizeof(void*) * size ); + assert(p_vec->p_buffer != NULL); p_vec->capacity = size; } @@ -194,7 +202,8 @@ void vec_clear(vec_t* p_vec) static void vec_free(void* p_vec) { vec_clear((vec_t*)p_vec); - free(((vec_t*)p_vec)->p_buffer); + if (NULL != ((vec_t*)p_vec)->p_buffer) + free(((vec_t*)p_vec)->p_buffer); } static void vec_free_range(void** p_buffer, size_t start_idx, size_t end_idx) @@ -202,7 +211,8 @@ static void vec_free_range(void** p_buffer, size_t start_idx, size_t end_idx) size_t i; for(i = start_idx; i < end_idx; i++) { - mem_release(p_buffer[i]); + if (NULL != p_buffer[i]) + mem_release(p_buffer[i]); } } diff --git a/tests/test_list.c b/tests/test_list.c index 94edf04..45bcf52 100644 --- a/tests/test_list.c +++ b/tests/test_list.c @@ -5,454 +5,456 @@ #include "list.h" #include "mem.h" -//static void test_setup(void) { } +static void test_setup(void) { } //----------------------------------------------------------------------------- // Begin Unit Tests //----------------------------------------------------------------------------- TEST_SUITE(List) { -// //------------------------------------------------------------------------- -// // 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 ); -// mem_release( 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_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 ); + mem_release( list ); + } + + //------------------------------------------------------------------------- + // Test list_new_node function + //------------------------------------------------------------------------- + TEST(Verify_list_new_node_returns_newly_allocated_node_with_given_contents) + { + list_node_t* node = list_new_node( mem_box(0) ); + CHECK( NULL != node ); + CHECK( 0 == mem_unbox(node->contents) ); + CHECK( NULL == node->next ); + mem_release( node ); + } + + //------------------------------------------------------------------------- + // 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 ) ); + mem_release( 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 = list_new(); + list_node_t* node = list_push_front( list, mem_box(0x1234) ); + CHECK( NULL != node ); + CHECK( 0x1234 == mem_unbox(node->contents) ); + CHECK( NULL == node->next ); + CHECK( node == list->head ); + CHECK( node == list->tail ); + mem_release(list); + } + + TEST(Verify_list_push_front_pushes_to_front_of_list_of_length_1) + { + list_t* list = list_new(); + list_node_t* node1 = list_push_front( list, mem_box(0x1234) ); + list_node_t* node2 = list_push_front( list, mem_box(0x1235) ); + CHECK( 0x1235 == mem_unbox(list->head->contents) ); + CHECK( node1 == list->head->next ); + CHECK( node2 == list->head ); + CHECK( node1 == list->tail ); + mem_release(list); + } + +#if 0 + //------------------------------------------------------------------------- + // 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) ); + } + + 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) ); + 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); + 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); + 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); + 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(); + list_clear(list); + CHECK( NULL == list->head ); + CHECK( NULL == list->tail ); + mem_release(list); + } + + TEST(Verify_list_clear_clears_a_list_of_length_1) + { + list_t* list = list_new(); + (void)list_push_front(list,(void*)0x1234); + list_clear(list); + CHECK( NULL == list->head ); + CHECK( NULL == list->tail ); + mem_release(list); + } + + 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); + list_clear(list); + CHECK( NULL == list->head ); + CHECK( NULL == list->tail ); + mem_release(list); + } + + 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); + list_clear(list); + CHECK( NULL == list->head ); + CHECK( NULL == list->tail ); + mem_release(list); + } +#endif } diff --git a/tests/test_vec.c b/tests/test_vec.c index c98a5a6..6e73261 100644 --- a/tests/test_vec.c +++ b/tests/test_vec.c @@ -158,7 +158,6 @@ TEST_SUITE(Vector) { vec_t vector = { 0, 0, NULL }; vec_reserve(&vector,5); CHECK( 5 == vector.capacity ); - free( vector.p_buffer ); } //------------------------------------------------------------------------- @@ -169,7 +168,6 @@ TEST_SUITE(Vector) { void* array[2] = { (void*)0x1234, (void*)0x4321 }; vec_t vector = { 2, 2, array }; CHECK((void*)0x4321 == vec_at(&vector,1)); - } TEST(Verify_vec_at_returns_null_if_index_out_of_range) @@ -217,30 +215,28 @@ TEST_SUITE(Vector) { TEST(Verify_vec_insert_should_insert_items_at_the_given_index) { - //// 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(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); + 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(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) { - //// 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(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); + 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(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); } //------------------------------------------------------------------------- -- 2.52.0