Skip to content

Commit 9669020

Browse files
committed
Trying to check that values out of concatenation are correct
1 parent d9e2357 commit 9669020

File tree

1 file changed

+117
-20
lines changed

1 file changed

+117
-20
lines changed

tests/b2nd/test_b2nd_concatenate.c

Lines changed: 117 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,57 @@ typedef struct {
2121
} test_shapes_t;
2222

2323

24+
/**
25+
* Helper function for recursive region filling
26+
*/
27+
static int fill_recursive_region(uint8_t *buffer,
28+
int64_t *strides,
29+
int8_t ndim,
30+
int64_t *start,
31+
int64_t *stop,
32+
const void *value,
33+
uint8_t typesize,
34+
int dim,
35+
int64_t current_offset) {
36+
if (dim == ndim) {
37+
// We've reached the innermost dimension, copy the value
38+
memcpy(buffer + (current_offset * typesize), value, typesize);
39+
return 0;
40+
}
41+
42+
// Iterate through the current dimension within the region
43+
for (int64_t i = start[dim]; i < stop[dim]; i++) {
44+
int64_t offset = current_offset + i * strides[dim];
45+
int err = fill_recursive_region(buffer, strides, ndim, start, stop,
46+
value, typesize, dim + 1, offset);
47+
if (err < 0) return err;
48+
}
49+
return 0;
50+
}
51+
52+
/**
53+
* Fill a region of a multidimensional buffer with a constant value.
54+
*/
55+
int fill_buffer_region(uint8_t *buffer,
56+
int64_t *buffer_shape,
57+
int8_t ndim,
58+
int64_t *start,
59+
int64_t *stop,
60+
const void *value,
61+
uint8_t typesize) {
62+
// Calculate strides for the buffer
63+
int64_t strides[B2ND_MAX_DIM];
64+
strides[ndim - 1] = 1;
65+
for (int i = ndim - 2; i >= 0; i--) {
66+
strides[i] = strides[i + 1] * buffer_shape[i + 1];
67+
}
68+
69+
// Start the recursive filling
70+
return fill_recursive_region(buffer, strides, ndim, start, stop,
71+
value, typesize, 0, 0);
72+
}
73+
74+
2475
CUTEST_TEST_SETUP(concatenate) {
2576
blosc2_init();
2677

@@ -88,12 +139,16 @@ CUTEST_TEST_TEST(concatenate) {
88139
size_t buffersize = typesize;
89140
for (int i = 0; i < shapes.ndim; ++i) {
90141
if (i == axis) {
142+
helpershape[i] = shapes.shape1[i] + shapes.shape2[i];
91143
buffersize *= (size_t) (shapes.shape1[i] + shapes.shape2[i]);
92144
}
93145
else {
146+
helpershape[i] = shapes.shape1[i];
94147
buffersize *= (size_t) shapes.shape1[i];
95148
}
96149
}
150+
// Allocate a buffer for the concatenated array
151+
uint8_t *helperbuffer = malloc(buffersize);
97152

98153
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
99154
cparams.nthreads = 2;
@@ -112,6 +167,9 @@ CUTEST_TEST_TEST(concatenate) {
112167
b2nd_array_t *src1;
113168
BLOSC_ERROR(b2nd_zeros(ctx1, &src1));
114169

170+
// Fill helperbuffer with zeros
171+
memset(helperbuffer, 0, buffersize);
172+
115173
/* Create src2 with a value */
116174
b2nd_array_t *src2;
117175
uint8_t *value = malloc(typesize);
@@ -157,6 +215,25 @@ CUTEST_TEST_TEST(concatenate) {
157215
NULL, 0, NULL, 0);
158216
B2ND_TEST_ASSERT(b2nd_concatenate(ctx, src1, src2, &array, axis));
159217

218+
// Fill the proper section of the helperbuffer with the value from src2
219+
int64_t start_src2[B2ND_MAX_DIM] = {0};
220+
int64_t stop_src2[B2ND_MAX_DIM];
221+
222+
// Set up the region to fill (corresponding to src2's position)
223+
for (int i = 0; i < shapes.ndim; i++) {
224+
if (i == axis) {
225+
start_src2[i] = shapes.shape1[i]; // src2 starts after src1
226+
stop_src2[i] = shapes.shape1[i] + shapes.shape2[i];
227+
} else {
228+
start_src2[i] = 0;
229+
stop_src2[i] = shapes.shape2[i];
230+
}
231+
}
232+
233+
// Fill the region with the value
234+
fill_buffer_region(helperbuffer, helpershape, shapes.ndim,
235+
start_src2, stop_src2, value, typesize);
236+
160237
// Check the shape of the concatenated array
161238
for (int i = 0; i < ctx->ndim; ++i) {
162239
if (i == axis) {
@@ -175,6 +252,10 @@ CUTEST_TEST_TEST(concatenate) {
175252
}
176253

177254
// Check the data in the concatenated array
255+
printf("Array shapes: %ld x %ld\n", array->shape[0], array->shape[1]);
256+
printf("Helperbuffer shapes: %ld x %ld\n", helpershape[0], helpershape[1]);
257+
printf("Axis: %d\n", axis);
258+
178259
int64_t start[B2ND_MAX_DIM] = {0};
179260
int64_t stop[B2ND_MAX_DIM] = {0};
180261
int64_t buffershape[B2ND_MAX_DIM] = {0};
@@ -186,33 +267,49 @@ CUTEST_TEST_TEST(concatenate) {
186267
}
187268
uint8_t *buffer = malloc(buffersize);
188269
B2ND_TEST_ASSERT(b2nd_get_slice_cbuffer(array, start, stop, buffer, buffershape, buffersize));
270+
// Check if the data in the concatenated array matches the helperbuffer
271+
uint8_t *buffer_fill = malloc(typesize);
189272
for (int64_t i = 0; i < buffersize / typesize; ++i) {
273+
bool is_true = false;
190274
switch (typesize) {
191-
case 8:
192-
B2ND_TEST_ASSERT(((int64_t *) buffer)[i] == (i + 1));
193-
break;
194-
case 4:
195-
B2ND_TEST_ASSERT(((int32_t *) buffer)[i] == (i + 1));
196-
break;
197-
case 2:
198-
B2ND_TEST_ASSERT(((int16_t *) buffer)[i] == (i + 1));
199-
break;
200-
case 1:
201-
printf("Checking value at index %lld: %d\n", i, ((int8_t *) buffer)[i]);
202-
CUTEST_ASSERT("Value is not equal!", ((int8_t *) buffer)[i] == 0);
203-
break;
204-
default:
205-
// Check the value in the buffer
206-
for (int j = 0; j < typesize; ++j) {
207-
B2ND_TEST_ASSERT(buffer[i * typesize + j] == value[j]);
275+
case 8:
276+
is_true = ((int64_t *) buffer)[i] == ((int64_t *) helperbuffer)[i];
277+
break;
278+
case 4:
279+
is_true = ((int32_t *) buffer)[i] == ((int32_t *) helperbuffer)[i];
280+
break;
281+
case 2:
282+
is_true = ((int16_t *) buffer)[i] == ((int16_t *) helperbuffer)[i];
283+
break;
284+
case 1:
285+
is_true = ((int8_t *) buffer)[i] == ((int8_t *) helperbuffer)[i];
286+
break;
287+
default:
288+
// For default case, don't copy helperbuffer over buffer data
289+
memcpy(buffer_fill, &buffer[i * typesize], typesize);
290+
is_true = memcmp(buffer_fill, helperbuffer + i * typesize, typesize) == 0;
291+
break;
292+
}
293+
if (!is_true) {
294+
// Print the raw byte values for better debugging
295+
fprintf(stderr, "Data mismatch at index %ld: buffer bytes = ", i);
296+
for (int b = 0; b < typesize + 10; b++) {
297+
fprintf(stderr, "%02x ", buffer[i * typesize + b]);
298+
}
299+
fprintf(stderr, ", helperbuffer bytes = ");
300+
for (int b = 0; b < typesize + 10; b++) {
301+
fprintf(stderr, "%02x ", helperbuffer[i * typesize + b]);
208302
}
209-
break;
303+
fprintf(stderr, "\n");
210304
}
305+
CUTEST_ASSERT("Data in the concatenated array does not match the helperbuffer",
306+
is_true);
211307
}
308+
free(buffer_fill);
212309

213310
/* Free mallocs */
214-
// free(buffer);
215-
// free(destbuffer);
311+
free(buffer);
312+
free(helperbuffer);
216313
free(value);
217314
B2ND_TEST_ASSERT(b2nd_free(src1));
218315
B2ND_TEST_ASSERT(b2nd_free(src2));

0 commit comments

Comments
 (0)