|
| 1 | +#include "unity/unity.h" |
| 2 | +#include "../cross-level/alloc-cl.h" |
| 3 | + |
| 4 | +void setUp(void) { } |
| 5 | + |
| 6 | +void tearDown(void) { } |
| 7 | + |
| 8 | +// Test cases for alloc |
| 9 | +void test_alloc_should_AllocateMemory(void) { |
| 10 | +int *ptr = NULL; |
| 11 | + alloc((void**)&ptr, 2, sizeof(int)); |
| 12 | + TEST_ASSERT_NOT_NULL(ptr); |
| 13 | + free((void*)ptr); // Free the allocated memory |
| 14 | +} |
| 15 | + |
| 16 | +void test_alloc_should_InitializeMemoryToZero(void) { |
| 17 | +int *ptr = NULL; |
| 18 | + alloc((void**)&ptr, 2, sizeof(int)); |
| 19 | + for (int i = 0; i < 2; i++) { |
| 20 | + TEST_ASSERT_EQUAL(0, ptr[i]); |
| 21 | + } |
| 22 | + free((void*)ptr); // Free the allocated memory |
| 23 | +} |
| 24 | + |
| 25 | +// Test cases for alloc_2D |
| 26 | +void test_alloc_2D_should_Allocate2DArray(void) { |
| 27 | +int **ptr = NULL; |
| 28 | + alloc_2D((void***)&ptr, 2, 3, sizeof(int)); |
| 29 | + TEST_ASSERT_NOT_NULL(ptr); |
| 30 | + for (int i = 0; i < 2; i++) { |
| 31 | + TEST_ASSERT_NOT_NULL(ptr[i]); |
| 32 | + } |
| 33 | + free_2D((void**)ptr, 2); // Free the allocated memory |
| 34 | +} |
| 35 | + |
| 36 | +void test_alloc_2D_should_Initialize2DArrayToZero(void) { |
| 37 | +int **ptr = NULL; |
| 38 | + alloc_2D((void***)&ptr, 2, 3, sizeof(int)); |
| 39 | + for (int i = 0; i < 2; i++) { |
| 40 | + for (int j = 0; j < 3; j++) { |
| 41 | + TEST_ASSERT_EQUAL(0, ptr[i][j]); |
| 42 | + } |
| 43 | + } |
| 44 | + free_2D((void**)ptr, 2); // Free the allocated memory |
| 45 | +} |
| 46 | + |
| 47 | +// Test cases for alloc_2DC |
| 48 | +void test_alloc_2DC_should_AllocateContiguous2DArray(void) { |
| 49 | +int **ptr = NULL; |
| 50 | + alloc_2DC((void***)&ptr, 2, 3, sizeof(int)); |
| 51 | + TEST_ASSERT_NOT_NULL(ptr); |
| 52 | + TEST_ASSERT_NOT_NULL(ptr[0]); |
| 53 | + free_2DC((void**)ptr); // Free the allocated memory |
| 54 | +} |
| 55 | + |
| 56 | +void test_alloc_2DC_should_InitializeContiguous2DArrayToZero(void) { |
| 57 | +int **ptr = NULL; |
| 58 | + alloc_2DC((void***)&ptr, 2, 3, sizeof(int)); |
| 59 | + for (int i = 0; i < 2; i++) { |
| 60 | + for (int j = 0; j < 3; j++) { |
| 61 | + TEST_ASSERT_EQUAL(0, ptr[i][j]); |
| 62 | + } |
| 63 | + } |
| 64 | + free_2DC((void**)ptr); // Free the allocated memory |
| 65 | +} |
| 66 | + |
| 67 | +// Test cases for alloc_3D |
| 68 | +void test_alloc_3D_should_Allocate3DArray(void) { |
| 69 | +int ***ptr = NULL; |
| 70 | + alloc_3D((void****)&ptr, 2, 3, 4, sizeof(int)); |
| 71 | + TEST_ASSERT_NOT_NULL(ptr); |
| 72 | + for (int i = 0; i < 2; i++) { |
| 73 | + TEST_ASSERT_NOT_NULL(ptr[i]); |
| 74 | + for (int j = 0; j < 3; j++) { |
| 75 | + TEST_ASSERT_NOT_NULL(ptr[i][j]); |
| 76 | + } |
| 77 | + } |
| 78 | + free_3D((void***)ptr, 2, 3); // Free the allocated memory |
| 79 | +} |
| 80 | + |
| 81 | +void test_alloc_3D_should_Initialize3DArrayToZero(void) { |
| 82 | +int ***ptr = NULL; |
| 83 | + alloc_3D((void****)&ptr, 2, 3, 4, sizeof(int)); |
| 84 | + for (int i = 0; i < 2; i++) { |
| 85 | + for (int j = 0; j < 3; j++) { |
| 86 | + for (int k = 0; k < 4; k++) { |
| 87 | + TEST_ASSERT_EQUAL(0, ptr[i][j][k]); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + free_3D((void***)ptr, 2, 3); // Free the allocated memory |
| 92 | +} |
| 93 | + |
| 94 | +// Test cases for re_alloc |
| 95 | +void test_re_alloc_should_ReallocateMoreMemory(void) { |
| 96 | +int *ptr = NULL; |
| 97 | + alloc((void**)&ptr, 5, sizeof(int)); |
| 98 | + re_alloc((void**)&ptr, 5, 10, sizeof(int)); |
| 99 | + TEST_ASSERT_NOT_NULL(ptr); |
| 100 | + free(ptr); // Free the allocated memory |
| 101 | +} |
| 102 | + |
| 103 | +// Test cases for re_alloc |
| 104 | +void test_re_alloc_should_ReallocateLessMemory(void) { |
| 105 | +int *ptr = NULL; |
| 106 | + alloc((void**)&ptr, 10, sizeof(int)); |
| 107 | + re_alloc((void**)&ptr, 10, 5, sizeof(int)); |
| 108 | + TEST_ASSERT_NOT_NULL(ptr); |
| 109 | + free(ptr); // Free the allocated memory |
| 110 | +} |
| 111 | + |
| 112 | +void test_re_alloc_should_InitializeMoreMemoryToZero(void) { |
| 113 | +int *ptr = NULL; |
| 114 | + alloc((void**)&ptr, 5, sizeof(int)); |
| 115 | + re_alloc((void**)&ptr, 5, 10, sizeof(int)); |
| 116 | + for (int i = 0; i < 10; i++) { |
| 117 | + TEST_ASSERT_EQUAL(0, ptr[i]); |
| 118 | + } |
| 119 | + free(ptr); // Free the allocated memory |
| 120 | +} |
| 121 | + |
| 122 | +void test_re_alloc_should_InitializeLessMemoryToZero(void) { |
| 123 | +int *ptr = NULL; |
| 124 | + alloc((void**)&ptr, 10, sizeof(int)); |
| 125 | + re_alloc((void**)&ptr, 10, 5, sizeof(int)); |
| 126 | + for (int i = 0; i < 5; i++) { |
| 127 | + TEST_ASSERT_EQUAL(0, ptr[i]); |
| 128 | + } |
| 129 | + free(ptr); // Free the allocated memory |
| 130 | +} |
| 131 | + |
| 132 | +// Test cases for re_alloc_2D |
| 133 | +void test_re_alloc_2D_should_Reallocate2DArray(void) { |
| 134 | +int **ptr = NULL; |
| 135 | + alloc_2D((void***)&ptr, 2, 3, sizeof(int)); |
| 136 | + re_alloc_2D((void***)&ptr, 2, 3, 4, 6, sizeof(int)); |
| 137 | + TEST_ASSERT_NOT_NULL(ptr); |
| 138 | + for (int i = 0; i < 4; i++) { |
| 139 | + TEST_ASSERT_NOT_NULL(ptr[i]); |
| 140 | + } |
| 141 | + free_2D((void**)ptr, 4); // Free the allocated memory |
| 142 | +} |
| 143 | + |
| 144 | +void test_re_alloc_2D_should_InitializeNew2DArrayMemoryToZero(void) { |
| 145 | +int **ptr = NULL; |
| 146 | + alloc_2D((void***)&ptr, 2, 3, sizeof(int)); |
| 147 | + re_alloc_2D((void***)&ptr, 2, 3, 4, 6, sizeof(int)); |
| 148 | + for (int i = 0; i < 4; i++) { |
| 149 | + for (int j = 0; j < 6; j++) { |
| 150 | + TEST_ASSERT_EQUAL(0, ptr[i][j]); |
| 151 | + } |
| 152 | + } |
| 153 | + free_2D((void**)ptr, 4); // Free the allocated memory |
| 154 | +} |
| 155 | + |
| 156 | +// Test cases for re_alloc_2DC |
| 157 | +void test_re_alloc_2DC_should_ReallocateContiguous2DArray(void) { |
| 158 | +int **ptr = NULL; |
| 159 | + alloc_2DC((void***)&ptr, 2, 3, sizeof(int)); |
| 160 | + re_alloc_2DC((void***)&ptr, 2, 3, 4, 6, sizeof(int)); |
| 161 | + TEST_ASSERT_NOT_NULL(ptr); |
| 162 | + TEST_ASSERT_NOT_NULL(ptr[0]); |
| 163 | + free_2DC((void**)ptr); // Free the allocated memory |
| 164 | +} |
| 165 | + |
| 166 | +void test_re_alloc_2DC_should_InitializeNewContiguous2DArrayMemoryToZero(void) { |
| 167 | +int **ptr = NULL; |
| 168 | + alloc_2DC((void***)&ptr, 2, 3, sizeof(int)); |
| 169 | + re_alloc_2DC((void***)&ptr, 2, 3, 4, 6, sizeof(int)); |
| 170 | + for (int i = 0; i < 4; i++) { |
| 171 | + for (int j = 0; j < 6; j++) { |
| 172 | + TEST_ASSERT_EQUAL(0, ptr[i][j]); |
| 173 | + } |
| 174 | + } |
| 175 | + free_2DC((void**)ptr); // Free the allocated memory |
| 176 | +} |
| 177 | + |
| 178 | +// Test cases for re_alloc_3D |
| 179 | +void test_re_alloc_3D_should_Reallocate3DArray(void) { |
| 180 | +int ***ptr = NULL; |
| 181 | + alloc_3D((void****)&ptr, 2, 3, 5, sizeof(int)); |
| 182 | + re_alloc_3D((void****)&ptr, 2, 3, 5, 4, 6, 10, sizeof(int)); |
| 183 | + TEST_ASSERT_NOT_NULL(ptr); |
| 184 | + for (int i = 0; i < 4; i++) { |
| 185 | + TEST_ASSERT_NOT_NULL(ptr[i]); |
| 186 | + for (int j = 0; j < 6; j++) { |
| 187 | + TEST_ASSERT_NOT_NULL(ptr[i][j]); |
| 188 | + } |
| 189 | + } |
| 190 | + free_3D((void***)ptr, 4, 6); // Free the allocated memory |
| 191 | +} |
| 192 | + |
| 193 | +void test_re_alloc_3D_should_InitializeNew3DArrayMemoryToZero(void) { |
| 194 | +int ***ptr = NULL; |
| 195 | + alloc_3D((void****)&ptr, 2, 3, 5, sizeof(int)); |
| 196 | + re_alloc_3D((void****)&ptr, 2, 3, 5, 4, 6, 10, sizeof(int)); |
| 197 | + for (int i = 0; i < 4; i++) { |
| 198 | + for (int j = 0; j < 6; j++) { |
| 199 | + for (int k = 0; k < 10; k++) { |
| 200 | + TEST_ASSERT_EQUAL(0, ptr[i][j][k]); |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + free_3D((void***)ptr, 4, 6); // Free the allocated memory |
| 205 | +} |
| 206 | + |
| 207 | +int main(void) { |
| 208 | + |
| 209 | + UNITY_BEGIN(); |
| 210 | + |
| 211 | + RUN_TEST(test_alloc_should_AllocateMemory); |
| 212 | + RUN_TEST(test_alloc_should_InitializeMemoryToZero); |
| 213 | + |
| 214 | + RUN_TEST(test_alloc_2D_should_Allocate2DArray); |
| 215 | + RUN_TEST(test_alloc_2D_should_Initialize2DArrayToZero); |
| 216 | + |
| 217 | + RUN_TEST(test_alloc_2DC_should_AllocateContiguous2DArray); |
| 218 | + RUN_TEST(test_alloc_2DC_should_InitializeContiguous2DArrayToZero); |
| 219 | + |
| 220 | + RUN_TEST(test_alloc_3D_should_Allocate3DArray); |
| 221 | + RUN_TEST(test_alloc_3D_should_Initialize3DArrayToZero); |
| 222 | + |
| 223 | + RUN_TEST(test_re_alloc_should_ReallocateMoreMemory); |
| 224 | + RUN_TEST(test_re_alloc_should_InitializeMoreMemoryToZero); |
| 225 | + RUN_TEST(test_re_alloc_should_ReallocateLessMemory); |
| 226 | + RUN_TEST(test_re_alloc_should_InitializeLessMemoryToZero); |
| 227 | + |
| 228 | + RUN_TEST(test_re_alloc_2D_should_Reallocate2DArray); |
| 229 | + RUN_TEST(test_re_alloc_2D_should_InitializeNew2DArrayMemoryToZero); |
| 230 | + |
| 231 | + RUN_TEST(test_re_alloc_2DC_should_ReallocateContiguous2DArray); |
| 232 | + RUN_TEST(test_re_alloc_2DC_should_InitializeNewContiguous2DArrayMemoryToZero); |
| 233 | + |
| 234 | + RUN_TEST(test_re_alloc_3D_should_Reallocate3DArray); |
| 235 | + RUN_TEST(test_re_alloc_3D_should_InitializeNew3DArrayMemoryToZero); |
| 236 | + |
| 237 | + return UNITY_END(); |
| 238 | + |
| 239 | +} |
| 240 | + |
0 commit comments