From db7277df5ed4a0951698697b62afa823c3ba0d72 Mon Sep 17 00:00:00 2001 From: "Douglas B. Rumbaugh" Date: Sun, 2 Nov 2025 13:23:50 -0500 Subject: Intial commit: No free list management yet --- tests/liballoc_tests.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 tests/liballoc_tests.c (limited to 'tests') diff --git a/tests/liballoc_tests.c b/tests/liballoc_tests.c new file mode 100644 index 0000000..235656d --- /dev/null +++ b/tests/liballoc_tests.c @@ -0,0 +1,119 @@ +/* + * 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); + + // TODO: verify entry on the free list +} +END_TEST + +START_TEST(release_null) { + /* releasing NULL should take no action */ + release(NULL); + + // TODO: verify no entry on free list +} +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; + fprintf(stderr, "ALIGNMENT: %ld\n", first_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; + fprintf(stderr, "ALIGNMENT: %ld\n", 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); +} -- cgit v1.2.3