Skip to content

Commit d9e2357

Browse files
committed
Improvements in the test (not yet there)
1 parent 9d670ef commit d9e2357

File tree

2 files changed

+130
-68
lines changed

2 files changed

+130
-68
lines changed

blosc/b2nd.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,11 @@ int b2nd_concatenate(b2nd_context_t *ctx, const b2nd_array_t *src1,
13721372
src2->sc->typesize * src2->extchunknitems));
13731373
// Get multidimensional chunk position
13741374
int64_t nchunk_ndim[B2ND_MAX_DIM] = {0};
1375-
blosc2_unidim_to_multidim(src2->ndim, (int64_t*)(src2->chunkshape), nchunk, nchunk_ndim);
1375+
int64_t chunkshape[B2ND_MAX_DIM] = {0};
1376+
for (int8_t i = 0; i < src2->ndim; ++i) {
1377+
chunkshape[i] = src2->chunkshape[i];
1378+
}
1379+
blosc2_unidim_to_multidim(src2->ndim, chunkshape, nchunk, nchunk_ndim);
13761380

13771381
// Set positions for each dimension
13781382
for (int8_t i = 0; i < src2->ndim; ++i) {
@@ -1390,7 +1394,7 @@ int b2nd_concatenate(b2nd_context_t *ctx, const b2nd_array_t *src1,
13901394
}
13911395

13921396
// Copy the chunk to the correct position
1393-
BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, (int64_t*)(src2->chunkshape),
1397+
BLOSC_ERROR(b2nd_set_slice_cbuffer(buffer, chunkshape,
13941398
src2->sc->typesize * src2->extchunknitems,
13951399
start, stop, *array));
13961400
}

tests/b2nd/test_b2nd_concatenate.c

Lines changed: 124 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,31 @@
1212

1313
typedef struct {
1414
int8_t ndim;
15-
int64_t shape[B2ND_MAX_DIM];
16-
int32_t chunkshape[B2ND_MAX_DIM];
17-
int32_t blockshape[B2ND_MAX_DIM];
18-
int64_t start[B2ND_MAX_DIM];
19-
int64_t stop[B2ND_MAX_DIM];
15+
int64_t shape1[B2ND_MAX_DIM];
16+
int32_t chunkshape1[B2ND_MAX_DIM];
17+
int32_t blockshape1[B2ND_MAX_DIM];
18+
int64_t shape2[B2ND_MAX_DIM];
19+
int32_t chunkshape2[B2ND_MAX_DIM];
20+
int32_t blockshape2[B2ND_MAX_DIM];
2021
} test_shapes_t;
2122

2223

2324
CUTEST_TEST_SETUP(concatenate) {
2425
blosc2_init();
2526

26-
// Add parametrizations
27+
// Add parametrization
2728
CUTEST_PARAMETRIZE(typesize, uint8_t, CUTEST_DATA(
2829
1,
29-
2,
30-
4,
31-
8,
30+
// 2,
31+
// 4,
32+
// 8,
3233
));
3334

3435
CUTEST_PARAMETRIZE(backend, _test_backend, CUTEST_DATA(
3536
{false, false},
36-
{true, false},
37-
{true, true},
38-
{false, true},
37+
// {true, false},
38+
// {true, true},
39+
// {false, true},
3940
));
4041

4142

@@ -47,13 +48,7 @@ CUTEST_TEST_SETUP(concatenate) {
4748
// {2, {14, 10}, {8, 5}, {2, 2}, {5, 3}, {9, 10}},
4849
// {3, {12, 10, 14}, {3, 5, 9}, {3, 4, 4}, {3, 0, 3}, {6, 7, 10}},
4950
// {4, {10, 21, 30, 5}, {8, 7, 15, 3}, {5, 5, 10, 1}, {5, 4, 3, 3}, {10, 8, 8, 4}},
50-
{2, {50, 50}, {25, 13}, {8, 8}, {0, 0}, {10, 10}},
51-
// The case below makes qemu-aarch64 (AARCH64 emulation) in CI (Ubuntu 22.04) to crash with a segfault.
52-
// Interestingly, this works perfectly well on both intel64 (native) and in aarch64 (emulated via docker).
53-
// Moreover, valgrind does not issue any warning at all when run in the later platforms.
54-
// In conclusion, this *may* be revealing a bug in the qemu-aarch64 binaries in Ubuntu 22.04.
55-
// {2, {143, 41}, {18, 13}, {7, 7}, {4, 2}, {6, 5}},
56-
// Replacing the above line by this one makes qemu-aarch64 happy.
51+
{2, {50, 50}, {25, 13}, {5, 8}, {50, 50}, {25, 13}, {5, 8}},
5752
// {2, {150, 45}, {15, 15}, {7, 7}, {4, 2}, {6, 5}},
5853
// {2, {10, 10}, {5, 7}, {2, 2}, {0, 0}, {5, 5}},
5954
// // Checks for fast path in setting a single chunk that is C contiguous
@@ -62,8 +57,15 @@ CUTEST_TEST_SETUP(concatenate) {
6257
// {3, {3, 8, 5}, {1, 4, 5}, {1, 2, 5}, {1, 4, 0}, {2, 8, 5}},
6358
));
6459
CUTEST_PARAMETRIZE(fill_value, int8_t, CUTEST_DATA(
65-
3, 113, 33, -5
66-
));
60+
3,
61+
// 113,
62+
// 33,
63+
// -5
64+
));
65+
CUTEST_PARAMETRIZE(axis, int8_t, CUTEST_DATA(
66+
0,
67+
1,
68+
));
6769

6870
}
6971

@@ -72,38 +74,45 @@ CUTEST_TEST_TEST(concatenate) {
7274
CUTEST_GET_PARAMETER(shapes, test_shapes_t);
7375
CUTEST_GET_PARAMETER(typesize, uint8_t);
7476
CUTEST_GET_PARAMETER(fill_value, int8_t);
77+
CUTEST_GET_PARAMETER(axis, int8_t);
7578

7679
char *urlpath = "test_concatenate.b2frame";
80+
char *urlpath1 = "test_concatenate1.b2frame";
81+
char *urlpath2 = "test_concatenate2.b2frame";
7782
blosc2_remove_urlpath(urlpath);
83+
blosc2_remove_urlpath(urlpath1);
84+
blosc2_remove_urlpath(urlpath2);
85+
86+
// Create a helper buffer for storing the final array for the concatenation in C
87+
int64_t helpershape[B2ND_MAX_DIM] = {0};
88+
size_t buffersize = typesize;
89+
for (int i = 0; i < shapes.ndim; ++i) {
90+
if (i == axis) {
91+
buffersize *= (size_t) (shapes.shape1[i] + shapes.shape2[i]);
92+
}
93+
else {
94+
buffersize *= (size_t) shapes.shape1[i];
95+
}
96+
}
7897

7998
blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
8099
cparams.nthreads = 2;
81100
cparams.typesize = typesize;
82-
blosc2_storage b2_storage = {.cparams=&cparams};
101+
blosc2_storage b2_storage1 = {.cparams=&cparams};
83102
if (backend.persistent) {
84-
b2_storage.urlpath = urlpath;
103+
b2_storage1.urlpath = urlpath1;
85104
}
86-
b2_storage.contiguous = backend.contiguous;
105+
b2_storage1.contiguous = backend.contiguous;
106+
b2nd_context_t *ctx1 = b2nd_create_ctx(&b2_storage1, shapes.ndim, shapes.shape1,
107+
shapes.chunkshape1, shapes.blockshape1, NULL,
108+
0, NULL, 0);
87109

88-
b2nd_context_t *ctx = b2nd_create_ctx(&b2_storage, shapes.ndim, shapes.shape,
89-
shapes.chunkshape, shapes.blockshape, NULL, 0, NULL, 0);
90-
91-
/* Create dest buffer */
92-
int64_t shape[B2ND_MAX_DIM] = {0};
93-
int64_t buffersize = typesize;
94-
for (int i = 0; i < ctx->ndim; ++i) {
95-
shape[i] = shapes.stop[i] - shapes.start[i];
96-
buffersize *= shape[i];
97-
}
98-
99-
// uint8_t *buffer = malloc(buffersize);
100-
// CUTEST_ASSERT("Buffer filled incorrectly", fill_buf(buffer, typesize, buffersize / typesize));
101110

102111
/* Create src1 with zeros */
103112
b2nd_array_t *src1;
104-
BLOSC_ERROR(b2nd_zeros(ctx, &src1));
105-
106-
/* Create src2 with ones */
113+
BLOSC_ERROR(b2nd_zeros(ctx1, &src1));
114+
115+
/* Create src2 with a value */
107116
b2nd_array_t *src2;
108117
uint8_t *value = malloc(typesize);
109118
switch (typesize) {
@@ -126,35 +135,80 @@ CUTEST_TEST_TEST(concatenate) {
126135
}
127136
break;
128137
}
129-
B2ND_TEST_ASSERT(b2nd_full(ctx, &src2, value));
138+
blosc2_storage b2_storage2 = {.cparams=&cparams};
139+
if (backend.persistent) {
140+
b2_storage2.urlpath = urlpath2;
141+
}
142+
b2_storage2.contiguous = backend.contiguous;
143+
b2nd_context_t *ctx2 = b2nd_create_ctx(&b2_storage2, shapes.ndim, shapes.shape2,
144+
shapes.chunkshape2, shapes.blockshape2,
145+
NULL, 0, NULL, 0);
146+
B2ND_TEST_ASSERT(b2nd_full(ctx2, &src2, value));
130147

131148
/* Concatenate src1 and src2 */
132149
b2nd_array_t *array = NULL;
133-
B2ND_TEST_ASSERT(b2nd_concatenate(ctx, src1, src2, &array, 0));
134-
135-
// for (uint64_t i = 0; i < (uint64_t) buffersize / typesize; ++i) {
136-
// uint64_t k = i + 1;
137-
// switch (typesize) {
138-
// case 8:
139-
// CUTEST_ASSERT("Elements are not equals!",
140-
// (uint64_t) k == ((uint64_t *) destbuffer)[i]);
141-
// break;
142-
// case 4:
143-
// CUTEST_ASSERT("Elements are not equals!",
144-
// (uint32_t) k == ((uint32_t *) destbuffer)[i]);
145-
// break;
146-
// case 2:
147-
// CUTEST_ASSERT("Elements are not equals!",
148-
// (uint16_t) k == ((uint16_t *) destbuffer)[i]);
149-
// break;
150-
// case 1:
151-
// CUTEST_ASSERT("Elements are not equals!",
152-
// (uint8_t) k == ((uint8_t *) destbuffer)[i]);
153-
// break;
154-
// default:
155-
// B2ND_TEST_ASSERT(BLOSC2_ERROR_INVALID_PARAM);
156-
// }
157-
// }
150+
blosc2_storage b2_storage = {.cparams=&cparams};
151+
if (backend.persistent) {
152+
b2_storage.urlpath = urlpath;
153+
}
154+
b2_storage2.contiguous = backend.contiguous;
155+
b2nd_context_t *ctx = b2nd_create_ctx(&b2_storage, shapes.ndim, shapes.shape1,
156+
shapes.chunkshape1, shapes.blockshape1,
157+
NULL, 0, NULL, 0);
158+
B2ND_TEST_ASSERT(b2nd_concatenate(ctx, src1, src2, &array, axis));
159+
160+
// Check the shape of the concatenated array
161+
for (int i = 0; i < ctx->ndim; ++i) {
162+
if (i == axis) {
163+
CUTEST_ASSERT("Shape is not equal!",
164+
array->shape[i] == shapes.shape1[i] + shapes.shape2[i]);
165+
}
166+
else {
167+
CUTEST_ASSERT("Shape is not equal!",
168+
array->shape[i] == shapes.shape1[i]);
169+
}
170+
}
171+
172+
// Check the chunkshape of the concatenated array
173+
for (int i = 0; i < ctx->ndim; ++i) {
174+
CUTEST_ASSERT("Chunkshape is not equal!", array->chunkshape[i] == shapes.chunkshape1[i]);
175+
}
176+
177+
// Check the data in the concatenated array
178+
int64_t start[B2ND_MAX_DIM] = {0};
179+
int64_t stop[B2ND_MAX_DIM] = {0};
180+
int64_t buffershape[B2ND_MAX_DIM] = {0};
181+
for (int i = 0; i < ctx->ndim; ++i) {
182+
start[i] = 0;
183+
stop[i] = array->shape[i];
184+
buffershape[i] = stop[i] - start[i];
185+
buffersize *= buffershape[i];
186+
}
187+
uint8_t *buffer = malloc(buffersize);
188+
B2ND_TEST_ASSERT(b2nd_get_slice_cbuffer(array, start, stop, buffer, buffershape, buffersize));
189+
for (int64_t i = 0; i < buffersize / typesize; ++i) {
190+
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]);
208+
}
209+
break;
210+
}
211+
}
158212

159213
/* Free mallocs */
160214
// free(buffer);
@@ -164,7 +218,11 @@ CUTEST_TEST_TEST(concatenate) {
164218
B2ND_TEST_ASSERT(b2nd_free(src2));
165219
B2ND_TEST_ASSERT(b2nd_free(array));
166220
B2ND_TEST_ASSERT(b2nd_free_ctx(ctx));
221+
B2ND_TEST_ASSERT(b2nd_free_ctx(ctx1));
222+
B2ND_TEST_ASSERT(b2nd_free_ctx(ctx2));
167223
blosc2_remove_urlpath(urlpath);
224+
blosc2_remove_urlpath(urlpath1);
225+
blosc2_remove_urlpath(urlpath2);
168226

169227
return 0;
170228
}

0 commit comments

Comments
 (0)