/* * Extended validation tests for: * create_node, insert_at_head, insert_at_tail, insert_at_position, * delete_by_value, delete_at_position * * This extends the original test_suite.c with boundary conditions: * empty lists, single-element lists, head/tail deletions, duplicate * values, and out-of-range positions (negative and too-large). * insert_at_pos and delete_at_pos -return type int -1 success * else 0 * Build: * gcc -Wall -Wextra -std=c99 -o test_suite_extended test_suite_extended.c linked_list.c * Run: * ./test_suite_extended */ #include #include #include "linked_list.h" int pass_count = 0; int fail_count = 0; /* Prints the list as: 10 20 30 */ void print_list_values(LIST head) { NODE *curr = head; while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } printf("\n"); } /* Checks the list matches the expected values exactly (same length, same * order). Prints PASS/FAIL, and on FAIL shows what was expected vs actual. * expected_len == 0 checks that the list is empty (head == NULL). */ void check(const char *test_name, LIST head, int expected[], int expected_len) { NODE *curr = head; int i = 0; while (curr != NULL && i < expected_len) { if (curr->data != expected[i]) { printf("[FAIL] %s\n", test_name); printf(" Expected: "); for (int j = 0; j < expected_len; j++) printf("%d ", expected[j]); printf("\n Actual: "); print_list_values(head); fail_count++; return; } curr = curr->next; i++; } /* check lengths match */ if (i != expected_len || curr != NULL) { printf("[FAIL] %s\n", test_name); printf(" Expected: "); for (int j = 0; j < expected_len; j++) printf("%d ", expected[j]); printf("\n Actual: "); print_list_values(head); fail_count++; return; } printf("[PASS] %s\n", test_name); pass_count++; } /* Checks a plain integer return value (e.g. from insert/delete functions) */ void check_int(const char *test_name, int actual, int expected) { if (actual == expected) { printf("[PASS] %s\n", test_name); pass_count++; } else { printf("[FAIL] %s (expected %d, got %d)\n", test_name, expected, actual); fail_count++; } } /* Checks that a pointer is exactly NULL (used to confirm a list is empty) */ void check_null(const char *test_name, LIST head) { check_int(test_name, (head == NULL), 1); } int main(void) { /* ================= create_node ================= */ NODE *n1 = create_node(42); check_int("create_node: stores correct value", n1->data, 42); check_int("create_node: next is NULL", (n1->next == NULL), 1); free(n1); NODE *n2 = create_node(-7); check_int("create_node: stores a negative value correctly", n2->data, -7); free(n2); NODE *n3 = create_node(0); check_int("create_node: stores zero correctly", n3->data, 0); free(n3); /* ================= insert_at_head ================= */ LIST head1 = NULL; insert_at_head(&head1, 10); insert_at_head(&head1, 20); insert_at_head(&head1, 30); int expected1[] = {30, 20, 10}; check("insert_at_head: builds list in reverse order", head1, expected1, 3); free_list(&head1); LIST head1b = NULL; insert_at_head(&head1b, 99); int expected1b[] = {99}; check("insert_at_head: single insert into empty list", head1b, expected1b, 1); free_list(&head1b); /* ================= insert_at_tail ================= */ LIST head2 = NULL; insert_at_tail(&head2, 10); insert_at_tail(&head2, 20); insert_at_tail(&head2, 30); int expected2[] = {10, 20, 30}; check("insert_at_tail: builds list in order", head2, expected2, 3); free_list(&head2); LIST head2b = NULL; insert_at_tail(&head2b, 99); int expected2b[] = {99}; check("insert_at_tail: single insert into empty list", head2b, expected2b, 1); free_list(&head2b); /* ================= insert_at_position ================= */ LIST head3 = NULL; insert_at_tail(&head3, 10); insert_at_tail(&head3, 20); insert_at_tail(&head3, 30); int ret3 = insert_at_position(&head3, 99, 1); /* 10 [99] 20 30 */ check_int("insert_at_position: returns 1 for a valid middle position", ret3, 1); int expected3[] = {10, 99, 20, 30}; check("insert_at_position: inserts at given middle position", head3, expected3, 4); free_list(&head3); /* Boundary: position 0 should behave like insert_at_head */ LIST head3b = NULL; insert_at_tail(&head3b, 10); insert_at_tail(&head3b, 20); int ret3b = insert_at_position(&head3b, 99, 0); check_int("insert_at_position: returns 1 for position 0", ret3b, 1); int expected3b[] = {99, 10, 20}; check("insert_at_position: position 0 inserts at the head", head3b, expected3b, 3); free_list(&head3b); /* Boundary: position == current length should behave like insert_at_tail */ LIST head3c = NULL; insert_at_tail(&head3c, 10); insert_at_tail(&head3c, 20); int ret3c = insert_at_position(&head3c, 99, 2); /* length is 2 */ check_int("insert_at_position: returns 1 for position == length (tail)", ret3c, 1); int expected3c[] = {10, 20, 99}; check("insert_at_position: position == length inserts at the tail", head3c, expected3c, 3); free_list(&head3c); /* Boundary: position 0 on an EMPTY list */ LIST head3d = NULL; int ret3d = insert_at_position(&head3d, 55, 0); check_int("insert_at_position: returns 1 for position 0 on an empty list", ret3d, 1); int expected3d[] = {55}; check("insert_at_position: position 0 on empty list creates a single-node list", head3d, expected3d, 1); free_list(&head3d); /* Boundary: negative position is invalid -> list unchanged */ LIST head3e = NULL; insert_at_tail(&head3e, 10); insert_at_tail(&head3e, 20); int ret3e = insert_at_position(&head3e, 99, -1); check_int("insert_at_position: returns 0 for a negative position", ret3e, 0); int expected3e[] = {10, 20}; check("insert_at_position: negative position leaves the list unchanged", head3e, expected3e, 2); free_list(&head3e); /* Boundary: position beyond current length is invalid -> list unchanged */ LIST head3f = NULL; insert_at_tail(&head3f, 10); insert_at_tail(&head3f, 20); int ret3f = insert_at_position(&head3f, 99, 10); check_int("insert_at_position: returns 0 for a position beyond the list length", ret3f, 0); int expected3f[] = {10, 20}; check("insert_at_position: out-of-range position leaves the list unchanged", head3f, expected3f, 2); free_list(&head3f); /* ================= delete_by_value ================= */ LIST head4 = NULL; insert_at_tail(&head4, 10); insert_at_tail(&head4, 20); insert_at_tail(&head4, 30); int ret4 = delete_by_value(&head4, 20); check_int("delete_by_value: returns 1 when value found", ret4, 1); int expected4[] = {10, 30}; check("delete_by_value: removes correct value (middle)", head4, expected4, 2); int ret4b = delete_by_value(&head4, 999); check_int("delete_by_value: returns 0 when value not found", ret4b, 0); free_list(&head4); /* Boundary: deleting the HEAD value */ LIST head4c = NULL; insert_at_tail(&head4c, 10); insert_at_tail(&head4c, 20); insert_at_tail(&head4c, 30); int ret4c = delete_by_value(&head4c, 10); check_int("delete_by_value: returns 1 when deleting the head value", ret4c, 1); int expected4c[] = {20, 30}; check("delete_by_value: removes the head correctly, updates head pointer", head4c, expected4c, 2); free_list(&head4c); /* Boundary: deleting the TAIL value */ LIST head4d = NULL; insert_at_tail(&head4d, 10); insert_at_tail(&head4d, 20); insert_at_tail(&head4d, 30); int ret4d = delete_by_value(&head4d, 30); check_int("delete_by_value: returns 1 when deleting the tail value", ret4d, 1); int expected4d[] = {10, 20}; check("delete_by_value: removes the tail correctly (new tail's next is NULL)", head4d, expected4d, 2); free_list(&head4d); /* Boundary: deleting the ONLY node in a single-element list */ LIST head4e = NULL; insert_at_tail(&head4e, 77); int ret4e = delete_by_value(&head4e, 77); check_int("delete_by_value: returns 1 when deleting the only node", ret4e, 1); check_null("delete_by_value: list becomes empty (head is NULL) after removing the only node", head4e); /* Boundary: deleting from an EMPTY list */ LIST head4f = NULL; int ret4f = delete_by_value(&head4f, 5); check_int("delete_by_value: returns 0 when called on an empty list", ret4f, 0); check_null("delete_by_value: an empty list stays empty after a failed delete", head4f); /* Duplicate values: only the FIRST occurrence should be removed */ LIST head4g = NULL; insert_at_tail(&head4g, 10); insert_at_tail(&head4g, 20); insert_at_tail(&head4g, 10); int ret4g = delete_by_value(&head4g, 10); check_int("delete_by_value: returns 1 when duplicates exist", ret4g, 1); int expected4g[] = {20, 10}; check("delete_by_value: removes only the FIRST occurrence of a duplicated value", head4g, expected4g, 2); free_list(&head4g); /* ================= delete_at_position ================= */ LIST head5 = NULL; insert_at_tail(&head5, 10); insert_at_tail(&head5, 20); insert_at_tail(&head5, 30); int ret5 = delete_at_position(&head5, 1); check_int("delete_at_position: returns 1 on valid position", ret5, 1); int expected5[] = {10, 30}; check("delete_at_position: removes correct node (middle)", head5, expected5, 2); int ret5b = delete_at_position(&head5, 10); /* out of range */ check_int("delete_at_position: returns 0 on invalid position", ret5b, 0); free_list(&head5); /* Boundary: deleting at position 0 (the head) */ LIST head5c = NULL; insert_at_tail(&head5c, 10); insert_at_tail(&head5c, 20); insert_at_tail(&head5c, 30); int ret5c = delete_at_position(&head5c, 0); check_int("delete_at_position: returns 1 when deleting position 0", ret5c, 1); int expected5c[] = {20, 30}; check("delete_at_position: position 0 removes the head, updates head pointer", head5c, expected5c, 2); free_list(&head5c); /* Boundary: deleting the LAST valid position (the tail) */ LIST head5d = NULL; insert_at_tail(&head5d, 10); insert_at_tail(&head5d, 20); insert_at_tail(&head5d, 30); int ret5d = delete_at_position(&head5d, 2); /* last valid index for a 3-element list */ check_int("delete_at_position: returns 1 when deleting the last valid position", ret5d, 1); int expected5d[] = {10, 20}; check("delete_at_position: removes the tail correctly (new tail's next is NULL)", head5d, expected5d, 2); free_list(&head5d); /* Boundary: deleting the ONLY node (position 0) in a single-element list */ LIST head5e = NULL; insert_at_tail(&head5e, 77); int ret5e = delete_at_position(&head5e, 0); check_int("delete_at_position: returns 1 when deleting the only node at position 0", ret5e, 1); check_null("delete_at_position: list becomes empty (head is NULL) after removing the only node", head5e); /* Boundary: deleting from an EMPTY list */ LIST head5f = NULL; int ret5f = delete_at_position(&head5f, 0); check_int("delete_at_position: returns 0 when called on an empty list", ret5f, 0); check_null("delete_at_position: an empty list stays empty after a failed delete", head5f); /* Boundary: negative position is invalid */ LIST head5g = NULL; insert_at_tail(&head5g, 10); insert_at_tail(&head5g, 20); int ret5g = delete_at_position(&head5g, -1); check_int("delete_at_position: returns 0 for a negative position", ret5g, 0); int expected5g[] = {10, 20}; check("delete_at_position: negative position leaves the list unchanged", head5g, expected5g, 2); free_list(&head5g); /* Boundary: position == length (one past the last valid index) is invalid */ LIST head5h = NULL; insert_at_tail(&head5h, 10); insert_at_tail(&head5h, 20); int ret5h = delete_at_position(&head5h, 2); /* length is 2, valid indices are 0-1 */ check_int("delete_at_position: returns 0 when position == length", ret5h, 0); int expected5h[] = {10, 20}; check("delete_at_position: position == length leaves the list unchanged", head5h, expected5h, 2); free_list(&head5h); /* ================= combined / stress: insert then drain completely ================= */ LIST head6 = NULL; insert_at_tail(&head6, 1); insert_at_tail(&head6, 2); insert_at_tail(&head6, 3); insert_at_tail(&head6, 4); insert_at_tail(&head6, 5); int removed = 0; while (delete_at_position(&head6, 0) == 1) { removed++; } check_int("delete_at_position: repeatedly removing position 0 drains the whole list", removed, 5); check_null("delete_at_position: list is empty (head is NULL) after being fully drained", head6); /* ================= free_list ================= */ LIST head7 = NULL; insert_at_tail(&head7, 1); insert_at_tail(&head7, 2); free_list(&head7); check_null("free_list: sets head back to NULL after freeing a non-empty list", head7); LIST head7b = NULL; free_list(&head7b); /* freeing an already-empty list should not crash */ check_null("free_list: freeing an already-empty list is safe and head stays NULL", head7b); /* ================= summary ================= */ printf("\n----------------------------------\n"); printf("Passed: %d, Failed: %d, Total: %d\n", pass_count, fail_count, pass_count + fail_count); return (fail_count == 0) ? 0 : 1; }