Skip to content

Commit 299a1bb

Browse files
committed
alloc unit tests
1 parent 4b77cbd commit 299a1bb

File tree

4 files changed

+248
-5
lines changed

4 files changed

+248
-5
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ cross: string_cl enum_cl cite_cl utils_cl alloc_cl brick_cl imagefuns_cl param_c
124124
lower: table_ll param_ll meta_ll cube_ll equi7_ll glance7_ll atc_ll sunview_ll read_ll radtran_ll topo_ll cloud_ll gas_ll brdf_ll atmo_ll aod_ll resmerge_ll coreg_ll coregfuns_ll acix_ll modwvp_ll
125125
higher: param_hl progress_hl tasks_hl read-aux_hl read-ard_hl quality_hl bap_hl level3_hl cso_hl tsa_hl index_hl interpolate_hl stm_hl fold_hl standardize_hl pheno_hl polar_hl trend_hl ml_hl texture_hl lsm_hl lib_hl sample_hl imp_hl cfimp_hl l2imp_hl spec-adjust_hl pyp_hl rsp_hl udf_hl
126126
aux: param_aux param_train_aux train_aux
127-
unit-tests: test_utils-cl
127+
unit-tests: test_utils-cl test_alloc-cl
128128
exe: force-parameter force-qai-inflate force-tile-finder force-tabulate-grid force-l2ps force-higher-level force-train force-lut-modis force-mdcp force-stack force-import-modis force-cube-init force-hist force-stratified-sample
129129
.PHONY: temp all install install_ bash python rstats misc external clean build check
130130

@@ -437,6 +437,9 @@ force-stratified-sample: temp cross $(DA)/_stratified-sample.c
437437

438438
### UNIT TESTING
439439

440+
test_alloc-cl: temp cross $(DU)/unity/unity.c $(DU)/test_alloc-cl.c
441+
$(G11) $(CFLAGS) $(GDAL) $(GSL) $(CURL) -o $(TU)/test_alloc-cl $(DU)/test_alloc-cl.c $(TC)/*.o $(DU)/unity/unity.c $(LDGDAL) $(LDGSL) $(LDCURL)
442+
440443
test_utils-cl: temp cross $(DU)/unity/unity.c $(DU)/test_utils-cl.c
441444
$(G11) $(CFLAGS) $(GDAL) $(GSL) $(CURL) -o $(TU)/test_utils-cl $(DU)/test_utils-cl.c $(TC)/*.o $(DU)/unity/unity.c $(LDGDAL) $(LDGSL) $(LDCURL)
442445

misc/force-version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.7.12-dev:::2024-07-18_13:26:45
1+
3.7.12-dev:::2024-07-19_08:26:04

src/unit-testing/test_alloc-cl.c

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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+

src/unit-testing/test_utils-cl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ void test_num_decimal_places_NegativeNumbers(void) {
5858
}
5959

6060
void test_num_decimal_places_LargeNumbers(void) {
61-
TEST_ASSERT_EQUAL_INT(10, num_decimal_places(1234567890));
62-
TEST_ASSERT_EQUAL_INT(10, num_decimal_places(INT_MAX));
63-
TEST_ASSERT_EQUAL_INT(10, num_decimal_places(INT_MIN));
61+
TEST_ASSERT_EQUAL_INT(10, num_decimal_places(1234567890));
62+
TEST_ASSERT_EQUAL_INT(10, num_decimal_places(INT_MAX));
63+
TEST_ASSERT_EQUAL_INT(10, num_decimal_places(INT_MIN));
6464
}
6565

6666
int main(void) {

0 commit comments

Comments
 (0)