1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* tests/liballoc_tests.c
*
* 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
*
*/
#include "alloc.h"
#include "constants.h"
#include <check.h>
#include <stdlib.h>
#include <stdio.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);
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;
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);
}
|