From: Michael D. Lowis Date: Tue, 29 Jul 2014 01:23:01 +0000 (-0400) Subject: Fixed some tests for list.c and enabled the -Wextra flag X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=e17c15f4a0a433ade52ce80733cc2b5286703776;p=projs%2Flibcds.git Fixed some tests for list.c and enabled the -Wextra flag --- diff --git a/Rakefile b/Rakefile index ac83890..d6888f5 100644 --- a/Rakefile +++ b/Rakefile @@ -20,7 +20,7 @@ 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["CFLAGS"] += ['-Wall', '-Wextra', '-Werror'] env['CPPPATH'] += Dir['source/**/'] end diff --git a/source/list/list.c b/source/list/list.c index 8f6b12b..f50a398 100644 --- a/source/list/list.c +++ b/source/list/list.c @@ -72,17 +72,12 @@ list_node_t* list_at(list_t* list, size_t index) 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; } @@ -162,6 +157,10 @@ list_node_t* list_insert( list_t* list, size_t index, void* contents) list->tail = new_node; } } + else + { + mem_release(contents); + } } return new_node; } diff --git a/source/mem/mem.c b/source/mem/mem.c index f1b2d86..1977a7a 100644 --- a/source/mem/mem.c +++ b/source/mem/mem.c @@ -40,6 +40,7 @@ void mem_release(void* p_obj) void mem_autorelease(void* p_obj) { + (void)p_obj; } void mem_releaseall(void) diff --git a/source/vector/vec.c b/source/vector/vec.c index b104d42..5295447 100644 --- a/source/vector/vec.c +++ b/source/vector/vec.c @@ -61,7 +61,7 @@ void vec_resize(vec_t* p_vec, size_t size, void* data) assert(NULL != p_vec); if (size > p_vec->size) { - vec_reserve(p_vec,vec_next_capacity(size)); + vec_reserve(p_vec,vec_next_capacity(size+1)); for (; p_vec->size < size; p_vec->size++) { p_vec->p_buffer[ p_vec->size ] = data; diff --git a/tests/main.c b/tests/main.c index abc4d28..0e15d6b 100644 --- a/tests/main.c +++ b/tests/main.c @@ -2,6 +2,8 @@ int main(int argc, char** argv) { + (void)argc; + (void)argv; RUN_TEST_SUITE(Vector); RUN_TEST_SUITE(List); RUN_TEST_SUITE(Buffer); diff --git a/tests/test.c b/tests/test.c index e3f2129..7259f52 100644 --- a/tests/test.c +++ b/tests/test.c @@ -7,11 +7,12 @@ #include "test.h" #include #include +#include jmp_buf Landing_Pad; int Loop_Var; char* Curr_Test; -test_results_t Test_Results = {0}; +test_results_t Test_Results = {0,0,0}; static const char* Results_String = "\nUnit Test Summary" @@ -32,6 +33,7 @@ static void handle_signal(int sig) { case SIGSEGV: case SIGSYS: fprintf(stderr,"%s:%d:0:%s:CRASH (signal: %d)\n\t\n", __FILE__, __LINE__, Curr_Test, sig); \ + exit(1); //longjmp(Landing_Pad,1); break; diff --git a/tests/test_list.c b/tests/test_list.c index a1a1195..0ae7169 100644 --- a/tests/test_list.c +++ b/tests/test_list.c @@ -5,13 +5,12 @@ #include "list.h" #include "mem.h" -//static void test_setup(void) { } +static void test_setup(void) { } //----------------------------------------------------------------------------- // Begin Unit Tests //----------------------------------------------------------------------------- TEST_SUITE(List) { -#if 0 //------------------------------------------------------------------------- // Test list_new function //------------------------------------------------------------------------- @@ -195,25 +194,27 @@ TEST_SUITE(List) { //------------------------------------------------------------------------- TEST(Verify_list_push_back_pushes_to_empty_list) { - list_t list = { NULL, NULL }; - list_node_t* node = list_push_back( &list, (void*)0x1234 ); + list_t* list = list_new(); + list_node_t* node = list_push_back( list, mem_box(0x1234) ); CHECK( NULL != node ); - CHECK( (void*)0x1234 == node->contents ); + CHECK( 0x1234 == mem_unbox(node->contents) ); CHECK( NULL == node->next ); - CHECK( node == list.head ); - CHECK( node == list.tail ); + CHECK( node == list->head ); + CHECK( node == list->tail ); + mem_release(list); } 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 ); + list_t* list = list_new(); + list_push_back( list, mem_box(0x1234) ); + list_node_t* node = list_push_back( list, mem_box(0x1235) ); CHECK( NULL != node ); - CHECK( (void*)0x1234 == node->contents ); - CHECK( &node1 != node->next ); - CHECK( node != list.head ); - CHECK( node == list.tail ); + CHECK( 0x1235 == mem_unbox(node->contents) ); + CHECK( NULL == node->next ); + CHECK( node != list->head ); + CHECK( node == list->tail ); + mem_release(list); } //------------------------------------------------------------------------- @@ -299,60 +300,62 @@ TEST_SUITE(List) { //------------------------------------------------------------------------- TEST(Verify_insert_should_insert_into_empty_list) { - list_t list = { NULL, NULL }; - list_node_t* node = list_insert( &list, 0, (void*)0x1234 ); + list_t* list = list_new(); + list_node_t* node = list_insert( list, 0, mem_box(0x1234) ); CHECK( node != NULL ); CHECK( node->next == NULL ); - CHECK( node->contents == (void*)0x1234 ); - CHECK( list.head == node ); - CHECK( list.tail == node ); + CHECK( mem_unbox(node->contents) == 0x1234 ); + CHECK( list->head == node ); + CHECK( list->tail == node ); + mem_release(list); } 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 ); + list_t* list = list_new(); + list_insert( list, 0, mem_box(0x1234) ); + list_node_t* node = list_insert( list, 0, mem_box(0x1235) ); + CHECK( node != NULL ); + CHECK( node->next != NULL ); + CHECK( mem_unbox(list->head->contents) == 0x1235 ); + CHECK( list->head == node ); + CHECK( list->tail != node ); + mem_release(list); } 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 ); + list_t* list = list_new(); + list_insert( list, 0, mem_box(0x1236) ); + list_insert( list, 0, mem_box(0x1235) ); + list_insert( list, 0, mem_box(0x1234) ); + list_node_t* node = list_insert( list, 1, mem_box(0x1237) ); + CHECK( node != NULL ); + CHECK( node->next != NULL ); + CHECK( mem_unbox(list->head->next->contents) == 0x1237 ); + mem_release(list); } 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 ); + list_t* list = list_new(); + list_insert( list, 0, mem_box(0x1236) ); + list_insert( list, 0, mem_box(0x1235) ); + list_node_t* node = list_insert( list, 2, mem_box(0x1234) ); + CHECK( node != NULL ); + CHECK( node->next == NULL ); + CHECK( mem_unbox(list->tail->contents) == 0x1234 ); + mem_release(list); } 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 ); + list_t* list = list_new(); + list_insert( list, 0, mem_box(0x1236) ); + list_insert( list, 0, mem_box(0x1235) ); + list_node_t* node = list_insert( list, 3, mem_box(0x1234) ); + CHECK( node == NULL ); + mem_release(list); } //------------------------------------------------------------------------- @@ -364,6 +367,7 @@ TEST_SUITE(List) { CHECK( NULL == list_delete( &list, 0) ); } +#if 0 TEST(Verify_delete_deletes_the_first_element_of_a_list_of_length_1) { list_node_t* node = list_new_node((void*)0x1234); diff --git a/tests/test_vec.c b/tests/test_vec.c index 4594c01..d5f0102 100644 --- a/tests/test_vec.c +++ b/tests/test_vec.c @@ -111,7 +111,7 @@ TEST_SUITE(Vector) { vec_resize( p_vec, 4, mem_box(0x2A) ); CHECK( 4 == p_vec->size ); - CHECK( 4 == p_vec->capacity ); + CHECK( 8 == p_vec->capacity ); CHECK( 0x2A == mem_unbox(p_vec->p_buffer[3]) ); mem_release(p_vec); @@ -158,6 +158,7 @@ TEST_SUITE(Vector) { vec_t vector = { 0, 0, NULL }; vec_reserve(&vector,5); CHECK( 5 == vector.capacity ); + free(vector.p_buffer); } //------------------------------------------------------------------------- @@ -215,25 +216,25 @@ TEST_SUITE(Vector) { TEST(Verify_vec_insert_should_insert_items_at_the_given_index) { + //TODO: busted. 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(5 == p_vec->size); - //CHECK(8 == p_vec->capacity); + CHECK(4 == p_vec->size); + CHECK(8 == 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])); - puts("1"); mem_release(p_vec); - puts("2"); } TEST(Verify_vec_insert_should_insert_items_at_the_beginning) { + //TODO: busted. 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(5 == p_vec->size); - //CHECK(8 == p_vec->capacity); + CHECK(4 == p_vec->size); + CHECK(8 == 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])); @@ -315,7 +316,7 @@ TEST_SUITE(Vector) { 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( 8 == p_vec->capacity ); CHECK( 0x2A == mem_unbox(p_vec->p_buffer[3]) ); mem_release(p_vec); } @@ -325,7 +326,7 @@ TEST_SUITE(Vector) { 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( 2 == p_vec->capacity ); CHECK( 0x2A == mem_unbox(p_vec->p_buffer[0]) ); mem_release(p_vec); }