diff options
Diffstat (limited to 'tests/liballoc_tests.c')
| -rw-r--r-- | tests/liballoc_tests.c | 97 |
1 files changed, 45 insertions, 52 deletions
diff --git a/tests/liballoc_tests.c b/tests/liballoc_tests.c index 2d3a439..16d50d6 100644 --- a/tests/liballoc_tests.c +++ b/tests/liballoc_tests.c @@ -1,11 +1,11 @@ /* * tests/liballoc_tests.c * - * Unit tests for liballoc + * Unit tests for liballoc * CISC 301 -- Operating Systems, Project 3 * * Copyright (C) 2025 Douglas B. Rumbaugh <dbrumbaugh@harrisburgu.edu> - * + * * Distributed under the Modified BSD License * */ @@ -13,17 +13,17 @@ #include "constants.h" #include <check.h> -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> 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); + memset(memory, 0, ALIGNMENT * 3); - size_t alignment = (size_t) memory % ALIGNMENT; + size_t alignment = (size_t)memory % ALIGNMENT; ck_assert_int_eq(alignment, 0); /* leak the memory--we aren't testing release */ @@ -31,21 +31,21 @@ START_TEST(basic_allocate) { END_TEST START_TEST(multiple_allocations) { - size_t size = ALIGNMENT*5; + size_t size = ALIGNMENT * 5; void *allocations[100]; - for (size_t i=0; i<100; i++) { + for (size_t i = 0; i < 100; i++) { allocations[i] = allocate(size); ck_assert_ptr_nonnull(allocations[i]); memset(allocations[i], 0, size); - size_t alignment = (size_t) allocations[i]% ALIGNMENT; + size_t alignment = (size_t)allocations[i] % ALIGNMENT; ck_assert_int_eq(alignment, 0); } /* verify the headers */ - for (size_t i=0; i<100; i++) { + for (size_t i = 0; i < 100; i++) { header *hdr = allocations[i] - sizeof(header); ck_assert_int_eq(hdr->size, size); ck_assert_int_eq(hdr->magic_number, MAGIC_NUMBER); @@ -63,7 +63,7 @@ START_TEST(basic_release) { 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_int_eq(list_head->size, ALIGNMENT * 3); ck_assert_ptr_null(list_head->next); } END_TEST @@ -77,10 +77,10 @@ START_TEST(free_list_emptying) { 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_int_eq(list_head->size, ALIGNMENT * 3); ck_assert_ptr_null(list_head->next); - void *memory2 = allocate(ALIGNMENT *3); + void *memory2 = allocate(ALIGNMENT * 3); ck_assert_ptr_eq(memory, memory2); ck_assert_ptr_null(free_list_head()); } @@ -99,15 +99,15 @@ START_TEST(unaligned_allocation) { void *allocations[100]; /* ensure first allocation is aligned */ allocations[0] = allocate(unaligned_size); - size_t first_alignment = (size_t) allocations[0] % ALIGNMENT; + size_t first_alignment = (size_t)allocations[0] % ALIGNMENT; ck_assert_int_eq(first_alignment, 0); memset(allocations[0], 0, unaligned_size); /* now allocate several more times--each allocation should be aligned */ - for (size_t i=1; i<100; i++) { + for (size_t i = 1; i < 100; i++) { allocations[i] = allocate(unaligned_size); - size_t alignment = (size_t) allocations[i] % ALIGNMENT; + size_t alignment = (size_t)allocations[i] % ALIGNMENT; ck_assert_int_eq(alignment, 0); /* verify we can write the the memory */ @@ -115,23 +115,21 @@ START_TEST(unaligned_allocation) { /* just leak the memory--we aren't testing release here */ } - + /* verify the headers */ - for (size_t i=0; i<100; i++) { + for (size_t i = 0; i < 100; i++) { header *hdr = allocations[i] - sizeof(header); ck_assert_int_eq(hdr->magic_number, MAGIC_NUMBER); } - } - START_TEST(free_list_reuse) { - size_t size = ALIGNMENT *3; + size_t size = ALIGNMENT * 3; void *memory = allocate(size); release(memory); - for (size_t i=0; i<10; i++) { + for (size_t i = 0; i < 10; i++) { void *new_memory = allocate(size); ck_assert_ptr_eq(memory, new_memory); ck_assert_ptr_null(free_list_head()); @@ -141,51 +139,50 @@ START_TEST(free_list_reuse) { END_TEST START_TEST(free_list_coalesce_forward) { - size_t size =ALIGNMENT*3; + size_t size = ALIGNMENT * 3; const size_t n = 100; void *ptrs[n]; - for (size_t i=0; i<n; i++) { + for (size_t i = 0; i < n; i++) { ptrs[i] = allocate(size); } - /* + /* * release memory in forward order. Each release should be * coalesced, so there's only ever one free block - */ - size_t cnt = 0; - for(size_t i=0; i<n; i++) { - release(ptrs[i]); - cnt++; - free_nd *fl_head = free_list_head(); - ck_assert_ptr_null(fl_head->next); - ck_assert_int_eq(fl_head->size, (cnt)*size + (cnt-1)*sizeof(header)); - } + */ + size_t cnt = 0; + for (size_t i = 0; i < n; i++) { + release(ptrs[i]); + cnt++; + free_nd *fl_head = free_list_head(); + ck_assert_ptr_null(fl_head->next); + ck_assert_int_eq(fl_head->size, (cnt)*size + (cnt - 1) * sizeof(header)); + } } END_TEST - START_TEST(free_list_coalesce_backward) { - size_t size =ALIGNMENT*3; + size_t size = ALIGNMENT * 3; const size_t n = 100; void *ptrs[n]; - for (size_t i=0; i<n; i++) { + for (size_t i = 0; i < n; i++) { ptrs[i] = allocate(size); } - /* + /* * release memory in reverse order. Each release should be * coalesced, so there's only ever one free block - */ - size_t cnt = 0; - for(ssize_t i=n-1; i>=0; i--) { - release(ptrs[i]); - cnt++; - free_nd *fl_head = free_list_head(); - ck_assert_ptr_null(fl_head->next); - ck_assert_int_eq(fl_head->size, (cnt)*size + (cnt-1)*sizeof(header)); - } + */ + size_t cnt = 0; + for (ssize_t i = n - 1; i >= 0; i--) { + release(ptrs[i]); + cnt++; + free_nd *fl_head = free_list_head(); + ck_assert_ptr_null(fl_head->next); + ck_assert_int_eq(fl_head->size, (cnt)*size + (cnt - 1) * sizeof(header)); + } } END_TEST @@ -200,13 +197,12 @@ START_TEST(free_list_split_basic) { ck_assert_ptr_eq(memory, memory2); ck_assert_ptr_nonnull(free_list_head()); - } END_TEST START_TEST(free_list_split_below_threshold) { - size_t initial_size = 5*ALIGNMENT; - size_t second_size = 2*ALIGNMENT; + size_t initial_size = 5 * ALIGNMENT; + size_t second_size = 2 * ALIGNMENT; void *memory = allocate(initial_size); release(memory); @@ -217,8 +213,6 @@ START_TEST(free_list_split_below_threshold) { ck_assert_ptr_null(free_list_head()); } - - Suite *liballoc_suite(void) { Suite *s; TCase *unit; @@ -242,7 +236,6 @@ Suite *liballoc_suite(void) { return s; } - int main(void) { int number_failed; Suite *s; |