1 /*
2 * Copyright (c) 2018 Francesc Alted, Aleix Alcacer.
3 * Copyright (C) 2019-present Blosc Development team <blosc@blosc.org>
4 * All rights reserved.
5 *
6 * This source code is licensed under both the BSD-style license (found in the
7 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
8 * in the COPYING file in the root directory of this source tree).
9 * You may select, at your option, one of the above-listed licenses.
10 */
11
12 #include "test_common.h"
13
14
CUTEST_TEST_DATA(full)15 CUTEST_TEST_DATA(full) {
16 caterva_ctx_t *ctx;
17 };
18
19
CUTEST_TEST_SETUP(full)20 CUTEST_TEST_SETUP(full) {
21 caterva_config_t cfg = CATERVA_CONFIG_DEFAULTS;
22 cfg.nthreads = 2;
23 cfg.compcodec = BLOSC_BLOSCLZ;
24 caterva_ctx_new(&cfg, &data->ctx);
25
26 // Add parametrizations
27 CUTEST_PARAMETRIZE(itemsize, uint8_t, CUTEST_DATA(
28 1, 2, 4, 8
29 ));
30 CUTEST_PARAMETRIZE(shapes, _test_shapes, CUTEST_DATA(
31 {0, {0}, {0}, {0}}, // 0-dim
32 {1, {5}, {3}, {2}}, // 1-idim
33 {2, {20, 0}, {7, 0}, {3, 0}}, // 0-shape
34 {2, {20, 10}, {7, 5}, {3, 5}}, // 0-shape
35 {2, {14, 10}, {8, 5}, {2, 2}}, // general,
36 {3, {12, 10, 14}, {3, 5, 9}, {3, 4, 4}}, // general
37 {3, {10, 21, 30, 55}, {8, 7, 15, 3}, {5, 5, 10, 1}}, // general,
38 ));
39 CUTEST_PARAMETRIZE(backend, _test_backend, CUTEST_DATA(
40 {false, false},
41 {true, false},
42 {true, true},
43 {false, true},
44 ));
45 CUTEST_PARAMETRIZE(fill_value, int8_t, CUTEST_DATA(
46 3, 113, 33, -5
47 ));
48 }
49
50
CUTEST_TEST_TEST(full)51 CUTEST_TEST_TEST(full) {
52 CUTEST_GET_PARAMETER(backend, _test_backend);
53 CUTEST_GET_PARAMETER(shapes, _test_shapes);
54 CUTEST_GET_PARAMETER(itemsize, uint8_t);
55 CUTEST_GET_PARAMETER(fill_value, int8_t);
56
57
58 char *urlpath = "test_full.b2frame";
59 caterva_remove(data->ctx, urlpath);
60
61 caterva_params_t params;
62 params.itemsize = itemsize;
63 params.ndim = shapes.ndim;
64 for (int i = 0; i < shapes.ndim; ++i) {
65 params.shape[i] = shapes.shape[i];
66 }
67
68 caterva_storage_t storage = {0};
69 if (backend.persistent) {
70 storage.urlpath = urlpath;
71 }
72 storage.sequencial = backend.sequential;
73 for (int i = 0; i < shapes.ndim; ++i) {
74 storage.chunkshape[i] = shapes.chunkshape[i];
75 storage.blockshape[i] = shapes.blockshape[i];
76 }
77
78 /* Create original data */
79 int64_t buffersize = itemsize;
80 for (int i = 0; i < shapes.ndim; ++i) {
81 buffersize *= shapes.shape[i];
82 }
83
84 /* Create caterva_array_t with original data */
85 caterva_array_t *src;
86 uint8_t *value = malloc(itemsize);
87 switch (itemsize) {
88 case 8:
89 ((int64_t *) value)[0] = (int64_t) fill_value;
90 break;
91 case 4:
92 ((int32_t *) value)[0] = (int32_t) fill_value;
93 break;
94 case 2:
95 ((int16_t *) value)[0] = (int16_t) fill_value;
96 break;
97 case 1:
98 ((int8_t *) value)[0] = fill_value;
99 break;
100 default:
101 break;
102 }
103
104 CATERVA_TEST_ASSERT(caterva_full(data->ctx, ¶ms, &storage, value, &src));
105
106 /* Fill dest array with caterva_array_t data */
107 uint8_t *buffer_dest = malloc( buffersize);
108 CATERVA_TEST_ASSERT(caterva_to_buffer(data->ctx, src, buffer_dest, buffersize));
109
110 /* Testing */
111 for (int i = 0; i < buffersize / itemsize; ++i) {
112 bool is_true = false;
113 switch (itemsize) {
114 case 8:
115 is_true = ((int64_t *) buffer_dest)[i] == fill_value;
116 break;
117 case 4:
118 is_true = ((int32_t *) buffer_dest)[i] == fill_value;
119 break;
120 case 2:
121 is_true = ((int16_t *) buffer_dest)[i] == fill_value;
122 break;
123 case 1:
124 is_true = ((int8_t *) buffer_dest)[i] == fill_value;
125 break;
126 default:
127 break;
128 }
129 CUTEST_ASSERT("Elements are not equals", is_true);
130 }
131
132 /* Free mallocs */
133 free(buffer_dest);
134 free(value);
135 CATERVA_TEST_ASSERT(caterva_free(data->ctx, &src));
136 caterva_remove(data->ctx, urlpath);
137
138 return CATERVA_SUCCEED;
139 }
140
141
CUTEST_TEST_TEARDOWN(full)142 CUTEST_TEST_TEARDOWN(full) {
143 caterva_ctx_free(&data->ctx);
144 }
145
main()146 int main() {
147 CUTEST_TEST_RUN(full);
148 }
149