// 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);
+ }
}
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);
}
//-------------------------------------------------------------------------
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 );
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 );
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
}