]>
Commit | Line | Data |
---|---|---|
d88f5fd1 LC |
1 | /* |
2 | * QMP Input Visitor unit-tests. | |
3 | * | |
805017b7 | 4 | * Copyright (C) 2011, 2015 Red Hat Inc. |
d88f5fd1 LC |
5 | * |
6 | * Authors: | |
7 | * Luiz Capitulino <lcapitulino@redhat.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
10 | * See the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
13 | #include <glib.h> | |
14 | #include <stdarg.h> | |
15 | ||
79ee7df8 | 16 | #include "qemu-common.h" |
d88f5fd1 LC |
17 | #include "qapi/qmp-input-visitor.h" |
18 | #include "test-qapi-types.h" | |
19 | #include "test-qapi-visit.h" | |
7b1b5d19 | 20 | #include "qapi/qmp/types.h" |
d88f5fd1 LC |
21 | |
22 | typedef struct TestInputVisitorData { | |
23 | QObject *obj; | |
24 | QmpInputVisitor *qiv; | |
25 | } TestInputVisitorData; | |
26 | ||
27 | static void visitor_input_teardown(TestInputVisitorData *data, | |
28 | const void *unused) | |
29 | { | |
30 | qobject_decref(data->obj); | |
31 | data->obj = NULL; | |
32 | ||
33 | if (data->qiv) { | |
34 | qmp_input_visitor_cleanup(data->qiv); | |
35 | data->qiv = NULL; | |
36 | } | |
37 | } | |
38 | ||
39 | /* This is provided instead of a test setup function so that the JSON | |
40 | string used by the tests are kept in the test functions (and not | |
41 | int main()) */ | |
aba2107a SW |
42 | static GCC_FMT_ATTR(2, 3) |
43 | Visitor *visitor_input_test_init(TestInputVisitorData *data, | |
44 | const char *json_string, ...) | |
d88f5fd1 LC |
45 | { |
46 | Visitor *v; | |
47 | va_list ap; | |
48 | ||
49 | va_start(ap, json_string); | |
50 | data->obj = qobject_from_jsonv(json_string, &ap); | |
51 | va_end(ap); | |
52 | ||
53 | g_assert(data->obj != NULL); | |
54 | ||
55 | data->qiv = qmp_input_visitor_new(data->obj); | |
56 | g_assert(data->qiv != NULL); | |
57 | ||
58 | v = qmp_input_get_visitor(data->qiv); | |
59 | g_assert(v != NULL); | |
60 | ||
61 | return v; | |
62 | } | |
63 | ||
199e0f17 MR |
64 | /* similar to visitor_input_test_init(), but does not expect a string |
65 | * literal/format json_string argument and so can be used for | |
66 | * programatically generated strings (and we can't pass in programatically | |
67 | * generated strings via %s format parameters since qobject_from_jsonv() | |
68 | * will wrap those in double-quotes and treat the entire object as a | |
69 | * string) | |
70 | */ | |
71 | static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data, | |
72 | const char *json_string) | |
73 | { | |
74 | Visitor *v; | |
75 | ||
76 | data->obj = qobject_from_json(json_string); | |
77 | ||
78 | g_assert(data->obj != NULL); | |
79 | ||
80 | data->qiv = qmp_input_visitor_new(data->obj); | |
81 | g_assert(data->qiv != NULL); | |
82 | ||
83 | v = qmp_input_get_visitor(data->qiv); | |
84 | g_assert(v != NULL); | |
85 | ||
86 | return v; | |
87 | } | |
88 | ||
d88f5fd1 LC |
89 | static void test_visitor_in_int(TestInputVisitorData *data, |
90 | const void *unused) | |
91 | { | |
92 | int64_t res = 0, value = -42; | |
e940f543 | 93 | Error *err = NULL; |
d88f5fd1 LC |
94 | Visitor *v; |
95 | ||
aba2107a | 96 | v = visitor_input_test_init(data, "%" PRId64, value); |
d88f5fd1 | 97 | |
e940f543 MA |
98 | visit_type_int(v, &res, NULL, &err); |
99 | g_assert(!err); | |
d88f5fd1 LC |
100 | g_assert_cmpint(res, ==, value); |
101 | } | |
102 | ||
e92cfa0d MR |
103 | static void test_visitor_in_int_overflow(TestInputVisitorData *data, |
104 | const void *unused) | |
105 | { | |
106 | int64_t res = 0; | |
e940f543 | 107 | Error *err = NULL; |
e92cfa0d MR |
108 | Visitor *v; |
109 | ||
110 | /* this will overflow a Qint/int64, so should be deserialized into | |
111 | * a QFloat/double field instead, leading to an error if we pass it | |
112 | * to visit_type_int. confirm this. | |
113 | */ | |
114 | v = visitor_input_test_init(data, "%f", DBL_MAX); | |
115 | ||
e940f543 MA |
116 | visit_type_int(v, &res, NULL, &err); |
117 | g_assert(err); | |
118 | error_free(err); | |
e92cfa0d MR |
119 | } |
120 | ||
d88f5fd1 LC |
121 | static void test_visitor_in_bool(TestInputVisitorData *data, |
122 | const void *unused) | |
123 | { | |
e940f543 | 124 | Error *err = NULL; |
d88f5fd1 LC |
125 | bool res = false; |
126 | Visitor *v; | |
127 | ||
128 | v = visitor_input_test_init(data, "true"); | |
129 | ||
e940f543 MA |
130 | visit_type_bool(v, &res, NULL, &err); |
131 | g_assert(!err); | |
d88f5fd1 LC |
132 | g_assert_cmpint(res, ==, true); |
133 | } | |
134 | ||
135 | static void test_visitor_in_number(TestInputVisitorData *data, | |
136 | const void *unused) | |
137 | { | |
138 | double res = 0, value = 3.14; | |
e940f543 | 139 | Error *err = NULL; |
d88f5fd1 LC |
140 | Visitor *v; |
141 | ||
142 | v = visitor_input_test_init(data, "%f", value); | |
143 | ||
e940f543 MA |
144 | visit_type_number(v, &res, NULL, &err); |
145 | g_assert(!err); | |
d88f5fd1 LC |
146 | g_assert_cmpfloat(res, ==, value); |
147 | } | |
148 | ||
149 | static void test_visitor_in_string(TestInputVisitorData *data, | |
150 | const void *unused) | |
151 | { | |
152 | char *res = NULL, *value = (char *) "Q E M U"; | |
e940f543 | 153 | Error *err = NULL; |
d88f5fd1 LC |
154 | Visitor *v; |
155 | ||
156 | v = visitor_input_test_init(data, "%s", value); | |
157 | ||
e940f543 MA |
158 | visit_type_str(v, &res, NULL, &err); |
159 | g_assert(!err); | |
d88f5fd1 LC |
160 | g_assert_cmpstr(res, ==, value); |
161 | ||
162 | g_free(res); | |
163 | } | |
164 | ||
165 | static void test_visitor_in_enum(TestInputVisitorData *data, | |
166 | const void *unused) | |
167 | { | |
e940f543 | 168 | Error *err = NULL; |
d88f5fd1 LC |
169 | Visitor *v; |
170 | EnumOne i; | |
171 | ||
172 | for (i = 0; EnumOne_lookup[i]; i++) { | |
173 | EnumOne res = -1; | |
174 | ||
175 | v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]); | |
176 | ||
e940f543 MA |
177 | visit_type_EnumOne(v, &res, NULL, &err); |
178 | g_assert(!err); | |
d88f5fd1 LC |
179 | g_assert_cmpint(i, ==, res); |
180 | ||
181 | visitor_input_teardown(data, NULL); | |
182 | } | |
183 | ||
184 | data->obj = NULL; | |
185 | data->qiv = NULL; | |
186 | } | |
187 | ||
188 | typedef struct TestStruct | |
189 | { | |
190 | int64_t integer; | |
191 | bool boolean; | |
192 | char *string; | |
193 | } TestStruct; | |
194 | ||
195 | static void visit_type_TestStruct(Visitor *v, TestStruct **obj, | |
196 | const char *name, Error **errp) | |
197 | { | |
d195325b | 198 | Error *err = NULL; |
196857f8 MA |
199 | |
200 | visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct), | |
201 | &err); | |
297a3646 MA |
202 | if (err) { |
203 | goto out; | |
d195325b | 204 | } |
297a3646 MA |
205 | visit_type_int(v, &(*obj)->integer, "integer", &err); |
206 | if (err) { | |
207 | goto out_end; | |
208 | } | |
209 | visit_type_bool(v, &(*obj)->boolean, "boolean", &err); | |
210 | if (err) { | |
211 | goto out_end; | |
212 | } | |
213 | visit_type_str(v, &(*obj)->string, "string", &err); | |
214 | ||
215 | out_end: | |
216 | error_propagate(errp, err); | |
217 | err = NULL; | |
218 | visit_end_struct(v, &err); | |
219 | out: | |
196857f8 | 220 | error_propagate(errp, err); |
d88f5fd1 LC |
221 | } |
222 | ||
223 | static void test_visitor_in_struct(TestInputVisitorData *data, | |
224 | const void *unused) | |
225 | { | |
226 | TestStruct *p = NULL; | |
e940f543 | 227 | Error *err = NULL; |
d88f5fd1 LC |
228 | Visitor *v; |
229 | ||
230 | v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); | |
231 | ||
e940f543 MA |
232 | visit_type_TestStruct(v, &p, NULL, &err); |
233 | g_assert(!err); | |
d88f5fd1 LC |
234 | g_assert_cmpint(p->integer, ==, -42); |
235 | g_assert(p->boolean == true); | |
236 | g_assert_cmpstr(p->string, ==, "foo"); | |
237 | ||
238 | g_free(p->string); | |
239 | g_free(p); | |
240 | } | |
241 | ||
242 | static void check_and_free_str(char *str, const char *cmp) | |
243 | { | |
244 | g_assert_cmpstr(str, ==, cmp); | |
245 | g_free(str); | |
246 | } | |
247 | ||
248 | static void test_visitor_in_struct_nested(TestInputVisitorData *data, | |
249 | const void *unused) | |
250 | { | |
b6fcf32d | 251 | UserDefTwo *udp = NULL; |
e940f543 | 252 | Error *err = NULL; |
d88f5fd1 LC |
253 | Visitor *v; |
254 | ||
b6fcf32d EB |
255 | v = visitor_input_test_init(data, "{ 'string0': 'string0', " |
256 | "'dict1': { 'string1': 'string1', " | |
257 | "'dict2': { 'userdef': { 'integer': 42, " | |
258 | "'string': 'string' }, 'string': 'string2'}}}"); | |
d88f5fd1 | 259 | |
b6fcf32d | 260 | visit_type_UserDefTwo(v, &udp, NULL, &err); |
e940f543 | 261 | g_assert(!err); |
d88f5fd1 LC |
262 | |
263 | check_and_free_str(udp->string0, "string0"); | |
264 | check_and_free_str(udp->dict1.string1, "string1"); | |
b6fcf32d EB |
265 | g_assert_cmpint(udp->dict1.dict2.userdef->base->integer, ==, 42); |
266 | check_and_free_str(udp->dict1.dict2.userdef->string, "string"); | |
267 | check_and_free_str(udp->dict1.dict2.string, "string2"); | |
d88f5fd1 LC |
268 | g_assert(udp->dict1.has_dict3 == false); |
269 | ||
b6fcf32d | 270 | g_free(udp->dict1.dict2.userdef); |
d88f5fd1 LC |
271 | g_free(udp); |
272 | } | |
273 | ||
274 | static void test_visitor_in_list(TestInputVisitorData *data, | |
275 | const void *unused) | |
276 | { | |
277 | UserDefOneList *item, *head = NULL; | |
e940f543 | 278 | Error *err = NULL; |
d88f5fd1 LC |
279 | Visitor *v; |
280 | int i; | |
281 | ||
282 | v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); | |
283 | ||
e940f543 MA |
284 | visit_type_UserDefOneList(v, &head, NULL, &err); |
285 | g_assert(!err); | |
d88f5fd1 LC |
286 | g_assert(head != NULL); |
287 | ||
288 | for (i = 0, item = head; item; item = item->next, i++) { | |
289 | char string[12]; | |
290 | ||
291 | snprintf(string, sizeof(string), "string%d", i); | |
292 | g_assert_cmpstr(item->value->string, ==, string); | |
aabbd472 | 293 | g_assert_cmpint(item->value->base->integer, ==, 42 + i); |
d88f5fd1 LC |
294 | } |
295 | ||
296 | qapi_free_UserDefOneList(head); | |
297 | } | |
298 | ||
2fc00432 MA |
299 | static void test_visitor_in_union_flat(TestInputVisitorData *data, |
300 | const void *unused) | |
301 | { | |
302 | Visitor *v; | |
303 | Error *err = NULL; | |
304 | UserDefFlatUnion *tmp; | |
305 | ||
5223070c WX |
306 | v = visitor_input_test_init(data, |
307 | "{ 'enum1': 'value1', " | |
308 | "'string': 'str', " | |
309 | "'boolean': true }"); | |
2fc00432 MA |
310 | /* TODO when generator bug is fixed, add 'integer': 41 */ |
311 | ||
312 | visit_type_UserDefFlatUnion(v, &tmp, NULL, &err); | |
313 | g_assert(err == NULL); | |
5223070c WX |
314 | g_assert_cmpint(tmp->kind, ==, ENUM_ONE_VALUE1); |
315 | g_assert_cmpstr(tmp->string, ==, "str"); | |
2fc00432 | 316 | /* TODO g_assert_cmpint(tmp->integer, ==, 41); */ |
5223070c | 317 | g_assert_cmpint(tmp->value1->boolean, ==, true); |
2fc00432 MA |
318 | qapi_free_UserDefFlatUnion(tmp); |
319 | } | |
320 | ||
ab045267 EB |
321 | static void test_visitor_in_alternate(TestInputVisitorData *data, |
322 | const void *unused) | |
2c38b600 MA |
323 | { |
324 | Visitor *v; | |
325 | Error *err = NULL; | |
ab045267 | 326 | UserDefAlternate *tmp; |
2c38b600 MA |
327 | |
328 | v = visitor_input_test_init(data, "42"); | |
329 | ||
ab045267 | 330 | visit_type_UserDefAlternate(v, &tmp, NULL, &err); |
2c38b600 | 331 | g_assert(err == NULL); |
ab045267 | 332 | g_assert_cmpint(tmp->kind, ==, USER_DEF_ALTERNATE_KIND_I); |
2c38b600 | 333 | g_assert_cmpint(tmp->i, ==, 42); |
ab045267 | 334 | qapi_free_UserDefAlternate(tmp); |
2c38b600 MA |
335 | } |
336 | ||
199e0f17 MR |
337 | static void test_native_list_integer_helper(TestInputVisitorData *data, |
338 | const void *unused, | |
339 | UserDefNativeListUnionKind kind) | |
340 | { | |
341 | UserDefNativeListUnion *cvalue = NULL; | |
342 | Error *err = NULL; | |
343 | Visitor *v; | |
344 | GString *gstr_list = g_string_new(""); | |
345 | GString *gstr_union = g_string_new(""); | |
346 | int i; | |
347 | ||
348 | for (i = 0; i < 32; i++) { | |
349 | g_string_append_printf(gstr_list, "%d", i); | |
350 | if (i != 31) { | |
351 | g_string_append(gstr_list, ", "); | |
352 | } | |
353 | } | |
354 | g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }", | |
355 | UserDefNativeListUnionKind_lookup[kind], | |
356 | gstr_list->str); | |
357 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
358 | ||
359 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
360 | g_assert(err == NULL); | |
361 | g_assert(cvalue != NULL); | |
362 | g_assert_cmpint(cvalue->kind, ==, kind); | |
363 | ||
364 | switch (kind) { | |
365 | case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: { | |
366 | intList *elem = NULL; | |
367 | for (i = 0, elem = cvalue->integer; elem; elem = elem->next, i++) { | |
368 | g_assert_cmpint(elem->value, ==, i); | |
369 | } | |
370 | break; | |
371 | } | |
372 | case USER_DEF_NATIVE_LIST_UNION_KIND_S8: { | |
373 | int8List *elem = NULL; | |
374 | for (i = 0, elem = cvalue->s8; elem; elem = elem->next, i++) { | |
375 | g_assert_cmpint(elem->value, ==, i); | |
376 | } | |
377 | break; | |
378 | } | |
379 | case USER_DEF_NATIVE_LIST_UNION_KIND_S16: { | |
380 | int16List *elem = NULL; | |
381 | for (i = 0, elem = cvalue->s16; elem; elem = elem->next, i++) { | |
382 | g_assert_cmpint(elem->value, ==, i); | |
383 | } | |
384 | break; | |
385 | } | |
386 | case USER_DEF_NATIVE_LIST_UNION_KIND_S32: { | |
387 | int32List *elem = NULL; | |
388 | for (i = 0, elem = cvalue->s32; elem; elem = elem->next, i++) { | |
389 | g_assert_cmpint(elem->value, ==, i); | |
390 | } | |
391 | break; | |
392 | } | |
393 | case USER_DEF_NATIVE_LIST_UNION_KIND_S64: { | |
394 | int64List *elem = NULL; | |
395 | for (i = 0, elem = cvalue->s64; elem; elem = elem->next, i++) { | |
396 | g_assert_cmpint(elem->value, ==, i); | |
397 | } | |
398 | break; | |
399 | } | |
400 | case USER_DEF_NATIVE_LIST_UNION_KIND_U8: { | |
401 | uint8List *elem = NULL; | |
402 | for (i = 0, elem = cvalue->u8; elem; elem = elem->next, i++) { | |
403 | g_assert_cmpint(elem->value, ==, i); | |
404 | } | |
405 | break; | |
406 | } | |
407 | case USER_DEF_NATIVE_LIST_UNION_KIND_U16: { | |
408 | uint16List *elem = NULL; | |
409 | for (i = 0, elem = cvalue->u16; elem; elem = elem->next, i++) { | |
410 | g_assert_cmpint(elem->value, ==, i); | |
411 | } | |
412 | break; | |
413 | } | |
414 | case USER_DEF_NATIVE_LIST_UNION_KIND_U32: { | |
415 | uint32List *elem = NULL; | |
416 | for (i = 0, elem = cvalue->u32; elem; elem = elem->next, i++) { | |
417 | g_assert_cmpint(elem->value, ==, i); | |
418 | } | |
419 | break; | |
420 | } | |
421 | case USER_DEF_NATIVE_LIST_UNION_KIND_U64: { | |
422 | uint64List *elem = NULL; | |
423 | for (i = 0, elem = cvalue->u64; elem; elem = elem->next, i++) { | |
424 | g_assert_cmpint(elem->value, ==, i); | |
425 | } | |
426 | break; | |
427 | } | |
428 | default: | |
dfc6f865 | 429 | g_assert_not_reached(); |
199e0f17 MR |
430 | } |
431 | ||
432 | g_string_free(gstr_union, true); | |
433 | g_string_free(gstr_list, true); | |
434 | qapi_free_UserDefNativeListUnion(cvalue); | |
435 | } | |
436 | ||
437 | static void test_visitor_in_native_list_int(TestInputVisitorData *data, | |
438 | const void *unused) | |
439 | { | |
440 | test_native_list_integer_helper(data, unused, | |
441 | USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER); | |
442 | } | |
443 | ||
444 | static void test_visitor_in_native_list_int8(TestInputVisitorData *data, | |
445 | const void *unused) | |
446 | { | |
447 | test_native_list_integer_helper(data, unused, | |
448 | USER_DEF_NATIVE_LIST_UNION_KIND_S8); | |
449 | } | |
450 | ||
451 | static void test_visitor_in_native_list_int16(TestInputVisitorData *data, | |
452 | const void *unused) | |
453 | { | |
454 | test_native_list_integer_helper(data, unused, | |
455 | USER_DEF_NATIVE_LIST_UNION_KIND_S16); | |
456 | } | |
457 | ||
458 | static void test_visitor_in_native_list_int32(TestInputVisitorData *data, | |
459 | const void *unused) | |
460 | { | |
461 | test_native_list_integer_helper(data, unused, | |
462 | USER_DEF_NATIVE_LIST_UNION_KIND_S32); | |
463 | } | |
464 | ||
465 | static void test_visitor_in_native_list_int64(TestInputVisitorData *data, | |
466 | const void *unused) | |
467 | { | |
468 | test_native_list_integer_helper(data, unused, | |
469 | USER_DEF_NATIVE_LIST_UNION_KIND_S64); | |
470 | } | |
471 | ||
472 | static void test_visitor_in_native_list_uint8(TestInputVisitorData *data, | |
473 | const void *unused) | |
474 | { | |
475 | test_native_list_integer_helper(data, unused, | |
476 | USER_DEF_NATIVE_LIST_UNION_KIND_U8); | |
477 | } | |
478 | ||
479 | static void test_visitor_in_native_list_uint16(TestInputVisitorData *data, | |
480 | const void *unused) | |
481 | { | |
482 | test_native_list_integer_helper(data, unused, | |
483 | USER_DEF_NATIVE_LIST_UNION_KIND_U16); | |
484 | } | |
485 | ||
486 | static void test_visitor_in_native_list_uint32(TestInputVisitorData *data, | |
487 | const void *unused) | |
488 | { | |
489 | test_native_list_integer_helper(data, unused, | |
490 | USER_DEF_NATIVE_LIST_UNION_KIND_U32); | |
491 | } | |
492 | ||
493 | static void test_visitor_in_native_list_uint64(TestInputVisitorData *data, | |
494 | const void *unused) | |
495 | { | |
496 | test_native_list_integer_helper(data, unused, | |
497 | USER_DEF_NATIVE_LIST_UNION_KIND_U64); | |
498 | } | |
499 | ||
500 | static void test_visitor_in_native_list_bool(TestInputVisitorData *data, | |
501 | const void *unused) | |
502 | { | |
503 | UserDefNativeListUnion *cvalue = NULL; | |
504 | boolList *elem = NULL; | |
505 | Error *err = NULL; | |
506 | Visitor *v; | |
507 | GString *gstr_list = g_string_new(""); | |
508 | GString *gstr_union = g_string_new(""); | |
509 | int i; | |
510 | ||
511 | for (i = 0; i < 32; i++) { | |
512 | g_string_append_printf(gstr_list, "%s", | |
513 | (i % 3 == 0) ? "true" : "false"); | |
514 | if (i != 31) { | |
515 | g_string_append(gstr_list, ", "); | |
516 | } | |
517 | } | |
518 | g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }", | |
519 | gstr_list->str); | |
520 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
521 | ||
522 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
523 | g_assert(err == NULL); | |
524 | g_assert(cvalue != NULL); | |
525 | g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN); | |
526 | ||
527 | for (i = 0, elem = cvalue->boolean; elem; elem = elem->next, i++) { | |
528 | g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0); | |
529 | } | |
530 | ||
531 | g_string_free(gstr_union, true); | |
532 | g_string_free(gstr_list, true); | |
533 | qapi_free_UserDefNativeListUnion(cvalue); | |
534 | } | |
535 | ||
536 | static void test_visitor_in_native_list_string(TestInputVisitorData *data, | |
537 | const void *unused) | |
538 | { | |
539 | UserDefNativeListUnion *cvalue = NULL; | |
540 | strList *elem = NULL; | |
541 | Error *err = NULL; | |
542 | Visitor *v; | |
543 | GString *gstr_list = g_string_new(""); | |
544 | GString *gstr_union = g_string_new(""); | |
545 | int i; | |
546 | ||
547 | for (i = 0; i < 32; i++) { | |
548 | g_string_append_printf(gstr_list, "'%d'", i); | |
549 | if (i != 31) { | |
550 | g_string_append(gstr_list, ", "); | |
551 | } | |
552 | } | |
553 | g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }", | |
554 | gstr_list->str); | |
555 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
556 | ||
557 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
558 | g_assert(err == NULL); | |
559 | g_assert(cvalue != NULL); | |
560 | g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING); | |
561 | ||
562 | for (i = 0, elem = cvalue->string; elem; elem = elem->next, i++) { | |
563 | gchar str[8]; | |
564 | sprintf(str, "%d", i); | |
565 | g_assert_cmpstr(elem->value, ==, str); | |
566 | } | |
567 | ||
568 | g_string_free(gstr_union, true); | |
569 | g_string_free(gstr_list, true); | |
570 | qapi_free_UserDefNativeListUnion(cvalue); | |
571 | } | |
572 | ||
573 | #define DOUBLE_STR_MAX 16 | |
574 | ||
575 | static void test_visitor_in_native_list_number(TestInputVisitorData *data, | |
576 | const void *unused) | |
577 | { | |
578 | UserDefNativeListUnion *cvalue = NULL; | |
579 | numberList *elem = NULL; | |
580 | Error *err = NULL; | |
581 | Visitor *v; | |
582 | GString *gstr_list = g_string_new(""); | |
583 | GString *gstr_union = g_string_new(""); | |
584 | int i; | |
585 | ||
586 | for (i = 0; i < 32; i++) { | |
587 | g_string_append_printf(gstr_list, "%f", (double)i / 3); | |
588 | if (i != 31) { | |
589 | g_string_append(gstr_list, ", "); | |
590 | } | |
591 | } | |
592 | g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }", | |
593 | gstr_list->str); | |
594 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
595 | ||
596 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
597 | g_assert(err == NULL); | |
598 | g_assert(cvalue != NULL); | |
599 | g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER); | |
600 | ||
601 | for (i = 0, elem = cvalue->number; elem; elem = elem->next, i++) { | |
602 | GString *double_expected = g_string_new(""); | |
603 | GString *double_actual = g_string_new(""); | |
604 | ||
605 | g_string_printf(double_expected, "%.6f", (double)i / 3); | |
606 | g_string_printf(double_actual, "%.6f", elem->value); | |
607 | g_assert_cmpstr(double_expected->str, ==, double_actual->str); | |
608 | ||
609 | g_string_free(double_expected, true); | |
610 | g_string_free(double_actual, true); | |
611 | } | |
612 | ||
613 | g_string_free(gstr_union, true); | |
614 | g_string_free(gstr_list, true); | |
615 | qapi_free_UserDefNativeListUnion(cvalue); | |
616 | } | |
617 | ||
d88f5fd1 LC |
618 | static void input_visitor_test_add(const char *testpath, |
619 | TestInputVisitorData *data, | |
620 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) | |
621 | { | |
622 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, | |
623 | visitor_input_teardown); | |
624 | } | |
625 | ||
3dcf71f6 PB |
626 | static void test_visitor_in_errors(TestInputVisitorData *data, |
627 | const void *unused) | |
628 | { | |
629 | TestStruct *p = NULL; | |
e940f543 | 630 | Error *err = NULL; |
3dcf71f6 PB |
631 | Visitor *v; |
632 | ||
633 | v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }"); | |
634 | ||
e940f543 MA |
635 | visit_type_TestStruct(v, &p, NULL, &err); |
636 | g_assert(err); | |
3dcf71f6 PB |
637 | g_assert(p->string == NULL); |
638 | ||
e940f543 | 639 | error_free(err); |
3dcf71f6 PB |
640 | g_free(p->string); |
641 | g_free(p); | |
642 | } | |
643 | ||
d88f5fd1 LC |
644 | int main(int argc, char **argv) |
645 | { | |
646 | TestInputVisitorData in_visitor_data; | |
647 | ||
648 | g_test_init(&argc, &argv, NULL); | |
649 | ||
650 | input_visitor_test_add("/visitor/input/int", | |
651 | &in_visitor_data, test_visitor_in_int); | |
e92cfa0d MR |
652 | input_visitor_test_add("/visitor/input/int_overflow", |
653 | &in_visitor_data, test_visitor_in_int_overflow); | |
d88f5fd1 LC |
654 | input_visitor_test_add("/visitor/input/bool", |
655 | &in_visitor_data, test_visitor_in_bool); | |
656 | input_visitor_test_add("/visitor/input/number", | |
657 | &in_visitor_data, test_visitor_in_number); | |
658 | input_visitor_test_add("/visitor/input/string", | |
805017b7 | 659 | &in_visitor_data, test_visitor_in_string); |
d88f5fd1 | 660 | input_visitor_test_add("/visitor/input/enum", |
805017b7 | 661 | &in_visitor_data, test_visitor_in_enum); |
d88f5fd1 | 662 | input_visitor_test_add("/visitor/input/struct", |
805017b7 | 663 | &in_visitor_data, test_visitor_in_struct); |
d88f5fd1 | 664 | input_visitor_test_add("/visitor/input/struct-nested", |
805017b7 | 665 | &in_visitor_data, test_visitor_in_struct_nested); |
d88f5fd1 | 666 | input_visitor_test_add("/visitor/input/list", |
805017b7 | 667 | &in_visitor_data, test_visitor_in_list); |
2fc00432 | 668 | input_visitor_test_add("/visitor/input/union-flat", |
805017b7 | 669 | &in_visitor_data, test_visitor_in_union_flat); |
ab045267 EB |
670 | input_visitor_test_add("/visitor/input/alternate", |
671 | &in_visitor_data, test_visitor_in_alternate); | |
3dcf71f6 | 672 | input_visitor_test_add("/visitor/input/errors", |
805017b7 | 673 | &in_visitor_data, test_visitor_in_errors); |
199e0f17 | 674 | input_visitor_test_add("/visitor/input/native_list/int", |
805017b7 EB |
675 | &in_visitor_data, |
676 | test_visitor_in_native_list_int); | |
199e0f17 | 677 | input_visitor_test_add("/visitor/input/native_list/int8", |
805017b7 EB |
678 | &in_visitor_data, |
679 | test_visitor_in_native_list_int8); | |
199e0f17 | 680 | input_visitor_test_add("/visitor/input/native_list/int16", |
805017b7 EB |
681 | &in_visitor_data, |
682 | test_visitor_in_native_list_int16); | |
199e0f17 | 683 | input_visitor_test_add("/visitor/input/native_list/int32", |
805017b7 EB |
684 | &in_visitor_data, |
685 | test_visitor_in_native_list_int32); | |
199e0f17 | 686 | input_visitor_test_add("/visitor/input/native_list/int64", |
805017b7 EB |
687 | &in_visitor_data, |
688 | test_visitor_in_native_list_int64); | |
199e0f17 | 689 | input_visitor_test_add("/visitor/input/native_list/uint8", |
805017b7 EB |
690 | &in_visitor_data, |
691 | test_visitor_in_native_list_uint8); | |
199e0f17 | 692 | input_visitor_test_add("/visitor/input/native_list/uint16", |
805017b7 EB |
693 | &in_visitor_data, |
694 | test_visitor_in_native_list_uint16); | |
199e0f17 | 695 | input_visitor_test_add("/visitor/input/native_list/uint32", |
805017b7 EB |
696 | &in_visitor_data, |
697 | test_visitor_in_native_list_uint32); | |
199e0f17 | 698 | input_visitor_test_add("/visitor/input/native_list/uint64", |
805017b7 EB |
699 | &in_visitor_data, |
700 | test_visitor_in_native_list_uint64); | |
199e0f17 | 701 | input_visitor_test_add("/visitor/input/native_list/bool", |
805017b7 | 702 | &in_visitor_data, test_visitor_in_native_list_bool); |
199e0f17 | 703 | input_visitor_test_add("/visitor/input/native_list/str", |
805017b7 EB |
704 | &in_visitor_data, |
705 | test_visitor_in_native_list_string); | |
199e0f17 | 706 | input_visitor_test_add("/visitor/input/native_list/number", |
805017b7 EB |
707 | &in_visitor_data, |
708 | test_visitor_in_native_list_number); | |
d88f5fd1 LC |
709 | |
710 | g_test_run(); | |
711 | ||
712 | return 0; | |
713 | } |