File: test_urcodecs.c

package info (click to toggle)
c-blosc2 2.23.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,060 kB
  • sloc: ansic: 46,362; python: 332; lisp: 82; makefile: 36; sh: 3
file content (267 lines) | stat: -rw-r--r-- 6,491 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
  Copyright (c) 2021  Blosc Development Team <blosc@blosc.org>
  https://blosc.org
  License: BSD 3-Clause (see LICENSE.txt)
*/

#include <stdio.h>
#include <stdint.h>

#include "blosc2.h"
#include "cutest.h"


#define KB  1024
#define MB  (1024*KB)
#define GB  (1024*MB)

#define NCHUNKS (10)
#define CHUNKSIZE (5 * 1000)  // > NCHUNKS for the bench purposes
#define NTHREADS 4



int codec_encoder(const uint8_t* input, int32_t input_len,
                  uint8_t* output, int32_t output_len,
                  uint8_t meta,
                  blosc2_cparams* cparams, const void* chunk) {
  BLOSC_UNUSED_PARAM(chunk);
  if (cparams->schunk == NULL) {
    return -1;
  }
  if (cparams->typesize != 4) {
    fprintf(stderr, "Itemsize %d != 4", cparams->typesize);
    return BLOSC2_ERROR_FAILURE;
  }
  uint8_t *content;
  int32_t content_len;
  blosc2_vlmeta_get(cparams->schunk, "codec_arange", &content, &content_len);
  if (content[0] != 222) {
    return -1;
  }
  free(content);

  if (meta != 111) {
    return -1;
  }

  int32_t nelem = input_len / 4;
  int32_t *in_ = ((int32_t *) input);
  int32_t *out_ = ((int32_t *) output);

  // Check that is an arange
  int32_t start = in_[0];
  int32_t step = in_[1] - start;
  for (int i = 1; i < nelem - 1; ++i) {
    if (in_[i + 1] - in_[i] != step) {
      fprintf(stderr, "Buffer is not an arange");
      return BLOSC2_ERROR_FAILURE;
    }
  }

  if (8 > output_len) {
    return BLOSC2_ERROR_WRITE_BUFFER;
  }
  out_[0] = start;
  out_[1] = step;

  return 8;
}

int codec_decoder(const uint8_t* input, int32_t input_len,
                  uint8_t* output, int32_t output_len,
                  uint8_t meta,
                  blosc2_dparams *dparams, const void* chunk) {
  BLOSC_UNUSED_PARAM(chunk);
  if (dparams->schunk == NULL) {
    return -1;
  }

  uint8_t *content;
  int32_t content_len;
  blosc2_vlmeta_get(dparams->schunk, "codec_arange", &content, &content_len);
  if (content[0] != 222) {
    return -1;
  }
  free(content);

  if (meta != 111) {
    return -1;
  }

  int32_t nelem = output_len / 4;
  int32_t *in_ = ((int32_t *) input);
  int32_t *out_ = ((int32_t *) output);

  if (8 > input_len) {
    return BLOSC2_ERROR_WRITE_BUFFER;
  }
  int32_t start = in_[0];
  int32_t step = in_[1];
  for (int i = 0; i < nelem; ++i) {
    out_[i] = start + i * step;
  }

  return output_len;
}

int codec_decoder_error(const uint8_t* input, int32_t input_len,
                        uint8_t* output, int32_t output_len,
                        uint8_t meta,
                        blosc2_dparams* dparams, const void* chunk) {
  BLOSC_UNUSED_PARAM(dparams);
  BLOSC_UNUSED_PARAM(chunk);
  if (meta != 111) {
    return -1;
  }

  int32_t nelem = output_len / 4;
  int32_t *in_ = ((int32_t *) input);
  int32_t *out_ = ((int32_t *) output);

  if (8 > input_len) {
    return BLOSC2_ERROR_WRITE_BUFFER;
  }
  int32_t start = in_[0];
  int32_t step = in_[1];
  for (int i = 0; i < nelem; ++i) {
    out_[i] = start + i * step + 10;
  }

  return output_len;
}

CUTEST_TEST_DATA(urcodecs) {
  blosc2_cparams cparams;
  char* urlpath;
};

CUTEST_TEST_SETUP(urcodecs) {
  blosc2_init();
  data->cparams = BLOSC2_CPARAMS_DEFAULTS;
  data->cparams.typesize = sizeof(int32_t);
  data->cparams.clevel = 9;
  data->cparams.nthreads = NTHREADS;
  data->urlpath = "test_udcodecs.b2frame";

  CUTEST_PARAMETRIZE(correct_backward, bool, CUTEST_DATA(
      true, false,
  ));

}


CUTEST_TEST_TEST(urcodecs) {
  CUTEST_GET_PARAMETER(correct_backward, bool);

  int32_t isize = CHUNKSIZE * sizeof(int32_t);
  uint8_t *bdata = malloc(isize);
  uint8_t *bdata_dest = malloc(isize);

  int dsize;

  blosc2_codec udcodec;
  udcodec.compname = "arange";
  udcodec.version = 1;
  udcodec.encoder = codec_encoder;
  if (correct_backward) {
    udcodec.compcode = 250;
    udcodec.complib = 250;
    udcodec.decoder = codec_decoder;
  } else {
    udcodec.compcode = 251;
    udcodec.complib = 251;
    udcodec.decoder = codec_decoder_error;
  }
  int rc = blosc2_register_codec(&udcodec);
  if (rc != 0) {
    fprintf(stderr, "Error registering the code.");
    return BLOSC2_ERROR_FAILURE;
  }

  blosc2_cparams cparams = BLOSC2_CPARAMS_DEFAULTS;
  for (int i = 0; i < BLOSC2_MAX_FILTERS; ++i) {
    cparams.filters[i] = 0;
  }
  cparams.compcode = udcodec.compcode;
  cparams.compcode_meta = 111;

  blosc2_dparams dparams = BLOSC2_DPARAMS_DEFAULTS;


  blosc2_schunk* schunk;
  int i, nchunk;

  /* Create a super-chunk container */
  cparams.typesize = sizeof(int32_t);
  cparams.clevel = 9;
  blosc2_storage storage = {.cparams=&cparams,
                            .dparams=&dparams,
                            .urlpath=data->urlpath,
                            .contiguous=true};
  remove(data->urlpath);
  schunk = blosc2_schunk_new(&storage);
  uint8_t codec_params = 222;
  blosc2_cparams cparams2 = BLOSC2_CPARAMS_DEFAULTS;
  blosc2_vlmeta_add(schunk, "codec_arange", &codec_params, 1, &cparams2);

  for (nchunk = 0; nchunk < NCHUNKS; nchunk++) {
    for (i = 0; i < CHUNKSIZE; i++) {
      ((int32_t *) bdata)[i] = i * nchunk;
    }
    int64_t nchunks_ = blosc2_schunk_append_buffer(schunk, bdata, isize);
    if (nchunks_ != nchunk + 1) {
      fprintf(stderr, "Unexpected nchunks!");
      return -1;
    }
  }
  blosc2_schunk_free(schunk);

  schunk = blosc2_schunk_open(data->urlpath);

  /* Retrieve and decompress the chunks (0-based count) */
  for (nchunk = NCHUNKS-1; nchunk >= 0; nchunk--) {
    dsize = blosc2_schunk_decompress_chunk(schunk, nchunk, bdata_dest, isize);
    if (dsize < 0) {
      fprintf(stderr, "Decompression error.  Error code: %d\n", dsize);
      return dsize;
    }
  }

  /* Check integrity of the second chunk (made of non-zeros) */
  blosc2_schunk_decompress_chunk(schunk, 1, bdata_dest, isize);
  for (i = 0; i < CHUNKSIZE; i++) {
    bool equals = false;
    if (((int32_t *) bdata_dest)[i] == i) {
      equals = true;
    }

    if (!equals && correct_backward) {
      fprintf(stderr, "Decompressed bdata differs from original!\n");
      return -1;
    }
    if (equals && !correct_backward) {
      fprintf(stderr, "Decompressed bdata is equal than original!\n");
      return -1;
    }
  }

  /* Free resources */
  /* Destroy the super-chunk */
  blosc2_schunk_free(schunk);
  free(bdata);
  free(bdata_dest);

  return BLOSC2_ERROR_SUCCESS;
}


CUTEST_TEST_TEARDOWN(urcodecs) {
  BLOSC_UNUSED_PARAM(data);
  blosc2_destroy();
}


int main() {
  CUTEST_TEST_RUN(urcodecs);
}