Skip to content

Commit 5d45b39

Browse files
committed
Add an alignment test for the heapmem module.
1 parent 235bc58 commit 5d45b39

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

tests/08-native-runs/12-heapmem/test-heapmem.c

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* Nicolas Tsiftes <[email protected]>
3535
*/
3636

37+
#include <stdint.h>
3738
#include <stdio.h>
3839
#include <stdlib.h>
3940
#include <string.h>
@@ -77,6 +78,13 @@ UNIT_TEST(do_many_allocations)
7778
char *ptrs[TEST_CONCURRENT] = { NULL };
7879
unsigned failed_allocations = 0;
7980
unsigned failed_deallocations = 0;
81+
unsigned misalignments = 0;
82+
unsigned min_alignment = heapmem_alignment();
83+
84+
UNIT_TEST_ASSERT(min_alignment != 0);
85+
UNIT_TEST_ASSERT(!(min_alignment & (min_alignment - 1)));
86+
87+
printf("Using heapmem alignment of %u\n", min_alignment);
8088

8189
/*
8290
* Do a number of allocations (TEST_LIMIT) of a random size in the range
@@ -103,6 +111,9 @@ UNIT_TEST(do_many_allocations)
103111
if(ptrs[alloc_index] == NULL) {
104112
failed_allocations++;
105113
} else {
114+
if((uintptr_t)ptrs[alloc_index] & (min_alignment - 1)) {
115+
misalignments++;
116+
}
106117
memset(ptrs[alloc_index], '!', alloc_size);
107118
}
108119
}
@@ -118,8 +129,10 @@ UNIT_TEST(do_many_allocations)
118129

119130
printf("Failed allocations: %u\n", failed_allocations);
120131
printf("Failed deallocations: %u\n", failed_deallocations);
132+
printf("Misaligned addresses: %u\n", misalignments);
121133
UNIT_TEST_ASSERT(failed_allocations == 0);
122134
UNIT_TEST_ASSERT(failed_deallocations == 0);
135+
UNIT_TEST_ASSERT(misalignments == 0);
123136

124137
UNIT_TEST_END();
125138
}

0 commit comments

Comments
 (0)