File: array_encoders_test.c

package info (click to toggle)
mysql-8.0 8.0.43-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,904 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,184; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,196; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (52 lines) | stat: -rw-r--r-- 1,792 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include "assertions.h"
#include "cbor.h"

unsigned char buffer[512];

static void test_embedded_array_start(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(1, cbor_encode_array_start(1, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0x81}), 1);
}

static void test_array_start(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(5, cbor_encode_array_start(1000000, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0x9A, 0x00, 0x0F, 0x42, 0x40}),
                      5);
}

static void test_indef_array_start(void **_CBOR_UNUSED(_state)) {
  assert_size_equal(1, cbor_encode_indef_array_start(buffer, 512));
  assert_size_equal(0, cbor_encode_indef_array_start(buffer, 0));
  assert_memory_equal(buffer, ((unsigned char[]){0x9F}), 1);
}

static void test_indef_array_encoding(void **_CBOR_UNUSED(_state)) {
  cbor_item_t *array = cbor_new_indefinite_array();
  cbor_item_t *one = cbor_build_uint8(1);
  cbor_item_t *two = cbor_build_uint8(2);
  assert_true(cbor_array_push(array, one));
  assert_true(cbor_array_push(array, two));

  assert_size_equal(4, cbor_serialize_array(array, buffer, 512));
  assert_memory_equal(buffer, ((unsigned char[]){0x9F, 0x01, 0x02, 0xFF}), 4);

  cbor_decref(&array);
  cbor_decref(&one);
  cbor_decref(&two);
}

int main(void) {
  const struct CMUnitTest tests[] = {
      cmocka_unit_test(test_embedded_array_start),
      cmocka_unit_test(test_array_start),
      cmocka_unit_test(test_indef_array_start),
      cmocka_unit_test(test_indef_array_encoding)};
  return cmocka_run_group_tests(tests, NULL, NULL);
}