|
| 1 | +/************************************************************************/ |
| 2 | +/* Centrallix Application Server System */ |
| 3 | +/* Centrallix Base Library */ |
| 4 | +/* */ |
| 5 | +/* Copyright (C) 2005 LightSys Technology Services, Inc. */ |
| 6 | +/* */ |
| 7 | +/* You may use these files and this library under the terms of the */ |
| 8 | +/* GNU Lesser General Public License, Version 2.1, contained in the */ |
| 9 | +/* included file "COPYING". */ |
| 10 | +/* */ |
| 11 | +/* Module: test_newmalloc_00.c */ |
| 12 | +/* Author: Israel Fuller */ |
| 13 | +/* Creation: November 25th, 2025 */ |
| 14 | +/* Description: Test the nmSysMalloc(), nmSysFree(), nmSysRealloc(), */ |
| 15 | +/* and nmSysStrDup functions from the NewMalloc library. */ |
| 16 | +/************************************************************************/ |
| 17 | + |
| 18 | +#include <math.h> |
| 19 | +#include <stdbool.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <stdlib.h> |
| 22 | + |
| 23 | +/** Test dependencies. **/ |
| 24 | +#include "test_utils.h" |
| 25 | +#include "util.h" |
| 26 | + |
| 27 | +/** Tested module. **/ |
| 28 | +#include "newmalloc.h" |
| 29 | + |
| 30 | +static unsigned int seed_counter = 0; |
| 31 | +static char* err_buf; |
| 32 | +static unsigned int err_buf_i; |
| 33 | +static unsigned int err_buf_size; |
| 34 | + |
| 35 | +static int mock_error_fn(char* error_msg) |
| 36 | + { |
| 37 | + const size_t len = strlen(error_msg) + 1lu; |
| 38 | + |
| 39 | + /** Ensure enough space to store the error. **/ |
| 40 | + while (len > err_buf_size - err_buf_i) |
| 41 | + { |
| 42 | + err_buf_size *= 2; |
| 43 | + err_buf = check_ptr(realloc(err_buf, err_buf_size)); |
| 44 | + } |
| 45 | + |
| 46 | + err_buf_i += snprintf( |
| 47 | + err_buf + err_buf_i, |
| 48 | + err_buf_size - err_buf_i, |
| 49 | + "> %s\n", error_msg |
| 50 | + ); |
| 51 | + |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | +/** Initialize memory of a given size with random data. **/ |
| 56 | +static void* random_init(void* ptr, size_t size) |
| 57 | + { |
| 58 | + if (ptr == NULL) return NULL; |
| 59 | + unsigned char* p = (unsigned char*)ptr; |
| 60 | + for (size_t i = 0; i < size; i++) { |
| 61 | + p[i] = (unsigned char)(rand() % 256); |
| 62 | + } |
| 63 | + return ptr; |
| 64 | + } |
| 65 | + |
| 66 | +static bool do_tests(void) |
| 67 | + { |
| 68 | + bool success = true; |
| 69 | + |
| 70 | + /** Set a consistent, distinct seed for each test iteration. **/ |
| 71 | + srand(seed_counter++); |
| 72 | + |
| 73 | + /** Initialize the mock error function. **/ |
| 74 | + err_buf = check_ptr(malloc(err_buf_size = 256)); |
| 75 | + err_buf_i = snprintf(err_buf, err_buf_size, "%s", ""); |
| 76 | + nmSetErrFunction(mock_error_fn); |
| 77 | + |
| 78 | + /** Baseline: Should leak. **/ |
| 79 | + success &= EXPECT_NOT_NULL(nmSysMalloc(42)); |
| 80 | + |
| 81 | + /** Basic string data. **/ |
| 82 | + char* str1; |
| 83 | + success &= EXPECT_NOT_NULL(str1 = nmSysMalloc(16)); |
| 84 | + snprintf(str1, 16, "ThisIsSomeData!"); |
| 85 | + char* str2; |
| 86 | + success &= EXPECT_NOT_NULL(str2 = nmSysMalloc(32)); |
| 87 | + snprintf(str2, 32, "ThisDataIsDifferentStringData.\n"); |
| 88 | + success &= EXPECT_STR_EQL(str1, "ThisIsSomeData!"); |
| 89 | + success &= EXPECT_STR_EQL(str2, "ThisDataIsDifferentStringData.\n"); |
| 90 | + |
| 91 | + /** 128 MB random data, varying sizes. **/ |
| 92 | + #define TEST_LIMIT 16384 |
| 93 | + void** data = check_ptr(malloc(TEST_LIMIT * sizeof(void*))); |
| 94 | + void** test = check_ptr(malloc(TEST_LIMIT * sizeof(void*))); |
| 95 | + for (size_t i = 1lu; i < TEST_LIMIT; i++) |
| 96 | + { |
| 97 | + success &= EXPECT_NOT_NULL(test[i] = nmSysMalloc(i)); |
| 98 | + data[i] = random_init(check_ptr(malloc(i)), i); |
| 99 | + memcpy(test[i], data[i], i); |
| 100 | + } |
| 101 | + for (size_t i = TEST_LIMIT - 1lu; i > 0lu; i--) |
| 102 | + success &= EXPECT_EQL(memcmp(data[i], test[i], i), 0, "%d"); |
| 103 | + |
| 104 | + /** Basic string data is unharmed. **/ |
| 105 | + success &= EXPECT_STR_EQL(str1, "ThisIsSomeData!"); |
| 106 | + success &= EXPECT_STR_EQL(str2, "ThisDataIsDifferentStringData.\n"); |
| 107 | + |
| 108 | + /** Reallocate all variably sized memory to a different size. **/ |
| 109 | + for (size_t i = TEST_LIMIT - 1lu; i > 0lu; i--) |
| 110 | + success &= EXPECT_NOT_NULL(test[i] = nmSysRealloc(test[i], i)); |
| 111 | + for (size_t i = 1lu; i < TEST_LIMIT; i++) |
| 112 | + success &= EXPECT_EQL(memcmp(data[i], test[i], min(i, TEST_LIMIT - i)), 0, "%d"); |
| 113 | + |
| 114 | + /** Basic string data is unharmed. **/ |
| 115 | + success &= EXPECT_STR_EQL(str1, "ThisIsSomeData!"); |
| 116 | + success &= EXPECT_STR_EQL(str2, "ThisDataIsDifferentStringData.\n"); |
| 117 | + |
| 118 | + /** Testing strdup. **/ |
| 119 | + char* str_dup1; |
| 120 | + char* str_dup2; |
| 121 | + success &= EXPECT_NOT_NULL(str_dup1 = nmSysStrdup(str1)); |
| 122 | + success &= EXPECT_NOT_NULL(str_dup2 = nmSysStrdup(str2)); |
| 123 | + success &= EXPECT_STR_EQL(str_dup1, "ThisIsSomeData!"); |
| 124 | + success &= EXPECT_STR_EQL(str_dup2, "ThisDataIsDifferentStringData.\n"); |
| 125 | + str_dup1[12] = '\0'; |
| 126 | + str_dup2[2] = 'a'; |
| 127 | + str_dup2[3] = 't'; |
| 128 | + success &= EXPECT_STR_EQL(str_dup1, "ThisIsSomeDa"); |
| 129 | + success &= EXPECT_STR_EQL(str_dup2, "ThatDataIsDifferentStringData.\n"); |
| 130 | + |
| 131 | + /** Basic string data is unharmed. **/ |
| 132 | + success &= EXPECT_STR_EQL(str1, "ThisIsSomeData!"); |
| 133 | + success &= EXPECT_STR_EQL(str2, "ThisDataIsDifferentStringData.\n"); |
| 134 | + |
| 135 | + /** Free random data, varying sizes. **/ |
| 136 | + for (size_t i = 1lu; i < TEST_LIMIT; i++) |
| 137 | + { |
| 138 | + free(data[i]); |
| 139 | + nmSysFree(test[i]); |
| 140 | + } |
| 141 | + |
| 142 | + /** Basic string data is unharmed. **/ |
| 143 | + success &= EXPECT_STR_EQL(str1, "ThisIsSomeData!"); |
| 144 | + success &= EXPECT_STR_EQL(str2, "ThisDataIsDifferentStringData.\n"); |
| 145 | + |
| 146 | + /** Free data. **/ |
| 147 | + nmSysFree(str1); |
| 148 | + nmSysFree(str2); |
| 149 | + |
| 150 | + /** Dup string data is unharmed. **/ |
| 151 | + success &= EXPECT_STR_EQL(str_dup1, "ThisIsSomeDa"); |
| 152 | + success &= EXPECT_STR_EQL(str_dup2, "ThatDataIsDifferentStringData.\n"); |
| 153 | + |
| 154 | + /** Large singular allocation. **/ |
| 155 | + #define _256MB 256000000lu |
| 156 | + void* large_buf; |
| 157 | + success &= EXPECT_NOT_NULL(large_buf = nmSysMalloc(_256MB)); |
| 158 | + for (size_t i = _256MB - 1lu; i > 0lu; i--) |
| 159 | + *((unsigned char*)large_buf + i) = (unsigned char)(i % 255lu); |
| 160 | + *(unsigned char*)large_buf = 0u; |
| 161 | + for (size_t i = 0lu; i < _256MB; i++) |
| 162 | + success &= EXPECT_EQL(*((unsigned char*)large_buf + i), (unsigned char)(i % 255lu), "%d"); |
| 163 | + |
| 164 | + /** Dup string data is unharmed. **/ |
| 165 | + success &= EXPECT_STR_EQL(str_dup1, "ThisIsSomeDa"); |
| 166 | + success &= EXPECT_STR_EQL(str_dup2, "ThatDataIsDifferentStringData.\n"); |
| 167 | + |
| 168 | + /** Free dups. **/ |
| 169 | + nmSysFree(str_dup1); |
| 170 | + nmSysFree(str_dup2); |
| 171 | + |
| 172 | + /** Free large allocation. **/ |
| 173 | + nmSysFree(large_buf); |
| 174 | + |
| 175 | + /** Expect no captured errors. **/ |
| 176 | + success &= EXPECT_STR_EQL(err_buf, ""); |
| 177 | + |
| 178 | + return success; |
| 179 | + } |
| 180 | + |
| 181 | +long long test(char** tname) |
| 182 | + { |
| 183 | + *tname = "newmalloc-00 nmSysMalloc(), nmSysFree(), nmSysRealloc(), & nmSysStrdup()"; |
| 184 | + return loop_tests(do_tests); |
| 185 | + } |
| 186 | + |
| 187 | +/** Scope cleanup. **/ |
| 188 | +#undef TEST_LIMIT |
| 189 | +#undef _256MB |
0 commit comments