/* * gcc test_cqueue.c cqueue.c linked_list.c -o test_cqueue * ./test_cqueue */ #include #include #include #include #include "cqueue.h" 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) static int arrays_equal(const int *a, const int *b, int n) { for (int i = 0; i < n; i++) if (a[i] != b[i]) return 0; return 1; } static void print_array(const int *arr, int n) { printf("["); for (int i = 0; i < n; i++) printf("%d%s", arr[i], (i < n - 1) ? ", " : ""); printf("]"); } static void check_int_array(const int *expected, int expected_n, const int *actual, int actual_n, const char *msg) { total_tests++; if (expected_n == actual_n && arrays_equal(expected, actual, expected_n)) { passed_tests++; printf(" [PASS] %s\n", msg); } else { printf(" [FAIL] %s (expected ", msg); print_array(expected, expected_n); printf(", got "); print_array(actual, actual_n); printf(")\n"); } } static int drain_queue(CQueue *q, int *out, int max_count) { int n = 0; element_type val; while (n < max_count && cqueueDequeue(q, &val)) { out[n++] = (int) val; } return n; } static int extract_ints(const char *str, int *out, int max_count) { int n = 0; const char *p = str; while (*p != '\0' && n < max_count) { char *end; long val = strtol(p, &end, 10); if (end != p) { out[n++] = (int) val; p = end; } else { p++; } } return n; } static void capture_print(CQueue *q, char *buf, size_t buf_size) { FILE *tmp = tmpfile(); if (!tmp) { buf[0] = '\0'; return; } fflush(stdout); int saved_fd = dup(fileno(stdout)); dup2(fileno(tmp), fileno(stdout)); cqueuePrint(q); fflush(stdout); dup2(saved_fd, fileno(stdout)); close(saved_fd); rewind(tmp); size_t n = fread(buf, 1, buf_size - 1, tmp); buf[n] = '\0'; fclose(tmp); } static void test_init_and_empty(void) { printf("\n== cqueueInit / cqueueIsEmpty ==\n"); CQueue q; cqueueInit(&q); check(cqueueIsEmpty(&q), "freshly initialized queue is empty"); cqueueEnqueue(&q, 7); check(!cqueueIsEmpty(&q), "queue is not empty after one enqueue"); element_type val; cqueueDequeue(&q, &val); check(cqueueIsEmpty(&q), "queue becomes empty again after dequeuing its only element"); cqueueFree(&q); } static void test_single_element_roundtrip(void) { printf("\n== single enqueue/dequeue roundtrip ==\n"); CQueue q; cqueueInit(&q); cqueueEnqueue(&q, 99); element_type val = -1; int ok = cqueueDequeue(&q, &val); check_int(1, ok, "dequeue after single enqueue reports success (1)"); check_int(99, val, "dequeued value matches the single enqueued value (99)"); check(cqueueIsEmpty(&q), "queue is empty after draining its only element"); cqueueFree(&q); } static void test_fifo_order(void) { printf("\n== FIFO ordering ==\n"); CQueue q; cqueueInit(&q); cqueueEnqueue(&q, 10); cqueueEnqueue(&q, 20); cqueueEnqueue(&q, 30); cqueueEnqueue(&q, 40); cqueueEnqueue(&q, 50); int actual[10]; int n = drain_queue(&q, actual, 10); int expected[] = {10, 20, 30, 40, 50}; check_int_array(expected, 5, actual, n, "5 enqueues dequeue back out in FIFO order"); check(cqueueIsEmpty(&q), "queue is empty after draining all 5 elements"); cqueueFree(&q); } static void test_dequeue_from_empty(void) { printf("\n== dequeue from empty queue ==\n"); CQueue q; cqueueInit(&q); element_type val; int ok = cqueueDequeue(&q, &val); check_int(0, ok, "dequeue on a freshly initialized empty queue reports failure (0)"); cqueueEnqueue(&q, 1); element_type v2; cqueueDequeue(&q, &v2); /* now empty again */ int ok2 = cqueueDequeue(&q, &val); check_int(0, ok2, "dequeue on a queue drained back to empty reports failure (0)"); cqueueFree(&q); } static void test_interleaved_enqueue_dequeue(void) { printf("\n== interleaved enqueue/dequeue ==\n"); CQueue q; cqueueInit(&q); element_type val; int ok; int all_ok = 1; cqueueEnqueue(&q, 1); cqueueEnqueue(&q, 2); ok = cqueueDequeue(&q, &val); all_ok &= ok; all_ok &= (val == 1); cqueueEnqueue(&q, 3); ok = cqueueDequeue(&q, &val); all_ok &= ok; all_ok &= (val == 2); cqueueEnqueue(&q, 4); ok = cqueueDequeue(&q, &val); all_ok &= ok; all_ok &= (val == 3); ok = cqueueDequeue(&q, &val); all_ok &= ok; all_ok &= (val == 4); check(all_ok, "interleaved enqueue/dequeue calls preserve correct FIFO order throughout"); check(cqueueIsEmpty(&q), "queue is empty after final interleaved dequeue"); cqueueFree(&q); } static void test_reuse_after_drained_to_empty(void) { printf("\n== reuse after being fully drained ==\n"); printf(" (catches bugs where the circular pointer isn't reset correctly\n"); printf(" once the queue becomes empty, corrupting later enqueues)\n"); CQueue q; cqueueInit(&q); cqueueEnqueue(&q, 1); cqueueEnqueue(&q, 2); int drained[10]; int n = drain_queue(&q, drained, 10); check_int(2, n, "drained exactly the 2 elements first enqueued"); check(cqueueIsEmpty(&q), "queue reports empty right after being fully drained"); /* Reuse the same (now-empty) queue */ cqueueEnqueue(&q, 100); cqueueEnqueue(&q, 200); cqueueEnqueue(&q, 300); int drained2[10]; int n2 = drain_queue(&q, drained2, 10); int expected2[] = {100, 200, 300}; check_int_array(expected2, 3, drained2, n2, "queue works correctly when reused after being emptied"); check(cqueueIsEmpty(&q), "queue is empty again after draining the reused elements"); cqueueFree(&q); } static void test_print(void) { printf("\n== cqueuePrint ==\n"); printf(" (checks the sequence of numbers printed, not exact formatting)\n"); CQueue q; cqueueInit(&q); char buf[512]; int actual[10]; /* empty queue */ capture_print(&q, buf, sizeof(buf)); int n = extract_ints(buf, actual, 10); check_int(0, n, "cqueuePrint on an empty queue prints no numbers"); /* non-empty queue, front to rear */ cqueueEnqueue(&q, 5); cqueueEnqueue(&q, 6); cqueueEnqueue(&q, 7); capture_print(&q, buf, sizeof(buf)); n = extract_ints(buf, actual, 10); int expected[] = {5, 6, 7}; check_int_array(expected, 3, actual, n, "cqueuePrint prints elements front-to-rear (5,6,7)"); /* Print must not consume the queue */ int drained[10]; int dn = drain_queue(&q, drained, 10); int expected_drain[] = {5, 6, 7}; check_int_array(expected_drain, 3, drained, dn, "cqueuePrint does not remove elements from the queue"); cqueueFree(&q); } static void test_stress_many_cycles(void) { printf("\n== stress test: many enqueue/dequeue cycles ==\n"); printf(" (exercises the circular wraparound repeatedly to catch pointer bugs\n"); printf(" that only surface after many cycles - run under valgrind for extra\n"); printf(" confidence there are no leaks or invalid accesses)\n"); CQueue q; cqueueInit(&q); int all_correct = 1; for (int cycle = 0; cycle < 20; cycle++) { for (int i = 0; i < 5; i++) { cqueueEnqueue(&q, cycle * 5 + i); } int actual[5]; int n = drain_queue(&q, actual, 5); if (n != 5) { all_correct = 0; break; } for (int i = 0; i < 5; i++) { if (actual[i] != cycle * 5 + i) { all_correct = 0; break; } } if (!all_correct) break; } check(all_correct, "20 cycles of 5 enqueues + 5 dequeues each all produce correct FIFO order"); check(cqueueIsEmpty(&q), "queue is empty after all stress cycles complete"); cqueueFree(&q); } static void test_free_is_safe(void) { printf("\n== cqueueFree ==\n"); CQueue q; cqueueInit(&q); cqueueEnqueue(&q, 1); cqueueEnqueue(&q, 2); cqueueEnqueue(&q, 3); cqueueFree(&q); check(cqueueIsEmpty(&q), "queue reports empty after cqueueFree"); /* freeing an already-empty queue should not crash */ cqueueFree(&q); check(cqueueIsEmpty(&q), "freeing an already-empty queue is safe and leaves it empty"); } int main(void) { printf("=====================================\n"); printf(" Circular Queue (cqueue.h) - Tests\n"); printf("=====================================\n"); test_init_and_empty(); test_single_element_roundtrip(); test_fifo_order(); test_dequeue_from_empty(); test_interleaved_enqueue_dequeue(); test_reuse_after_drained_to_empty(); test_print(); test_stress_many_cycles(); test_free_is_safe(); printf("\n=====================================\n"); printf(" RESULT: %d / %d tests passed\n", passed_tests, total_tests); printf("=====================================\n"); return (passed_tests == total_tests) ? 0 : 1; }