acpi: make bios_linker_loader_add_checksum() API offset based
[qemu/ar7.git] / tests / test-qmp-input-visitor.c
blobcee07ce8dd2b5c3f20acb0b24b9185b4818ebc92
1 /*
2 * QMP Input Visitor unit-tests.
4 * Copyright (C) 2011-2016 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include <glib.h>
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp-input-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
23 typedef struct TestInputVisitorData {
24 QObject *obj;
25 QmpInputVisitor *qiv;
26 } TestInputVisitorData;
28 static void visitor_input_teardown(TestInputVisitorData *data,
29 const void *unused)
31 qobject_decref(data->obj);
32 data->obj = NULL;
34 if (data->qiv) {
35 qmp_input_visitor_cleanup(data->qiv);
36 data->qiv = NULL;
40 /* The various test_init functions are provided instead of a test setup
41 function so that the JSON string used by the tests are kept in the test
42 functions (and not in main()). */
43 static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data,
44 const char *json_string,
45 va_list *ap)
47 Visitor *v;
49 visitor_input_teardown(data, NULL);
51 data->obj = qobject_from_jsonv(json_string, ap);
52 g_assert(data->obj);
54 data->qiv = qmp_input_visitor_new(data->obj, false);
55 g_assert(data->qiv);
57 v = qmp_input_get_visitor(data->qiv);
58 g_assert(v);
60 return v;
63 static GCC_FMT_ATTR(2, 3)
64 Visitor *visitor_input_test_init(TestInputVisitorData *data,
65 const char *json_string, ...)
67 Visitor *v;
68 va_list ap;
70 va_start(ap, json_string);
71 v = visitor_input_test_init_internal(data, json_string, &ap);
72 va_end(ap);
73 return v;
76 /* similar to visitor_input_test_init(), but does not expect a string
77 * literal/format json_string argument and so can be used for
78 * programatically generated strings (and we can't pass in programatically
79 * generated strings via %s format parameters since qobject_from_jsonv()
80 * will wrap those in double-quotes and treat the entire object as a
81 * string)
83 static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
84 const char *json_string)
86 return visitor_input_test_init_internal(data, json_string, NULL);
89 static void test_visitor_in_int(TestInputVisitorData *data,
90 const void *unused)
92 int64_t res = 0, value = -42;
93 Visitor *v;
95 v = visitor_input_test_init(data, "%" PRId64, value);
97 visit_type_int(v, NULL, &res, &error_abort);
98 g_assert_cmpint(res, ==, value);
101 static void test_visitor_in_int_overflow(TestInputVisitorData *data,
102 const void *unused)
104 int64_t res = 0;
105 Error *err = NULL;
106 Visitor *v;
108 /* this will overflow a Qint/int64, so should be deserialized into
109 * a QFloat/double field instead, leading to an error if we pass it
110 * to visit_type_int. confirm this.
112 v = visitor_input_test_init(data, "%f", DBL_MAX);
114 visit_type_int(v, NULL, &res, &err);
115 error_free_or_abort(&err);
118 static void test_visitor_in_bool(TestInputVisitorData *data,
119 const void *unused)
121 bool res = false;
122 Visitor *v;
124 v = visitor_input_test_init(data, "true");
126 visit_type_bool(v, NULL, &res, &error_abort);
127 g_assert_cmpint(res, ==, true);
130 static void test_visitor_in_number(TestInputVisitorData *data,
131 const void *unused)
133 double res = 0, value = 3.14;
134 Visitor *v;
136 v = visitor_input_test_init(data, "%f", value);
138 visit_type_number(v, NULL, &res, &error_abort);
139 g_assert_cmpfloat(res, ==, value);
142 static void test_visitor_in_string(TestInputVisitorData *data,
143 const void *unused)
145 char *res = NULL, *value = (char *) "Q E M U";
146 Visitor *v;
148 v = visitor_input_test_init(data, "%s", value);
150 visit_type_str(v, NULL, &res, &error_abort);
151 g_assert_cmpstr(res, ==, value);
153 g_free(res);
156 static void test_visitor_in_enum(TestInputVisitorData *data,
157 const void *unused)
159 Visitor *v;
160 EnumOne i;
162 for (i = 0; EnumOne_lookup[i]; i++) {
163 EnumOne res = -1;
165 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
167 visit_type_EnumOne(v, NULL, &res, &error_abort);
168 g_assert_cmpint(i, ==, res);
173 static void test_visitor_in_struct(TestInputVisitorData *data,
174 const void *unused)
176 TestStruct *p = NULL;
177 Visitor *v;
179 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
181 visit_type_TestStruct(v, NULL, &p, &error_abort);
182 g_assert_cmpint(p->integer, ==, -42);
183 g_assert(p->boolean == true);
184 g_assert_cmpstr(p->string, ==, "foo");
186 g_free(p->string);
187 g_free(p);
190 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
191 const void *unused)
193 UserDefTwo *udp = NULL;
194 Visitor *v;
196 v = visitor_input_test_init(data, "{ 'string0': 'string0', "
197 "'dict1': { 'string1': 'string1', "
198 "'dict2': { 'userdef': { 'integer': 42, "
199 "'string': 'string' }, 'string': 'string2'}}}");
201 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
203 g_assert_cmpstr(udp->string0, ==, "string0");
204 g_assert_cmpstr(udp->dict1->string1, ==, "string1");
205 g_assert_cmpint(udp->dict1->dict2->userdef->integer, ==, 42);
206 g_assert_cmpstr(udp->dict1->dict2->userdef->string, ==, "string");
207 g_assert_cmpstr(udp->dict1->dict2->string, ==, "string2");
208 g_assert(udp->dict1->has_dict3 == false);
210 qapi_free_UserDefTwo(udp);
213 static void test_visitor_in_list(TestInputVisitorData *data,
214 const void *unused)
216 UserDefOneList *item, *head = NULL;
217 Visitor *v;
218 int i;
220 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
222 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
223 g_assert(head != NULL);
225 for (i = 0, item = head; item; item = item->next, i++) {
226 char string[12];
228 snprintf(string, sizeof(string), "string%d", i);
229 g_assert_cmpstr(item->value->string, ==, string);
230 g_assert_cmpint(item->value->integer, ==, 42 + i);
233 qapi_free_UserDefOneList(head);
234 head = NULL;
236 /* An empty list is valid */
237 v = visitor_input_test_init(data, "[]");
238 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
239 g_assert(!head);
242 static void test_visitor_in_any(TestInputVisitorData *data,
243 const void *unused)
245 QObject *res = NULL;
246 Visitor *v;
247 QInt *qint;
248 QBool *qbool;
249 QString *qstring;
250 QDict *qdict;
251 QObject *qobj;
253 v = visitor_input_test_init(data, "-42");
254 visit_type_any(v, NULL, &res, &error_abort);
255 qint = qobject_to_qint(res);
256 g_assert(qint);
257 g_assert_cmpint(qint_get_int(qint), ==, -42);
258 qobject_decref(res);
260 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
261 visit_type_any(v, NULL, &res, &error_abort);
262 qdict = qobject_to_qdict(res);
263 g_assert(qdict && qdict_size(qdict) == 3);
264 qobj = qdict_get(qdict, "integer");
265 g_assert(qobj);
266 qint = qobject_to_qint(qobj);
267 g_assert(qint);
268 g_assert_cmpint(qint_get_int(qint), ==, -42);
269 qobj = qdict_get(qdict, "boolean");
270 g_assert(qobj);
271 qbool = qobject_to_qbool(qobj);
272 g_assert(qbool);
273 g_assert(qbool_get_bool(qbool) == true);
274 qobj = qdict_get(qdict, "string");
275 g_assert(qobj);
276 qstring = qobject_to_qstring(qobj);
277 g_assert(qstring);
278 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
279 qobject_decref(res);
282 static void test_visitor_in_null(TestInputVisitorData *data,
283 const void *unused)
285 Visitor *v;
286 Error *err = NULL;
287 char *tmp;
290 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
291 * test visit_type_null() by reading into a QAPI struct then
292 * checking that it was populated correctly. The best we can do
293 * for now is ensure that we consumed null from the input, proven
294 * by the fact that we can't re-read the key; and that we detect
295 * when input is not null.
298 v = visitor_input_test_init(data, "{ 'a': null, 'b': '' }");
299 visit_start_struct(v, NULL, NULL, 0, &error_abort);
300 visit_type_null(v, "a", &error_abort);
301 visit_type_str(v, "a", &tmp, &err);
302 g_assert(!tmp);
303 error_free_or_abort(&err);
304 visit_type_null(v, "b", &err);
305 error_free_or_abort(&err);
306 visit_check_struct(v, &error_abort);
307 visit_end_struct(v);
310 static void test_visitor_in_union_flat(TestInputVisitorData *data,
311 const void *unused)
313 Visitor *v;
314 UserDefFlatUnion *tmp;
315 UserDefUnionBase *base;
317 v = visitor_input_test_init(data,
318 "{ 'enum1': 'value1', "
319 "'integer': 41, "
320 "'string': 'str', "
321 "'boolean': true }");
323 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
324 g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1);
325 g_assert_cmpstr(tmp->string, ==, "str");
326 g_assert_cmpint(tmp->integer, ==, 41);
327 g_assert_cmpint(tmp->u.value1.boolean, ==, true);
329 base = qapi_UserDefFlatUnion_base(tmp);
330 g_assert(&base->enum1 == &tmp->enum1);
332 qapi_free_UserDefFlatUnion(tmp);
335 static void test_visitor_in_alternate(TestInputVisitorData *data,
336 const void *unused)
338 Visitor *v;
339 Error *err = NULL;
340 UserDefAlternate *tmp;
341 WrapAlternate *wrap;
343 v = visitor_input_test_init(data, "42");
344 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
345 g_assert_cmpint(tmp->type, ==, QTYPE_QINT);
346 g_assert_cmpint(tmp->u.i, ==, 42);
347 qapi_free_UserDefAlternate(tmp);
349 v = visitor_input_test_init(data, "'string'");
350 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
351 g_assert_cmpint(tmp->type, ==, QTYPE_QSTRING);
352 g_assert_cmpstr(tmp->u.s, ==, "string");
353 qapi_free_UserDefAlternate(tmp);
355 v = visitor_input_test_init(data, "{'integer':1, 'string':'str', "
356 "'enum1':'value1', 'boolean':true}");
357 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
358 g_assert_cmpint(tmp->type, ==, QTYPE_QDICT);
359 g_assert_cmpint(tmp->u.udfu.integer, ==, 1);
360 g_assert_cmpstr(tmp->u.udfu.string, ==, "str");
361 g_assert_cmpint(tmp->u.udfu.enum1, ==, ENUM_ONE_VALUE1);
362 g_assert_cmpint(tmp->u.udfu.u.value1.boolean, ==, true);
363 g_assert_cmpint(tmp->u.udfu.u.value1.has_a_b, ==, false);
364 qapi_free_UserDefAlternate(tmp);
366 v = visitor_input_test_init(data, "false");
367 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
368 error_free_or_abort(&err);
369 qapi_free_UserDefAlternate(tmp);
371 v = visitor_input_test_init(data, "{ 'alt': 42 }");
372 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
373 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QINT);
374 g_assert_cmpint(wrap->alt->u.i, ==, 42);
375 qapi_free_WrapAlternate(wrap);
377 v = visitor_input_test_init(data, "{ 'alt': 'string' }");
378 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
379 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QSTRING);
380 g_assert_cmpstr(wrap->alt->u.s, ==, "string");
381 qapi_free_WrapAlternate(wrap);
383 v = visitor_input_test_init(data, "{ 'alt': {'integer':1, 'string':'str', "
384 "'enum1':'value1', 'boolean':true} }");
385 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
386 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QDICT);
387 g_assert_cmpint(wrap->alt->u.udfu.integer, ==, 1);
388 g_assert_cmpstr(wrap->alt->u.udfu.string, ==, "str");
389 g_assert_cmpint(wrap->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE1);
390 g_assert_cmpint(wrap->alt->u.udfu.u.value1.boolean, ==, true);
391 g_assert_cmpint(wrap->alt->u.udfu.u.value1.has_a_b, ==, false);
392 qapi_free_WrapAlternate(wrap);
395 static void test_visitor_in_alternate_number(TestInputVisitorData *data,
396 const void *unused)
398 Visitor *v;
399 Error *err = NULL;
400 AltStrBool *asb;
401 AltStrNum *asn;
402 AltNumStr *ans;
403 AltStrInt *asi;
404 AltIntNum *ain;
405 AltNumInt *ani;
407 /* Parsing an int */
409 v = visitor_input_test_init(data, "42");
410 visit_type_AltStrBool(v, NULL, &asb, &err);
411 error_free_or_abort(&err);
412 qapi_free_AltStrBool(asb);
414 v = visitor_input_test_init(data, "42");
415 visit_type_AltStrNum(v, NULL, &asn, &error_abort);
416 g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT);
417 g_assert_cmpfloat(asn->u.n, ==, 42);
418 qapi_free_AltStrNum(asn);
420 v = visitor_input_test_init(data, "42");
421 visit_type_AltNumStr(v, NULL, &ans, &error_abort);
422 g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT);
423 g_assert_cmpfloat(ans->u.n, ==, 42);
424 qapi_free_AltNumStr(ans);
426 v = visitor_input_test_init(data, "42");
427 visit_type_AltStrInt(v, NULL, &asi, &error_abort);
428 g_assert_cmpint(asi->type, ==, QTYPE_QINT);
429 g_assert_cmpint(asi->u.i, ==, 42);
430 qapi_free_AltStrInt(asi);
432 v = visitor_input_test_init(data, "42");
433 visit_type_AltIntNum(v, NULL, &ain, &error_abort);
434 g_assert_cmpint(ain->type, ==, QTYPE_QINT);
435 g_assert_cmpint(ain->u.i, ==, 42);
436 qapi_free_AltIntNum(ain);
438 v = visitor_input_test_init(data, "42");
439 visit_type_AltNumInt(v, NULL, &ani, &error_abort);
440 g_assert_cmpint(ani->type, ==, QTYPE_QINT);
441 g_assert_cmpint(ani->u.i, ==, 42);
442 qapi_free_AltNumInt(ani);
444 /* Parsing a double */
446 v = visitor_input_test_init(data, "42.5");
447 visit_type_AltStrBool(v, NULL, &asb, &err);
448 error_free_or_abort(&err);
449 qapi_free_AltStrBool(asb);
451 v = visitor_input_test_init(data, "42.5");
452 visit_type_AltStrNum(v, NULL, &asn, &error_abort);
453 g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT);
454 g_assert_cmpfloat(asn->u.n, ==, 42.5);
455 qapi_free_AltStrNum(asn);
457 v = visitor_input_test_init(data, "42.5");
458 visit_type_AltNumStr(v, NULL, &ans, &error_abort);
459 g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT);
460 g_assert_cmpfloat(ans->u.n, ==, 42.5);
461 qapi_free_AltNumStr(ans);
463 v = visitor_input_test_init(data, "42.5");
464 visit_type_AltStrInt(v, NULL, &asi, &err);
465 error_free_or_abort(&err);
466 qapi_free_AltStrInt(asi);
468 v = visitor_input_test_init(data, "42.5");
469 visit_type_AltIntNum(v, NULL, &ain, &error_abort);
470 g_assert_cmpint(ain->type, ==, QTYPE_QFLOAT);
471 g_assert_cmpfloat(ain->u.n, ==, 42.5);
472 qapi_free_AltIntNum(ain);
474 v = visitor_input_test_init(data, "42.5");
475 visit_type_AltNumInt(v, NULL, &ani, &error_abort);
476 g_assert_cmpint(ani->type, ==, QTYPE_QFLOAT);
477 g_assert_cmpfloat(ani->u.n, ==, 42.5);
478 qapi_free_AltNumInt(ani);
481 static void test_native_list_integer_helper(TestInputVisitorData *data,
482 const void *unused,
483 UserDefNativeListUnionKind kind)
485 UserDefNativeListUnion *cvalue = NULL;
486 Visitor *v;
487 GString *gstr_list = g_string_new("");
488 GString *gstr_union = g_string_new("");
489 int i;
491 for (i = 0; i < 32; i++) {
492 g_string_append_printf(gstr_list, "%d", i);
493 if (i != 31) {
494 g_string_append(gstr_list, ", ");
497 g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }",
498 UserDefNativeListUnionKind_lookup[kind],
499 gstr_list->str);
500 v = visitor_input_test_init_raw(data, gstr_union->str);
502 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
503 g_assert(cvalue != NULL);
504 g_assert_cmpint(cvalue->type, ==, kind);
506 switch (kind) {
507 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
508 intList *elem = NULL;
509 for (i = 0, elem = cvalue->u.integer.data;
510 elem; elem = elem->next, i++) {
511 g_assert_cmpint(elem->value, ==, i);
513 break;
515 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
516 int8List *elem = NULL;
517 for (i = 0, elem = cvalue->u.s8.data; elem; elem = elem->next, i++) {
518 g_assert_cmpint(elem->value, ==, i);
520 break;
522 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
523 int16List *elem = NULL;
524 for (i = 0, elem = cvalue->u.s16.data; elem; elem = elem->next, i++) {
525 g_assert_cmpint(elem->value, ==, i);
527 break;
529 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
530 int32List *elem = NULL;
531 for (i = 0, elem = cvalue->u.s32.data; elem; elem = elem->next, i++) {
532 g_assert_cmpint(elem->value, ==, i);
534 break;
536 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
537 int64List *elem = NULL;
538 for (i = 0, elem = cvalue->u.s64.data; elem; elem = elem->next, i++) {
539 g_assert_cmpint(elem->value, ==, i);
541 break;
543 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
544 uint8List *elem = NULL;
545 for (i = 0, elem = cvalue->u.u8.data; elem; elem = elem->next, i++) {
546 g_assert_cmpint(elem->value, ==, i);
548 break;
550 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
551 uint16List *elem = NULL;
552 for (i = 0, elem = cvalue->u.u16.data; elem; elem = elem->next, i++) {
553 g_assert_cmpint(elem->value, ==, i);
555 break;
557 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
558 uint32List *elem = NULL;
559 for (i = 0, elem = cvalue->u.u32.data; elem; elem = elem->next, i++) {
560 g_assert_cmpint(elem->value, ==, i);
562 break;
564 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
565 uint64List *elem = NULL;
566 for (i = 0, elem = cvalue->u.u64.data; elem; elem = elem->next, i++) {
567 g_assert_cmpint(elem->value, ==, i);
569 break;
571 default:
572 g_assert_not_reached();
575 g_string_free(gstr_union, true);
576 g_string_free(gstr_list, true);
577 qapi_free_UserDefNativeListUnion(cvalue);
580 static void test_visitor_in_native_list_int(TestInputVisitorData *data,
581 const void *unused)
583 test_native_list_integer_helper(data, unused,
584 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
587 static void test_visitor_in_native_list_int8(TestInputVisitorData *data,
588 const void *unused)
590 test_native_list_integer_helper(data, unused,
591 USER_DEF_NATIVE_LIST_UNION_KIND_S8);
594 static void test_visitor_in_native_list_int16(TestInputVisitorData *data,
595 const void *unused)
597 test_native_list_integer_helper(data, unused,
598 USER_DEF_NATIVE_LIST_UNION_KIND_S16);
601 static void test_visitor_in_native_list_int32(TestInputVisitorData *data,
602 const void *unused)
604 test_native_list_integer_helper(data, unused,
605 USER_DEF_NATIVE_LIST_UNION_KIND_S32);
608 static void test_visitor_in_native_list_int64(TestInputVisitorData *data,
609 const void *unused)
611 test_native_list_integer_helper(data, unused,
612 USER_DEF_NATIVE_LIST_UNION_KIND_S64);
615 static void test_visitor_in_native_list_uint8(TestInputVisitorData *data,
616 const void *unused)
618 test_native_list_integer_helper(data, unused,
619 USER_DEF_NATIVE_LIST_UNION_KIND_U8);
622 static void test_visitor_in_native_list_uint16(TestInputVisitorData *data,
623 const void *unused)
625 test_native_list_integer_helper(data, unused,
626 USER_DEF_NATIVE_LIST_UNION_KIND_U16);
629 static void test_visitor_in_native_list_uint32(TestInputVisitorData *data,
630 const void *unused)
632 test_native_list_integer_helper(data, unused,
633 USER_DEF_NATIVE_LIST_UNION_KIND_U32);
636 static void test_visitor_in_native_list_uint64(TestInputVisitorData *data,
637 const void *unused)
639 test_native_list_integer_helper(data, unused,
640 USER_DEF_NATIVE_LIST_UNION_KIND_U64);
643 static void test_visitor_in_native_list_bool(TestInputVisitorData *data,
644 const void *unused)
646 UserDefNativeListUnion *cvalue = NULL;
647 boolList *elem = NULL;
648 Visitor *v;
649 GString *gstr_list = g_string_new("");
650 GString *gstr_union = g_string_new("");
651 int i;
653 for (i = 0; i < 32; i++) {
654 g_string_append_printf(gstr_list, "%s",
655 (i % 3 == 0) ? "true" : "false");
656 if (i != 31) {
657 g_string_append(gstr_list, ", ");
660 g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }",
661 gstr_list->str);
662 v = visitor_input_test_init_raw(data, gstr_union->str);
664 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
665 g_assert(cvalue != NULL);
666 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
668 for (i = 0, elem = cvalue->u.boolean.data; elem; elem = elem->next, i++) {
669 g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0);
672 g_string_free(gstr_union, true);
673 g_string_free(gstr_list, true);
674 qapi_free_UserDefNativeListUnion(cvalue);
677 static void test_visitor_in_native_list_string(TestInputVisitorData *data,
678 const void *unused)
680 UserDefNativeListUnion *cvalue = NULL;
681 strList *elem = NULL;
682 Visitor *v;
683 GString *gstr_list = g_string_new("");
684 GString *gstr_union = g_string_new("");
685 int i;
687 for (i = 0; i < 32; i++) {
688 g_string_append_printf(gstr_list, "'%d'", i);
689 if (i != 31) {
690 g_string_append(gstr_list, ", ");
693 g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }",
694 gstr_list->str);
695 v = visitor_input_test_init_raw(data, gstr_union->str);
697 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
698 g_assert(cvalue != NULL);
699 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
701 for (i = 0, elem = cvalue->u.string.data; elem; elem = elem->next, i++) {
702 gchar str[8];
703 sprintf(str, "%d", i);
704 g_assert_cmpstr(elem->value, ==, str);
707 g_string_free(gstr_union, true);
708 g_string_free(gstr_list, true);
709 qapi_free_UserDefNativeListUnion(cvalue);
712 #define DOUBLE_STR_MAX 16
714 static void test_visitor_in_native_list_number(TestInputVisitorData *data,
715 const void *unused)
717 UserDefNativeListUnion *cvalue = NULL;
718 numberList *elem = NULL;
719 Visitor *v;
720 GString *gstr_list = g_string_new("");
721 GString *gstr_union = g_string_new("");
722 int i;
724 for (i = 0; i < 32; i++) {
725 g_string_append_printf(gstr_list, "%f", (double)i / 3);
726 if (i != 31) {
727 g_string_append(gstr_list, ", ");
730 g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }",
731 gstr_list->str);
732 v = visitor_input_test_init_raw(data, gstr_union->str);
734 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
735 g_assert(cvalue != NULL);
736 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
738 for (i = 0, elem = cvalue->u.number.data; elem; elem = elem->next, i++) {
739 GString *double_expected = g_string_new("");
740 GString *double_actual = g_string_new("");
742 g_string_printf(double_expected, "%.6f", (double)i / 3);
743 g_string_printf(double_actual, "%.6f", elem->value);
744 g_assert_cmpstr(double_expected->str, ==, double_actual->str);
746 g_string_free(double_expected, true);
747 g_string_free(double_actual, true);
750 g_string_free(gstr_union, true);
751 g_string_free(gstr_list, true);
752 qapi_free_UserDefNativeListUnion(cvalue);
755 static void input_visitor_test_add(const char *testpath,
756 TestInputVisitorData *data,
757 void (*test_func)(TestInputVisitorData *data, const void *user_data))
759 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
760 visitor_input_teardown);
763 static void test_visitor_in_errors(TestInputVisitorData *data,
764 const void *unused)
766 TestStruct *p = NULL;
767 Error *err = NULL;
768 Visitor *v;
769 strList *q = NULL;
771 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', "
772 "'string': -42 }");
774 visit_type_TestStruct(v, NULL, &p, &err);
775 error_free_or_abort(&err);
776 g_assert(!p);
778 v = visitor_input_test_init(data, "[ '1', '2', false, '3' ]");
779 visit_type_strList(v, NULL, &q, &err);
780 error_free_or_abort(&err);
781 assert(!q);
784 static void test_visitor_in_wrong_type(TestInputVisitorData *data,
785 const void *unused)
787 TestStruct *p = NULL;
788 Visitor *v;
789 strList *q = NULL;
790 int64_t i;
791 Error *err = NULL;
793 /* Make sure arrays and structs cannot be confused */
795 v = visitor_input_test_init(data, "[]");
796 visit_type_TestStruct(v, NULL, &p, &err);
797 error_free_or_abort(&err);
798 g_assert(!p);
800 v = visitor_input_test_init(data, "{}");
801 visit_type_strList(v, NULL, &q, &err);
802 error_free_or_abort(&err);
803 assert(!q);
805 /* Make sure primitives and struct cannot be confused */
807 v = visitor_input_test_init(data, "1");
808 visit_type_TestStruct(v, NULL, &p, &err);
809 error_free_or_abort(&err);
810 g_assert(!p);
812 v = visitor_input_test_init(data, "{}");
813 visit_type_int(v, NULL, &i, &err);
814 error_free_or_abort(&err);
816 /* Make sure primitives and arrays cannot be confused */
818 v = visitor_input_test_init(data, "1");
819 visit_type_strList(v, NULL, &q, &err);
820 error_free_or_abort(&err);
821 assert(!q);
823 v = visitor_input_test_init(data, "[]");
824 visit_type_int(v, NULL, &i, &err);
825 error_free_or_abort(&err);
828 int main(int argc, char **argv)
830 TestInputVisitorData in_visitor_data;
832 g_test_init(&argc, &argv, NULL);
834 input_visitor_test_add("/visitor/input/int",
835 &in_visitor_data, test_visitor_in_int);
836 input_visitor_test_add("/visitor/input/int_overflow",
837 &in_visitor_data, test_visitor_in_int_overflow);
838 input_visitor_test_add("/visitor/input/bool",
839 &in_visitor_data, test_visitor_in_bool);
840 input_visitor_test_add("/visitor/input/number",
841 &in_visitor_data, test_visitor_in_number);
842 input_visitor_test_add("/visitor/input/string",
843 &in_visitor_data, test_visitor_in_string);
844 input_visitor_test_add("/visitor/input/enum",
845 &in_visitor_data, test_visitor_in_enum);
846 input_visitor_test_add("/visitor/input/struct",
847 &in_visitor_data, test_visitor_in_struct);
848 input_visitor_test_add("/visitor/input/struct-nested",
849 &in_visitor_data, test_visitor_in_struct_nested);
850 input_visitor_test_add("/visitor/input/list",
851 &in_visitor_data, test_visitor_in_list);
852 input_visitor_test_add("/visitor/input/any",
853 &in_visitor_data, test_visitor_in_any);
854 input_visitor_test_add("/visitor/input/null",
855 &in_visitor_data, test_visitor_in_null);
856 input_visitor_test_add("/visitor/input/union-flat",
857 &in_visitor_data, test_visitor_in_union_flat);
858 input_visitor_test_add("/visitor/input/alternate",
859 &in_visitor_data, test_visitor_in_alternate);
860 input_visitor_test_add("/visitor/input/errors",
861 &in_visitor_data, test_visitor_in_errors);
862 input_visitor_test_add("/visitor/input/wrong-type",
863 &in_visitor_data, test_visitor_in_wrong_type);
864 input_visitor_test_add("/visitor/input/alternate-number",
865 &in_visitor_data, test_visitor_in_alternate_number);
866 input_visitor_test_add("/visitor/input/native_list/int",
867 &in_visitor_data,
868 test_visitor_in_native_list_int);
869 input_visitor_test_add("/visitor/input/native_list/int8",
870 &in_visitor_data,
871 test_visitor_in_native_list_int8);
872 input_visitor_test_add("/visitor/input/native_list/int16",
873 &in_visitor_data,
874 test_visitor_in_native_list_int16);
875 input_visitor_test_add("/visitor/input/native_list/int32",
876 &in_visitor_data,
877 test_visitor_in_native_list_int32);
878 input_visitor_test_add("/visitor/input/native_list/int64",
879 &in_visitor_data,
880 test_visitor_in_native_list_int64);
881 input_visitor_test_add("/visitor/input/native_list/uint8",
882 &in_visitor_data,
883 test_visitor_in_native_list_uint8);
884 input_visitor_test_add("/visitor/input/native_list/uint16",
885 &in_visitor_data,
886 test_visitor_in_native_list_uint16);
887 input_visitor_test_add("/visitor/input/native_list/uint32",
888 &in_visitor_data,
889 test_visitor_in_native_list_uint32);
890 input_visitor_test_add("/visitor/input/native_list/uint64",
891 &in_visitor_data,
892 test_visitor_in_native_list_uint64);
893 input_visitor_test_add("/visitor/input/native_list/bool",
894 &in_visitor_data, test_visitor_in_native_list_bool);
895 input_visitor_test_add("/visitor/input/native_list/str",
896 &in_visitor_data,
897 test_visitor_in_native_list_string);
898 input_visitor_test_add("/visitor/input/native_list/number",
899 &in_visitor_data,
900 test_visitor_in_native_list_number);
902 g_test_run();
904 return 0;