File: pretty_printer_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 (205 lines) | stat: -rw-r--r-- 7,994 bytes parent folder | download | duplicates (2)
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
/*
 * 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 <stdio.h>
#include <string.h>

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

void assert_describe_result(cbor_item_t *item, char *expected_result) {
#if CBOR_PRETTY_PRINTER
  // We know the expected size based on `expected_result`, but read everything
  // in order to get the full actual output in a useful error message.
  const size_t buffer_size = 512;
  FILE *outfile = tmpfile();
  cbor_describe(item, outfile);
  rewind(outfile);
  // Treat string as null-terminated since cmocka doesn't have asserts
  // for explicit length strings.
  char *output = malloc(buffer_size);
  assert_non_null(output);
  size_t output_size = fread(output, sizeof(char), buffer_size, outfile);
  output[output_size] = '\0';
  assert_string_equal(output, expected_result);
  assert_true(feof(outfile));
  free(output);
  fclose(outfile);
#endif
}

static void test_uint(void **_CBOR_UNUSED(_state)) {
  cbor_item_t *item = cbor_build_uint8(42);
  assert_describe_result(item, "[CBOR_TYPE_UINT] Width: 1B, Value: 42\n");
  cbor_decref(&item);
}

static void test_negint(void **_CBOR_UNUSED(_state)) {
  cbor_item_t *item = cbor_build_negint16(40);
  assert_describe_result(item,
                         "[CBOR_TYPE_NEGINT] Width: 2B, Value: -40 - 1\n");
  cbor_decref(&item);
}

static void test_definite_bytestring(void **_CBOR_UNUSED(_state)) {
  unsigned char data[] = {0x01, 0x02, 0x03};
  cbor_item_t *item = cbor_build_bytestring(data, 3);
  assert_describe_result(item,
                         "[CBOR_TYPE_BYTESTRING] Definite, Length: 3B, Data:\n"
                         "    010203\n");
  cbor_decref(&item);
}

static void test_indefinite_bytestring(void **_CBOR_UNUSED(_state)) {
  unsigned char data[] = {0x01, 0x02, 0x03};
  cbor_item_t *item = cbor_new_indefinite_bytestring();
  assert_true(cbor_bytestring_add_chunk(
      item, cbor_move(cbor_build_bytestring(data, 3))));
  assert_true(cbor_bytestring_add_chunk(
      item, cbor_move(cbor_build_bytestring(data, 2))));
  assert_describe_result(
      item,
      "[CBOR_TYPE_BYTESTRING] Indefinite, Chunks: 2, Chunk data:\n"
      "    [CBOR_TYPE_BYTESTRING] Definite, Length: 3B, Data:\n"
      "        010203\n"
      "    [CBOR_TYPE_BYTESTRING] Definite, Length: 2B, Data:\n"
      "        0102\n");
  cbor_decref(&item);
}

static void test_definite_string(void **_CBOR_UNUSED(_state)) {
  char *string = "Hello!";
  cbor_item_t *item = cbor_build_string(string);
  assert_describe_result(
      item,
      "[CBOR_TYPE_STRING] Definite, Length: 6B, Codepoints: 6, Data:\n"
      "    Hello!\n");
  cbor_decref(&item);
}

static void test_indefinite_string(void **_CBOR_UNUSED(_state)) {
  char *string = "Hello!";
  cbor_item_t *item = cbor_new_indefinite_string();
  assert_true(
      cbor_string_add_chunk(item, cbor_move(cbor_build_string(string))));
  assert_true(
      cbor_string_add_chunk(item, cbor_move(cbor_build_string(string))));
  assert_describe_result(
      item,
      "[CBOR_TYPE_STRING] Indefinite, Chunks: 2, Chunk data:\n"
      "    [CBOR_TYPE_STRING] Definite, Length: 6B, Codepoints: 6, Data:\n"
      "        Hello!\n"
      "    [CBOR_TYPE_STRING] Definite, Length: 6B, Codepoints: 6, Data:\n"
      "        Hello!\n");
  cbor_decref(&item);
}

static void test_multibyte_string(void **_CBOR_UNUSED(_state)) {
  // "Štěstíčko" in UTF-8
  char *string = "\xc5\xa0t\xc4\x9bst\xc3\xad\xc4\x8dko";
  cbor_item_t *item = cbor_build_string(string);
  assert_describe_result(
      item,
      "[CBOR_TYPE_STRING] Definite, Length: 13B, Codepoints: 9, Data:\n"
      "    \xc5\xa0t\xc4\x9bst\xc3\xad\xc4\x8dko\n");
  cbor_decref(&item);
}

static void test_definite_array(void **_CBOR_UNUSED(_state)) {
  cbor_item_t *item = cbor_new_definite_array(2);
  assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(1))));
  assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(2))));
  assert_describe_result(item,
                         "[CBOR_TYPE_ARRAY] Definite, Size: 2, Contents:\n"
                         "    [CBOR_TYPE_UINT] Width: 1B, Value: 1\n"
                         "    [CBOR_TYPE_UINT] Width: 1B, Value: 2\n");
  cbor_decref(&item);
}

static void test_indefinite_array(void **_CBOR_UNUSED(_state)) {
  cbor_item_t *item = cbor_new_indefinite_array();
  assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(1))));
  assert_true(cbor_array_push(item, cbor_move(cbor_build_uint8(2))));
  assert_describe_result(item,
                         "[CBOR_TYPE_ARRAY] Indefinite, Size: 2, Contents:\n"
                         "    [CBOR_TYPE_UINT] Width: 1B, Value: 1\n"
                         "    [CBOR_TYPE_UINT] Width: 1B, Value: 2\n");
  cbor_decref(&item);
}

static void test_definite_map(void **_CBOR_UNUSED(_state)) {
  cbor_item_t *item = cbor_new_definite_map(1);
  assert_true(cbor_map_add(
      item, (struct cbor_pair){.key = cbor_move(cbor_build_uint8(1)),
                               .value = cbor_move(cbor_build_uint8(2))}));
  assert_describe_result(item,
                         "[CBOR_TYPE_MAP] Definite, Size: 1, Contents:\n"
                         "    Map entry 0\n"
                         "        [CBOR_TYPE_UINT] Width: 1B, Value: 1\n"
                         "        [CBOR_TYPE_UINT] Width: 1B, Value: 2\n");
  cbor_decref(&item);
}

static void test_indefinite_map(void **_CBOR_UNUSED(_state)) {
  cbor_item_t *item = cbor_new_indefinite_map();
  assert_true(cbor_map_add(
      item, (struct cbor_pair){.key = cbor_move(cbor_build_uint8(1)),
                               .value = cbor_move(cbor_build_uint8(2))}));
  assert_describe_result(item,
                         "[CBOR_TYPE_MAP] Indefinite, Size: 1, Contents:\n"
                         "    Map entry 0\n"
                         "        [CBOR_TYPE_UINT] Width: 1B, Value: 1\n"
                         "        [CBOR_TYPE_UINT] Width: 1B, Value: 2\n");
  cbor_decref(&item);
}

static void test_tag(void **_CBOR_UNUSED(_state)) {
  cbor_item_t *item = cbor_build_tag(42, cbor_move(cbor_build_uint8(1)));
  assert_describe_result(item,
                         "[CBOR_TYPE_TAG] Value: 42\n"
                         "    [CBOR_TYPE_UINT] Width: 1B, Value: 1\n");
  cbor_decref(&item);
}

static void test_floats(void **_CBOR_UNUSED(_state)) {
  cbor_item_t *item = cbor_new_indefinite_array();
  assert_true(cbor_array_push(item, cbor_move(cbor_build_bool(true))));
  assert_true(
      cbor_array_push(item, cbor_move(cbor_build_ctrl(CBOR_CTRL_UNDEF))));
  assert_true(
      cbor_array_push(item, cbor_move(cbor_build_ctrl(CBOR_CTRL_NULL))));
  assert_true(cbor_array_push(item, cbor_move(cbor_build_ctrl(24))));
  assert_true(cbor_array_push(item, cbor_move(cbor_build_float4(3.14f))));
  assert_describe_result(
      item,
      "[CBOR_TYPE_ARRAY] Indefinite, Size: 5, Contents:\n"
      "    [CBOR_TYPE_FLOAT_CTRL] Bool: true\n"
      "    [CBOR_TYPE_FLOAT_CTRL] Undefined\n"
      "    [CBOR_TYPE_FLOAT_CTRL] Null\n"
      "    [CBOR_TYPE_FLOAT_CTRL] Simple value: 24\n"
      "    [CBOR_TYPE_FLOAT_CTRL] Width: 4B, Value: 3.140000\n");
  cbor_decref(&item);
}

int main(void) {
  const struct CMUnitTest tests[] = {
      cmocka_unit_test(test_uint),
      cmocka_unit_test(test_negint),
      cmocka_unit_test(test_definite_bytestring),
      cmocka_unit_test(test_indefinite_bytestring),
      cmocka_unit_test(test_definite_string),
      cmocka_unit_test(test_indefinite_string),
      cmocka_unit_test(test_multibyte_string),
      cmocka_unit_test(test_definite_array),
      cmocka_unit_test(test_indefinite_array),
      cmocka_unit_test(test_definite_map),
      cmocka_unit_test(test_indefinite_map),
      cmocka_unit_test(test_tag),
      cmocka_unit_test(test_floats),
  };
  return cmocka_run_group_tests(tests, NULL, NULL);
}