Debian Codegraph @

test/unit/test_tuple.c

raw
1 #include <sqlite3.h>
2
3 #include "../../src/tuple.h"
4
5 #include "../lib/runner.h"
6
7 /* tinycc doesn't have this builtin, nor the warning that it's meant to silence.
8 */
9 #ifdef __TINYC__
10 #define __builtin_assume_aligned(x, y) x
11 #endif
12
13 TEST_MODULE(tuple);
14
15 /******************************************************************************
16 *
17 * Helper macros.
18 *
19 ******************************************************************************/
20
21 #define DECODER_INIT(N) \
22 { \
23 int rc2; \
24 int format_; \
25 format_ = (N > 0) ? TUPLE__ROW : TUPLE__PARAMS; \
26 rc2 = tuple_decoder__init(&decoder, N, format_, &cursor); \
27 munit_assert_int(rc2, ==, 0); \
28 }
29
30 #define DECODER_INIT_PARAMS32 \
31 { \
32 int rc2; \
33 rc2 = tuple_decoder__init(&decoder, 0, TUPLE__PARAMS32, \
34 &cursor); \
35 munit_assert_int(rc2, ==, 0); \
36 }
37
38 #define DECODER_NEXT \
39 { \
40 int rc2; \
41 rc2 = tuple_decoder__next(&decoder, &value); \
42 munit_assert_int(rc2, ==, 0); \
43 }
44
45 #define ENCODER_INIT(N, FORMAT) \
46 { \
47 int rc2; \
48 rc2 = tuple_encoder__init(&f->encoder, N, FORMAT, &f->buffer); \
49 munit_assert_int(rc2, ==, 0); \
50 }
51
52 #define ENCODER_NEXT \
53 { \
54 int rc2; \
55 rc2 = tuple_encoder__next(&f->encoder, &value); \
56 munit_assert_int(rc2, ==, 0); \
57 }
58
59 /******************************************************************************
60 *
61 * Assertions.
62 *
63 ******************************************************************************/
64
65 #define ASSERT_VALUE_TYPE(TYPE) munit_assert_int(value.type, ==, TYPE)
66
67 /******************************************************************************
68 *
69 * Decoder.
70 *
71 ******************************************************************************/
72
73 TEST_SUITE(decoder);
74
75 TEST_GROUP(decoder, init);
76
77 /* If n is 0, then the parameters format is used to determine the number of
78 * elements of the tuple. */
79 TEST_CASE(decoder, init, param, NULL)
80 {
81 struct tuple_decoder decoder;
82 char buf[] = {2, 0, 0, 0, 0, 0, 0, 0};
83 struct cursor cursor = {buf, sizeof buf};
84 (void)data;
85 (void)params;
86 DECODER_INIT(0);
87 munit_assert_uint(decoder.n, ==, 2);
88 munit_assert_uint(tuple_decoder__n(&decoder), ==, 2);
89 return MUNIT_OK;
90 }
91
92 TEST_CASE(decoder, init, param32, NULL)
93 {
94 struct tuple_decoder decoder;
95 char buf[] = {2, 0, 0, 0, 0, 0, 0, 0};
96 struct cursor cursor = {buf, sizeof buf};
97 (void)data;
98 (void)params;
99 DECODER_INIT_PARAMS32;
100 munit_assert_uint(decoder.n, ==, 2);
101 munit_assert_uint(tuple_decoder__n(&decoder), ==, 2);
102 return MUNIT_OK;
103 }
104
105 /* If n is not 0, then it is the number of elements. */
106 TEST_CASE(decoder, init, row, NULL)
107 {
108 struct tuple_decoder decoder;
109 char buf[] = {2, 0, 0, 0, 0, 0, 0, 0, 0};
110 struct cursor cursor = {buf, sizeof buf};
111 (void)data;
112 (void)params;
113 DECODER_INIT(3);
114 munit_assert_uint(decoder.n, ==, 3);
115 munit_assert_uint(tuple_decoder__n(&decoder), ==, 3);
116 return MUNIT_OK;
117 }
118
119 TEST_GROUP(decoder, row);
120
121 /* Decode a tuple with row format and only one value. */
122 TEST_CASE(decoder, row, one_value, NULL)
123 {
124 struct tuple_decoder decoder;
125 uint8_t buf[][8] = {
126 {SQLITE_INTEGER, 0, 0, 0, 0, 0, 0, 0},
127 {7, 0, 0, 0, 0, 0, 0, 0},
128 };
129 struct cursor cursor = {buf, sizeof buf};
130 struct value value;
131
132 (void)data;
133 (void)params;
134
135 DECODER_INIT(1);
136 DECODER_NEXT;
137
138 ASSERT_VALUE_TYPE(SQLITE_INTEGER);
139 munit_assert_int64(value.integer, ==, 7);
140
141 return MUNIT_OK;
142 }
143
144 /* Decode a tuple with row format and two values. */
145 TEST_CASE(decoder, row, two_values, NULL)
146 {
147 struct tuple_decoder decoder;
148 uint8_t buf[][8] = {
149 {SQLITE_INTEGER | SQLITE_TEXT << 4, 0, 0, 0, 0, 0, 0, 0},
150 {7, 0, 0, 0, 0, 0, 0, 0},
151 {'h', 'e', 'l', 'l', 'o', 0, 0, 0},
152 };
153 struct cursor cursor = {buf, sizeof buf};
154 struct value value;
155
156 (void)data;
157 (void)params;
158
159 DECODER_INIT(2);
160 DECODER_NEXT;
161
162 ASSERT_VALUE_TYPE(SQLITE_INTEGER);
163 munit_assert_int64(value.integer, ==, 7);
164
165 DECODER_NEXT;
166
167 ASSERT_VALUE_TYPE(SQLITE_TEXT);
168 munit_assert_string_equal(value.text, "hello");
169
170 return MUNIT_OK;
171 }
172
173 TEST_GROUP(decoder, params);
174
175 /* Decode a tuple with params format and only one value. */
176 TEST_CASE(decoder, params, one_value, NULL)
177 {
178 struct tuple_decoder decoder;
179 uint8_t buf[][8] = {
180 {1, SQLITE_INTEGER, 0, 0, 0, 0, 0, 0},
181 {7, 0, 0, 0, 0, 0, 0, 0},
182 };
183 struct cursor cursor = {buf, sizeof buf};
184 struct value value;
185
186 (void)data;
187 (void)params;
188
189 DECODER_INIT(0);
190 DECODER_NEXT;
191
192 ASSERT_VALUE_TYPE(SQLITE_INTEGER);
193 munit_assert_int64(value.integer, ==, 7);
194
195 return MUNIT_OK;
196 }
197
198 /* Decode a tuple with params format and two values. */
199 TEST_CASE(decoder, params, two_values, NULL)
200 {
201 struct tuple_decoder decoder;
202 uint8_t buf[][8] = {
203 {2, SQLITE_INTEGER, SQLITE_TEXT, 0, 0, 0, 0, 0},
204 {7, 0, 0, 0, 0, 0, 0, 0},
205 {'h', 'e', 'l', 'l', 'o', 0, 0, 0},
206 };
207 struct cursor cursor = {buf, sizeof buf};
208 struct value value;
209
210 (void)data;
211 (void)params;
212
213 DECODER_INIT(0);
214 DECODER_NEXT;
215
216 ASSERT_VALUE_TYPE(SQLITE_INTEGER);
217 munit_assert_int64(value.integer, ==, 7);
218
219 DECODER_NEXT;
220
221 ASSERT_VALUE_TYPE(SQLITE_TEXT);
222 munit_assert_string_equal(value.text, "hello");
223
224 return MUNIT_OK;
225 }
226
227 TEST_GROUP(decoder, params32);
228
229 /* Decode a tuple with params32 format and only one value. */
230 TEST_CASE(decoder, params32, one_value, NULL)
231 {
232 struct tuple_decoder decoder;
233 uint8_t buf[][8] = {
234 {1, 0, 0, 0, SQLITE_INTEGER, 0, 0, 0},
235 {7, 0, 0, 0, 0, 0, 0, 0},
236 };
237 struct cursor cursor = {buf, sizeof buf};
238 struct value value;
239
240 (void)data;
241 (void)params;
242
243 DECODER_INIT_PARAMS32;
244 DECODER_NEXT;
245
246 ASSERT_VALUE_TYPE(SQLITE_INTEGER);
247 munit_assert_int64(value.integer, ==, 7);
248
249 return MUNIT_OK;
250 }
251
252 /* Decode a tuple with params32 format and two values. */
253 TEST_CASE(decoder, params32, two_values, NULL)
254 {
255 struct tuple_decoder decoder;
256 uint8_t buf[][8] = {
257 {2, 0, 0, 0, SQLITE_INTEGER, SQLITE_TEXT, 0, 0},
258 {7, 0, 0, 0, 0, 0, 0, 0},
259 {'h', 'e', 'l', 'l', 'o', 0, 0, 0},
260 };
261 struct cursor cursor = {buf, sizeof buf};
262 struct value value;
263
264 (void)data;
265 (void)params;
266
267 DECODER_INIT_PARAMS32;
268 DECODER_NEXT;
269
270 ASSERT_VALUE_TYPE(SQLITE_INTEGER);
271 munit_assert_int64(value.integer, ==, 7);
272
273 DECODER_NEXT;
274
275 ASSERT_VALUE_TYPE(SQLITE_TEXT);
276 munit_assert_string_equal(value.text, "hello");
277
278 return MUNIT_OK;
279 }
280
281 TEST_GROUP(decoder, type);
282
283 /* Decode a floating point number. */
284 TEST_CASE(decoder, type, float, NULL)
285 {
286 struct tuple_decoder decoder;
287 uint8_t buf[][8] __attribute__((aligned(sizeof(uint64_t)))) = {
288 {SQLITE_FLOAT, 0, 0, 0, 0, 0, 0, 0},
289 {0, 0, 0, 0, 0, 0, 0, 0},
290 };
291 struct cursor cursor = {buf, sizeof buf};
292 struct value value;
293 double pi = 3.1415;
294
295 (void)data;
296 (void)params;
297
298 memcpy(buf[1], &pi, sizeof pi);
299 uint64_t *buf_value =
300 __builtin_assume_aligned(buf[1], sizeof(uint64_t));
301 *buf_value = ByteFlipLe64(*buf_value);
302
303 DECODER_INIT(1);
304 DECODER_NEXT;
305
306 ASSERT_VALUE_TYPE(SQLITE_FLOAT);
307 munit_assert_double(value.float_, ==, 3.1415);
308
309 return MUNIT_OK;
310 }
311
312 /* Decode a null value. */
313 TEST_CASE(decoder, type, null, NULL)
314 {
315 struct tuple_decoder decoder;
316 uint8_t buf[][8] __attribute__((aligned(sizeof(uint64_t)))) = {
317 {SQLITE_NULL, 0, 0, 0, 0, 0, 0, 0},
318 {0, 0, 0, 0, 0, 0, 0, 0},
319 };
320 struct cursor cursor = {buf, sizeof buf};
321 struct value value;
322
323 (void)data;
324 (void)params;
325
326 DECODER_INIT(1);
327 DECODER_NEXT;
328
329 ASSERT_VALUE_TYPE(SQLITE_NULL);
330
331 return MUNIT_OK;
332 }
333
334 /* Decode a date string in ISO8601 format. */
335 TEST_CASE(decoder, type, iso8601, NULL)
336 {
337 struct tuple_decoder decoder;
338 uint8_t buf[5][8] __attribute__((aligned(sizeof(uint64_t)))) = {
339 {COWSQL_ISO8601, 0, 0, 0, 0, 0, 0, 0},
340 };
341 struct cursor cursor = {buf, sizeof buf};
342 struct value value;
343
344 (void)data;
345 (void)params;
346
347 strcpy((char *)buf[1], "2018-07-20 09:49:05+00:00");
348
349 DECODER_INIT(1);
350 DECODER_NEXT;
351
352 ASSERT_VALUE_TYPE(COWSQL_ISO8601);
353 munit_assert_string_equal(value.iso8601, "2018-07-20 09:49:05+00:00");
354
355 return MUNIT_OK;
356 }
357
358 /* Decode a boolean. */
359 TEST_CASE(decoder, type, boolean, NULL)
360 {
361 struct tuple_decoder decoder;
362 uint8_t buf[][8] __attribute__((aligned(sizeof(uint64_t)))) = {
363 {COWSQL_BOOLEAN, 0, 0, 0, 0, 0, 0, 0},
364 {1, 0, 0, 0, 0, 0, 0, 0},
365 };
366 struct cursor cursor = {buf, sizeof buf};
367 struct value value;
368
369 (void)data;
370 (void)params;
371
372 DECODER_INIT(1);
373 DECODER_NEXT;
374
375 ASSERT_VALUE_TYPE(COWSQL_BOOLEAN);
376 munit_assert_uint64(value.boolean, ==, 1);
377
378 return MUNIT_OK;
379 }
380
381 /******************************************************************************
382 *
383 * Encoder.
384 *
385 ******************************************************************************/
386
387 struct encoder_fixture
388 {
389 struct buffer buffer;
390 struct tuple_encoder encoder;
391 };
392
393 TEST_SUITE(encoder);
394 TEST_SETUP(encoder)
395 {
396 struct encoder_fixture *f = munit_malloc(sizeof *f);
397 int rc;
398 (void)params;
399 (void)user_data;
400 rc = buffer__init(&f->buffer);
401 munit_assert_int(rc, ==, 0);
402 return f;
403 }
404 TEST_TEAR_DOWN(encoder)
405 {
406 struct encoder_fixture *f = data;
407 buffer__close(&f->buffer);
408 free(data);
409 }
410
411 TEST_GROUP(encoder, row);
412
413 /* Encode a tuple with row format and only one value. */
414 TEST_CASE(encoder, row, one_value, NULL)
415 {
416 struct encoder_fixture *f = data;
417 struct value value;
418 uint8_t(*buf)[8] = f->buffer.data;
419 (void)params;
420
421 ENCODER_INIT(1, TUPLE__ROW);
422
423 value.type = SQLITE_INTEGER;
424 value.integer = 7;
425 ENCODER_NEXT;
426
427 munit_assert_int(buf[0][0], ==, SQLITE_INTEGER);
428 /* malloc'ed buffer is aligned suitably */
429 uint64_t *value_ptr =
430 __builtin_assume_aligned(buf[1], sizeof(uint64_t));
431 munit_assert_uint64(*value_ptr, ==, ByteFlipLe64(7));
432
433 return MUNIT_OK;
434 }
435
436 /* Encode a tuple with row format and two values. */
437 TEST_CASE(encoder, row, two_values, NULL)
438 {
439 struct encoder_fixture *f = data;
440 struct value value;
441 uint8_t(*buf)[8] = f->buffer.data;
442 (void)params;
443
444 ENCODER_INIT(2, TUPLE__ROW);
445
446 value.type = SQLITE_INTEGER;
447 value.integer = 7;
448 ENCODER_NEXT;
449
450 value.type = SQLITE_TEXT;
451 value.text = "hello";
452 ENCODER_NEXT;
453
454 munit_assert_int(buf[0][0], ==, SQLITE_INTEGER | SQLITE_TEXT << 4);
455 /* malloc'ed buffer is aligned suitably */
456 uint64_t *value_ptr =
457 __builtin_assume_aligned(buf[1], sizeof(uint64_t));
458 munit_assert_uint64(*value_ptr, ==, ByteFlipLe64(7));
459 munit_assert_string_equal((const char *)buf[2], "hello");
460
461 return MUNIT_OK;
462 }
463
464 TEST_GROUP(encoder, params);
465
466 /* Encode a tuple with params format and only one value. */
467 TEST_CASE(encoder, params, one_value, NULL)
468 {
469 struct encoder_fixture *f = data;
470 struct value value;
471 uint8_t(*buf)[8] = f->buffer.data;
472 (void)params;
473
474 ENCODER_INIT(1, TUPLE__PARAMS);
475
476 value.type = SQLITE_INTEGER;
477 value.integer = 7;
478 ENCODER_NEXT;
479
480 munit_assert_int(buf[0][0], ==, 1);
481 munit_assert_int(buf[0][1], ==, SQLITE_INTEGER);
482 uint64_t *value_ptr =
483 __builtin_assume_aligned(buf[1], sizeof(uint64_t));
484 munit_assert_uint64(*value_ptr, ==, ByteFlipLe64(7));
485
486 return MUNIT_OK;
487 }
488
489 /* Encode a tuple with params format and two values. */
490 TEST_CASE(encoder, params, two_values, NULL)
491 {
492 struct encoder_fixture *f = data;
493 struct value value;
494 uint8_t(*buf)[8] = f->buffer.data;
495 (void)params;
496
497 ENCODER_INIT(2, TUPLE__PARAMS);
498
499 value.type = SQLITE_INTEGER;
500 value.integer = 7;
501 ENCODER_NEXT;
502
503 value.type = SQLITE_TEXT;
504 value.text = "hello";
505 ENCODER_NEXT;
506
507 munit_assert_int(buf[0][0], ==, 2);
508 munit_assert_int(buf[0][1], ==, SQLITE_INTEGER);
509 munit_assert_int(buf[0][2], ==, SQLITE_TEXT);
510 uint64_t *value_ptr =
511 __builtin_assume_aligned(buf[1], sizeof(uint64_t));
512 munit_assert_uint64(*value_ptr, ==, ByteFlipLe64(7));
513 munit_assert_string_equal((const char *)buf[2], "hello");
514
515 return MUNIT_OK;
516 }
517
518 TEST_GROUP(encoder, params32);
519
520 /* Encode a tuple with params32 format and only one value. */
521 TEST_CASE(encoder, params32, one_value, NULL)
522 {
523 struct encoder_fixture *f = data;
524 struct value value;
525 uint8_t(*buf)[8] = f->buffer.data;
526 (void)params;
527
528 ENCODER_INIT(1, TUPLE__PARAMS32);
529
530 value.type = SQLITE_INTEGER;
531 value.integer = 7;
532 ENCODER_NEXT;
533
534 munit_assert_int(buf[0][0], ==, 1);
535 munit_assert_int(buf[0][1], ==, 0);
536 munit_assert_int(buf[0][2], ==, 0);
537 munit_assert_int(buf[0][3], ==, 0);
538 munit_assert_int(buf[0][4], ==, SQLITE_INTEGER);
539 uint64_t *value_ptr =
540 __builtin_assume_aligned(buf[1], sizeof(uint64_t));
541 munit_assert_uint64(*value_ptr, ==, ByteFlipLe64(7));
542
543 return MUNIT_OK;
544 }
545
546 /* Encode a tuple with params32 format and two values. */
547 TEST_CASE(encoder, params32, two_values, NULL)
548 {
549 struct encoder_fixture *f = data;
550 struct value value;
551 uint8_t(*buf)[8] = f->buffer.data;
552 (void)params;
553
554 ENCODER_INIT(2, TUPLE__PARAMS32);
555
556 value.type = SQLITE_INTEGER;
557 value.integer = 7;
558 ENCODER_NEXT;
559
560 value.type = SQLITE_TEXT;
561 value.text = "hello";
562 ENCODER_NEXT;
563
564 munit_assert_int(buf[0][0], ==, 2);
565 munit_assert_int(buf[0][1], ==, 0);
566 munit_assert_int(buf[0][2], ==, 0);
567 munit_assert_int(buf[0][3], ==, 0);
568 munit_assert_int(buf[0][4], ==, SQLITE_INTEGER);
569 munit_assert_int(buf[0][5], ==, SQLITE_TEXT);
570 uint64_t *value_ptr =
571 __builtin_assume_aligned(buf[1], sizeof(uint64_t));
572 munit_assert_uint64(*value_ptr, ==, ByteFlipLe64(7));
573 munit_assert_string_equal((const char *)buf[2], "hello");
574
575 return MUNIT_OK;
576 }
577
578 TEST_GROUP(encoder, type);
579
580 /* Encode a float parameter. */
581 TEST_CASE(encoder, type, float, NULL)
582 {
583 struct encoder_fixture *f = data;
584 struct value value;
585 uint8_t(*buf)[8] = f->buffer.data;
586 (void)params;
587
588 ENCODER_INIT(1, TUPLE__ROW);
589
590 value.type = SQLITE_FLOAT;
591 value.float_ = 3.1415;
592 ENCODER_NEXT;
593
594 munit_assert_int(buf[0][0], ==, SQLITE_FLOAT);
595 uint64_t *value_ptr =
596 __builtin_assume_aligned(buf[1], sizeof(uint64_t));
597 munit_assert_uint64(*value_ptr, ==,
598 ByteFlipLe64(*(uint64_t *)&value.float_));
599
600 return MUNIT_OK;
601 }
602
603 /* Encode a unix time parameter. */
604 TEST_CASE(encoder, type, unixtime, NULL)
605 {
606 struct encoder_fixture *f = data;
607 struct value value;
608 uint8_t(*buf)[8] = f->buffer.data;
609 (void)params;
610
611 ENCODER_INIT(1, TUPLE__ROW);
612
613 value.type = COWSQL_UNIXTIME;
614 value.unixtime = 12345;
615 ENCODER_NEXT;
616
617 munit_assert_int(buf[0][0], ==, COWSQL_UNIXTIME);
618 uint64_t *value_ptr =
619 __builtin_assume_aligned(buf[1], sizeof(uint64_t));
620 munit_assert_uint64(*value_ptr, ==,
621 ByteFlipLe64((uint64_t)value.unixtime));
622
623 return MUNIT_OK;
624 }
625
626 /* Encode an ISO8601 date string time parameter. */
627 TEST_CASE(encoder, type, iso8601, NULL)
628 {
629 struct encoder_fixture *f = data;
630 struct value value;
631 uint8_t(*buf)[8] = f->buffer.data;
632 (void)params;
633
634 ENCODER_INIT(1, TUPLE__ROW);
635
636 value.type = COWSQL_ISO8601;
637 value.iso8601 = "2018-07-20 09:49:05+00:00";
638 ENCODER_NEXT;
639
640 munit_assert_int(buf[0][0], ==, COWSQL_ISO8601);
641 munit_assert_string_equal((char *)buf[1], "2018-07-20 09:49:05+00:00");
642
643 return MUNIT_OK;
644 }
645
646 /* Encode a boolean parameter. */
647 TEST_CASE(encoder, type, boolean, NULL)
648 {
649 struct encoder_fixture *f = data;
650 struct value value;
651 uint8_t(*buf)[8] = f->buffer.data;
652 (void)params;
653
654 ENCODER_INIT(1, TUPLE__ROW);
655
656 value.type = COWSQL_BOOLEAN;
657 value.boolean = 1;
658 ENCODER_NEXT;
659
660 munit_assert_int(buf[0][0], ==, COWSQL_BOOLEAN);
661 uint64_t *value_ptr =
662 __builtin_assume_aligned(buf[1], sizeof(uint64_t));
663 munit_assert_uint64(*value_ptr, ==, ByteFlipLe64(value.boolean));
664
665 return MUNIT_OK;
666 }