From: Michael D. Lowis Date: Tue, 29 Jul 2014 14:55:33 +0000 (-0400) Subject: 100% of tests now passing X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=38b1865bd8d99368ffda5ade441b6e804759efb8;p=projs%2Flibcds.git 100% of tests now passing --- diff --git a/source/buffer/buf.c b/source/buffer/buf.c index d93f0aa..5a8bc79 100644 --- a/source/buffer/buf.c +++ b/source/buffer/buf.c @@ -43,9 +43,12 @@ bool buf_full(buf_t* buf) void buf_clear(buf_t* buf) { + void* entry; while ( !buf_empty(buf) ) { - mem_release( buf_read(buf) ); + entry = buf_read(buf); + if (NULL != entry) + mem_release( entry ); } buf->reads = 0; buf->writes = 0; @@ -79,3 +82,4 @@ static void buf_free(void* p_buf) buf_clear((buf_t*)p_buf); free( ((buf_t*)p_buf)->buffer ); } + diff --git a/source/list/list.c b/source/list/list.c index f50a398..89182ab 100644 --- a/source/list/list.c +++ b/source/list/list.c @@ -201,9 +201,12 @@ list_node_t* list_delete( list_t* list, size_t index) void list_clear(list_t* list) { - mem_release((void*)list->head); - list->head = NULL; - list->tail = NULL; + if (NULL != list->head) + { + mem_release((void*)list->head); + list->head = NULL; + list->tail = NULL; + } } static void list_free(void* p_list) diff --git a/tests/test_buf.c b/tests/test_buf.c index 557a467..4e0fdad 100644 --- a/tests/test_buf.c +++ b/tests/test_buf.c @@ -3,142 +3,141 @@ // File To Test #include "buf.h" +#include "mem.h" -//static void test_setup(void) { } +static void test_setup(void) { } //----------------------------------------------------------------------------- // Begin Unit Tests //----------------------------------------------------------------------------- TEST_SUITE(Buffer) { -// //------------------------------------------------------------------------- -// // 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 ); + mem_release(buf); + } + + TEST(Verify_buf_new_returns_null_if_passed_a_size_of_0) + { + CHECK( NULL == buf_new(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 ) ); + mem_release(buf); + } + + //------------------------------------------------------------------------- + // Test buf_empty function + //------------------------------------------------------------------------- + TEST(Verify_buf_empty_returns_1_when_buffer_is_empty) + { + buf_t buf = { NULL, 5, 1, 1 }; + CHECK( true == buf_empty( &buf ) ); + } + + TEST(Verify_buf_empty_returns_0_when_buffer_is_empty) + { + buf_t buf = { NULL, 5, 1, 2 }; + CHECK( false == 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( true == buf_full( &buf ) ); + } + + TEST(Verify_buf_full_returns_0_if_buffer_empty) + { + buf_t buf = { NULL, 5, 1, 1 }; + CHECK( false == buf_full( &buf ) ); + } + + TEST(Verify_buf_full_returns_0_if_buffer_not_full) + { + buf_t buf = { NULL, 5, 1, 5 }; + CHECK( false == buf_full( &buf ) ); + } + + //------------------------------------------------------------------------- + // Test buf_clear function + //------------------------------------------------------------------------- + TEST(Verify_buf_clears_the_buffer_and_frees_the_contents) + { + buf_t* buf = buf_new(3); + buf_write( buf, mem_box(0x1234) ); + buf_write( buf, mem_box(0x1235) ); + buf_write( buf, mem_box(0x1236) ); + buf_clear( buf ); + CHECK( buf->reads == 0 ); + CHECK( buf->writes == 0 ); + mem_release(buf); + } + + //------------------------------------------------------------------------- + // 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 ) ); + mem_release(buf); + } + + TEST(Verify_buf_read_should_return_the_next_piece_of_data_from_the_buffer) + { + buf_t* buf = buf_new(3); + void* contents; + buf_write( buf, mem_box(0x1234) ); + buf_write( buf, mem_box(0x1235) ); + buf_write( buf, mem_box(0x1236) ); + + contents = buf_read(buf); + CHECK( 0x1234 == mem_unbox(contents) ); + mem_release(contents); + contents = buf_read(buf); + CHECK( 0x1235 == mem_unbox(contents) ); + mem_release(contents); + contents = buf_read(buf); + CHECK( 0x1236 == mem_unbox(contents) ); + mem_release(contents); + CHECK( NULL == buf_read(buf) ); + + mem_release(buf); + } + + //------------------------------------------------------------------------- + // Test buf_write function + //------------------------------------------------------------------------- + TEST(Verify_buf_write_should_return_0_if_buffer_is_full) + { + buf_t* buf = buf_new(1); + CHECK( true == buf_write(buf, mem_box(0x1234))); + CHECK( false == buf_write(buf, mem_box(0x1234))); + mem_release(buf); + } + + TEST(Verify_buf_write_should_return_1_if_data_successfully_wrote) + { + buf_t* buf = buf_new(1); + CHECK( true == buf_write(buf, mem_box(0x1234))); + mem_release(buf); + } } diff --git a/tests/test_list.c b/tests/test_list.c index 0ae7169..9e1d78d 100644 --- a/tests/test_list.c +++ b/tests/test_list.c @@ -367,53 +367,54 @@ 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); - list_t list = { node, node }; - CHECK( NULL == list_delete( &list, 0) ); - CHECK( list.head == NULL ); - CHECK( list.tail == NULL ); + list_t* list = list_new(); + list_push_back(list,mem_box(0x1234)); + CHECK( NULL == list_delete( list, 0) ); + CHECK( list->head == NULL ); + CHECK( list->tail == NULL ); + mem_release(list); } 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 ); + list_t* list = list_new(); + list_push_back(list,mem_box(0x1234)); + list_push_back(list,mem_box(0x1235)); + CHECK( NULL != list_delete( list, 0) ); + CHECK( list->head != NULL ); + CHECK( list->tail != NULL ); + CHECK( list->head == list->tail ); + CHECK( mem_unbox(list->head->contents) == 0x1235 ); + mem_release(list); } 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 ); + list_t* list = list_new(); + list_push_back(list,mem_box(0x1234)); + list_push_back(list,mem_box(0x1235)); + list_push_back(list,mem_box(0x1236)); + CHECK( NULL != list_delete( list, 1) ); + CHECK( 2 == list_size( list ) ); + CHECK( list->head != NULL ); + CHECK( list->tail != NULL ); + CHECK( list->head != list->tail ); + mem_release(list); } 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 ); + list_t* list = list_new(); + list_push_back(list,mem_box(0x1234)); + list_push_back(list,mem_box(0x1235)); + CHECK( NULL == list_delete( list, 1) ); + CHECK( list->head != NULL ); + CHECK( list->tail != NULL ); + CHECK( list->head == list->tail ); + CHECK( mem_unbox(list->head->contents) == 0x1234 ); + mem_release(list); } //------------------------------------------------------------------------- @@ -431,7 +432,7 @@ TEST_SUITE(List) { TEST(Verify_list_clear_clears_a_list_of_length_1) { list_t* list = list_new(); - (void)list_push_front(list,(void*)0x1234); + list_push_front(list,mem_box(0x1234)); list_clear(list); CHECK( NULL == list->head ); CHECK( NULL == list->tail ); @@ -441,8 +442,8 @@ TEST_SUITE(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_push_front(list,mem_box(0x1234)); + list_push_front(list,mem_box(0x1234)); list_clear(list); CHECK( NULL == list->head ); CHECK( NULL == list->tail ); @@ -452,13 +453,12 @@ TEST_SUITE(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_push_front(list,mem_box(0x1234)); + list_push_front(list,mem_box(0x1234)); + list_push_front(list,mem_box(0x1234)); list_clear(list); CHECK( NULL == list->head ); CHECK( NULL == list->tail ); mem_release(list); } -#endif }