/* * Automated test suite for doubly_linked_list.h * * Compile together with your implementation file, e.g.: * gcc test_doubly_linked_list.c doubly_linked_list.c -o test * ./test * * ASSUMPTIONS MADE (the header alone doesn't specify these — check/adjust * if your implementation uses different conventions): * 1. Position indexing for *_at_position / *_after_pos / *_before_pos is * 0-BASED (position 0 = head). * 2. delete_by_value / delete_at_position return 1 on success and * 0 on failure (see OP_SUCCESS / OP_FAILURE below — flip if needed). * 3. insert_between_nodes(head, value1, value2, x) inserts a NEW node * containing x between two ADJACENT existing nodes containing value1 * followed by value2 (i.e. value1 -> x -> value2). Adjust * test_insert_between_nodes() if your intended semantics differ * (e.g. value1/value2 not required to be adjacent). */ #include #include #include "doubly_linked_list.h" #define OP_SUCCESS 1 #define OP_FAILURE 0 static int total_tests = 0; static int passed_tests = 0; #define check(cond, msg) do { \ total_tests++; \ if (cond) { passed_tests++; printf(" [PASS] %s\n", msg); } \ else { printf(" [FAIL] %s\n", msg); } \ } while (0) #define check_int(expected, actual, msg) do { \ total_tests++; \ int _exp = (int)(expected); int _act = (int)(actual); \ if (_exp == _act) { passed_tests++; printf(" [PASS] %s\n", msg); } \ else { printf(" [FAIL] %s (expected %d, got %d)\n", msg, _exp, _act); } \ } while (0) /* ---------- helpers ---------- */ static int to_array(LIST head, element_type *arr, int max_len) { int n = 0; NODE *cur = head; while (cur != NULL && n < max_len) { arr[n++] = cur->data; cur = cur->next; } return n; } static NODE *get_tail(LIST head) { if (head == NULL) return NULL; NODE *cur = head; while (cur->next != NULL) cur = cur->next; return cur; } /* Walks the list forward checking that head->prev == NULL, that every node's next->prev points back to itself, and that the last node's next == NULL. Returns 1 if all links are consistent. */ static int verify_double_links(LIST head) { if (head == NULL) return 1; if (head->prev != NULL) return 0; NODE *cur = head; while (cur->next != NULL) { if (cur->next->prev != cur) return 0; cur = cur->next; } return (cur->next == NULL); } /* ---------- tests ---------- */ static void test_create_node(void) { printf("\n== create_node ==\n"); NODE *n = create_node(42); check(n != NULL, "create_node returns non-NULL pointer"); if (n != NULL) { check_int(42, n->data, "create_node stores correct data"); check(n->prev == NULL, "create_node sets prev to NULL"); check(n->next == NULL, "create_node sets next to NULL"); free(n); } } static void test_insert_at_head(void) { printf("\n== insert_at_head ==\n"); LIST head = NULL; insert_at_head(&head, 10); check(head != NULL, "list non-empty after first insert_at_head"); check_int(10, head ? head->data : -1, "first node has correct data"); check(head && head->prev == NULL, "head->prev is NULL"); insert_at_head(&head, 20); insert_at_head(&head, 30); /* expect order: 30 20 10 */ element_type arr[10]; int n = to_array(head, arr, 10); check_int(3, n, "count after 3 insert_at_head calls"); if (n == 3) { check_int(30, arr[0], "most recent insert is at front (30)"); check_int(20, arr[1], "second element is 20"); check_int(10, arr[2], "third element is 10"); } check(verify_double_links(head), "prev/next links consistent after insert_at_head"); free_list(&head); } static void test_insert_at_tail(void) { printf("\n== insert_at_tail ==\n"); LIST head = NULL; insert_at_tail(&head, 10); check(head != NULL, "list non-empty after first insert_at_tail"); check_int(10, head ? head->data : -1, "first node has correct data"); insert_at_tail(&head, 20); insert_at_tail(&head, 30); /* expect order: 10 20 30 */ element_type arr[10]; int n = to_array(head, arr, 10); check_int(3, n, "count after 3 insert_at_tail calls"); if (n == 3) { check_int(10, arr[0], "first inserted stays at front (10)"); check_int(20, arr[1], "second element is 20"); check_int(30, arr[2], "most recent insert is at back (30)"); } NODE *tail = get_tail(head); check(tail && tail->next == NULL, "tail->next is NULL"); check(verify_double_links(head), "prev/next links consistent after insert_at_tail"); free_list(&head); } static void test_insert_at_position(void) { printf("\n== insert_at_position ==\n"); printf(" (assumes 0-based position indexing)\n"); LIST head = NULL; insert_at_position(&head, 10, 0); /* [10] */ check_int(1, count(&head), "count after inserting into empty list at position 0"); insert_at_position(&head, 30, 1); /* [10, 30] */ insert_at_position(&head, 20, 1); /* [10, 20, 30] */ element_type arr[10]; int n = to_array(head, arr, 10); check_int(3, n, "count after 3 insert_at_position calls"); if (n == 3) { check_int(10, arr[0], "position 0 has value 10"); check_int(20, arr[1], "position 1 has value 20"); check_int(30, arr[2], "position 2 has value 30"); } check(verify_double_links(head), "prev/next links consistent after insert_at_position"); free_list(&head); } static void test_count(void) { printf("\n== count ==\n"); LIST head = NULL; check_int(0, count(&head), "count of empty list is 0"); insert_at_tail(&head, 1); insert_at_tail(&head, 2); insert_at_tail(&head, 3); check_int(3, count(&head), "count after 3 inserts"); free_list(&head); } static void test_delete_by_value(void) { printf("\n== delete_by_value ==\n"); printf(" (assumes success=1, failure/not-found=0)\n"); LIST head = NULL; insert_at_tail(&head, 10); insert_at_tail(&head, 20); insert_at_tail(&head, 30); insert_at_tail(&head, 40); /* list: 10 20 30 40 */ check_int(OP_SUCCESS, delete_by_value(&head, 30), "delete middle value 30 succeeds"); check_int(3, count(&head), "count decreases after deleting middle value"); check_int(OP_SUCCESS, delete_by_value(&head, 10), "delete head value 10 succeeds"); check(head && head->data == 20, "new head is 20 after deleting old head"); check(head && head->prev == NULL, "new head prev is NULL"); check_int(OP_SUCCESS, delete_by_value(&head, 40), "delete tail value 40 succeeds"); NODE *tail = get_tail(head); check(tail && tail->data == 20, "new tail is 20 after deleting old tail"); check(tail && tail->next == NULL, "new tail next is NULL"); check_int(OP_FAILURE, delete_by_value(&head, 999), "deleting nonexistent value fails"); check(verify_double_links(head), "prev/next links consistent after deletions"); free_list(&head); LIST empty = NULL; check_int(OP_FAILURE, delete_by_value(&empty, 5), "delete from empty list fails safely"); } static void test_delete_at_position(void) { printf("\n== delete_at_position ==\n"); printf(" (assumes 0-based position indexing, success=1/failure=0)\n"); LIST head = NULL; insert_at_tail(&head, 10); insert_at_tail(&head, 20); insert_at_tail(&head, 30); insert_at_tail(&head, 40); /* list: 10 20 30 40 */ check_int(OP_SUCCESS, delete_at_position(&head, 1), "delete at position 1 (value 20) succeeds"); element_type arr[10]; int n = to_array(head, arr, 10); check_int(3, n, "count after deleting position 1"); if (n == 3) { check_int(10, arr[0], "position 0 unchanged (10)"); check_int(30, arr[1], "position 1 now holds old position-2 value (30)"); check_int(40, arr[2], "position 2 now holds old position-3 value (40)"); } check_int(OP_SUCCESS, delete_at_position(&head, 0), "delete at position 0 (head) succeeds"); check(head && head->data == 30, "head updated correctly after deleting position 0"); int last_pos = count(&head) - 1; check_int(OP_SUCCESS, delete_at_position(&head, last_pos), "delete at last position succeeds"); check_int(OP_FAILURE, delete_at_position(&head, 99), "delete at out-of-range position fails"); check(verify_double_links(head), "prev/next links consistent after deletions"); free_list(&head); } static void test_insert_between_nodes(void) { printf("\n== insert_between_nodes ==\n"); printf(" ASSUMPTION: insert_between_nodes(head, value1, value2, x)\n"); printf(" inserts a node containing x between two ADJACENT existing\n"); printf(" nodes containing value1 followed by value2. Adjust this test\n"); printf(" if your intended semantics differ.\n"); LIST head = NULL; insert_at_tail(&head, 10); insert_at_tail(&head, 30); /* list: 10 30 */ insert_between_nodes(&head, 10, 30, 20); /* expected: 10 20 30 */ element_type arr[10]; int n = to_array(head, arr, 10); check_int(3, n, "count after insert_between_nodes"); if (n == 3) { check_int(10, arr[0], "position 0 unchanged (10)"); check_int(20, arr[1], "new value x inserted between value1 and value2 (20)"); check_int(30, arr[2], "position 2 unchanged (30)"); } check(verify_double_links(head), "prev/next links consistent after insert_between_nodes"); /* Also verify with a longer list, inserting between a non-head/non-tail pair */ LIST head2 = NULL; insert_at_tail(&head2, 1); insert_at_tail(&head2, 2); insert_at_tail(&head2, 4); insert_at_tail(&head2, 5); /* list: 1 2 4 5 */ insert_between_nodes(&head2, 2, 4, 3); /* expected: 1 2 3 4 5 */ element_type arr2[10]; int n2 = to_array(head2, arr2, 10); check_int(5, n2, "count after insert_between_nodes in a longer list"); if (n2 == 5) { check_int(1, arr2[0], "unaffected element unchanged (1)"); check_int(2, arr2[1], "value1 unchanged (2)"); check_int(3, arr2[2], "x correctly inserted between value1 and value2 (3)"); check_int(4, arr2[3], "value2 unchanged (4)"); check_int(5, arr2[4], "unaffected element unchanged (5)"); } check(verify_double_links(head2), "prev/next links consistent after insert_between_nodes in longer list"); free_list(&head); free_list(&head2); } static void test_insert_after_pos(void) { printf("\n== insert_after_pos ==\n"); printf(" (assumes 0-based position indexing)\n"); LIST head = NULL; insert_at_tail(&head, 10); insert_at_tail(&head, 30); /* list: 10 30 */ insert_after_pos(&head, 20, 0); /* insert after position 0 -> 10 20 30 */ element_type arr[10]; int n = to_array(head, arr, 10); check_int(3, n, "count after insert_after_pos"); if (n == 3) { check_int(10, arr[0], "position 0 unchanged (10)"); check_int(20, arr[1], "value inserted right after position 0 (20)"); check_int(30, arr[2], "position 2 unchanged (30)"); } insert_after_pos(&head, 40, 2); /* insert after last position -> append at tail */ n = to_array(head, arr, 10); check_int(4, n, "count after inserting after last position"); if (n == 4) check_int(40, arr[3], "value appended after last position becomes new tail"); check(verify_double_links(head), "prev/next links consistent after insert_after_pos"); free_list(&head); } static void test_insert_before_pos(void) { printf("\n== insert_before_pos ==\n"); printf(" (assumes 0-based position indexing)\n"); LIST head = NULL; insert_at_tail(&head, 10); insert_at_tail(&head, 30); /* list: 10 30 */ insert_before_pos(&head, 20, 1); /* insert before position 1 -> 10 20 30 */ element_type arr[10]; int n = to_array(head, arr, 10); check_int(3, n, "count after insert_before_pos"); if (n == 3) { check_int(10, arr[0], "position 0 unchanged (10)"); check_int(20, arr[1], "value inserted before old position 1 (20)"); check_int(30, arr[2], "position 2 unchanged (30)"); } insert_before_pos(&head, 5, 0); /* insert before head -> new head */ n = to_array(head, arr, 10); check_int(4, n, "count after inserting before position 0"); if (n == 4) { check_int(5, arr[0], "new head after insert_before_pos at 0 (5)"); check(head && head->prev == NULL, "new head prev is NULL"); } check(verify_double_links(head), "prev/next links consistent after insert_before_pos"); free_list(&head); } static void test_free_list(void) { printf("\n== free_list ==\n"); LIST head = NULL; insert_at_tail(&head, 1); insert_at_tail(&head, 2); insert_at_tail(&head, 3); free_list(&head); check(head == NULL, "free_list sets head pointer to NULL"); } static void test_mixed_operations_integrity(void) { printf("\n== mixed operations integrity check ==\n"); LIST head = NULL; insert_at_head(&head, 5); /* [5] */ insert_at_tail(&head, 15); /* [5, 15] */ insert_at_position(&head, 10, 1); /* [5, 10, 15] */ insert_after_pos(&head, 12, 1); /* [5, 10, 12, 15] */ delete_by_value(&head, 12); /* [5, 10, 15] */ insert_before_pos(&head, 3, 0); /* [3, 5, 10, 15] */ delete_at_position(&head, 2); /* [3, 5, 15] */ element_type arr[10]; int n = to_array(head, arr, 10); check_int(3, n, "final length correct after mixed operations"); if (n == 3) { check_int(3, arr[0], "final position 0 correct (3)"); check_int(5, arr[1], "final position 1 correct (5)"); check_int(15, arr[2], "final position 2 correct (15)"); } check(verify_double_links(head), "prev/next links remain consistent after mixed operations"); free_list(&head); check(head == NULL, "list correctly freed after mixed operations"); } /* ---------- main ---------- */ int main(void) { printf("=====================================\n"); printf(" Doubly Linked List - Automated Tests\n"); printf("=====================================\n"); test_create_node(); test_insert_at_head(); test_insert_at_tail(); test_insert_at_position(); test_count(); test_delete_by_value(); test_delete_at_position(); test_insert_between_nodes(); test_insert_after_pos(); test_insert_before_pos(); test_free_list(); test_mixed_operations_integrity(); printf("\n=====================================\n"); printf(" RESULT: %d / %d tests passed\n", passed_tests, total_tests); printf("=====================================\n"); return (passed_tests == total_tests) ? 0 : 1; }