diff --git a/unit-test/cf_clist_tests.c b/unit-test/cf_clist_tests.c index 37304745..0f8ee277 100644 --- a/unit-test/cf_clist_tests.c +++ b/unit-test/cf_clist_tests.c @@ -52,838 +52,192 @@ int UT_CListFn_Rm(CF_CListNode_t *node, void *context) /******************************************************************************* ** -** cf_clist_tests Setup and Teardown +** Tests ** *******************************************************************************/ -void cf_clist_tests_Setup(void) +void Test_CF_CList_InitNode(void) { - cf_tests_Setup(); -} /* end cf_clist_tests_Setup */ + CF_CListNode_t node; -void cf_clist_tests_Teardown(void) -{ - cf_tests_Teardown(); -} /* end cf_clist_tests_Teardown */ - -/* end cf_clist_tests Setup and Teardown */ - -/******************************************************************************* -** -** CF_CList_InitNode tests -** -*******************************************************************************/ - -void Test_CF_CList_InitNode_PointNodeToItselfAsNextAndPrev(void) -{ - /* Arrange */ - CF_CListNode_t test_node; - - test_node.next = NULL; - test_node.prev = NULL; - - /* Act */ - CF_CList_InitNode(&test_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(test_node.prev, &test_node); - UtAssert_ADDRESS_EQ(test_node.next, &test_node); - -} /* Test_CF_CList_InitNode_PointNodeToItselfAsNextAndPrev */ - -/******************************************************************************* -** -** CF_CList_InsertFront tests -** -*******************************************************************************/ - -void Test_CF_CList_InsertFront_InsertNodeIntoEmptyList(void) -{ - /* Arrange */ - CF_CListNode_t * dummy_head = NULL; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_node; - CF_CListNode_t * arg_node = &dummy_node; - - arg_node->next = &dummy_node; - arg_node->prev = &dummy_node; - - /* Act */ - CF_CList_InsertFront(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(*arg_head, arg_node); -} /* end CF_CList_InsertFront_InsertNodeIntoEmptyList */ - -void Test_CF_CList_InsertFront_WhenHeadIsOnlyNodeAndTheyPointToEachOtherInsertNode(void) -{ - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_node; - CF_CListNode_t * arg_node = &dummy_node; - - (*arg_head)->next = dummy_head; - (*arg_head)->prev = dummy_head; - - arg_node->next = &dummy_node; - arg_node->prev = &dummy_node; - - /* Act */ - CF_CList_InsertFront(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(arg_node->prev, &dummy_head_node); - UtAssert_ADDRESS_EQ(arg_node->next, &dummy_head_node); - UtAssert_ADDRESS_EQ(dummy_head_node.prev, arg_node); - UtAssert_ADDRESS_EQ(dummy_head_node.next, arg_node); - UtAssert_ADDRESS_EQ(*arg_head, arg_node); - -} /* end Test_CF_CList_InsertFront_WhenHeadIsOnlyNodeAndTheyPointToEachOtherInsertNode */ - -void Test_CF_CList_InsertFront_WhenHeadIsOneOfTwoNodesAndTheyPointToCorrectNodesInsertNode(void) -{ - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_node; - CF_CListNode_t * arg_node = &dummy_node; - CF_CListNode_t dummy_last_node; - - dummy_last_node.next = &dummy_head_node; - dummy_last_node.prev = &dummy_head_node; - - (*arg_head)->next = &dummy_last_node; - (*arg_head)->prev = &dummy_last_node; - - arg_node->next = &dummy_node; - arg_node->prev = &dummy_node; - - /* Act */ - CF_CList_InsertFront(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(arg_node->prev, &dummy_last_node); - UtAssert_ADDRESS_EQ(arg_node->next, &dummy_head_node); - UtAssert_ADDRESS_EQ(dummy_head_node.prev, arg_node); - UtAssert_ADDRESS_EQ(dummy_head_node.next, &dummy_last_node); - UtAssert_ADDRESS_EQ(dummy_last_node.prev, &dummy_head_node); - UtAssert_ADDRESS_EQ(dummy_last_node.next, arg_node); - UtAssert_ADDRESS_EQ(*arg_head, arg_node); - -} /* end Test_CF_CList_InsertFront_WhenHeadIsOneOfTwoNodesAndTheyPointToCorrectNodesInsertNode */ - -void Test_CF_CList_InsertFront_WhenNodeListIsGreaterThanTwoNodesAndTheyPointToCorrectNodesInsertNode(void) -{ - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_node; - CF_CListNode_t * arg_node = &dummy_node; - CF_CListNode_t dummy_last_node; - CF_CListNode_t * dummy_second_node; - CF_CListNode_t * dummy_next_to_last_node; - uint8 num_extraneous_nodes = Any_uint8_LessThan(18) + 1; // 1 to 18 - int i = 0; - - dummy_second_node = NULL; - dummy_next_to_last_node = NULL; - - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *dummy_clist_node = malloc(sizeof(*dummy_clist_node)); - - if (i == 0) - { - dummy_second_node = dummy_clist_node; - dummy_second_node->prev = dummy_head; - dummy_second_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_second_node; - } - else - { - dummy_next_to_last_node->next = dummy_clist_node; - dummy_clist_node->prev = dummy_next_to_last_node; - dummy_clist_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_clist_node; - } - } - - dummy_last_node.next = &dummy_head_node; - dummy_last_node.prev = dummy_next_to_last_node; - - (*arg_head)->next = dummy_second_node; - (*arg_head)->prev = &dummy_last_node; - - arg_node->next = &dummy_node; - arg_node->prev = &dummy_node; - - /* Act */ - CF_CList_InsertFront(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(arg_node->prev, &dummy_last_node); - UtAssert_ADDRESS_EQ(arg_node->next, &dummy_head_node); - UtAssert_ADDRESS_EQ(dummy_head_node.prev, arg_node); - UtAssert_ADDRESS_EQ(dummy_head_node.next, dummy_second_node); - UtAssert_ADDRESS_EQ(dummy_last_node.prev, dummy_next_to_last_node); - UtAssert_ADDRESS_EQ(dummy_last_node.next, arg_node); - UtAssert_ADDRESS_EQ(*arg_head, arg_node); - - /* removes all malloc nodes - arg_node head (not malloc) -> next is old head (not malloc) -> next is second node - * (malloc) */ - CF_CListNode_t *free_up_node = arg_node->next->next; - - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *old_free_up_node = free_up_node; - free_up_node = old_free_up_node->next; - free(old_free_up_node); - } - -} /* end Test_CF_CList_InsertFront_WhenNodeListIsGreaterThanTwoNodesAndTheyPointToCorrectNodesInsertNode */ - -/******************************************************************************* -** -** CF_CList_InsertBack tests -** -*******************************************************************************/ - -void Test_CF_CList_InsertBack_InsertNodeIntoEmptyList(void) -{ - /* Arrange */ - CF_CListNode_t * dummy_head = NULL; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_node; - CF_CListNode_t * arg_node = &dummy_node; - - arg_node->next = &dummy_node; - arg_node->prev = &dummy_node; - - /* Act */ - CF_CList_InsertBack(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(*arg_head, arg_node); -} /* end Test_CF_CList_InsertBack_InsertNodeIntoEmptyList */ - -void Test_CF_CList_InsertBack_WhenHeadIsOnlyNodeAndTheyPointToEachOtherInsertNode(void) -{ - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_node; - CF_CListNode_t * arg_node = &dummy_node; - - (*arg_head)->next = dummy_head; - (*arg_head)->prev = dummy_head; - - arg_node->next = &dummy_node; - arg_node->prev = &dummy_node; - - /* Act */ - CF_CList_InsertBack(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(arg_node->prev, &dummy_head_node); - UtAssert_ADDRESS_EQ(arg_node->next, &dummy_head_node); - UtAssert_ADDRESS_EQ(dummy_head_node.prev, arg_node); - UtAssert_ADDRESS_EQ(dummy_head_node.next, arg_node); - UtAssert_ADDRESS_EQ(*arg_head, dummy_head); - -} /* end Test_CF_CList_InsertBack_WhenHeadIsOnlyNodeAndTheyPointToEachOtherInsertNode */ - -void Test_CF_CList_InsertBack_WhenHeadIsOneOfTwoNodesAndTheyPointToCorrectNodesInsertNode(void) -{ - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_node; - CF_CListNode_t * arg_node = &dummy_node; - CF_CListNode_t dummy_last_node; - - dummy_last_node.next = &dummy_head_node; - dummy_last_node.prev = &dummy_head_node; - - (*arg_head)->next = &dummy_last_node; - (*arg_head)->prev = &dummy_last_node; - - arg_node->next = &dummy_node; - arg_node->prev = &dummy_node; - - /* Act */ - CF_CList_InsertBack(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(arg_node->prev, &dummy_last_node); - UtAssert_ADDRESS_EQ(arg_node->next, &dummy_head_node); - UtAssert_ADDRESS_EQ(dummy_head_node.prev, arg_node); - UtAssert_ADDRESS_EQ(dummy_head_node.next, &dummy_last_node); - UtAssert_ADDRESS_EQ(dummy_last_node.prev, &dummy_head_node); - UtAssert_ADDRESS_EQ(dummy_last_node.next, arg_node); - UtAssert_ADDRESS_EQ(*arg_head, dummy_head); - -} /* end Test_CF_CList_InsertBack_WhenHeadIsOneOfTwoNodesAndTheyPointToCorrectNodesInsertNode */ - -void Test_CF_CList_InsertBack_WhenNodeListIsGreaterThanTwoNodesAndTheyPointToCorrectNodesInsertNode(void) -{ - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_node; - CF_CListNode_t * arg_node = &dummy_node; - CF_CListNode_t dummy_last_node; - CF_CListNode_t * dummy_second_node; - CF_CListNode_t * dummy_next_to_last_node; - uint8 num_extraneous_nodes = Any_uint8_LessThan(18) + 1; // 1 to 18 - int i = 0; - - dummy_second_node = NULL; - dummy_next_to_last_node = NULL; - - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *dummy_clist_node = malloc(sizeof(*dummy_clist_node)); - - if (i == 0) - { - dummy_second_node = dummy_clist_node; - dummy_second_node->prev = dummy_head; - dummy_second_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_second_node; - } - else - { - dummy_next_to_last_node->next = dummy_clist_node; - dummy_clist_node->prev = dummy_next_to_last_node; - dummy_clist_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_clist_node; - } - } - - dummy_last_node.next = &dummy_head_node; - dummy_last_node.prev = dummy_next_to_last_node; - - (*arg_head)->next = dummy_second_node; - (*arg_head)->prev = &dummy_last_node; - - arg_node->next = &dummy_node; - arg_node->prev = &dummy_node; - - /* Act */ - CF_CList_InsertBack(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(arg_node->prev, &dummy_last_node); - UtAssert_ADDRESS_EQ(arg_node->next, &dummy_head_node); - UtAssert_ADDRESS_EQ(dummy_head_node.prev, arg_node); - UtAssert_ADDRESS_EQ(dummy_head_node.next, dummy_second_node); - UtAssert_ADDRESS_EQ(dummy_last_node.prev, dummy_next_to_last_node); - UtAssert_ADDRESS_EQ(dummy_last_node.next, arg_node); - UtAssert_ADDRESS_EQ(*arg_head, dummy_head); - - /* removes all malloc nodes */ - CF_CListNode_t *free_up_node = dummy_head->next; - - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *old_free_up_node = free_up_node; - free_up_node = old_free_up_node->next; - free(old_free_up_node); - } - -} /* end Test_CF_CList_InsertBack_WhenNodeListIsGreaterThanTwoNodesAndTheyPointToCorrectNodesInsertNode */ - -/******************************************************************************* -** -** CF_CList_Pop tests - In File Calls:CF_CList_Remove -** -*******************************************************************************/ - -void Test_CF_CList_Pop_WhenListIsEmptySuccessReturn_NULL(void) -{ - /* Arrange */ - CF_CListNode_t * dummy_head = NULL; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t * local_result; - - /* Act */ - local_result = CF_CList_Pop(arg_head); - - /* Assert */ - UtAssert_ADDRESS_EQ(local_result, NULL); -} /* end Test_CF_CList_Pop_WhenListIsEmptySuccessReturn_NULL */ - -void Test_CF_CList_Pop_WhenItIsOnlyNodePopHeadNodeAndReturn_head_(void) -{ - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t * local_result; - - /* Arrange unstubbable: CF_CList_Remove */ - dummy_head->prev = dummy_head; - dummy_head->next = dummy_head; - - /* Act */ - local_result = CF_CList_Pop(arg_head); - - /* Assert */ - UtAssert_ADDRESS_EQ(local_result, &dummy_head_node); -} /* end Test_CF_CList_Pop_WhenItIsOnlyNodePopHeadNodeAndReturn_head_ */ - -void Test_CF_CList_Pop_WhenListIsAnySizeGreaterThanOneSuccessPopsHeadNodeAndReturns_head(void) -{ - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_last_node; - CF_CListNode_t * dummy_second_node = &dummy_last_node; - CF_CListNode_t * dummy_next_to_last_node = dummy_head; - uint8 num_extraneous_nodes = Any_uint8_LessThan(19); // 0 to 18 - int i = 0; - CF_CListNode_t * local_result; - - /* Arrange unstubbable: CF_CList_Remove */ - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *dummy_clist_node = malloc(sizeof(*dummy_clist_node)); - - if (i == 0) - { - dummy_second_node = dummy_clist_node; - dummy_second_node->prev = dummy_head; - dummy_second_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_second_node; - } - else - { - dummy_next_to_last_node->next = dummy_clist_node; - dummy_clist_node->prev = dummy_next_to_last_node; - dummy_clist_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_clist_node; - } - } - - dummy_head_node.prev = &dummy_last_node; - dummy_head_node.next = dummy_second_node; - - dummy_last_node.prev = dummy_next_to_last_node; - dummy_last_node.next = dummy_head; - - /* Act */ - local_result = CF_CList_Pop(arg_head); - - /* Assert */ - UtAssert_ADDRESS_EQ(local_result, &dummy_head_node); - - /* removes all malloc nodes */ - CF_CListNode_t *free_up_node = dummy_second_node; - - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *old_free_up_node = free_up_node; - free_up_node = old_free_up_node->next; - free(old_free_up_node); - } -} /* end Test_CF_CList_Pop_WhenListIsAnySizeGreaterThanOneSuccessPopsHeadNodeAndReturns_head */ - -/******************************************************************************* -** -** CF_CList_Remove tests -** -*******************************************************************************/ - -void Test_CF_ClistRemove_WhenOnlyNodeSetHeadTo_NULL(void) -{ - /* Arrange */ - CF_CListNode_t dummy_node; - CF_CListNode_t * arg_node = &dummy_node; - CF_CListNode_t **arg_head = &arg_node; - - arg_node->prev = arg_node; - arg_node->next = arg_node; - - /* Act */ - CF_CList_Remove(arg_head, arg_node); + memset(&node, 0, sizeof(node)); - /* Assert */ - UtAssert_ADDRESS_EQ(*arg_head, NULL); - /* Assert for CF_CList_InitNode - note this cannot be verified because node state does not change */ - UtAssert_ADDRESS_EQ(dummy_node.prev, &dummy_node); - UtAssert_ADDRESS_EQ(dummy_node.next, &dummy_node); -} /* end Test_CF_ClistRemove_WhenOnlyNodeSetHeadTo_NULL */ - -void Test_CF_ClistRemove_WhenOnlyTwoNodesAndLastIsRemovedSetHeadToPointToItself(void) -{ - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_arg_node; - CF_CListNode_t * arg_node = &dummy_arg_node; - - dummy_head_node.prev = arg_node; - dummy_head_node.next = arg_node; - - dummy_arg_node.prev = dummy_head; - dummy_arg_node.next = dummy_head; - - /* Act */ - CF_CList_Remove(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(*arg_head, &dummy_head_node); - UtAssert_ADDRESS_EQ(dummy_head->prev, dummy_head); - UtAssert_ADDRESS_EQ(dummy_head->next, dummy_head); - /* Assert for CF_CList_InitNode - note this cannot be verified because node state does not change */ - UtAssert_ADDRESS_EQ(arg_node->prev, arg_node); - UtAssert_ADDRESS_EQ(arg_node->next, arg_node); -} /* end Test_CF_ClistRemove_WhenOnlyTwoNodesAndLastIsRemovedSetHeadToPointToItself */ + UtAssert_VOIDCALL(CF_CList_InitNode(&node)); + UtAssert_ADDRESS_EQ(&node, node.next); + UtAssert_ADDRESS_EQ(&node, node.prev); +} -void Test_CF_ClistRemove_RemovingHeadSetSecondNodeToHeadAndUpdateLastNode(void) +void Test_CF_CList_InsertFront(void) { - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t * arg_node = *arg_head; - CF_CListNode_t dummy_last_node; - CF_CListNode_t * dummy_second_node; - CF_CListNode_t * dummy_next_to_last_node; - uint8 num_extraneous_nodes = Any_uint8_LessThan(18) + 1; // 1 to 18 - int i = 0; - - dummy_second_node = NULL; - dummy_next_to_last_node = NULL; + CF_CListNode_t node[3]; + CF_CListNode_t *head = NULL; - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *dummy_clist_node = malloc(sizeof(*dummy_clist_node)); - - if (i == 0) - { - dummy_second_node = dummy_clist_node; - dummy_second_node->prev = dummy_head; - dummy_second_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_second_node; - } - else - { - dummy_next_to_last_node->next = dummy_clist_node; - dummy_clist_node->prev = dummy_next_to_last_node; - dummy_clist_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_clist_node; - } - } - - dummy_last_node.next = &dummy_head_node; - dummy_last_node.prev = dummy_next_to_last_node; - - (*arg_head)->next = dummy_second_node; - (*arg_head)->prev = &dummy_last_node; - - /* Act */ - CF_CList_Remove(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(*arg_head, dummy_second_node); - UtAssert_ADDRESS_EQ((*arg_head)->prev, &dummy_last_node); - UtAssert_ADDRESS_EQ(dummy_last_node.next, dummy_second_node); - /* Assert for CF_CList_InitNode */ - UtAssert_ADDRESS_EQ(dummy_head_node.prev, &dummy_head_node); - UtAssert_ADDRESS_EQ(dummy_head_node.next, &dummy_head_node); - - /* removes all malloc nodes */ - /* dummy_head is old second node (head removed) which was malloc'd */ - CF_CListNode_t *free_up_node = dummy_head; + memset(node, 0, sizeof(node)); - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *old_free_up_node = free_up_node; - free_up_node = old_free_up_node->next; - free(old_free_up_node); - } -} /* end Test_CF_ClistRemove_RemovingHeadSetSecondNodeToHeadAndUpdateLastNode */ + /* Already tested, so OK to use to initialize */ + CF_CList_InitNode(&node[0]); + + /* Insert to empty list */ + UtAssert_VOIDCALL(CF_CList_InsertFront(&head, &node[0])); + UtAssert_ADDRESS_EQ(head, node); + + /* Insert to single node list */ + UtAssert_VOIDCALL(CF_CList_InsertFront(&head, &node[1])); + UtAssert_ADDRESS_EQ(head, &node[1]); + UtAssert_ADDRESS_EQ(node[0].next, &node[1]); + UtAssert_ADDRESS_EQ(node[0].prev, &node[1]); + UtAssert_ADDRESS_EQ(node[1].next, &node[0]); + UtAssert_ADDRESS_EQ(node[1].prev, &node[0]); + + /* Insert to list with 2 nodes */ + UtAssert_VOIDCALL(CF_CList_InsertFront(&head, &node[2])); + UtAssert_ADDRESS_EQ(head, &node[2]); + UtAssert_ADDRESS_EQ(node[0].next, &node[2]); + UtAssert_ADDRESS_EQ(node[0].prev, &node[1]); + UtAssert_ADDRESS_EQ(node[1].next, &node[0]); + UtAssert_ADDRESS_EQ(node[1].prev, &node[2]); + UtAssert_ADDRESS_EQ(node[2].next, &node[1]); + UtAssert_ADDRESS_EQ(node[2].prev, &node[0]); +} -void Test_CF_ClistRemove_RemovingLastPointHeadAndNextToLastToEachOther(void) +void Test_CF_CList_InsertBack(void) { - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_last_node; - CF_CListNode_t * arg_node = &dummy_last_node; - CF_CListNode_t * dummy_second_node; - CF_CListNode_t * dummy_next_to_last_node; - uint8 num_extraneous_nodes = Any_uint8_LessThan(18) + 1; // 1 to 18 - int i = 0; + CF_CListNode_t node[3]; + CF_CListNode_t *head = NULL; - dummy_second_node = NULL; - dummy_next_to_last_node = NULL; - - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *dummy_clist_node = malloc(sizeof(*dummy_clist_node)); - - if (i == 0) - { - dummy_second_node = dummy_clist_node; - dummy_second_node->prev = dummy_head; - dummy_second_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_second_node; - } - else - { - dummy_next_to_last_node->next = dummy_clist_node; - dummy_clist_node->prev = dummy_next_to_last_node; - dummy_clist_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_clist_node; - } - } - - dummy_last_node.next = &dummy_head_node; - dummy_last_node.prev = dummy_next_to_last_node; - - (*arg_head)->next = dummy_second_node; - (*arg_head)->prev = &dummy_last_node; - - /* Act */ - CF_CList_Remove(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(*arg_head, dummy_head); - UtAssert_ADDRESS_EQ((*arg_head)->prev, dummy_next_to_last_node); - UtAssert_ADDRESS_EQ(dummy_next_to_last_node->next, dummy_head); - /* Assert for CF_CList_InitNode */ - UtAssert_ADDRESS_EQ(dummy_last_node.prev, &dummy_last_node); - UtAssert_ADDRESS_EQ(dummy_last_node.next, &dummy_last_node); - - /* removes all malloc nodes */ - CF_CListNode_t *free_up_node = dummy_head->next; + memset(node, 0, sizeof(node)); - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *old_free_up_node = free_up_node; - free_up_node = old_free_up_node->next; - free(old_free_up_node); - } -} /* end Test_CF_ClistRemove_RemovingLastPointHeadAndNextToLastToEachOther */ + /* Already tested, so OK to use to initialize */ + CF_CList_InitNode(&node[0]); + + /* Insert to empty list */ + UtAssert_VOIDCALL(CF_CList_InsertBack(&head, &node[0])); + UtAssert_ADDRESS_EQ(head, node); + + /* Insert to single node list */ + UtAssert_VOIDCALL(CF_CList_InsertBack(&head, &node[1])); + UtAssert_ADDRESS_EQ(head, &node[0]); + UtAssert_ADDRESS_EQ(node[0].next, &node[1]); + UtAssert_ADDRESS_EQ(node[0].prev, &node[1]); + UtAssert_ADDRESS_EQ(node[1].next, &node[0]); + UtAssert_ADDRESS_EQ(node[1].prev, &node[0]); + + /* Insert to list with 2 nodes */ + UtAssert_VOIDCALL(CF_CList_InsertBack(&head, &node[2])); + UtAssert_ADDRESS_EQ(head, &node[0]); + UtAssert_ADDRESS_EQ(node[0].next, &node[1]); + UtAssert_ADDRESS_EQ(node[0].prev, &node[2]); + UtAssert_ADDRESS_EQ(node[1].next, &node[2]); + UtAssert_ADDRESS_EQ(node[1].prev, &node[0]); + UtAssert_ADDRESS_EQ(node[2].next, &node[0]); + UtAssert_ADDRESS_EQ(node[2].prev, &node[1]); +} -void Test_CF_ClistRemove_RemovingAnyNodeHasNodesPrevAndNextPointToEachOther(void) +void Test_CF_CList_Pop(void) { - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_last_node; - CF_CListNode_t * dummy_removed_node; - CF_CListNode_t * dummy_removed_node_prev; - CF_CListNode_t * dummy_removed_node_next; - CF_CListNode_t * arg_node; - CF_CListNode_t * dummy_second_node; - CF_CListNode_t * dummy_next_to_last_node; - uint8 num_extraneous_nodes = Any_uint8_LessThan(18) + 1; // 1 to 18 - uint8 num_of_removed_node = Any_uint8_LessThan(num_extraneous_nodes); - int i = 0; - - dummy_removed_node = NULL; - arg_node = NULL; - dummy_second_node = NULL; - dummy_next_to_last_node = NULL; - - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *dummy_clist_node = malloc(sizeof(*dummy_clist_node)); - - if (i == num_of_removed_node) - { - arg_node = dummy_removed_node = dummy_clist_node; - } - - if (i == 0) - { - dummy_second_node = dummy_clist_node; - dummy_second_node->prev = dummy_head; - dummy_second_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_second_node; - } - else - { - dummy_next_to_last_node->next = dummy_clist_node; - dummy_clist_node->prev = dummy_next_to_last_node; - dummy_clist_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_clist_node; - } - } - - dummy_removed_node_prev = dummy_removed_node->prev; - dummy_removed_node_next = dummy_removed_node->next; + CF_CListNode_t node[3]; + CF_CListNode_t *head = NULL; - dummy_last_node.next = &dummy_head_node; - dummy_last_node.prev = dummy_next_to_last_node; - - (*arg_head)->next = dummy_second_node; - (*arg_head)->prev = &dummy_last_node; - - /* Act */ - CF_CList_Remove(arg_head, arg_node); - - /* Assert */ - UtAssert_ADDRESS_EQ(*arg_head, dummy_head); - UtAssert_ADDRESS_EQ(dummy_removed_node_prev->next, dummy_removed_node_next); - UtAssert_ADDRESS_EQ(dummy_removed_node_next->prev, dummy_removed_node_prev); - /* Assert for CF_CList_InitNode */ - UtAssert_ADDRESS_EQ(dummy_removed_node->prev, dummy_removed_node); - UtAssert_ADDRESS_EQ(dummy_removed_node->next, dummy_removed_node); - - /* removes all malloc nodes */ - CF_CListNode_t *free_up_node = dummy_head->next; - /* free removed node because it was malloc'd */ - free(dummy_removed_node); - /* subtract one because of node removal */ - for (i = 0; i < num_extraneous_nodes - 1; ++i) - { - CF_CListNode_t *old_free_up_node = free_up_node; - free_up_node = old_free_up_node->next; - free(old_free_up_node); - } - -} /* end Test_CF_ClistRemove_RemovingAnyNodeHasNodesPrevAndNextPointToEachOther */ - -/* end CF_CList_Remove tests */ + memset(node, 0, sizeof(node)); -/******************************************************************************* -** -** CF_CList_InsertAfter tests -** -*******************************************************************************/ + /* Already tested, so OK to use to initialize */ + CF_CList_InitNode(&node[0]); + CF_CList_InsertBack(&head, &node[0]); + CF_CList_InsertBack(&head, &node[1]); + CF_CList_InsertBack(&head, &node[2]); + + /* Pop leaves 2 nodes */ + UtAssert_ADDRESS_EQ(CF_CList_Pop(&head), &node[0]); + UtAssert_ADDRESS_EQ(head, &node[1]); + UtAssert_ADDRESS_EQ(node[1].next, &node[2]); + UtAssert_ADDRESS_EQ(node[1].prev, &node[2]); + UtAssert_ADDRESS_EQ(node[2].next, &node[1]); + UtAssert_ADDRESS_EQ(node[2].prev, &node[1]); + + /* Pop leaves 1 node */ + UtAssert_ADDRESS_EQ(CF_CList_Pop(&head), &node[1]); + UtAssert_ADDRESS_EQ(head, &node[2]); + UtAssert_ADDRESS_EQ(node[2].next, &node[2]); + UtAssert_ADDRESS_EQ(node[2].prev, &node[2]); + + /* Pop leaves empty list */ + UtAssert_ADDRESS_EQ(CF_CList_Pop(&head), &node[2]); + UtAssert_ADDRESS_EQ(head, NULL); + + /* Pop a NULL list */ + UtAssert_ADDRESS_EQ(CF_CList_Pop(&head), NULL); +} -void Test_CF_CList_InsertAfter_WhenOnlyOneNodeSuccess_after_IsInsertedAfter_start(void) +void Test_CF_CList_Remove(void) { - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t * arg_start = dummy_head; - CF_CListNode_t dummy_after_node; - CF_CListNode_t * arg_after = &dummy_after_node; - - dummy_head->prev = dummy_head; - dummy_head->next = dummy_head; + CF_CListNode_t node[3]; + CF_CListNode_t *head = NULL; - arg_after->prev = arg_after; - arg_after->next = arg_after; - - /* Act */ - CF_CList_InsertAfter(arg_head, arg_start, arg_after); + memset(node, 0, sizeof(node)); - /* Assert */ - UtAssert_ADDRESS_EQ(dummy_head->prev, arg_after); - UtAssert_ADDRESS_EQ(dummy_head->next, arg_after); - UtAssert_ADDRESS_EQ(arg_after->prev, dummy_head); - UtAssert_ADDRESS_EQ(arg_after->next, dummy_head); -} /* end Test_CF_CList_InsertAfter_WhenOnlyOneNodeSuccess_after_IsInsertedAfter_start */ + /* Already tested, so OK to use to initialize */ + CF_CList_InitNode(&node[0]); + CF_CList_InsertBack(&head, &node[0]); + CF_CList_InsertBack(&head, &node[1]); + CF_CList_InsertBack(&head, &node[2]); + + /* Note Pop tests exercise removing from the front, and null */ + + /* Remove from the middle */ + UtAssert_VOIDCALL(CF_CList_Remove(&head, &node[1])); + UtAssert_ADDRESS_EQ(head, &node[0]); + UtAssert_ADDRESS_EQ(node[0].next, &node[2]); + UtAssert_ADDRESS_EQ(node[0].prev, &node[2]); + UtAssert_ADDRESS_EQ(node[2].next, &node[0]); + UtAssert_ADDRESS_EQ(node[2].prev, &node[0]); + + /* Remove from the end */ + UtAssert_VOIDCALL(CF_CList_Remove(&head, &node[2])); + UtAssert_ADDRESS_EQ(head, &node[0]); + UtAssert_ADDRESS_EQ(node[0].next, &node[0]); + UtAssert_ADDRESS_EQ(node[0].prev, &node[0]); +} -void Test_CF_CList_InsertAfter_WhenAnyNodeSuccess_after_IsInsertedAfter_start(void) +void Test_CF_CList_InsertAfter(void) { - /* Arrange */ - /* Arrange */ - CF_CListNode_t dummy_head_node; - CF_CListNode_t * dummy_head = &dummy_head_node; - CF_CListNode_t **arg_head = &dummy_head; - CF_CListNode_t dummy_last_node; - CF_CListNode_t * dummy_second_node; - CF_CListNode_t * dummy_next_to_last_node; - CF_CListNode_t * arg_start; - CF_CListNode_t dummy_after; - CF_CListNode_t * arg_after = &dummy_after; - CF_CListNode_t * dummy_after_next; - uint8 num_extraneous_nodes = Any_uint8_LessThan(18) + 1; // 1 to 18 - uint8 insertion_point = Any_uint8_LessThan(num_extraneous_nodes); - int i = 0; - - dummy_second_node = NULL; - dummy_next_to_last_node = NULL; - - for (i = 0; i < num_extraneous_nodes; ++i) - { - CF_CListNode_t *dummy_clist_node = malloc(sizeof(*dummy_clist_node)); - - if (i == 0) - { - dummy_second_node = dummy_clist_node; - dummy_second_node->prev = dummy_head; - dummy_second_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_second_node; - } - else - { - dummy_next_to_last_node->next = dummy_clist_node; - dummy_clist_node->prev = dummy_next_to_last_node; - dummy_clist_node->next = &dummy_last_node; - dummy_next_to_last_node = dummy_clist_node; - } - } - - dummy_last_node.next = &dummy_head_node; - dummy_last_node.prev = dummy_next_to_last_node; - - (*arg_head)->next = dummy_second_node; - (*arg_head)->prev = &dummy_last_node; - - /* set insertion point */ - arg_start = dummy_head; - for (i = 0; i < insertion_point; ++i) - { - arg_start = arg_start->next; - } - dummy_after_next = arg_start->next; - - arg_after->prev = arg_after; - arg_after->next = arg_after; - - /* Act */ - CF_CList_InsertAfter(arg_head, arg_start, arg_after); - - /* Assert */ - UtAssert_ADDRESS_EQ(arg_after->next, dummy_after_next); - UtAssert_ADDRESS_EQ(arg_start->next, arg_after); - UtAssert_ADDRESS_EQ(arg_after->prev, arg_start); - UtAssert_ADDRESS_EQ(dummy_after_next->prev, arg_after); + CF_CListNode_t node[4]; + CF_CListNode_t *head = node; - /* removes all malloc nodes */ - CF_CListNode_t *free_up_node = dummy_head->next; - - for (i = 0; i < num_extraneous_nodes; ++i) - { - /* skip inserted node that was not malloc'd */ - if (i == insertion_point) - { - free_up_node = free_up_node->next; - } - - CF_CListNode_t *old_free_up_node = free_up_node; - free_up_node = old_free_up_node->next; - free(old_free_up_node); - } -} /* end Test_CF_CList_InsertAfter_WhenAnyNodeSuccess_after_IsInsertedAfter_start */ - -/* end CF_CList_InsertAfter tests */ + memset(node, 0, sizeof(node)); -/******************************************************************************* -** -** CF_CList_Traverse tests -** -*******************************************************************************/ + /* Already tested, so OK to use to initialize */ + CF_CList_InitNode(&node[0]); + + /* Insert to a single node list */ + UtAssert_VOIDCALL(CF_CList_InsertAfter(&head, &node[0], &node[1])); + UtAssert_ADDRESS_EQ(head, node); + UtAssert_ADDRESS_EQ(node[0].next, &node[1]); + UtAssert_ADDRESS_EQ(node[0].prev, &node[1]); + UtAssert_ADDRESS_EQ(node[1].next, &node[0]); + UtAssert_ADDRESS_EQ(node[1].prev, &node[0]); + + /* Insert at the end of a 2 node list to confirm head node gets updated correctly */ + UtAssert_VOIDCALL(CF_CList_InsertAfter(&head, &node[1], &node[2])); + UtAssert_ADDRESS_EQ(head, node); + UtAssert_ADDRESS_EQ(node[0].next, &node[1]); + UtAssert_ADDRESS_EQ(node[0].prev, &node[2]); + UtAssert_ADDRESS_EQ(node[1].next, &node[2]); + UtAssert_ADDRESS_EQ(node[1].prev, &node[0]); + UtAssert_ADDRESS_EQ(node[2].next, &node[0]); + UtAssert_ADDRESS_EQ(node[2].prev, &node[1]); + + /* Insert in the middle */ + UtAssert_VOIDCALL(CF_CList_InsertAfter(&head, &node[1], &node[3])); + UtAssert_ADDRESS_EQ(head, node); + UtAssert_ADDRESS_EQ(node[0].next, &node[1]); + UtAssert_ADDRESS_EQ(node[0].prev, &node[2]); + UtAssert_ADDRESS_EQ(node[1].next, &node[3]); + UtAssert_ADDRESS_EQ(node[1].prev, &node[0]); + UtAssert_ADDRESS_EQ(node[2].next, &node[0]); + UtAssert_ADDRESS_EQ(node[2].prev, &node[3]); + UtAssert_ADDRESS_EQ(node[3].next, &node[2]); + UtAssert_ADDRESS_EQ(node[3].prev, &node[1]); +} void Test_CF_CList_Traverse(void) { @@ -924,12 +278,6 @@ void Test_CF_CList_Traverse(void) UtAssert_INT32_EQ(context, -2); } -/******************************************************************************* -** -** CF_CList_Traverse_R tests -** -*******************************************************************************/ - void Test_CF_CList_Traverse_R(void) { CF_CListNode_t node[3]; @@ -975,104 +323,15 @@ void Test_CF_CList_Traverse_R(void) UtAssert_INT32_EQ(context, -2); } -/******************************************************************************* -** -** cf_clist_tests UtTest_Add groups -** -*******************************************************************************/ - -void add_CF_CList_InitNode_tests(void) -{ - UtTest_Add(Test_CF_CList_InitNode_PointNodeToItselfAsNextAndPrev, cf_clist_tests_Setup, cf_clist_tests_Teardown, - "Test_CF_CList_InitNode_PointNodeToItselfAsNextAndPrev"); -} /* end add_CF_CList_InitNode_tests */ - -void add_CF_CList_InsertFront_tests(void) -{ - UtTest_Add(Test_CF_CList_InsertFront_InsertNodeIntoEmptyList, cf_clist_tests_Setup, cf_clist_tests_Teardown, - "Test_CF_CList_InsertFront_InsertNodeIntoEmptyList"); - UtTest_Add(Test_CF_CList_InsertFront_WhenHeadIsOnlyNodeAndTheyPointToEachOtherInsertNode, cf_clist_tests_Setup, - cf_clist_tests_Teardown, - "Test_CF_CList_InsertFront_WhenHeadIsOnlyNodeAndTheyPointToEachOtherInsertNode"); - UtTest_Add(Test_CF_CList_InsertFront_WhenHeadIsOneOfTwoNodesAndTheyPointToCorrectNodesInsertNode, - cf_clist_tests_Setup, cf_clist_tests_Teardown, - "Test_CF_CList_InsertFront_WhenHeadIsOneOfTwoNodesAndTheyPointToCorrectNodesInsertNode"); - UtTest_Add(Test_CF_CList_InsertFront_WhenNodeListIsGreaterThanTwoNodesAndTheyPointToCorrectNodesInsertNode, - cf_clist_tests_Setup, cf_clist_tests_Teardown, - "Test_CF_CList_InsertFront_WhenNodeListIsGreaterThanTwoNodesAndTheyPointToCorrectNodesInsertNode"); -} /* end add_CF_CList_InsertFront_tests */ - -void add_CF_CList_InsertBack_tests(void) -{ - UtTest_Add(Test_CF_CList_InsertBack_InsertNodeIntoEmptyList, cf_clist_tests_Setup, cf_clist_tests_Teardown, - "Test_CF_CList_InsertBack_InsertNodeIntoEmptyList"); - UtTest_Add(Test_CF_CList_InsertBack_WhenHeadIsOnlyNodeAndTheyPointToEachOtherInsertNode, cf_clist_tests_Setup, - cf_clist_tests_Teardown, "Test_CF_CList_InsertBack_WhenHeadIsOnlyNodeAndTheyPointToEachOtherInsertNode"); - UtTest_Add(Test_CF_CList_InsertBack_WhenHeadIsOneOfTwoNodesAndTheyPointToCorrectNodesInsertNode, - cf_clist_tests_Setup, cf_clist_tests_Teardown, - "Test_CF_CList_InsertBack_WhenHeadIsOneOfTwoNodesAndTheyPointToCorrectNodesInsertNode"); - UtTest_Add(Test_CF_CList_InsertBack_WhenNodeListIsGreaterThanTwoNodesAndTheyPointToCorrectNodesInsertNode, - cf_clist_tests_Setup, cf_clist_tests_Teardown, - "Test_CF_CList_InsertBack_WhenNodeListIsGreaterThanTwoNodesAndTheyPointToCorrectNodesInsertNode"); -} /* end add_CF_CList_InsertBack_tests */ - -void add_CF_CList_Pop_tests(void) -{ - UtTest_Add(Test_CF_CList_Pop_WhenListIsEmptySuccessReturn_NULL, cf_clist_tests_Setup, cf_clist_tests_Teardown, - "Test_CF_CList_Pop_WhenListIsEmptySuccessReturn_NULL"); - UtTest_Add(Test_CF_CList_Pop_WhenItIsOnlyNodePopHeadNodeAndReturn_head_, cf_clist_tests_Setup, - cf_clist_tests_Teardown, "Test_CF_CList_Pop_WhenItIsOnlyNodePopHeadNodeAndReturn_head_"); - UtTest_Add(Test_CF_CList_Pop_WhenListIsAnySizeGreaterThanOneSuccessPopsHeadNodeAndReturns_head, - cf_clist_tests_Setup, cf_clist_tests_Teardown, - "Test_CF_CList_Pop_WhenListIsAnySizeGreaterThanOneSuccessPopsHeadNodeAndReturns_head"); -} /* end add_CF_CList_Pop_tests */ - -void add_CF_CList_Remove_tests(void) -{ - UtTest_Add(Test_CF_ClistRemove_WhenOnlyNodeSetHeadTo_NULL, cf_clist_tests_Setup, cf_clist_tests_Teardown, - "Test_CF_ClistRemove_WhenOnlyNodeSetHeadTo_NULL"); - UtTest_Add(Test_CF_ClistRemove_WhenOnlyTwoNodesAndLastIsRemovedSetHeadToPointToItself, cf_clist_tests_Setup, - cf_clist_tests_Teardown, "Test_CF_ClistRemove_WhenOnlyTwoNodesAndLastIsRemovedSetHeadToPointToItself"); - UtTest_Add(Test_CF_ClistRemove_RemovingHeadSetSecondNodeToHeadAndUpdateLastNode, cf_clist_tests_Setup, - cf_clist_tests_Teardown, "Test_CF_ClistRemove_RemovingHeadSetSecondNodeToHeadAndUpdateLastNode"); - UtTest_Add(Test_CF_ClistRemove_RemovingLastPointHeadAndNextToLastToEachOther, cf_clist_tests_Setup, - cf_clist_tests_Teardown, "Test_CF_ClistRemove_RemovingLastPointHeadAndNextToLastToEachOther"); - UtTest_Add(Test_CF_ClistRemove_RemovingAnyNodeHasNodesPrevAndNextPointToEachOther, cf_clist_tests_Setup, - cf_clist_tests_Teardown, "Test_CF_ClistRemove_RemovingAnyNodeHasNodesPrevAndNextPointToEachOther"); -} /* end add_CF_CList_Remove_tests */ - -void add_CF_CList_InsertAfter_tests(void) -{ - UtTest_Add(Test_CF_CList_InsertAfter_WhenOnlyOneNodeSuccess_after_IsInsertedAfter_start, cf_clist_tests_Setup, - cf_clist_tests_Teardown, "Test_CF_CList_InsertAfter_WhenOnlyOneNodeSuccess_after_IsInsertedAfter_start"); - UtTest_Add(Test_CF_CList_InsertAfter_WhenAnyNodeSuccess_after_IsInsertedAfter_start, cf_clist_tests_Setup, - cf_clist_tests_Teardown, "Test_CF_CList_InsertAfter_WhenAnyNodeSuccess_after_IsInsertedAfter_start"); -} /* end add_CF_CList_InsertAfter_tests */ - -/******************************************************************************* -** -** cf_clist_tests UtTest_Setup -** -*******************************************************************************/ - +/* Add tests */ void UtTest_Setup(void) { - TestUtil_InitializeRandomSeed(); - - add_CF_CList_InitNode_tests(); - - add_CF_CList_InsertFront_tests(); - - add_CF_CList_InsertBack_tests(); - - add_CF_CList_Pop_tests(); - - add_CF_CList_Remove_tests(); - - add_CF_CList_InsertAfter_tests(); - - UtTest_Add(Test_CF_CList_Traverse, cf_clist_tests_Setup, cf_clist_tests_Teardown, "Test_CF_CList_Traverse"); - UtTest_Add(Test_CF_CList_Traverse_R, cf_clist_tests_Setup, cf_clist_tests_Teardown, "Test_CF_CList_Traverse_R"); -} /* end UtTest_Setup for cf_clist_tests.c */ - -/* end cf_clist_tests.c */ + TEST_CF_ADD(Test_CF_CList_InitNode); + TEST_CF_ADD(Test_CF_CList_InsertFront); + TEST_CF_ADD(Test_CF_CList_InsertBack); + TEST_CF_ADD(Test_CF_CList_Pop); + TEST_CF_ADD(Test_CF_CList_Remove); + TEST_CF_ADD(Test_CF_CList_InsertAfter); + TEST_CF_ADD(Test_CF_CList_Traverse); + TEST_CF_ADD(Test_CF_CList_Traverse_R); +} diff --git a/unit-test/cf_crc_tests.c b/unit-test/cf_crc_tests.c index 7cc3fec8..ebed6dcb 100644 --- a/unit-test/cf_crc_tests.c +++ b/unit-test/cf_crc_tests.c @@ -20,528 +20,70 @@ /* cf testing includes */ #include "cf_test_utils.h" -/******************************************************************************* -** -** cf_crc_tests local utility functions -** -*******************************************************************************/ - -uint8 Any_cf_crc_index(void) -{ - uint8 possible_indexes[4] = {0, 1, 2, 3}; - - return Any_uint8_FromThese(possible_indexes, sizeof(possible_indexes) / sizeof(possible_indexes[0])); -} - -uint8 Any_cf_crc_index_except_3(void) -{ - uint8 possible_indexes[3] = {0, 1, 2}; - - return Any_uint8_FromThese(possible_indexes, sizeof(possible_indexes) / sizeof(possible_indexes[0])); -} -/* end cf_crc_tests local utility functions */ - -/******************************************************************************* -** -** cf_crc_tests Setup and Teardown -** -*******************************************************************************/ - -void cf_crc_tests_Setup(void) -{ - cf_tests_Setup(); -} /* end cf_crc_tests_Setup */ - -void cf_crc_tests_Teardown(void) -{ - cf_tests_Teardown(); -} /* end cf_crc_tests_Teardown */ - -/* end cf_crc_tests Setup and Teardown */ - -/******************************************************************************* -** -** CF_CRC_Start tests -** -*******************************************************************************/ -void test_CF_CRC_Start_ReinitializeGiven_c_ToAllZeroValues(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t *arg_c = &dummy_c; - - arg_c->working = Any_uint32_Except(0); - arg_c->result = Any_uint32_Except(0); - arg_c->index = Any_uint8_Except(0); - - /* Act */ - CF_CRC_Start(arg_c); - - /* Assert */ - UtAssert_ZERO(arg_c->working); - UtAssert_ZERO(arg_c->result); - UtAssert_ZERO(arg_c->index); - -} /* end test_CF_CRC_Start_ReinitializeGiven_c_ToAllZeroValues*/ - -/* end CF_CRC_Start tests */ - -/******************************************************************************* -** -** CF_CRC_Digest tests -** -*******************************************************************************/ - -/* NOTE: not testing len > data's size, by contract this should not occur, -** should it be an assumption in the doxygen comments? */ - -void Test_CF_CRC_Digest_When_len_Is_0_DoNotAlter_c_working_or_c_result_or_c_index(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t * arg_c = &dummy_c; - const uint8 arg_data[1] = {UT_UINT_8_DEFAULT}; - int32 arg_len = 0; - uint32 initial_c_working = Any_uint32(); - uint32 initial_c_result = Any_uint32(); - uint8 initial_c_index = Any_cf_crc_index(); - - arg_c->working = initial_c_working; - arg_c->result = initial_c_result; - arg_c->index = initial_c_index; - - /* Act */ - CF_CRC_Digest(arg_c, arg_data, arg_len); - - /* Assert */ - UtAssert_UINT32_EQ(arg_c->working, initial_c_working); - UtAssert_UINT32_EQ(arg_c->result, initial_c_result); - UtAssert_UINT32_EQ(arg_c->index, initial_c_index); - -} /* end Test_CF_CRC_Digest_When_len_Is_0_DoNotAlter_c_working_or_c_result_or_c_index */ - -void Test_CF_CRC_Digest_When_len_Eq_1_PushDataLeftOnto_c_working(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t * arg_c = &dummy_c; - const uint8 arg_data[1] = {UT_UINT_8_DEFAULT}; - int32 arg_len = 1; - int i = 0; - uint32 expected_c_working; - - arg_c->working = Any_uint32(); - arg_c->index = Any_cf_crc_index(); - - expected_c_working = arg_c->working; - expected_c_working <<= 8; - expected_c_working += arg_data[i]; - - /* Act */ - CF_CRC_Digest(arg_c, arg_data, arg_len); - - /* Assert */ - UtAssert_UINT32_EQ(arg_c->working, expected_c_working); - -} /* end Test_CF_CRC_Digest_When_len_Eq_1_PushDataLeftOnto_c_working */ - -void Test_CF_CRC_Digest_PushDataLeftOnto_c_working_NumberOfTimesEqTo_len(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t * arg_c = &dummy_c; - const uint8 *arg_data; - int32 arg_len; - int i = 0; - uint32 expected_c_working; - - arg_c->working = Any_uint32(); - arg_c->index = Any_cf_crc_index(); - arg_len = Any_uint16_GreaterThan(1) / - 2; // NOTE: change type if len changes, Any_uint16_GreaterThan(1) / 2 roughly translates to a positive - // int that runs the test within a reasonable timeframe for size of len, this could change when more is - // learned about what len is supposed to be - - arg_data = malloc((size_t)arg_len); - AnyBufferOf_uint8_WithSize((uint8 *)arg_data, arg_len); - - expected_c_working = arg_c->working; - for (i = 0; i < arg_len; ++i) - { - expected_c_working <<= 8; - expected_c_working |= arg_data[i]; - } - - /* Act */ - CF_CRC_Digest(arg_c, arg_data, arg_len); - - /* Assert */ - UtAssert_UINT32_EQ(arg_c->working, expected_c_working); - - /* local Teardown */ - free((uint8 *)arg_data); - -} /* end Test_CF_CRC_Digest_PushDataLeftOnto_c_working_NumberOfTimesEqTo_len */ - -void Test_CF_CRC_Digest_When_index_IsNot_3_DoNotUpdate_c_result(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t * arg_c = &dummy_c; - const uint8 arg_data[1] = {UT_UINT_8_DEFAULT}; - int32 arg_len = 1; - uint32 initial_c_result = Any_uint32(); - - arg_c->working = Any_uint32(); - arg_c->result = initial_c_result; - arg_c->index = Any_cf_crc_index_except_3(); - - /* Act */ - CF_CRC_Digest(arg_c, arg_data, arg_len); - - /* Assert */ - UtAssert_UINT32_EQ(arg_c->result, initial_c_result); - -} /* end Test_CF_CRC_Digest_When_index_IsNot_3_DoNotUpdate_c_result */ - -void Test_CF_CRC_Digest_When_c_index_Is_3_Update_c_result(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t * arg_c = &dummy_c; - const uint8 arg_data[1] = {UT_UINT_8_DEFAULT}; - int32 arg_len; - int i = 0; - uint32 expected_c_result; - - arg_c->working = Any_uint32(); - arg_c->index = 3; - arg_c->result = Any_uint32(); - arg_len = 1; - - expected_c_result = arg_c->working; - expected_c_result <<= 8; - expected_c_result += arg_data[i]; - expected_c_result += arg_c->result; - - /* Act */ - CF_CRC_Digest(arg_c, arg_data, arg_len); - - /* Assert */ - UtAssert_UINT32_EQ(arg_c->result, expected_c_result); - -} /* end Test_CF_CRC_Digest_When_c_index_Is_3_Update_c_result */ - -void Test_CF_CRC_Digest_Update_c_result_TheNumberOfTimes_index_Reaches4(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t * arg_c = &dummy_c; - uint8 * arg_data; - int32 arg_len; - uint32 dummy_c_working; - int i = 0; - unsigned int num_times_index_at_4; - uint32 expected_c_result; - - arg_c->working = Any_uint32(); - arg_c->index = Any_cf_crc_index(); - arg_c->result = Any_uint32(); - arg_len = (int32)(Any_uint16_GreaterThan(1) / - 2); // NOTE: change type if len changes, Any_uint16_GreaterThan(1) / 2 roughly translates to a - // positive int that runs the test within a reasonable timeframe for size of len, this could - // change when more is learned about what len is supposed to be - - arg_data = malloc((size_t)arg_len); - memset(arg_data, 0, arg_len); - - AnyBufferOf_uint8_WithSize(arg_data, arg_len); - - dummy_c_working = arg_c->working; - expected_c_result = arg_c->result; - - for (i = 0; i < (3 - arg_c->index); ++i) /* num shifts before first add */ - { - dummy_c_working <<= 8; - dummy_c_working += arg_data[i]; - } /* end for */ - - num_times_index_at_4 = (arg_len + arg_c->index) / 4; - - for (i = 0; i < num_times_index_at_4; ++i) - { - dummy_c_working <<= 8; - dummy_c_working += arg_data[(3 - arg_c->index) + (i * 4)]; - expected_c_result += dummy_c_working; - - if (i + 1 != num_times_index_at_4) - { - int j; - - for (j = 1; j < 4; ++j) - { - dummy_c_working <<= 8; - dummy_c_working += arg_data[(3 - arg_c->index) + (i * 4) + j]; - } /* end for */ - - } /* end if */ - - } /* end for */ - - /* Act */ - CF_CRC_Digest(arg_c, arg_data, arg_len); - - /* Assert */ - UtAssert_UINT32_EQ(arg_c->result, expected_c_result); - - /* local Teardown */ - free((uint8 *)arg_data); - -} /* end Test_CF_CRC_Digest_Update_c_result_TheNumberOfTimes_index_Reaches4 */ - -void Test_CF_CRC_Digest_When_len_Eq1_And_c_index_LessThan_3_Update_c_index_By_1(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t * arg_c = &dummy_c; - const uint8 arg_data[1] = {UT_UINT_8_DEFAULT}; - int32 arg_len; - uint8 expected_c_index; - - arg_c->working = Any_uint32(); - arg_c->index = Any_cf_crc_index_except_3(); - arg_c->result = Any_uint32(); - arg_len = 1; - - expected_c_index = arg_c->index + 1; - - /* Act */ - CF_CRC_Digest(arg_c, arg_data, arg_len); - - /* Assert */ - UtAssert_UINT32_EQ(arg_c->index, expected_c_index); - -} /* end Test_CF_CRC_Digest_When_len_Eq1_And_c_index_LessThan_3_Update_c_index_By_1 */ - -void Test_CF_CRC_Digest_When_len_Eq1_And_c_index_Is_3_Update_c_index_To_0(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t * arg_c = &dummy_c; - const uint8 arg_data[1] = {UT_UINT_8_DEFAULT}; - int32 arg_len; - - arg_c->working = Any_uint32(); - arg_c->index = 3; - arg_c->result = Any_uint32(); - arg_len = 1; - - /* Act */ - CF_CRC_Digest(arg_c, arg_data, arg_len); - - /* Assert */ - UtAssert_ZERO(arg_c->index); - -} /* end Test_CF_CRC_Digest_When_len_Eq1_And_c_index_Is_3_Update_c_index_To_0 */ - -void Test_CF_CRC_Digest_Update_c_index_CorrectlyDependingOn_c_index_And_len_Values(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t * arg_c = &dummy_c; - const uint8 *arg_data; - int32 arg_len; - uint8 expected_c_index; - - arg_c->working = Any_uint32(); - arg_c->index = Any_cf_crc_index(); - arg_c->result = Any_uint32(); - arg_len = (int32)(Any_uint16_GreaterThan(1) / - 2); // NOTE: change type if len changes, Any_uint16_GreaterThan(1) / 2 roughly translates to a - // positive int that runs the test within a reasonable timeframe for size of len, this could - // change when more is learned about what len is supposed to be - - arg_data = malloc((size_t)arg_len); - AnyBufferOf_uint8_WithSize((uint8 *)arg_data, arg_len); - - expected_c_index = (arg_len + arg_c->index) % 4; - - /* Act */ - CF_CRC_Digest(arg_c, arg_data, arg_len); - - /* Assert */ - UtAssert_UINT32_EQ(arg_c->index, expected_c_index); - - /* local Teardown */ - free((uint8 *)arg_data); - -} /* end Test_CF_CRC_Digest_Update_c_index_CorrectlyDependingOn_c_index_And_len_Values */ - -/* end CF_CRC_Digest tests */ - -/******************************************************************************* -** -** CF_CRC_Finalize tests -** -*******************************************************************************/ - -void Test_CF_CRC_Finalize_When_index_Is_0_DoNothing(void) +void Test_CF_CRC_Start(void) { /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t *arg_c = &dummy_c; - uint32 initial_c_working = Any_uint32(); - uint32 initial_c_result = Any_uint32(); + CF_Crc_t c; - arg_c->working = initial_c_working; - arg_c->result = initial_c_result; - arg_c->index = 0; + memset(&c, 0, sizeof(c)); /* Act */ - CF_CRC_Finalize(arg_c); + UtAssert_VOIDCALL(CF_CRC_Start(&c)); /* Assert */ - UtAssert_UINT32_EQ(arg_c->working, initial_c_working); - UtAssert_UINT32_EQ(arg_c->result, initial_c_result); - UtAssert_ZERO(arg_c->index); -} /* end Test_CF_CRC_Finalize_When_index_Is_0_DoNothing */ - -void Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_1(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t *arg_c = &dummy_c; - uint32 working_shift = 256 * 256 * 256; /* 3 left shifts */ - uint32 expected_result; - - arg_c->index = 1; - arg_c->working = Any_uint32(); - arg_c->result = Any_uint32(); - - expected_result = arg_c->result + (arg_c->working * working_shift); - - /* Act */ - CF_CRC_Finalize(arg_c); - - /* Assert */ - UtAssert_ZERO(arg_c->working); - UtAssert_UINT32_EQ(arg_c->result, expected_result); - UtAssert_ZERO(arg_c->index); -} /* end Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_1 */ - -void Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_2(void) -{ - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t *arg_c = &dummy_c; - uint8 dummy_index = 2; - uint32 working_shift = 256 * 256; /* 2 left shifts */ - uint32 expected_result; - - arg_c->index = dummy_index; - arg_c->working = Any_uint32(); - arg_c->result = Any_uint32(); - - expected_result = arg_c->result + (arg_c->working * working_shift); - - /* Act */ - CF_CRC_Finalize(arg_c); - - /* Assert */ - UtAssert_ZERO(arg_c->working); - UtAssert_UINT32_EQ(arg_c->result, expected_result); - UtAssert_ZERO(arg_c->index); -} /* end Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_2 */ + UtAssert_ZERO(c.working); + UtAssert_ZERO(c.result); + UtAssert_ZERO(c.index); +} -void Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_3(void) +void Test_CF_CRC_Digest(void) { - /* Arrange */ - CF_Crc_t dummy_c; - CF_Crc_t *arg_c = &dummy_c; - uint8 dummy_index = 3; - uint32 working_shift = 256; /* 1 left shift */ - uint32 expected_result; - - arg_c->index = dummy_index; - arg_c->working = Any_uint32(); - arg_c->result = Any_uint32(); - - expected_result = arg_c->result + (arg_c->working * working_shift); - - /* Act */ - CF_CRC_Finalize(arg_c); + CF_Crc_t c; + uint8 data[] = {1, 2, 3, 4, 5}; - /* Assert */ - UtAssert_ZERO(arg_c->working); - UtAssert_UINT32_EQ(arg_c->result, expected_result); - UtAssert_ZERO(arg_c->index); -} /* end Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_3 */ + /* Already tested, so OK to use */ + CF_CRC_Start(&c); -/* end CF_CRC_Finalize tests */ + /* Zero length should leave c as zeros */ + UtAssert_VOIDCALL(CF_CRC_Digest(&c, NULL, 0)); + UtAssert_ZERO(c.working); + UtAssert_ZERO(c.result); + UtAssert_ZERO(c.index); -/******************************************************************************* -** -** cf_crc_tests UtTest_Add groups -** -*******************************************************************************/ + /* Digest data and confirm */ + UtAssert_VOIDCALL(CF_CRC_Digest(&c, data, sizeof(data))); + UtAssert_UINT32_EQ(c.working, (data[1] << 24) + (data[2] << 16) + (data[3] << 8) + data[4]); + UtAssert_UINT32_EQ(c.result, (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); + UtAssert_UINT32_EQ(c.index, 1); +} -void add_CF_CRC_Start_tests(void) +void Test_CF_CRC_Finalize(void) { - UtTest_Add(test_CF_CRC_Start_ReinitializeGiven_c_ToAllZeroValues, cf_crc_tests_Setup, cf_crc_tests_Teardown, - "test_CF_CRC_Start_ReinitializeGiven_c_ToAllZeroValues"); -} /* end add_CF_CRC_Start_tests */ + CF_Crc_t c; + uint8 data[] = {1, 2, 3, 4, 5}; -void add_CF_CRC_Digest_tests(void) -{ - UtTest_Add(Test_CF_CRC_Digest_When_len_Is_0_DoNotAlter_c_working_or_c_result_or_c_index, cf_crc_tests_Setup, - cf_crc_tests_Teardown, "Test_CF_CRC_Digest_When_len_Is_0_DoNotAlter_c_working_or_c_result_or_c_index"); - UtTest_Add(Test_CF_CRC_Digest_When_len_Eq_1_PushDataLeftOnto_c_working, cf_crc_tests_Setup, cf_crc_tests_Teardown, - "Test_CF_CRC_Digest_When_len_Eq_1_PushDataLeftOnto_c_working"); - UtTest_Add(Test_CF_CRC_Digest_PushDataLeftOnto_c_working_NumberOfTimesEqTo_len, cf_crc_tests_Setup, - cf_crc_tests_Teardown, "Test_CF_CRC_Digest_PushDataLeftOnto_c_working_NumberOfTimesEqTo_len"); - UtTest_Add(Test_CF_CRC_Digest_When_index_IsNot_3_DoNotUpdate_c_result, cf_crc_tests_Setup, cf_crc_tests_Teardown, - "Test_CF_CRC_Digest_When_index_IsNot_3_DoNotUpdate_c_result"); - UtTest_Add(Test_CF_CRC_Digest_When_c_index_Is_3_Update_c_result, cf_crc_tests_Setup, cf_crc_tests_Teardown, - "Test_CF_CRC_Digest_When_c_index_Is_3_Update_c_result"); - UtTest_Add(Test_CF_CRC_Digest_Update_c_result_TheNumberOfTimes_index_Reaches4, cf_crc_tests_Setup, - cf_crc_tests_Teardown, "Test_CF_CRC_Digest_Update_c_result_TheNumberOfTimes_index_Reaches4"); - UtTest_Add(Test_CF_CRC_Digest_When_len_Eq1_And_c_index_LessThan_3_Update_c_index_By_1, cf_crc_tests_Setup, - cf_crc_tests_Teardown, "Test_CF_CRC_Digest_When_len_Eq1_And_c_index_LessThan_3_Update_c_index_By_1"); - UtTest_Add(Test_CF_CRC_Digest_When_len_Eq1_And_c_index_Is_3_Update_c_index_To_0, cf_crc_tests_Setup, - cf_crc_tests_Teardown, "Test_CF_CRC_Digest_When_len_Eq1_And_c_index_Is_3_Update_c_index_To_0"); - UtTest_Add(Test_CF_CRC_Digest_Update_c_index_CorrectlyDependingOn_c_index_And_len_Values, cf_crc_tests_Setup, - cf_crc_tests_Teardown, "Test_CF_CRC_Digest_Update_c_index_CorrectlyDependingOn_c_index_And_len_Values"); -} /* end add_CF_CRC_Digest_tests */ + /* Already tested, so OK to use */ + CF_CRC_Start(&c); -void add_CF_CRC_Finalize_tests(void) -{ - UtTest_Add(Test_CF_CRC_Finalize_When_index_Is_0_DoNothing, cf_crc_tests_Setup, cf_crc_tests_Teardown, - "Test_CF_CRC_Finalize_When_index_Is_0_DoNothing"); - UtTest_Add(Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_1, cf_crc_tests_Setup, cf_crc_tests_Teardown, - "Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_1"); - UtTest_Add(Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_2, cf_crc_tests_Setup, cf_crc_tests_Teardown, - "Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_2"); - UtTest_Add(Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_3, cf_crc_tests_Setup, cf_crc_tests_Teardown, - "Test_CF_CRC_Finalize_ReceiveExpectedResultAt_index_3"); -} /* end add_CF_CRC_Finalize_tests */ + /* Test with clear c */ + UtAssert_VOIDCALL(CF_CRC_Finalize(&c)); + UtAssert_ZERO(c.working); + UtAssert_ZERO(c.result); + UtAssert_ZERO(c.index); -/* end cf_crc_tests UtTest_Add groups */ + /* Already tested, so OK to use */ + CF_CRC_Digest(&c, data, sizeof(data)); -/******************************************************************************* -** -** cf_crc_tests UtTest_Setup -** -*******************************************************************************/ + /* Test with filled in c */ + UtAssert_VOIDCALL(CF_CRC_Finalize(&c)); + UtAssert_ZERO(c.working); + UtAssert_UINT32_EQ(c.result, ((data[0] + data[4]) << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); + UtAssert_ZERO(c.index); +} void UtTest_Setup(void) { - TestUtil_InitializeRandomSeed(); - - add_CF_CRC_Start_tests(); - - add_CF_CRC_Digest_tests(); - - add_CF_CRC_Finalize_tests(); -} /* end UtTest_Setup for cf_crc_tests.c */ - -/* end cf_crc_tests.c */ + TEST_CF_ADD(Test_CF_CRC_Start); + TEST_CF_ADD(Test_CF_CRC_Digest); + TEST_CF_ADD(Test_CF_CRC_Finalize); +} diff --git a/unit-test/utilities/cf_test_utils.h b/unit-test/utilities/cf_test_utils.h index d75cbfc3..ae871ef8 100644 --- a/unit-test/utilities/cf_test_utils.h +++ b/unit-test/utilities/cf_test_utils.h @@ -39,10 +39,6 @@ #include "utstubs.h" #include "utgenstub.h" -/* cf_tests constants */ -#define UT_INT_16_DEFAULT 0xBADD /* 0xBADD == 47837 decimal */ -#define UT_UINT_8_DEFAULT 0xED /* 0xED == 237 decimal */ - #define MAX_INT 2147484647 /* Set at 32bit for now, pow(2, 31) - 1 */ #define ALL_CHANNELS 255