Coverage Report

Created: 2024-10-21 12:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/home/liu/actions-runner/_work/ccv/ccv/test/unit/io.tests.c
Line
Count
Source
1
#include "ccv.h"
2
#include "case.h"
3
#include "ccv_case.h"
4
5
TEST_CASE("read raw memory, rgb => gray")
6
1
{
7
1
  unsigned char rgb[] = {
8
1
    10, 20, 30, 40, 50, 60, 70,
9
1
    80, 90, 15, 25, 35, 45, 55,
10
1
  };
11
1
  ccv_dense_matrix_t* x = 0;
12
1
  ccv_read(rgb, &x, CCV_IO_RGB_RAW | CCV_IO_GRAY, 2, 2, 7);
13
1
  unsigned char hx1[] = {
14
1
    18, 48,
15
1
  };
16
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 2, "1st row when reading raw rgb ordered memory block into grayscale matrix doesn't match");
17
1
  unsigned char hx2[] = {
18
1
    82, 33,
19
1
  };
20
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 2, "2nd row when reading raw rgb ordered memory block into grayscale matrix doesn't match");
21
1
  ccv_matrix_free(x);
22
1
}
23
24
TEST_CASE("read raw memory, rgb => rgb")
25
1
{
26
1
  unsigned char rgb[] = {
27
1
    10, 20, 30, 40, 50, 60, 70,
28
1
    80, 90, 15, 25, 35, 45, 55,
29
1
  };
30
1
  ccv_dense_matrix_t* x = 0;
31
1
  ccv_read(rgb, &x, CCV_IO_RGB_RAW | CCV_IO_RGB_COLOR, 2, 2, 7);
32
1
  REQUIRE_ARRAY_EQ(unsigned char, rgb, x->data.u8, 6, "1st row when reading raw rgb ordered memory block into rgb matrix doesn't match");
33
1
  REQUIRE_ARRAY_EQ(unsigned char, rgb + 7, x->data.u8 + x->step, 6, "2nd row when reading raw rgb ordered memory block into rgb matrix doesn't match");
34
1
  ccv_matrix_free(x);
35
1
}
36
37
TEST_CASE("read raw memory, rgba => rgb")
38
1
{
39
1
  unsigned char rgba[] = {
40
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
41
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
42
1
  };
43
1
  ccv_dense_matrix_t* x = 0;
44
1
  ccv_read(rgba, &x, CCV_IO_RGBA_RAW | CCV_IO_RGB_COLOR, 2, 2, 9);
45
1
  unsigned char hx1[] = {
46
1
    10, 20, 30, 50, 60, 70,
47
1
  };
48
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 6, "1st row when reading raw rgba memory block into rgb matrix doesn't match");
49
1
  unsigned char hx2[] = {
50
1
    15, 25, 35, 55, 65, 75,
51
1
  };
52
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 6, "2nd row when reading raw rgba memory block into rgb matrix doesn't match");
53
1
  ccv_matrix_free(x);
54
1
}
55
56
TEST_CASE("read raw memory, rgba => gray")
57
1
{
58
1
  unsigned char rgba[] = {
59
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
60
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
61
1
  };
62
1
  ccv_dense_matrix_t* x = 0;
63
1
  ccv_read(rgba, &x, CCV_IO_RGBA_RAW | CCV_IO_GRAY, 2, 2, 9);
64
1
  unsigned char hx1[] = {
65
1
    18, 58,
66
1
  };
67
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 2, "1st row when reading raw rgba memory block into grayscale matrix doesn't match");
68
1
  unsigned char hx2[] = {
69
1
    23, 63,
70
1
  };
71
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 2, "2nd row when reading raw rgba memory block into grayscale matrix doesn't match");
72
1
  ccv_matrix_free(x);
73
1
}
74
75
TEST_CASE("read raw memory, rgba => rgba")
76
1
{
77
1
  unsigned char rgba[] = {
78
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
79
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
80
1
  };
81
1
  ccv_dense_matrix_t* x = 0;
82
1
  ccv_read(rgba, &x, CCV_IO_RGBA_RAW, 2, 2, 9);
83
1
  REQUIRE_ARRAY_EQ(unsigned char, rgba, x->data.u8, 8, "1st row when reading raw rgba memory block into rgba matrix doesn't match");
84
1
  REQUIRE_ARRAY_EQ(unsigned char, rgba + 9, x->data.u8 + x->step, 8, "2nd row when reading raw rgba memory block into rgba matrix doesn't match");
85
1
  ccv_matrix_free(x);
86
1
}
87
88
TEST_CASE("read raw memory, argb => rgb")
89
1
{
90
1
  unsigned char argb[] = {
91
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
92
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
93
1
  };
94
1
  ccv_dense_matrix_t* x = 0;
95
1
  ccv_read(argb, &x, CCV_IO_ARGB_RAW | CCV_IO_RGB_COLOR, 2, 2, 9);
96
1
  unsigned char hx1[] = {
97
1
    20, 30, 40, 60, 70, 80,
98
1
  };
99
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 6, "1st row when reading raw argb memory block into rgb matrix doesn't match");
100
1
  unsigned char hx2[] = {
101
1
    25, 35, 45, 65, 75, 85,
102
1
  };
103
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 6, "2nd row when reading raw argb memory block into rgb matrix doesn't match");
104
1
  ccv_matrix_free(x);
105
1
}
106
107
TEST_CASE("read raw memory, argb => gray")
108
1
{
109
1
  unsigned char argb[] = {
110
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
111
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
112
1
  };
113
1
  ccv_dense_matrix_t* x = 0;
114
1
  ccv_read(argb, &x, CCV_IO_ARGB_RAW | CCV_IO_GRAY, 2, 2, 9);
115
1
  unsigned char hx1[] = {
116
1
    28, 68,
117
1
  };
118
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 2, "1st row when reading raw argb memory block into grayscale matrix doesn't match");
119
1
  unsigned char hx2[] = {
120
1
    33, 73,
121
1
  };
122
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 2, "2nd row when reading raw argb memory block into grayscale matrix doesn't match");
123
1
  ccv_matrix_free(x);
124
1
}
125
126
TEST_CASE("read raw memory, argb => argb")
127
1
{
128
1
  unsigned char argb[] = {
129
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
130
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
131
1
  };
132
1
  ccv_dense_matrix_t* x = 0;
133
1
  ccv_read(argb, &x, CCV_IO_ARGB_RAW, 2, 2, 9);
134
1
  REQUIRE_ARRAY_EQ(unsigned char, argb, x->data.u8, 8, "1st row when reading raw argb memory block into argb matrix doesn't match");
135
1
  REQUIRE_ARRAY_EQ(unsigned char, argb + 9, x->data.u8 + x->step, 8, "2nd row when reading raw argb memory block into argb matrix doesn't match");
136
1
  ccv_matrix_free(x);
137
1
}
138
139
TEST_CASE("read raw memory, bgr => gray")
140
1
{
141
1
  unsigned char bgr[] = {
142
1
    10, 20, 30, 40, 50, 60, 70,
143
1
    80, 90, 15, 25, 35, 45, 55,
144
1
  };
145
1
  ccv_dense_matrix_t* x = 0;
146
1
  ccv_read(bgr, &x, CCV_IO_BGR_RAW | CCV_IO_GRAY, 2, 2, 7);
147
1
  unsigned char hx1[] = {
148
1
    21, 51,
149
1
  };
150
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 2, "1st row when reading raw bgr ordered memory block into grayscale matrix doesn't match");
151
1
  unsigned char hx2[] = {
152
1
    73, 36,
153
1
  };
154
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 2, "2nd row when reading raw bgr ordered memory block into grayscale matrix doesn't match");
155
1
  ccv_matrix_free(x);
156
1
}
157
158
TEST_CASE("read raw memory, bgr => rgb")
159
1
{
160
1
  unsigned char bgr[] = {
161
1
    10, 20, 30, 40, 50, 60, 70,
162
1
    80, 90, 15, 25, 35, 45, 55,
163
1
  };
164
1
  ccv_dense_matrix_t* x = 0;
165
1
  ccv_read(bgr, &x, CCV_IO_BGR_RAW | CCV_IO_RGB_COLOR, 2, 2, 7);
166
1
  unsigned char hx1[] = {
167
1
    30, 20, 10, 60, 50, 40,
168
1
  };
169
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 6, "1st row when reading raw bgr ordered memory block into rgb matrix doesn't match");
170
1
  unsigned char hx2[] = {
171
1
    15, 90, 80, 45, 35, 25,
172
1
  };
173
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 6, "2nd row when reading raw bgr ordered memory block into rgb matrix doesn't match");
174
1
  ccv_matrix_free(x);
175
1
}
176
177
TEST_CASE("read raw memory, bgr => bgr")
178
1
{
179
1
  unsigned char bgr[] = {
180
1
    10, 20, 30, 40, 50, 60, 70,
181
1
    80, 90, 15, 25, 35, 45, 55,
182
1
  };
183
1
  ccv_dense_matrix_t* x = 0;
184
1
  ccv_read(bgr, &x, CCV_IO_BGR_RAW, 2, 2, 7);
185
1
  REQUIRE_ARRAY_EQ(unsigned char, bgr, x->data.u8, 6, "1st row when reading raw bgr ordered memory block into rgb matrix doesn't match");
186
1
  REQUIRE_ARRAY_EQ(unsigned char, bgr + 7, x->data.u8 + x->step, 6, "2nd row when reading raw bgr ordered memory block into rgb matrix doesn't match");
187
1
  ccv_matrix_free(x);
188
1
}
189
190
TEST_CASE("read raw memory, bgra => rgb")
191
1
{
192
1
  unsigned char bgra[] = {
193
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
194
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
195
1
  };
196
1
  ccv_dense_matrix_t* x = 0;
197
1
  ccv_read(bgra, &x, CCV_IO_BGRA_RAW | CCV_IO_RGB_COLOR, 2, 2, 9);
198
1
  unsigned char hx1[] = {
199
1
    30, 20, 10, 70, 60, 50,
200
1
  };
201
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 6, "1st row when reading raw bgra memory block into rgb matrix doesn't match");
202
1
  unsigned char hx2[] = {
203
1
    35, 25, 15, 75, 65, 55,
204
1
  };
205
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 6, "2nd row when reading raw bgra memory block into rgb matrix doesn't match");
206
1
  ccv_matrix_free(x);
207
1
}
208
209
TEST_CASE("read raw memory, bgra => gray")
210
1
{
211
1
  unsigned char bgra[] = {
212
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
213
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
214
1
  };
215
1
  ccv_dense_matrix_t* x = 0;
216
1
  ccv_read(bgra, &x, CCV_IO_BGRA_RAW | CCV_IO_GRAY, 2, 2, 9);
217
1
  unsigned char hx1[] = {
218
1
    21, 61,
219
1
  };
220
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 2, "1st row when reading raw bgra memory block into grayscale matrix doesn't match");
221
1
  unsigned char hx2[] = {
222
1
    26, 66,
223
1
  };
224
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 2, "2nd row when reading raw bgra memory block into grayscale matrix doesn't match");
225
1
  ccv_matrix_free(x);
226
1
}
227
228
TEST_CASE("read raw memory, bgra => bgra")
229
1
{
230
1
  unsigned char bgra[] = {
231
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
232
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
233
1
  };
234
1
  ccv_dense_matrix_t* x = 0;
235
1
  ccv_read(bgra, &x, CCV_IO_BGRA_RAW, 2, 2, 9);
236
1
  REQUIRE_ARRAY_EQ(unsigned char, bgra, x->data.u8, 8, "1st row when reading raw bgra memory block into bgra matrix doesn't match");
237
1
  REQUIRE_ARRAY_EQ(unsigned char, bgra + 9, x->data.u8 + x->step, 8, "2nd row when reading raw bgra memory block into bgra matrix doesn't match");
238
1
  ccv_matrix_free(x);
239
1
}
240
241
TEST_CASE("read raw memory, abgr => rgb")
242
1
{
243
1
  unsigned char abgr[] = {
244
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
245
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
246
1
  };
247
1
  ccv_dense_matrix_t* x = 0;
248
1
  ccv_read(abgr, &x, CCV_IO_ABGR_RAW | CCV_IO_RGB_COLOR, 2, 2, 9);
249
1
  unsigned char hx1[] = {
250
1
    40, 30, 20, 80, 70, 60,
251
1
  };
252
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 6, "1st row when reading raw abgr memory block into rgb matrix doesn't match");
253
1
  unsigned char hx2[] = {
254
1
    45, 35, 25, 85, 75, 65,
255
1
  };
256
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 6, "2nd row when reading raw abgr memory block into rgb matrix doesn't match");
257
1
  ccv_matrix_free(x);
258
1
}
259
260
TEST_CASE("read raw memory, abgr => gray")
261
1
{
262
1
  unsigned char abgr[] = {
263
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
264
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
265
1
  };
266
1
  ccv_dense_matrix_t* x = 0;
267
1
  ccv_read(abgr, &x, CCV_IO_ABGR_RAW | CCV_IO_GRAY, 2, 2, 9);
268
1
  unsigned char hx1[] = {
269
1
    31, 71,
270
1
  };
271
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 2, "1st row when reading raw abgr memory block into grayscale matrix doesn't match");
272
1
  unsigned char hx2[] = {
273
1
    36, 76,
274
1
  };
275
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 2, "2nd row when reading raw abgr memory block into grayscale matrix doesn't match");
276
1
  ccv_matrix_free(x);
277
1
}
278
279
TEST_CASE("read raw memory, abgr => abgr")
280
1
{
281
1
  unsigned char abgr[] = {
282
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
283
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
284
1
  };
285
1
  ccv_dense_matrix_t* x = 0;
286
1
  ccv_read(abgr, &x, CCV_IO_ABGR_RAW, 2, 2, 9);
287
1
  REQUIRE_ARRAY_EQ(unsigned char, abgr, x->data.u8, 8, "1st row when reading raw abgr memory block into abgr matrix doesn't match");
288
1
  REQUIRE_ARRAY_EQ(unsigned char, abgr + 9, x->data.u8 + x->step, 8, "2nd row when reading raw abgr memory block into abgr matrix doesn't match");
289
1
  ccv_matrix_free(x);
290
1
}
291
292
TEST_CASE("read raw memory, gray => rgb")
293
1
{
294
1
  unsigned char g[] = {
295
1
    10, 20, 30, 40, 50,
296
1
    15, 25, 35, 45, 55,
297
1
  };
298
1
  ccv_dense_matrix_t* x = 0;
299
1
  ccv_read(g, &x, CCV_IO_GRAY_RAW | CCV_IO_RGB_COLOR, 2, 2, 5);
300
1
  unsigned char hx1[] = {
301
1
    10, 10, 10, 20, 20, 20,
302
1
  };
303
1
  REQUIRE_ARRAY_EQ(unsigned char, hx1, x->data.u8, 6, "1st row when reading raw grayscale memory block into rgb matrix doesn't match");
304
1
  unsigned char hx2[] = {
305
1
    15, 15, 15, 25, 25, 25,
306
1
  };
307
1
  REQUIRE_ARRAY_EQ(unsigned char, hx2, x->data.u8 + x->step, 6, "2nd row when reading raw grayscale memory block into rgb matrix doesn't match");
308
1
  ccv_matrix_free(x);
309
1
}
310
311
TEST_CASE("read raw memory, gray => gray")
312
1
{
313
1
  unsigned char g[] = {
314
1
    10, 20, 30, 40, 50,
315
1
    15, 25, 35, 45, 55,
316
1
  };
317
1
  ccv_dense_matrix_t* x = 0;
318
1
  ccv_read(g, &x, CCV_IO_GRAY_RAW | CCV_IO_GRAY, 2, 2, 5);
319
1
  REQUIRE_ARRAY_EQ(unsigned char, g, x->data.u8, 2, "1st row when reading raw grayscale memory block into grayscale matrix doesn't match");
320
1
  REQUIRE_ARRAY_EQ(unsigned char, g + 5, x->data.u8 + x->step, 2, "2nd row when reading raw grayscale memory block into grayscale matrix doesn't match");
321
1
  ccv_matrix_free(x);
322
1
}
323
324
TEST_CASE("read raw memory with no copy mode")
325
1
{
326
1
  unsigned char rgb[] = {
327
1
    10, 20, 30, 40, 50, 60, 70, 80, 90,
328
1
    15, 25, 35, 45, 55, 65, 75, 85, 95,
329
1
  };
330
1
  ccv_dense_matrix_t* x = 0;
331
1
  ccv_read(rgb, &x, CCV_IO_ANY_RAW | CCV_IO_NO_COPY, 2, 2, 9);
332
1
  REQUIRE_EQ(9, x->step, "its step value should be equal to the passing scanline value in no copy mode");
333
1
  REQUIRE(rgb == x->data.u8, "its data section should point to the same memory region");
334
1
  ccv_matrix_free(x);
335
1
}
336
337
#ifdef HAVE_LIBJPEG
338
TEST_CASE("read JPEG from memory")
339
1
{
340
1
  ccv_dense_matrix_t* x = 0;
341
1
  ccv_read("../../samples/cmyk-jpeg-format.jpg", &x, CCV_IO_ANY_FILE);
342
1
  ccv_dense_matrix_t* y = 0;
343
1
  FILE* rb = fopen("../../samples/cmyk-jpeg-format.jpg", "rb");
344
1
  fseek(rb, 0, SEEK_END);
345
1
  long size = ftell(rb);
346
1
  char* data = (char*)ccmalloc(size);
347
1
  fseek(rb, 0, SEEK_SET);
348
1
  fread(data, 1, size, rb);
349
1
  fclose(rb);
350
1
  ccv_read(data, &y, CCV_IO_ANY_STREAM, size);
351
1
  ccfree(data);
352
1
  REQUIRE_MATRIX_EQ(x, y, "read cmyk-jpeg-format.jpg from file system and memory should be the same");
353
1
  ccv_matrix_free(y);
354
1
  ccv_matrix_free(x);
355
1
}
356
#endif
357
358
#ifdef HAVE_LIBPNG
359
TEST_CASE("read PNG from memory")
360
1
{
361
1
  ccv_dense_matrix_t* x = 0;
362
1
  ccv_read("../../samples/nature.png", &x, CCV_IO_ANY_FILE);
363
1
  ccv_dense_matrix_t* y = 0;
364
1
  FILE* rb = fopen("../../samples/nature.png", "rb");
365
1
  fseek(rb, 0, SEEK_END);
366
1
  long size = ftell(rb);
367
1
  char* data = (char*)ccmalloc(size);
368
1
  fseek(rb, 0, SEEK_SET);
369
1
  fread(data, 1, size, rb);
370
1
  fclose(rb);
371
1
  ccv_read(data, &y, CCV_IO_ANY_STREAM, size);
372
1
  ccfree(data);
373
1
  REQUIRE_MATRIX_EQ(x, y, "read nature.png from file system and memory should be the same");
374
1
  ccv_matrix_free(y);
375
1
  ccv_matrix_free(x);
376
1
}
377
#endif
378
379
#ifdef HAVE_LIBJPEG
380
TEST_CASE("write JPEG to memory")
381
1
{
382
1
  char sanitized_test_case_name[1024] = "/tmp/";
383
1
  strncpy(sanitized_test_case_name + 5, __case_name__, 1024 - 5);
384
1
  int i;
385
16
  for (i = 10; i < 1024 && sanitized_test_case_name[i]; 
i++15
)
386
    // If not A-Za-z0-9, replace with _
387
15
    if (!((sanitized_test_case_name[i] >= 'A' && 
sanitized_test_case_name[i] <= 'Z'12
) ||
388
15
       
(11
sanitized_test_case_name[i] >= 'a'11
&&
sanitized_test_case_name[i] <= 'z'8
) ||
389
15
       
(3
sanitized_test_case_name[i] >= '0'3
&&
sanitized_test_case_name[i] <= '9'0
)))
390
3
      sanitized_test_case_name[i] = '_';
391
1
  assert(i < 1024);
392
1
  sanitized_test_case_name[i] = '.';
393
1
  sanitized_test_case_name[i + 1] = 'j';
394
1
  sanitized_test_case_name[i + 2] = 'p';
395
1
  sanitized_test_case_name[i + 3] = 'g';
396
1
  sanitized_test_case_name[i + 4] = 0;
397
1
  ccv_dense_matrix_t* x = 0;
398
1
  ccv_read("../../samples/nature.png", &x, CCV_IO_ANY_FILE);
399
1
  char* data = (char*)ccmalloc(x->rows * x->cols * 2);
400
1
  size_t len = x->rows * x->cols * 2;
401
1
  ccv_write(x, data, &len, CCV_IO_JPEG_STREAM, 0);
402
1
  ccv_write(x, sanitized_test_case_name, 0, CCV_IO_JPEG_FILE, 0);
403
1
  assert(len < x->rows * x->cols * 2);
404
1
  ccv_dense_matrix_t* y = 0;
405
1
  ccv_read(data, &y, CCV_IO_ANY_STREAM, len);
406
1
  ccfree(data);
407
1
  ccv_dense_matrix_t* z = 0;
408
1
  ccv_read(sanitized_test_case_name, &z, CCV_IO_ANY_FILE, 0);
409
1
  FILE* fd = fopen(sanitized_test_case_name, "rb");
410
1
  fseek(fd, 0L, SEEK_END);
411
1
  size_t fsz = ftell(fd);
412
1
  fclose(fd);
413
1
  REQUIRE_EQ(fsz, len, "size of both memory and file should be the same");
414
1
  REQUIRE_MATRIX_EQ(y, z, "write nature.png to JPEG in file system and memory should be the same");
415
1
  ccv_matrix_free(z);
416
1
  ccv_matrix_free(y);
417
1
  ccv_matrix_free(x);
418
1
}
419
420
TEST_CASE("write JPEG to memory fail at small memory allocation")
421
1
{
422
1
  ccv_dense_matrix_t* x = 0;
423
1
  ccv_read("../../samples/nature.png", &x, CCV_IO_ANY_FILE);
424
1
  char* data = (char*)ccmalloc(100);
425
1
  size_t len = 100;
426
1
  int error = ccv_write(x, data, &len, CCV_IO_JPEG_STREAM, 0);
427
1
  REQUIRE_EQ(error, CCV_IO_ERROR, "fail at write the file");
428
1
  ccfree(data);
429
1
  ccv_matrix_free(x);
430
1
}
431
#endif
432
433
#ifdef HAVE_LIBPNG
434
TEST_CASE("write PNG to memory")
435
1
{
436
1
  char sanitized_test_case_name[1024] = "/tmp/";
437
1
  strncpy(sanitized_test_case_name + 5, __case_name__, 1024 - 5);
438
1
  int i;
439
15
  for (i = 10; i < 1024 && sanitized_test_case_name[i]; 
i++14
)
440
    // If not A-Za-z0-9, replace with _
441
14
    if (!((sanitized_test_case_name[i] >= 'A' && 
sanitized_test_case_name[i] <= 'Z'11
) ||
442
14
       
(11
sanitized_test_case_name[i] >= 'a'11
&&
sanitized_test_case_name[i] <= 'z'8
) ||
443
14
       
(3
sanitized_test_case_name[i] >= '0'3
&&
sanitized_test_case_name[i] <= '9'0
)))
444
3
      sanitized_test_case_name[i] = '_';
445
1
  assert(i < 1024);
446
1
  sanitized_test_case_name[i] = '.';
447
1
  sanitized_test_case_name[i + 1] = 'p';
448
1
  sanitized_test_case_name[i + 2] = 'n';
449
1
  sanitized_test_case_name[i + 3] = 'g';
450
1
  sanitized_test_case_name[i + 4] = 0;
451
1
  ccv_dense_matrix_t* x = 0;
452
1
  ccv_read("../../samples/nature.png", &x, CCV_IO_ANY_FILE);
453
1
  char* data = (char*)ccmalloc(x->rows * x->cols * 2);
454
1
  size_t len = x->rows * x->cols * 2;
455
1
  ccv_write(x, data, &len, CCV_IO_PNG_STREAM, 0);
456
1
  ccv_write(x, sanitized_test_case_name, 0, CCV_IO_PNG_FILE, 0);
457
1
  assert(len < x->rows * x->cols * 2);
458
1
  ccv_dense_matrix_t* y = 0;
459
1
  ccv_read(data, &y, CCV_IO_ANY_STREAM, len);
460
1
  ccfree(data);
461
1
  ccv_dense_matrix_t* z = 0;
462
1
  ccv_read(sanitized_test_case_name, &z, CCV_IO_ANY_FILE, 0);
463
1
  FILE* fd = fopen(sanitized_test_case_name, "rb");
464
1
  fseek(fd, 0L, SEEK_END);
465
1
  size_t fsz = ftell(fd);
466
1
  fclose(fd);
467
1
  REQUIRE_EQ(fsz, len, "size of both memory and file should be the same");
468
1
  REQUIRE_MATRIX_EQ(y, z, "write nature.png to PNG in file system and memory should be the same");
469
1
  ccv_matrix_free(z);
470
1
  ccv_matrix_free(y);
471
1
  ccv_matrix_free(x);
472
1
}
473
474
TEST_CASE("write PNG to memory fail at small memory allocation")
475
1
{
476
1
  ccv_dense_matrix_t* x = 0;
477
1
  ccv_read("../../samples/nature.png", &x, CCV_IO_ANY_FILE);
478
1
  char* data = (char*)ccmalloc(100);
479
1
  size_t len = 100;
480
1
  int error = ccv_write(x, data, &len, CCV_IO_PNG_STREAM, 0);
481
1
  REQUIRE_EQ(error, CCV_IO_ERROR, "fail at write the file");
482
1
  ccfree(data);
483
1
  ccv_matrix_free(x);
484
1
}
485
#endif
486
487
#ifdef HAVE_LIBPNG
488
TEST_CASE("write binary to memory")
489
1
{
490
1
  char sanitized_test_case_name[1024] = "/tmp/";
491
1
  strncpy(sanitized_test_case_name + 5, __case_name__, 1024 - 5);
492
1
  int i;
493
18
  for (i = 10; i < 1024 && sanitized_test_case_name[i]; 
i++17
)
494
    // If not A-Za-z0-9, replace with _
495
17
    if (!((sanitized_test_case_name[i] >= 'A' && 
sanitized_test_case_name[i] <= 'Z'14
) ||
496
17
       (sanitized_test_case_name[i] >= 'a' && 
sanitized_test_case_name[i] <= 'z'14
) ||
497
17
       
(3
sanitized_test_case_name[i] >= '0'3
&&
sanitized_test_case_name[i] <= '9'0
)))
498
3
      sanitized_test_case_name[i] = '_';
499
1
  assert(i < 1024);
500
1
  sanitized_test_case_name[i] = '.';
501
1
  sanitized_test_case_name[i + 1] = 'b';
502
1
  sanitized_test_case_name[i + 2] = 'i';
503
1
  sanitized_test_case_name[i + 3] = 'n';
504
1
  sanitized_test_case_name[i + 4] = 0;
505
1
  ccv_dense_matrix_t* x = 0;
506
1
  ccv_read("../../samples/nature.png", &x, CCV_IO_ANY_FILE);
507
1
  char* data = (char*)ccmalloc(x->rows * x->cols * 4);
508
1
  size_t len = x->rows * x->cols * 4;
509
1
  ccv_write(x, data, &len, CCV_IO_PLAIN_STREAM, 0);
510
1
  ccv_write(x, sanitized_test_case_name, 0, CCV_IO_BINARY_FILE, 0);
511
1
  assert(len < x->rows * x->cols * 4);
512
1
  ccv_dense_matrix_t* y = 0;
513
1
  ccv_read(data, &y, CCV_IO_ANY_STREAM, len);
514
1
  ccfree(data);
515
1
  ccv_dense_matrix_t* z = 0;
516
1
  ccv_read(sanitized_test_case_name, &z, CCV_IO_ANY_FILE, 0);
517
1
  FILE* fd = fopen(sanitized_test_case_name, "rb");
518
1
  fseek(fd, 0L, SEEK_END);
519
1
  size_t fsz = ftell(fd);
520
1
  fclose(fd);
521
1
  REQUIRE_EQ(fsz, len, "size of both memory and file should be the same");
522
1
  REQUIRE_MATRIX_EQ(y, z, "write nature.png to binary in file system and memory should be the same");
523
1
  ccv_matrix_free(z);
524
1
  ccv_matrix_free(y);
525
1
  ccv_matrix_free(x);
526
1
}
527
528
TEST_CASE("write binary to memory fail at small memory allocation")
529
1
{
530
1
  ccv_dense_matrix_t* x = 0;
531
1
  ccv_read("../../samples/nature.png", &x, CCV_IO_ANY_FILE);
532
1
  char* data = (char*)ccmalloc(100);
533
1
  size_t len = 100;
534
1
  int error = ccv_write(x, data, &len, CCV_IO_PLAIN_STREAM, 0);
535
1
  REQUIRE_EQ(error, CCV_IO_ERROR, "fail at write the file");
536
1
  ccfree(data);
537
1
  ccv_matrix_free(x);
538
1
}
539
#endif
540
541
#include "case_main.h"