/* * tests/liballoc_tests.c * * Unit tests for liballoc * CISC 301 -- Operating Systems, Project 3 * * Copyright (C) 2025 Douglas B. Rumbaugh * * Distributed under the Modified BSD License * */ #include "alloc.h" #include "constants.h" #include #include #include START_TEST(basic_allocate) { void *memory = allocate(ALIGNMENT * 3); ck_assert_ptr_nonnull(memory); /* verify we can write to the memory w/o segfaulting */ memset(memory, 0, ALIGNMENT*3); size_t alignment = (size_t) memory % ALIGNMENT; ck_assert_int_eq(alignment, 0); /* leak the memory--we aren't testing release */ } END_TEST START_TEST(multiple_allocations) { size_t size = ALIGNMENT*5; for (size_t i=0; i<100; i++) { void *memory = allocate(size); ck_assert_ptr_nonnull(memory); memset(memory, 0, size); size_t alignment = (size_t) memory % ALIGNMENT; ck_assert_int_eq(alignment, 0); } /* leak the memory--we aren't testing release */ } START_TEST(basic_release) { void *memory = allocate(ALIGNMENT * 3); ck_assert_ptr_nonnull(memory); release(memory); free_nd *list_head = free_list_head(); ck_assert_ptr_nonnull(list_head); ck_assert_ptr_eq(list_head, memory); ck_assert_int_eq(list_head->size, ALIGNMENT*3); ck_assert_ptr_null(list_head->next); } END_TEST START_TEST(release_null) { /* releasing NULL should take no action */ release(NULL); ck_assert_ptr_null(free_list_head()); } END_TEST START_TEST(unaligned_allocation) { size_t unaligned_size = ALIGNMENT + 3; /* ensure first allocation is aligned */ void *first_memory = allocate(unaligned_size); size_t first_alignment = (size_t) first_memory % ALIGNMENT; ck_assert_int_eq(first_alignment, 0); /* now allocate several more times--each allocation should be aligned */ for (size_t i=0; i<10; i++) { void *memory = allocate(unaligned_size); size_t alignment = (size_t) memory % ALIGNMENT; ck_assert_int_eq(alignment, 0); /* just leak the memory--we aren't testing release here */ } } Suite *liballoc_suite(void) { Suite *s; TCase *unit; s = suite_create("liballoc"); unit = tcase_create("unit"); tcase_add_test(unit, basic_allocate); tcase_add_test(unit, multiple_allocations); tcase_add_test(unit, unaligned_allocation); tcase_add_test(unit, basic_release); tcase_add_test(unit, release_null); suite_add_tcase(s, unit); return s; } int main(void) { int number_failed; Suite *s; SRunner *sr; s = liballoc_suite(); sr = srunner_create(s); srunner_run_all(sr, CK_NORMAL); number_failed = srunner_ntests_failed(sr); srunner_free(sr); exit((number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE); }