void test_List_addLast_should_add_last_third_element(){ linkedList *list; // initialize the list list = create_linkedList(); //Test fixture Element elemArray1 = {.next = NULL, .data = 2}; Element elemArray0 = {.next = NULL, .data = 1}; Element elemArrayA = {.next = NULL, .data = 4}; Element elemArrayB = {.next = NULL, .data = 7}; // list->head = &elemArray0; // list->tail = &elemArray0; // list->length = 1; List_addLast(list,&elemArray0); TEST_ASSERT_NULL(elemArray0.next); TEST_ASSERT_EQUAL_PTR(&elemArray0,list->head); TEST_ASSERT_EQUAL_PTR(&elemArray0,list->tail); TEST_ASSERT_EQUAL(1,elemArray0.data); TEST_ASSERT_EQUAL(1,list->length); List_addLast(list,&elemArray1); TEST_ASSERT_NULL(elemArray1.next); TEST_ASSERT_EQUAL_PTR(&elemArray0,list->head); TEST_ASSERT_EQUAL_PTR(&elemArray1,list->tail); TEST_ASSERT_EQUAL(1,elemArray0.data); TEST_ASSERT_EQUAL(2,elemArray1.data); TEST_ASSERT_EQUAL(2,list->length); List_addLast(list,&elemArrayA); TEST_ASSERT_NULL(elemArrayA.next); TEST_ASSERT_EQUAL_PTR(&elemArray0,list->head); TEST_ASSERT_EQUAL_PTR(&elemArrayA,list->tail); TEST_ASSERT_EQUAL(1,elemArray0.data); TEST_ASSERT_EQUAL(2,elemArray1.data); TEST_ASSERT_EQUAL(3,list->length); List_addLast(list,&elemArrayB); TEST_ASSERT_EQUAL(4,list->length); List_removeMiddle(list,&elemArrayA,&elemArray1); TEST_ASSERT_NULL(elemArrayB.next); // TEST_ASSERT_EQUAL_PTR(&elemArray0,list->head); TEST_ASSERT_EQUAL(&elemArrayB,elemArray1.next); // TEST_ASSERT_EQUAL(2,list->length); // TEST_ASSERT_EQUAL_PTR(&elemArray0,list->tail->next); // TEST_ASSERT_EQUAL_PTR(&elemArrayA,list->tail); }
int main(int argc, char **argv) { List_t l = List_new(); fprintf(stdout, "test List...\n"); int i; for (i = 0; i < 100; i++) { List_addFirst(l, i); } fprintf(stdout, "List_getFirst\n"); int r = (int) List_getFirst(l); assert(r == 99); fprintf(stdout, "List_getIndexOf\n"); for (i = 0; i < 100; i++) { r = (int) List_getIndexOf(l, i); assert(r == (99 - i)); } fprintf(stdout, "List_addLast\n"); List_addLast(l, 200); r = (int) List_getIndexOf(l, 100); assert(r == 200); fprintf(stdout, "List_size\n"); r = List_size(l); assert(r == 101); fprintf(stdout, "List_isEmpty\n"); r = List_isEmpty(l); assert(r == 0); List_t l2 = List_new(); r = List_isEmpty(l2); assert(r == 1); fprintf(stdout, "List_remove\n"); for (i = 0; i < 100; i++) { r = (int) List_removeFirst(l); assert(r == (99 - i)); } r = (int) List_removeFirst(l); assert(r == 200); r = List_isEmpty(l); assert(r == 1); return 0; }
void test_add_last_with_head_and_tail_is_NULL(){ linkedList list; Element elementOne = {.next = NULL , .data = 10}; initLinkedList(&list); List_addLast(&list,&elementOne); TEST_ASSERT_EQUAL(1,list.length); TEST_ASSERT_EQUAL(10,elementOne.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementOne,list.tail); } void test_add_last_with_head_is_point_to_elementOne_and_tail_point_to_elementTwo(){ linkedList list; Element elementOne = {.next = NULL , .data = 10}; Element elementTwo = {.next = NULL , .data = 20}; initLinkedList(&list); List_addLast(&list,&elementOne); TEST_ASSERT_EQUAL(1,list.length); TEST_ASSERT_EQUAL(10,elementOne.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementOne,list.tail); List_addLast(&list,&elementTwo); TEST_ASSERT_EQUAL(2,list.length); TEST_ASSERT_EQUAL(20,elementTwo.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementTwo,list.tail); } void test_add_last_with_head_is_point_to_elementOne_and_tail_point_to_elementTThree(){ linkedList list; Element elementOne = {.next = NULL , .data = 10}; Element elementTwo = {.next = NULL , .data = 20}; Element elementThree = {.next = NULL , .data = 70}; initLinkedList(&list); List_addLast(&list,&elementOne); TEST_ASSERT_EQUAL(1,list.length); TEST_ASSERT_EQUAL(10,elementOne.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementOne,list.tail); TEST_ASSERT_EQUAL(NULL,elementOne.next); List_addLast(&list,&elementTwo); TEST_ASSERT_EQUAL(2,list.length); TEST_ASSERT_EQUAL(20,elementTwo.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementTwo,list.tail); TEST_ASSERT_EQUAL(&elementTwo,elementOne.next); TEST_ASSERT_EQUAL(NULL,elementTwo.next); List_addLast(&list,&elementThree); TEST_ASSERT_EQUAL(3,list.length); TEST_ASSERT_EQUAL(70,elementThree.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementThree,list.tail); TEST_ASSERT_EQUAL(&elementTwo,elementOne.next); TEST_ASSERT_EQUAL(&elementThree,elementTwo.next); TEST_ASSERT_EQUAL(NULL,elementThree.next); } void test_remove_first_with_head_is_point_to_elementOne_and_tail_point_to_elementThree(){ linkedList list; Element elementOne = {.next = NULL , .data = 10}; Element elementTwo = {.next = NULL , .data = 20}; Element elementThree = {.next = NULL , .data = 70}; initLinkedList(&list); List_addLast(&list,&elementOne); TEST_ASSERT_EQUAL(1,list.length); TEST_ASSERT_EQUAL(10,elementOne.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementOne,list.tail); TEST_ASSERT_EQUAL(NULL,elementOne.next); List_addLast(&list,&elementTwo); TEST_ASSERT_EQUAL(2,list.length); TEST_ASSERT_EQUAL(20,elementTwo.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementTwo,list.tail); TEST_ASSERT_EQUAL(&elementTwo,elementOne.next); TEST_ASSERT_EQUAL(NULL,elementTwo.next); List_addLast(&list,&elementThree); TEST_ASSERT_EQUAL(3,list.length); TEST_ASSERT_EQUAL(70,elementThree.data); TEST_ASSERT_EQUAL(&elementOne,list.head); TEST_ASSERT_EQUAL(&elementThree,list.tail); TEST_ASSERT_EQUAL(&elementTwo,elementOne.next); TEST_ASSERT_EQUAL(&elementThree,elementTwo.next); TEST_ASSERT_EQUAL(NULL,elementThree.next); List_removeFirst(&list); TEST_ASSERT_EQUAL(2,list.length); TEST_ASSERT_EQUAL(10,elementOne.data); TEST_ASSERT_EQUAL(&elementTwo,list.head); TEST_ASSERT_EQUAL(&elementThree,list.tail); TEST_ASSERT_EQUAL(&elementTwo,elementOne.next); List_removeFirst(&list); TEST_ASSERT_EQUAL(1,list.length); TEST_ASSERT_EQUAL(20,elementTwo.data); TEST_ASSERT_EQUAL(&elementThree,list.head); TEST_ASSERT_EQUAL(&elementThree,list.tail); List_removeFirst(&list); TEST_ASSERT_EQUAL(0,list.length); TEST_ASSERT_EQUAL(70,elementThree.data); TEST_ASSERT_EQUAL(NULL,list.head); TEST_ASSERT_EQUAL(NULL,list.tail); }
void Stack_push(Stack stack, void* data) { List_addLast(stack, data); }
void test_List_addLast_should_add_last_third_element(){ linkedList *list; list = create_linkedList(); Element elemArray1 = {.next = ((void *)0), .data = 2}; Element elemArray0 = {.next = ((void *)0), .data = 1}; Element elemArrayA = {.next = ((void *)0), .data = 4}; Element elemArrayB = {.next = ((void *)0), .data = 7}; List_addLast(list,&elemArray0); if ((((elemArray0.next)) == ((void *)0))) {} else {UnityFail( (" Expected NULL"), (_U_UINT)(_U_UINT)(_U_UINT)91);;}; UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArray0)), (_U_SINT)(_UP)((list->head)), (((void *)0)), (_U_UINT)92, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArray0)), (_U_SINT)(_UP)((list->tail)), (((void *)0)), (_U_UINT)93, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)((1)), (_U_SINT)((elemArray0.data)), (((void *)0)), (_U_UINT)94, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((1)), (_U_SINT)((list->length)), (((void *)0)), (_U_UINT)95, UNITY_DISPLAY_STYLE_INT); List_addLast(list,&elemArray1); if ((((elemArray1.next)) == ((void *)0))) {} else {UnityFail( (" Expected NULL"), (_U_UINT)(_U_UINT)(_U_UINT)98);;}; UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArray0)), (_U_SINT)(_UP)((list->head)), (((void *)0)), (_U_UINT)99, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArray1)), (_U_SINT)(_UP)((list->tail)), (((void *)0)), (_U_UINT)100, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)((1)), (_U_SINT)((elemArray0.data)), (((void *)0)), (_U_UINT)101, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((2)), (_U_SINT)((elemArray1.data)), (((void *)0)), (_U_UINT)102, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((2)), (_U_SINT)((list->length)), (((void *)0)), (_U_UINT)103, UNITY_DISPLAY_STYLE_INT); List_addFirst(list,&elemArrayA); if ((((elemArray1.next)) == ((void *)0))) {} else {UnityFail( (" Expected NULL"), (_U_UINT)(_U_UINT)(_U_UINT)106);;}; UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArrayA)), (_U_SINT)(_UP)((list->head)), (((void *)0)), (_U_UINT)107, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArray0)), (_U_SINT)(_UP)((elemArrayA.next)), (((void *)0)), (_U_UINT)108, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArray1)), (_U_SINT)(_UP)((list->tail)), (((void *)0)), (_U_UINT)109, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)((1)), (_U_SINT)((elemArray0.data)), (((void *)0)), (_U_UINT)110, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((2)), (_U_SINT)((elemArray1.data)), (((void *)0)), (_U_UINT)111, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((4)), (_U_SINT)((elemArrayA.data)), (((void *)0)), (_U_UINT)112, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((3)), (_U_SINT)((list->length)), (((void *)0)), (_U_UINT)113, UNITY_DISPLAY_STYLE_INT); List_addFirst(list,&elemArrayB); if ((((elemArray1.next)) == ((void *)0))) {} else {UnityFail( (" Expected NULL"), (_U_UINT)(_U_UINT)(_U_UINT)116);;}; UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArrayB)), (_U_SINT)(_UP)((list->head)), (((void *)0)), (_U_UINT)117, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArray1)), (_U_SINT)(_UP)((list->tail)), (((void *)0)), (_U_UINT)118, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)((1)), (_U_SINT)((elemArray0.data)), (((void *)0)), (_U_UINT)119, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((2)), (_U_SINT)((elemArray1.data)), (((void *)0)), (_U_UINT)120, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((4)), (_U_SINT)((elemArrayA.data)), (((void *)0)), (_U_UINT)121, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((7)), (_U_SINT)((elemArrayB.data)), (((void *)0)), (_U_UINT)122, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((4)), (_U_SINT)((list->length)), (((void *)0)), (_U_UINT)123, UNITY_DISPLAY_STYLE_INT); List_removeLast(list); if ((((elemArray1.next)) == ((void *)0))) {} else {UnityFail( (" Expected NULL"), (_U_UINT)(_U_UINT)(_U_UINT)126);;}; UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArrayB)), (_U_SINT)(_UP)((list->head)), (((void *)0)), (_U_UINT)127, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArray0)), (_U_SINT)(_UP)((list->tail)), (((void *)0)), (_U_UINT)128, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)((1)), (_U_SINT)((elemArray0.data)), (((void *)0)), (_U_UINT)129, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((4)), (_U_SINT)((elemArrayA.data)), (((void *)0)), (_U_UINT)130, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((7)), (_U_SINT)((elemArrayB.data)), (((void *)0)), (_U_UINT)131, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((3)), (_U_SINT)((list->length)), (((void *)0)), (_U_UINT)132, UNITY_DISPLAY_STYLE_INT); List_removeFirst(list); if ((((elemArray1.next)) == ((void *)0))) {} else {UnityFail( (" Expected NULL"), (_U_UINT)(_U_UINT)(_U_UINT)135);;}; UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArrayA)), (_U_SINT)(_UP)((list->head)), (((void *)0)), (_U_UINT)136, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)(_UP)((&elemArray0)), (_U_SINT)(_UP)((list->tail)), (((void *)0)), (_U_UINT)137, UNITY_DISPLAY_STYLE_HEX32); UnityAssertEqualNumber((_U_SINT)((1)), (_U_SINT)((elemArray0.data)), (((void *)0)), (_U_UINT)138, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((4)), (_U_SINT)((elemArrayA.data)), (((void *)0)), (_U_UINT)139, UNITY_DISPLAY_STYLE_INT); UnityAssertEqualNumber((_U_SINT)((2)), (_U_SINT)((list->length)), (((void *)0)), (_U_UINT)140, UNITY_DISPLAY_STYLE_INT); }