xref: /netbsd-src/external/mit/libcbor/dist/test/stream_expectations.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 #include "stream_expectations.h"
2 
3 /* Ordered from 0 to queue_size - 1 */
4 struct test_assertion assertions_queue[MAX_QUEUE_ITEMS];
5 size_t queue_size = 0;
6 size_t current_expectation = 0;
7 decoder_t* decoder;
8 
9 void set_decoder(decoder_t* dec) { decoder = dec; }
10 
11 /* Callbacks */
12 struct test_assertion current() {
13   return assertions_queue[current_expectation];
14 }
15 
16 /* Assertions builders and matcher callbacks */
17 
18 void assert_uint8_eq(uint8_t actual) {
19   assertions_queue[queue_size++] = (struct test_assertion){
20       UINT8_EQ, (union test_expectation_data){.int8 = actual}};
21 }
22 
23 void uint8_callback(void* context, uint8_t actual) {
24   assert_true(current().expectation == UINT8_EQ);
25   assert_true(current().data.int8 == actual);
26   current_expectation++;
27 }
28 
29 void assert_uint16_eq(uint16_t actual) {
30   assertions_queue[queue_size++] = (struct test_assertion){
31       UINT16_EQ, (union test_expectation_data){.int16 = actual}};
32 }
33 
34 void uint16_callback(void* context, uint16_t actual) {
35   assert_true(current().expectation == UINT16_EQ);
36   assert_true(current().data.int16 == actual);
37   current_expectation++;
38 }
39 
40 void assert_uint32_eq(uint32_t actual) {
41   assertions_queue[queue_size++] = (struct test_assertion){
42       UINT32_EQ, (union test_expectation_data){.int32 = actual}};
43 }
44 
45 void uint32_callback(void* context, uint32_t actual) {
46   assert_true(current().expectation == UINT32_EQ);
47   assert_true(current().data.int32 == actual);
48   current_expectation++;
49 }
50 
51 void assert_uint64_eq(uint64_t actual) {
52   assertions_queue[queue_size++] = (struct test_assertion){
53       UINT64_EQ, (union test_expectation_data){.int64 = actual}};
54 }
55 
56 void uint64_callback(void* context, uint64_t actual) {
57   assert_true(current().expectation == UINT64_EQ);
58   assert_true(current().data.int64 == actual);
59   current_expectation++;
60 }
61 
62 void assert_negint8_eq(uint8_t actual) {
63   assertions_queue[queue_size++] = (struct test_assertion){
64       NEGINT8_EQ, (union test_expectation_data){.int8 = actual}};
65 }
66 
67 void negint8_callback(void* context, uint8_t actual) {
68   assert_true(current().expectation == NEGINT8_EQ);
69   assert_true(current().data.int8 == actual);
70   current_expectation++;
71 }
72 
73 void assert_negint16_eq(uint16_t actual) {
74   assertions_queue[queue_size++] = (struct test_assertion){
75       NEGINT16_EQ, (union test_expectation_data){.int16 = actual}};
76 }
77 
78 void negint16_callback(void* context, uint16_t actual) {
79   assert_true(current().expectation == NEGINT16_EQ);
80   assert_true(current().data.int16 == actual);
81   current_expectation++;
82 }
83 
84 void assert_negint32_eq(uint32_t actual) {
85   assertions_queue[queue_size++] = (struct test_assertion){
86       NEGINT32_EQ, (union test_expectation_data){.int32 = actual}};
87 }
88 
89 void negint32_callback(void* context, uint32_t actual) {
90   assert_true(current().expectation == NEGINT32_EQ);
91   assert_true(current().data.int32 == actual);
92   current_expectation++;
93 }
94 
95 void assert_negint64_eq(uint64_t actual) {
96   assertions_queue[queue_size++] = (struct test_assertion){
97       NEGINT64_EQ, (union test_expectation_data){.int64 = actual}};
98 }
99 
100 void negint64_callback(void* context, uint64_t actual) {
101   assert_true(current().expectation == NEGINT64_EQ);
102   assert_true(current().data.int64 == actual);
103   current_expectation++;
104 }
105 
106 void assert_bstring_mem_eq(cbor_data address, size_t length) {
107   assertions_queue[queue_size++] = (struct test_assertion){
108       BSTRING_MEM_EQ,
109       (union test_expectation_data){.string = {address, length}}};
110 }
111 
112 void byte_string_callback(void* context, cbor_data address, size_t length) {
113   assert_true(current().expectation == BSTRING_MEM_EQ);
114   assert_true(current().data.string.address == address);
115   assert_true(current().data.string.length == length);
116   current_expectation++;
117 }
118 
119 void assert_bstring_indef_start() {
120   assertions_queue[queue_size++] =
121       (struct test_assertion){.expectation = BSTRING_INDEF_START};
122 }
123 
124 void byte_string_start_callback(void* context) {
125   assert_true(current().expectation == BSTRING_INDEF_START);
126   current_expectation++;
127 }
128 
129 void assert_indef_break() {
130   assertions_queue[queue_size++] =
131       (struct test_assertion){.expectation = INDEF_BREAK};
132 }
133 
134 void indef_break_callback(void* context) {
135   assert_true(current().expectation == INDEF_BREAK);
136   current_expectation++;
137 }
138 
139 void assert_array_start(size_t length) {
140   assertions_queue[queue_size++] =
141       (struct test_assertion){ARRAY_START, {.length = length}};
142 }
143 
144 void array_start_callback(void* context, size_t length) {
145   assert_true(current().expectation == ARRAY_START);
146   assert_true(current().data.length == length);
147   current_expectation++;
148 }
149 
150 void assert_indef_array_start() {
151   assertions_queue[queue_size++] =
152       (struct test_assertion){.expectation = ARRAY_INDEF_START};
153 }
154 
155 void indef_array_start_callback(void* context) {
156   assert_true(current().expectation == ARRAY_INDEF_START);
157   current_expectation++;
158 }
159 
160 void assert_map_start(size_t length) {
161   assertions_queue[queue_size++] =
162       (struct test_assertion){MAP_START, {.length = length}};
163 }
164 
165 void map_start_callback(void* context, size_t length) {
166   assert_true(current().expectation == MAP_START);
167   assert_true(current().data.length == length);
168   current_expectation++;
169 }
170 
171 void assert_indef_map_start() {
172   assertions_queue[queue_size++] =
173       (struct test_assertion){.expectation = MAP_INDEF_START};
174 }
175 
176 void indef_map_start_callback(void* context) {
177   assert_true(current().expectation == MAP_INDEF_START);
178   current_expectation++;
179 }
180 
181 void assert_tag_eq(uint64_t value) {
182   assertions_queue[queue_size++] =
183       (struct test_assertion){TAG_EQ, {.int64 = value}};
184 }
185 
186 void tag_callback(void* context, uint64_t value) {
187   assert_true(current().expectation == TAG_EQ);
188   assert_true(current().data.int64 == value);
189   current_expectation++;
190 }
191 
192 void assert_half(float value) {
193   assertions_queue[queue_size++] =
194       (struct test_assertion){HALF_EQ, {.float2 = value}};
195 }
196 
197 void half_callback(void* context, float actual) {
198   assert_true(current().expectation == HALF_EQ);
199   assert_true(current().data.float2 == actual);
200   current_expectation++;
201 }
202 
203 void assert_float(float value) {
204   assertions_queue[queue_size++] =
205       (struct test_assertion){FLOAT_EQ, {.float4 = value}};
206 }
207 
208 void float_callback(void* context, float actual) {
209   assert_true(current().expectation == FLOAT_EQ);
210   assert_true(current().data.float4 == actual);
211   current_expectation++;
212 }
213 
214 void assert_double(double value) {
215   assertions_queue[queue_size++] =
216       (struct test_assertion){DOUBLE_EQ, {.float8 = value}};
217 }
218 
219 void double_callback(void* context, double actual) {
220   assert_true(current().expectation == DOUBLE_EQ);
221   assert_true(current().data.float8 == actual);
222   current_expectation++;
223 }
224 
225 void assert_bool(bool value) {
226   assertions_queue[queue_size++] =
227       (struct test_assertion){BOOL_EQ, {.boolean = value}};
228 }
229 
230 void assert_nil() {
231   assertions_queue[queue_size++] = (struct test_assertion){.expectation = NIL};
232 }
233 
234 void assert_undef() {
235   assertions_queue[queue_size++] =
236       (struct test_assertion){.expectation = UNDEF};
237 }
238 
239 void bool_callback(void* context, bool actual) {
240   assert_true(current().expectation == BOOL_EQ);
241   assert_true(current().data.boolean == actual);
242   current_expectation++;
243 }
244 
245 void null_callback(void* context) {
246   assert_true(current().expectation == NIL);
247   current_expectation++;
248 }
249 
250 void undef_callback(void* context) {
251   assert_true(current().expectation == UNDEF);
252   current_expectation++;
253 }
254 
255 const struct cbor_callbacks asserting_callbacks = {
256 
257     .uint8 = &uint8_callback,
258 
259     .uint16 = &uint16_callback,
260 
261     .uint32 = &uint32_callback,
262 
263     .uint64 = &uint64_callback,
264 
265     .negint8 = &negint8_callback,
266 
267     .negint16 = &negint16_callback,
268 
269     .negint32 = &negint32_callback,
270 
271     .negint64 = &negint64_callback,
272 
273     .byte_string = &byte_string_callback,
274     .byte_string_start = &byte_string_start_callback,
275 
276     .array_start = &array_start_callback,
277     .indef_array_start = &indef_array_start_callback,
278 
279     .map_start = &map_start_callback,
280     .indef_map_start = &indef_map_start_callback,
281 
282     .tag = &tag_callback,
283 
284     .float2 = &half_callback,
285 
286     .float4 = &float_callback,
287 
288     .float8 = &double_callback,
289 
290     .undefined = &undef_callback,
291     .boolean = &bool_callback,
292     .null = &null_callback,
293     .indef_break = &indef_break_callback};
294 
295 struct cbor_decoder_result decode(cbor_data source, size_t source_size) {
296   struct cbor_decoder_result result =
297       decoder(source, source_size, &asserting_callbacks, NULL);
298   /* Check remaining assertions */
299 
300   assert_true(current_expectation == queue_size);
301   /* Clean up */
302   current_expectation = queue_size = 0;
303   return result;
304 }
305