diff options
Diffstat (limited to 'mem/test')
| -rw-r--r-- | mem/test/mem_test.c | 232 | ||||
| -rw-r--r-- | mem/test/test.h | 185 |
2 files changed, 417 insertions, 0 deletions
diff --git a/mem/test/mem_test.c b/mem/test/mem_test.c new file mode 100644 index 0000000..6ab4c7c --- /dev/null +++ b/mem/test/mem_test.c | |||
| @@ -0,0 +1,232 @@ | |||
| 1 | #include "mem.h" | ||
| 2 | |||
| 3 | #include "test.h" | ||
| 4 | |||
| 5 | #define NUM_BLOCKS 10 | ||
| 6 | |||
| 7 | DEF_MEM(test_mem, int, NUM_BLOCKS); | ||
| 8 | |||
| 9 | static int count(test_mem* mem) { | ||
| 10 | int count = 0; | ||
| 11 | mem_foreach(mem, n, { count++; }); | ||
| 12 | return count; | ||
| 13 | } | ||
| 14 | |||
| 15 | static int sum(test_mem* mem) { | ||
| 16 | int sum = 0; | ||
| 17 | mem_foreach(mem, n, { sum += *n; }); | ||
| 18 | return sum; | ||
| 19 | } | ||
| 20 | |||
| 21 | // Create a statically-backed allocator. | ||
| 22 | TEST_CASE(mem_create) { | ||
| 23 | test_mem mem; | ||
| 24 | mem_make(&mem); | ||
| 25 | } | ||
| 26 | |||
| 27 | // Create a dynamically-backed allocator. | ||
| 28 | TEST_CASE(mem_create_dyn) { | ||
| 29 | DEF_MEM_DYN(dyn_mem, int); | ||
| 30 | |||
| 31 | dyn_mem mem; | ||
| 32 | mem_make_dyn(&mem, NUM_BLOCKS, sizeof(int)); | ||
| 33 | } | ||
| 34 | |||
| 35 | // Allocate N chunks of 1 block each. | ||
| 36 | TEST_CASE(mem_fully_allocate) { | ||
| 37 | test_mem mem; | ||
| 38 | mem_make(&mem); | ||
| 39 | |||
| 40 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 41 | const int* block = mem_alloc(&mem, 1); | ||
| 42 | TEST_TRUE(block != 0); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | // Allocate N chunks of 1 block each, then free them. | ||
| 47 | TEST_CASE(mem_fill_then_free) { | ||
| 48 | test_mem mem; | ||
| 49 | mem_make(&mem); | ||
| 50 | |||
| 51 | int* blocks[NUM_BLOCKS] = {0}; | ||
| 52 | for (int i = 0; i < NUM_BLOCKS; i++) { | ||
| 53 | blocks[i] = mem_alloc(&mem, 1); | ||
| 54 | TEST_TRUE(blocks[i] != 0); | ||
| 55 | } | ||
| 56 | |||
| 57 | for (int i = 0; i < NUM_BLOCKS; i++) { | ||
| 58 | mem_free(&mem, &blocks[i]); | ||
| 59 | TEST_EQUAL(blocks[i], 0); // Pointer should be set to 0 on free. | ||
| 60 | } | ||
| 61 | |||
| 62 | TEST_EQUAL(count(&mem), 0); | ||
| 63 | } | ||
| 64 | |||
| 65 | // Attempt to allocate blocks past the maximum allocator size. | ||
| 66 | // The allocator should handle the failed allocations gracefully. | ||
| 67 | TEST_CASE(mem_allocate_beyond_max_size) { | ||
| 68 | test_mem mem; | ||
| 69 | mem_make(&mem); | ||
| 70 | |||
| 71 | // Fully allocate the mem. | ||
| 72 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 73 | TEST_TRUE(mem_alloc(&mem, 1) != 0); | ||
| 74 | } | ||
| 75 | |||
| 76 | // Past the end. | ||
| 77 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 78 | TEST_EQUAL(mem_alloc(&mem, 1), 0); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | // Free blocks should always remain zeroed out. | ||
| 83 | // This tests the invariant right after creating the allocator. | ||
| 84 | TEST_CASE(mem_zero_free_blocks_after_creation) { | ||
| 85 | test_mem mem; | ||
| 86 | mem_make(&mem); | ||
| 87 | |||
| 88 | const int zero = 0; | ||
| 89 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 90 | const int* block = (const int*)(mem.blocks) + i; | ||
| 91 | TEST_EQUAL(memcmp(block, &zero, sizeof(int)), 0); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | // Free blocks should always remain zeroed out. | ||
| 96 | // This tests the invariant after freeing a block. | ||
| 97 | TEST_CASE(mem_zero_free_block_after_free) { | ||
| 98 | test_mem mem; | ||
| 99 | mem_make(&mem); | ||
| 100 | |||
| 101 | int* val = mem_alloc(&mem, 1); | ||
| 102 | TEST_TRUE(val != 0); | ||
| 103 | *val = 177; | ||
| 104 | |||
| 105 | int* old_val = val; | ||
| 106 | mem_free(&mem, &val); // val pointer is set to 0. | ||
| 107 | TEST_EQUAL(*old_val, 0); // Block is zeroed out after free. | ||
| 108 | } | ||
| 109 | |||
| 110 | // Traverse an empty allocator. | ||
| 111 | TEST_CASE(mem_traverse_empty) { | ||
| 112 | test_mem mem; | ||
| 113 | mem_make(&mem); | ||
| 114 | |||
| 115 | TEST_EQUAL(count(&mem), 0); | ||
| 116 | } | ||
| 117 | |||
| 118 | // Traverse a partially full allocator. | ||
| 119 | TEST_CASE(mem_traverse_partially_full) { | ||
| 120 | const int N = NUM_BLOCKS / 2; | ||
| 121 | |||
| 122 | test_mem mem; | ||
| 123 | mem_make(&mem); | ||
| 124 | |||
| 125 | for (int i = 0; i < N; ++i) { | ||
| 126 | int* val = mem_alloc(&mem, 1); | ||
| 127 | TEST_TRUE(val != 0); | ||
| 128 | *val = i + 1; | ||
| 129 | } | ||
| 130 | |||
| 131 | TEST_EQUAL(sum(&mem), (N) * (N + 1) / 2); | ||
| 132 | } | ||
| 133 | |||
| 134 | // Traverse a full allocator. | ||
| 135 | TEST_CASE(mem_traverse_full) { | ||
| 136 | test_mem mem; | ||
| 137 | mem_make(&mem); | ||
| 138 | |||
| 139 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 140 | int* val = mem_alloc(&mem, 1); | ||
| 141 | TEST_TRUE(val != 0); | ||
| 142 | *val = i + 1; | ||
| 143 | } | ||
| 144 | |||
| 145 | TEST_EQUAL(sum(&mem), (NUM_BLOCKS) * (NUM_BLOCKS + 1) / 2); | ||
| 146 | } | ||
| 147 | |||
| 148 | // Get the ith (allocated) chunk. | ||
| 149 | TEST_CASE(mem_get_block) { | ||
| 150 | test_mem mem; | ||
| 151 | mem_make(&mem); | ||
| 152 | |||
| 153 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 154 | int* block = mem_alloc(&mem, 1); | ||
| 155 | TEST_TRUE(block != 0); | ||
| 156 | *block = i; | ||
| 157 | TEST_EQUAL(mem_get_chunk_handle(&mem, block), (size_t)i); | ||
| 158 | } | ||
| 159 | |||
| 160 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 161 | TEST_EQUAL(*mem_get_chunk(&mem, i), i); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | // Test merging. | ||
| 166 | // 1. Allocate chunks of variable sizes. | ||
| 167 | // 2. Free them in a different order. | ||
| 168 | // 3. Then we should be able to allocate 1 chunk of N blocks. | ||
| 169 | TEST_CASE(mem_fragmentation) { | ||
| 170 | test_mem mem; | ||
| 171 | mem_make(&mem); | ||
| 172 | |||
| 173 | int* blocks[NUM_BLOCKS] = {0}; | ||
| 174 | int next_block = 0; | ||
| 175 | |||
| 176 | #define ALLOC(num_blocks) \ | ||
| 177 | blocks[next_block] = mem_alloc(&mem, num_blocks); \ | ||
| 178 | TEST_TRUE(blocks[next_block] != 0); \ | ||
| 179 | next_block++; | ||
| 180 | |||
| 181 | #define FREE(block_idx) mem_free(&mem, &blocks[block_idx]) | ||
| 182 | |||
| 183 | // 5 total allocations of variable chunk sizes. | ||
| 184 | ALLOC(2); // 2; idx = 0 | ||
| 185 | ALLOC(3); // 5; idx = 1 | ||
| 186 | ALLOC(1); // 6; idx = 2 | ||
| 187 | ALLOC(3); // 9; idx = 3 | ||
| 188 | ALLOC(1); // 10; idx = 4 | ||
| 189 | |||
| 190 | // Free the 5 allocations in a different order. | ||
| 191 | FREE(1); | ||
| 192 | FREE(3); | ||
| 193 | FREE(4); | ||
| 194 | FREE(2); | ||
| 195 | FREE(0); | ||
| 196 | |||
| 197 | // Should be able to allocate 1 chunk of N blocks. | ||
| 198 | const void* chunk = mem_alloc(&mem, NUM_BLOCKS); | ||
| 199 | TEST_TRUE(chunk != 0); | ||
| 200 | } | ||
| 201 | |||
| 202 | // Clear and re-use an allocator. | ||
| 203 | TEST_CASE(mem_clear_then_reuse) { | ||
| 204 | test_mem mem; | ||
| 205 | mem_make(&mem); | ||
| 206 | |||
| 207 | // Allocate chunks, contents not important. | ||
| 208 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 209 | int* chunk = mem_alloc(&mem, 1); | ||
| 210 | TEST_TRUE(chunk != 0); | ||
| 211 | } | ||
| 212 | |||
| 213 | mem_clear(&mem); | ||
| 214 | |||
| 215 | // Allocate chunks and assign values 0..N. | ||
| 216 | for (int i = 0; i < NUM_BLOCKS; ++i) { | ||
| 217 | int* chunk = mem_alloc(&mem, 1); | ||
| 218 | TEST_TRUE(chunk != 0); | ||
| 219 | *chunk = i + 1; | ||
| 220 | } | ||
| 221 | |||
| 222 | TEST_EQUAL(sum(&mem), NUM_BLOCKS * (NUM_BLOCKS + 1) / 2); | ||
| 223 | } | ||
| 224 | |||
| 225 | // Stress test. | ||
| 226 | // | ||
| 227 | // 1. Allocate the mem, either fully or partially. If fully, attempt to | ||
| 228 | // allocate some items past the end. | ||
| 229 | // | ||
| 230 | // 2. Free all allocated items in some random order. | ||
| 231 | |||
| 232 | int main() { return 0; } | ||
diff --git a/mem/test/test.h b/mem/test/test.h new file mode 100644 index 0000000..fd8dc22 --- /dev/null +++ b/mem/test/test.h | |||
| @@ -0,0 +1,185 @@ | |||
| 1 | // SPDX-License-Identifier: MIT | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #ifdef UNIT_TEST | ||
| 5 | |||
| 6 | #include <stdbool.h> | ||
| 7 | #include <stdio.h> | ||
| 8 | #include <stdlib.h> | ||
| 9 | #include <string.h> | ||
| 10 | |||
| 11 | #if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ | ||
| 12 | defined(__NetBSD__) || defined(__OpenBSD__) | ||
| 13 | #define USE_SYSCTL_FOR_ARGS 1 | ||
| 14 | // clang-format off | ||
| 15 | #include <sys/types.h> | ||
| 16 | #include <sys/sysctl.h> | ||
| 17 | // clang-format on | ||
| 18 | #include <unistd.h> // getpid | ||
| 19 | #endif | ||
| 20 | |||
| 21 | struct test_file_metadata; | ||
| 22 | |||
| 23 | struct test_failure { | ||
| 24 | bool present; | ||
| 25 | const char *message; | ||
| 26 | const char *file; | ||
| 27 | int line; | ||
| 28 | }; | ||
| 29 | |||
| 30 | struct test_case_metadata { | ||
| 31 | void (*fn)(struct test_case_metadata *, struct test_file_metadata *); | ||
| 32 | struct test_failure failure; | ||
| 33 | const char *name; | ||
| 34 | struct test_case_metadata *next; | ||
| 35 | }; | ||
| 36 | |||
| 37 | struct test_file_metadata { | ||
| 38 | bool registered; | ||
| 39 | const char *name; | ||
| 40 | struct test_file_metadata *next; | ||
| 41 | struct test_case_metadata *tests; | ||
| 42 | }; | ||
| 43 | |||
| 44 | struct test_file_metadata __attribute__((weak)) * test_file_head; | ||
| 45 | |||
| 46 | #define SET_FAILURE(_message) \ | ||
| 47 | metadata->failure = (struct test_failure) { \ | ||
| 48 | .message = _message, .file = __FILE__, .line = __LINE__, .present = true, \ | ||
| 49 | } | ||
| 50 | |||
| 51 | #define TEST_EQUAL(a, b) \ | ||
| 52 | do { \ | ||
| 53 | if ((a) != (b)) { \ | ||
| 54 | SET_FAILURE(#a " != " #b); \ | ||
| 55 | return; \ | ||
| 56 | } \ | ||
| 57 | } while (0) | ||
| 58 | |||
| 59 | #define TEST_TRUE(a) \ | ||
| 60 | do { \ | ||
| 61 | if (!(a)) { \ | ||
| 62 | SET_FAILURE(#a " is not true"); \ | ||
| 63 | return; \ | ||
| 64 | } \ | ||
| 65 | } while (0) | ||
| 66 | |||
| 67 | #define TEST_STREQUAL(a, b) \ | ||
| 68 | do { \ | ||
| 69 | if (strcmp(a, b) != 0) { \ | ||
| 70 | SET_FAILURE(#a " != " #b); \ | ||
| 71 | return; \ | ||
| 72 | } \ | ||
| 73 | } while (0) | ||
| 74 | |||
| 75 | #define TEST_CASE(_name) \ | ||
| 76 | static void __test_h_##_name(struct test_case_metadata *, \ | ||
| 77 | struct test_file_metadata *); \ | ||
| 78 | static struct test_file_metadata __test_h_file; \ | ||
| 79 | static struct test_case_metadata __test_h_meta_##_name = { \ | ||
| 80 | .name = #_name, \ | ||
| 81 | .fn = __test_h_##_name, \ | ||
| 82 | }; \ | ||
| 83 | static void __attribute__((constructor(101))) __test_h_##_name##_register(void) { \ | ||
| 84 | __test_h_meta_##_name.next = __test_h_file.tests; \ | ||
| 85 | __test_h_file.tests = &__test_h_meta_##_name; \ | ||
| 86 | if (!__test_h_file.registered) { \ | ||
| 87 | __test_h_file.name = __FILE__; \ | ||
| 88 | __test_h_file.next = test_file_head; \ | ||
| 89 | test_file_head = &__test_h_file; \ | ||
| 90 | __test_h_file.registered = true; \ | ||
| 91 | } \ | ||
| 92 | } \ | ||
| 93 | static void __test_h_##_name( \ | ||
| 94 | struct test_case_metadata *metadata __attribute__((unused)), \ | ||
| 95 | struct test_file_metadata *file_metadata __attribute__((unused))) | ||
| 96 | |||
| 97 | extern void __attribute__((weak)) (*test_h_unittest_setup)(void); | ||
| 98 | /// Run defined tests, return true if all tests succeeds | ||
| 99 | /// @param[out] tests_run if not NULL, set to whether tests were run | ||
| 100 | static inline void __attribute__((constructor(102))) run_tests(void) { | ||
| 101 | bool should_run = false; | ||
| 102 | #ifdef USE_SYSCTL_FOR_ARGS | ||
| 103 | int mib[] = { | ||
| 104 | CTL_KERN, | ||
| 105 | #if defined(__NetBSD__) || defined(__OpenBSD__) | ||
| 106 | KERN_PROC_ARGS, | ||
| 107 | getpid(), | ||
| 108 | KERN_PROC_ARGV, | ||
| 109 | #else | ||
| 110 | KERN_PROC, | ||
| 111 | KERN_PROC_ARGS, | ||
| 112 | getpid(), | ||
| 113 | #endif | ||
| 114 | }; | ||
| 115 | char *arg = NULL; | ||
| 116 | size_t arglen; | ||
| 117 | sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &arglen, NULL, 0); | ||
| 118 | arg = malloc(arglen); | ||
| 119 | sysctl(mib, sizeof(mib) / sizeof(mib[0]), arg, &arglen, NULL, 0); | ||
| 120 | #else | ||
| 121 | FILE *cmdlinef = fopen("/proc/self/cmdline", "r"); | ||
| 122 | char *arg = NULL; | ||
| 123 | int arglen; | ||
| 124 | fscanf(cmdlinef, "%ms%n", &arg, &arglen); | ||
| 125 | fclose(cmdlinef); | ||
| 126 | #endif | ||
| 127 | for (char *pos = arg; pos < arg + arglen; pos += strlen(pos) + 1) { | ||
| 128 | if (strcmp(pos, "--unittest") == 0) { | ||
| 129 | should_run = true; | ||
| 130 | break; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | free(arg); | ||
| 134 | |||
| 135 | if (!should_run) { | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | if (&test_h_unittest_setup) { | ||
| 140 | test_h_unittest_setup(); | ||
| 141 | } | ||
| 142 | |||
| 143 | struct test_file_metadata *i = test_file_head; | ||
| 144 | int failed = 0, success = 0; | ||
| 145 | while (i) { | ||
| 146 | fprintf(stderr, "Running tests from %s:\n", i->name); | ||
| 147 | struct test_case_metadata *j = i->tests; | ||
| 148 | while (j) { | ||
| 149 | fprintf(stderr, "\t%s ... ", j->name); | ||
| 150 | j->failure.present = false; | ||
| 151 | j->fn(j, i); | ||
| 152 | if (j->failure.present) { | ||
| 153 | fprintf(stderr, "failed (%s at %s:%d)\n", j->failure.message, | ||
| 154 | j->failure.file, j->failure.line); | ||
| 155 | failed++; | ||
| 156 | } else { | ||
| 157 | fprintf(stderr, "passed\n"); | ||
| 158 | success++; | ||
| 159 | } | ||
| 160 | j = j->next; | ||
| 161 | } | ||
| 162 | fprintf(stderr, "\n"); | ||
| 163 | i = i->next; | ||
| 164 | } | ||
| 165 | int total = failed + success; | ||
| 166 | fprintf(stderr, "Test results: passed %d/%d, failed %d/%d\n", success, total, | ||
| 167 | failed, total); | ||
| 168 | exit(failed == 0 ? EXIT_SUCCESS : EXIT_FAILURE); | ||
| 169 | } | ||
| 170 | |||
| 171 | #else | ||
| 172 | |||
| 173 | #include <stdbool.h> | ||
| 174 | |||
| 175 | #define TEST_CASE(name) static void __attribute__((unused)) __test_h_##name(void) | ||
| 176 | |||
| 177 | #define TEST_EQUAL(a, b) \ | ||
| 178 | (void)(a); \ | ||
| 179 | (void)(b) | ||
| 180 | #define TEST_TRUE(a) (void)(a) | ||
| 181 | #define TEST_STREQUAL(a, b) \ | ||
| 182 | (void)(a); \ | ||
| 183 | (void)(b) | ||
| 184 | |||
| 185 | #endif | ||
