s390x/kvm: fix cmma reset for KVM
[qemu/ar7.git] / tests / test-qobject-input-visitor.c
blob945404a9e0f203e764986d03a165cd1e0d3943c6
1 /*
2 * QObject 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"
15 #include "qemu-common.h"
16 #include "qapi/error.h"
17 #include "qapi/qobject-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
21 #include "qapi/qmp/qjson.h"
23 typedef struct TestInputVisitorData {
24 QObject *obj;
25 Visitor *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 visit_free(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_input_teardown(data, NULL);
49 data->obj = qobject_from_jsonv(json_string, ap);
50 g_assert(data->obj);
52 data->qiv = qobject_input_visitor_new(data->obj, false);
53 g_assert(data->qiv);
54 return data->qiv;
57 static GCC_FMT_ATTR(2, 3)
58 Visitor *visitor_input_test_init(TestInputVisitorData *data,
59 const char *json_string, ...)
61 Visitor *v;
62 va_list ap;
64 va_start(ap, json_string);
65 v = visitor_input_test_init_internal(data, json_string, &ap);
66 va_end(ap);
67 return v;
70 /* similar to visitor_input_test_init(), but does not expect a string
71 * literal/format json_string argument and so can be used for
72 * programatically generated strings (and we can't pass in programatically
73 * generated strings via %s format parameters since qobject_from_jsonv()
74 * will wrap those in double-quotes and treat the entire object as a
75 * string)
77 static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
78 const char *json_string)
80 return visitor_input_test_init_internal(data, json_string, NULL);
83 static void test_visitor_in_int(TestInputVisitorData *data,
84 const void *unused)
86 int64_t res = 0;
87 int value = -42;
88 Visitor *v;
90 v = visitor_input_test_init(data, "%d", value);
92 visit_type_int(v, NULL, &res, &error_abort);
93 g_assert_cmpint(res, ==, value);
96 static void test_visitor_in_int_overflow(TestInputVisitorData *data,
97 const void *unused)
99 int64_t res = 0;
100 Error *err = NULL;
101 Visitor *v;
103 /* this will overflow a Qint/int64, so should be deserialized into
104 * a QFloat/double field instead, leading to an error if we pass it
105 * to visit_type_int. confirm this.
107 v = visitor_input_test_init(data, "%f", DBL_MAX);
109 visit_type_int(v, NULL, &res, &err);
110 error_free_or_abort(&err);
113 static void test_visitor_in_bool(TestInputVisitorData *data,
114 const void *unused)
116 bool res = false;
117 Visitor *v;
119 v = visitor_input_test_init(data, "true");
121 visit_type_bool(v, NULL, &res, &error_abort);
122 g_assert_cmpint(res, ==, true);
125 static void test_visitor_in_number(TestInputVisitorData *data,
126 const void *unused)
128 double res = 0, value = 3.14;
129 Visitor *v;
131 v = visitor_input_test_init(data, "%f", value);
133 visit_type_number(v, NULL, &res, &error_abort);
134 g_assert_cmpfloat(res, ==, value);
137 static void test_visitor_in_string(TestInputVisitorData *data,
138 const void *unused)
140 char *res = NULL, *value = (char *) "Q E M U";
141 Visitor *v;
143 v = visitor_input_test_init(data, "%s", value);
145 visit_type_str(v, NULL, &res, &error_abort);
146 g_assert_cmpstr(res, ==, value);
148 g_free(res);
151 static void test_visitor_in_enum(TestInputVisitorData *data,
152 const void *unused)
154 Visitor *v;
155 EnumOne i;
157 for (i = 0; EnumOne_lookup[i]; i++) {
158 EnumOne res = -1;
160 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
162 visit_type_EnumOne(v, NULL, &res, &error_abort);
163 g_assert_cmpint(i, ==, res);
168 static void test_visitor_in_struct(TestInputVisitorData *data,
169 const void *unused)
171 TestStruct *p = NULL;
172 Visitor *v;
174 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
176 visit_type_TestStruct(v, NULL, &p, &error_abort);
177 g_assert_cmpint(p->integer, ==, -42);
178 g_assert(p->boolean == true);
179 g_assert_cmpstr(p->string, ==, "foo");
181 g_free(p->string);
182 g_free(p);
185 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
186 const void *unused)
188 UserDefTwo *udp = NULL;
189 Visitor *v;
191 v = visitor_input_test_init(data, "{ 'string0': 'string0', "
192 "'dict1': { 'string1': 'string1', "
193 "'dict2': { 'userdef': { 'integer': 42, "
194 "'string': 'string' }, 'string': 'string2'}}}");
196 visit_type_UserDefTwo(v, NULL, &udp, &error_abort);
198 g_assert_cmpstr(udp->string0, ==, "string0");
199 g_assert_cmpstr(udp->dict1->string1, ==, "string1");
200 g_assert_cmpint(udp->dict1->dict2->userdef->integer, ==, 42);
201 g_assert_cmpstr(udp->dict1->dict2->userdef->string, ==, "string");
202 g_assert_cmpstr(udp->dict1->dict2->string, ==, "string2");
203 g_assert(udp->dict1->has_dict3 == false);
205 qapi_free_UserDefTwo(udp);
208 static void test_visitor_in_list(TestInputVisitorData *data,
209 const void *unused)
211 UserDefOneList *item, *head = NULL;
212 Visitor *v;
213 int i;
215 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
217 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
218 g_assert(head != NULL);
220 for (i = 0, item = head; item; item = item->next, i++) {
221 char string[12];
223 snprintf(string, sizeof(string), "string%d", i);
224 g_assert_cmpstr(item->value->string, ==, string);
225 g_assert_cmpint(item->value->integer, ==, 42 + i);
228 qapi_free_UserDefOneList(head);
229 head = NULL;
231 /* An empty list is valid */
232 v = visitor_input_test_init(data, "[]");
233 visit_type_UserDefOneList(v, NULL, &head, &error_abort);
234 g_assert(!head);
237 static void test_visitor_in_any(TestInputVisitorData *data,
238 const void *unused)
240 QObject *res = NULL;
241 Visitor *v;
242 QInt *qint;
243 QBool *qbool;
244 QString *qstring;
245 QDict *qdict;
246 QObject *qobj;
248 v = visitor_input_test_init(data, "-42");
249 visit_type_any(v, NULL, &res, &error_abort);
250 qint = qobject_to_qint(res);
251 g_assert(qint);
252 g_assert_cmpint(qint_get_int(qint), ==, -42);
253 qobject_decref(res);
255 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
256 visit_type_any(v, NULL, &res, &error_abort);
257 qdict = qobject_to_qdict(res);
258 g_assert(qdict && qdict_size(qdict) == 3);
259 qobj = qdict_get(qdict, "integer");
260 g_assert(qobj);
261 qint = qobject_to_qint(qobj);
262 g_assert(qint);
263 g_assert_cmpint(qint_get_int(qint), ==, -42);
264 qobj = qdict_get(qdict, "boolean");
265 g_assert(qobj);
266 qbool = qobject_to_qbool(qobj);
267 g_assert(qbool);
268 g_assert(qbool_get_bool(qbool) == true);
269 qobj = qdict_get(qdict, "string");
270 g_assert(qobj);
271 qstring = qobject_to_qstring(qobj);
272 g_assert(qstring);
273 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
274 qobject_decref(res);
277 static void test_visitor_in_null(TestInputVisitorData *data,
278 const void *unused)
280 Visitor *v;
281 Error *err = NULL;
282 char *tmp;
285 * FIXME: Since QAPI doesn't know the 'null' type yet, we can't
286 * test visit_type_null() by reading into a QAPI struct then
287 * checking that it was populated correctly. The best we can do
288 * for now is ensure that we consumed null from the input, proven
289 * by the fact that we can't re-read the key; and that we detect
290 * when input is not null.
293 v = visitor_input_test_init(data, "{ 'a': null, 'b': '' }");
294 visit_start_struct(v, NULL, NULL, 0, &error_abort);
295 visit_type_null(v, "a", &error_abort);
296 visit_type_str(v, "a", &tmp, &err);
297 g_assert(!tmp);
298 error_free_or_abort(&err);
299 visit_type_null(v, "b", &err);
300 error_free_or_abort(&err);
301 visit_check_struct(v, &error_abort);
302 visit_end_struct(v, NULL);
305 static void test_visitor_in_union_flat(TestInputVisitorData *data,
306 const void *unused)
308 Visitor *v;
309 UserDefFlatUnion *tmp;
310 UserDefUnionBase *base;
312 v = visitor_input_test_init(data,
313 "{ 'enum1': 'value1', "
314 "'integer': 41, "
315 "'string': 'str', "
316 "'boolean': true }");
318 visit_type_UserDefFlatUnion(v, NULL, &tmp, &error_abort);
319 g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1);
320 g_assert_cmpstr(tmp->string, ==, "str");
321 g_assert_cmpint(tmp->integer, ==, 41);
322 g_assert_cmpint(tmp->u.value1.boolean, ==, true);
324 base = qapi_UserDefFlatUnion_base(tmp);
325 g_assert(&base->enum1 == &tmp->enum1);
327 qapi_free_UserDefFlatUnion(tmp);
330 static void test_visitor_in_alternate(TestInputVisitorData *data,
331 const void *unused)
333 Visitor *v;
334 Error *err = NULL;
335 UserDefAlternate *tmp;
336 WrapAlternate *wrap;
338 v = visitor_input_test_init(data, "42");
339 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
340 g_assert_cmpint(tmp->type, ==, QTYPE_QINT);
341 g_assert_cmpint(tmp->u.i, ==, 42);
342 qapi_free_UserDefAlternate(tmp);
344 v = visitor_input_test_init(data, "'string'");
345 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
346 g_assert_cmpint(tmp->type, ==, QTYPE_QSTRING);
347 g_assert_cmpstr(tmp->u.s, ==, "string");
348 qapi_free_UserDefAlternate(tmp);
350 v = visitor_input_test_init(data, "{'integer':1, 'string':'str', "
351 "'enum1':'value1', 'boolean':true}");
352 visit_type_UserDefAlternate(v, NULL, &tmp, &error_abort);
353 g_assert_cmpint(tmp->type, ==, QTYPE_QDICT);
354 g_assert_cmpint(tmp->u.udfu.integer, ==, 1);
355 g_assert_cmpstr(tmp->u.udfu.string, ==, "str");
356 g_assert_cmpint(tmp->u.udfu.enum1, ==, ENUM_ONE_VALUE1);
357 g_assert_cmpint(tmp->u.udfu.u.value1.boolean, ==, true);
358 g_assert_cmpint(tmp->u.udfu.u.value1.has_a_b, ==, false);
359 qapi_free_UserDefAlternate(tmp);
361 v = visitor_input_test_init(data, "false");
362 visit_type_UserDefAlternate(v, NULL, &tmp, &err);
363 error_free_or_abort(&err);
364 qapi_free_UserDefAlternate(tmp);
366 v = visitor_input_test_init(data, "{ 'alt': 42 }");
367 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
368 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QINT);
369 g_assert_cmpint(wrap->alt->u.i, ==, 42);
370 qapi_free_WrapAlternate(wrap);
372 v = visitor_input_test_init(data, "{ 'alt': 'string' }");
373 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
374 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QSTRING);
375 g_assert_cmpstr(wrap->alt->u.s, ==, "string");
376 qapi_free_WrapAlternate(wrap);
378 v = visitor_input_test_init(data, "{ 'alt': {'integer':1, 'string':'str', "
379 "'enum1':'value1', 'boolean':true} }");
380 visit_type_WrapAlternate(v, NULL, &wrap, &error_abort);
381 g_assert_cmpint(wrap->alt->type, ==, QTYPE_QDICT);
382 g_assert_cmpint(wrap->alt->u.udfu.integer, ==, 1);
383 g_assert_cmpstr(wrap->alt->u.udfu.string, ==, "str");
384 g_assert_cmpint(wrap->alt->u.udfu.enum1, ==, ENUM_ONE_VALUE1);
385 g_assert_cmpint(wrap->alt->u.udfu.u.value1.boolean, ==, true);
386 g_assert_cmpint(wrap->alt->u.udfu.u.value1.has_a_b, ==, false);
387 qapi_free_WrapAlternate(wrap);
390 static void test_visitor_in_alternate_number(TestInputVisitorData *data,
391 const void *unused)
393 Visitor *v;
394 Error *err = NULL;
395 AltStrBool *asb;
396 AltStrNum *asn;
397 AltNumStr *ans;
398 AltStrInt *asi;
399 AltIntNum *ain;
400 AltNumInt *ani;
402 /* Parsing an int */
404 v = visitor_input_test_init(data, "42");
405 visit_type_AltStrBool(v, NULL, &asb, &err);
406 error_free_or_abort(&err);
407 qapi_free_AltStrBool(asb);
409 v = visitor_input_test_init(data, "42");
410 visit_type_AltStrNum(v, NULL, &asn, &error_abort);
411 g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT);
412 g_assert_cmpfloat(asn->u.n, ==, 42);
413 qapi_free_AltStrNum(asn);
415 v = visitor_input_test_init(data, "42");
416 visit_type_AltNumStr(v, NULL, &ans, &error_abort);
417 g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT);
418 g_assert_cmpfloat(ans->u.n, ==, 42);
419 qapi_free_AltNumStr(ans);
421 v = visitor_input_test_init(data, "42");
422 visit_type_AltStrInt(v, NULL, &asi, &error_abort);
423 g_assert_cmpint(asi->type, ==, QTYPE_QINT);
424 g_assert_cmpint(asi->u.i, ==, 42);
425 qapi_free_AltStrInt(asi);
427 v = visitor_input_test_init(data, "42");
428 visit_type_AltIntNum(v, NULL, &ain, &error_abort);
429 g_assert_cmpint(ain->type, ==, QTYPE_QINT);
430 g_assert_cmpint(ain->u.i, ==, 42);
431 qapi_free_AltIntNum(ain);
433 v = visitor_input_test_init(data, "42");
434 visit_type_AltNumInt(v, NULL, &ani, &error_abort);
435 g_assert_cmpint(ani->type, ==, QTYPE_QINT);
436 g_assert_cmpint(ani->u.i, ==, 42);
437 qapi_free_AltNumInt(ani);
439 /* Parsing a double */
441 v = visitor_input_test_init(data, "42.5");
442 visit_type_AltStrBool(v, NULL, &asb, &err);
443 error_free_or_abort(&err);
444 qapi_free_AltStrBool(asb);
446 v = visitor_input_test_init(data, "42.5");
447 visit_type_AltStrNum(v, NULL, &asn, &error_abort);
448 g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT);
449 g_assert_cmpfloat(asn->u.n, ==, 42.5);
450 qapi_free_AltStrNum(asn);
452 v = visitor_input_test_init(data, "42.5");
453 visit_type_AltNumStr(v, NULL, &ans, &error_abort);
454 g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT);
455 g_assert_cmpfloat(ans->u.n, ==, 42.5);
456 qapi_free_AltNumStr(ans);
458 v = visitor_input_test_init(data, "42.5");
459 visit_type_AltStrInt(v, NULL, &asi, &err);
460 error_free_or_abort(&err);
461 qapi_free_AltStrInt(asi);
463 v = visitor_input_test_init(data, "42.5");
464 visit_type_AltIntNum(v, NULL, &ain, &error_abort);
465 g_assert_cmpint(ain->type, ==, QTYPE_QFLOAT);
466 g_assert_cmpfloat(ain->u.n, ==, 42.5);
467 qapi_free_AltIntNum(ain);
469 v = visitor_input_test_init(data, "42.5");
470 visit_type_AltNumInt(v, NULL, &ani, &error_abort);
471 g_assert_cmpint(ani->type, ==, QTYPE_QFLOAT);
472 g_assert_cmpfloat(ani->u.n, ==, 42.5);
473 qapi_free_AltNumInt(ani);
476 static void test_native_list_integer_helper(TestInputVisitorData *data,
477 const void *unused,
478 UserDefNativeListUnionKind kind)
480 UserDefNativeListUnion *cvalue = NULL;
481 Visitor *v;
482 GString *gstr_list = g_string_new("");
483 GString *gstr_union = g_string_new("");
484 int i;
486 for (i = 0; i < 32; i++) {
487 g_string_append_printf(gstr_list, "%d", i);
488 if (i != 31) {
489 g_string_append(gstr_list, ", ");
492 g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }",
493 UserDefNativeListUnionKind_lookup[kind],
494 gstr_list->str);
495 v = visitor_input_test_init_raw(data, gstr_union->str);
497 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
498 g_assert(cvalue != NULL);
499 g_assert_cmpint(cvalue->type, ==, kind);
501 switch (kind) {
502 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
503 intList *elem = NULL;
504 for (i = 0, elem = cvalue->u.integer.data;
505 elem; elem = elem->next, i++) {
506 g_assert_cmpint(elem->value, ==, i);
508 break;
510 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
511 int8List *elem = NULL;
512 for (i = 0, elem = cvalue->u.s8.data; elem; elem = elem->next, i++) {
513 g_assert_cmpint(elem->value, ==, i);
515 break;
517 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
518 int16List *elem = NULL;
519 for (i = 0, elem = cvalue->u.s16.data; elem; elem = elem->next, i++) {
520 g_assert_cmpint(elem->value, ==, i);
522 break;
524 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
525 int32List *elem = NULL;
526 for (i = 0, elem = cvalue->u.s32.data; elem; elem = elem->next, i++) {
527 g_assert_cmpint(elem->value, ==, i);
529 break;
531 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
532 int64List *elem = NULL;
533 for (i = 0, elem = cvalue->u.s64.data; elem; elem = elem->next, i++) {
534 g_assert_cmpint(elem->value, ==, i);
536 break;
538 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
539 uint8List *elem = NULL;
540 for (i = 0, elem = cvalue->u.u8.data; elem; elem = elem->next, i++) {
541 g_assert_cmpint(elem->value, ==, i);
543 break;
545 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
546 uint16List *elem = NULL;
547 for (i = 0, elem = cvalue->u.u16.data; elem; elem = elem->next, i++) {
548 g_assert_cmpint(elem->value, ==, i);
550 break;
552 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
553 uint32List *elem = NULL;
554 for (i = 0, elem = cvalue->u.u32.data; elem; elem = elem->next, i++) {
555 g_assert_cmpint(elem->value, ==, i);
557 break;
559 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
560 uint64List *elem = NULL;
561 for (i = 0, elem = cvalue->u.u64.data; elem; elem = elem->next, i++) {
562 g_assert_cmpint(elem->value, ==, i);
564 break;
566 default:
567 g_assert_not_reached();
570 g_string_free(gstr_union, true);
571 g_string_free(gstr_list, true);
572 qapi_free_UserDefNativeListUnion(cvalue);
575 static void test_visitor_in_native_list_int(TestInputVisitorData *data,
576 const void *unused)
578 test_native_list_integer_helper(data, unused,
579 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
582 static void test_visitor_in_native_list_int8(TestInputVisitorData *data,
583 const void *unused)
585 test_native_list_integer_helper(data, unused,
586 USER_DEF_NATIVE_LIST_UNION_KIND_S8);
589 static void test_visitor_in_native_list_int16(TestInputVisitorData *data,
590 const void *unused)
592 test_native_list_integer_helper(data, unused,
593 USER_DEF_NATIVE_LIST_UNION_KIND_S16);
596 static void test_visitor_in_native_list_int32(TestInputVisitorData *data,
597 const void *unused)
599 test_native_list_integer_helper(data, unused,
600 USER_DEF_NATIVE_LIST_UNION_KIND_S32);
603 static void test_visitor_in_native_list_int64(TestInputVisitorData *data,
604 const void *unused)
606 test_native_list_integer_helper(data, unused,
607 USER_DEF_NATIVE_LIST_UNION_KIND_S64);
610 static void test_visitor_in_native_list_uint8(TestInputVisitorData *data,
611 const void *unused)
613 test_native_list_integer_helper(data, unused,
614 USER_DEF_NATIVE_LIST_UNION_KIND_U8);
617 static void test_visitor_in_native_list_uint16(TestInputVisitorData *data,
618 const void *unused)
620 test_native_list_integer_helper(data, unused,
621 USER_DEF_NATIVE_LIST_UNION_KIND_U16);
624 static void test_visitor_in_native_list_uint32(TestInputVisitorData *data,
625 const void *unused)
627 test_native_list_integer_helper(data, unused,
628 USER_DEF_NATIVE_LIST_UNION_KIND_U32);
631 static void test_visitor_in_native_list_uint64(TestInputVisitorData *data,
632 const void *unused)
634 test_native_list_integer_helper(data, unused,
635 USER_DEF_NATIVE_LIST_UNION_KIND_U64);
638 static void test_visitor_in_native_list_bool(TestInputVisitorData *data,
639 const void *unused)
641 UserDefNativeListUnion *cvalue = NULL;
642 boolList *elem = NULL;
643 Visitor *v;
644 GString *gstr_list = g_string_new("");
645 GString *gstr_union = g_string_new("");
646 int i;
648 for (i = 0; i < 32; i++) {
649 g_string_append_printf(gstr_list, "%s",
650 (i % 3 == 0) ? "true" : "false");
651 if (i != 31) {
652 g_string_append(gstr_list, ", ");
655 g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }",
656 gstr_list->str);
657 v = visitor_input_test_init_raw(data, gstr_union->str);
659 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
660 g_assert(cvalue != NULL);
661 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
663 for (i = 0, elem = cvalue->u.boolean.data; elem; elem = elem->next, i++) {
664 g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0);
667 g_string_free(gstr_union, true);
668 g_string_free(gstr_list, true);
669 qapi_free_UserDefNativeListUnion(cvalue);
672 static void test_visitor_in_native_list_string(TestInputVisitorData *data,
673 const void *unused)
675 UserDefNativeListUnion *cvalue = NULL;
676 strList *elem = NULL;
677 Visitor *v;
678 GString *gstr_list = g_string_new("");
679 GString *gstr_union = g_string_new("");
680 int i;
682 for (i = 0; i < 32; i++) {
683 g_string_append_printf(gstr_list, "'%d'", i);
684 if (i != 31) {
685 g_string_append(gstr_list, ", ");
688 g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }",
689 gstr_list->str);
690 v = visitor_input_test_init_raw(data, gstr_union->str);
692 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
693 g_assert(cvalue != NULL);
694 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
696 for (i = 0, elem = cvalue->u.string.data; elem; elem = elem->next, i++) {
697 gchar str[8];
698 sprintf(str, "%d", i);
699 g_assert_cmpstr(elem->value, ==, str);
702 g_string_free(gstr_union, true);
703 g_string_free(gstr_list, true);
704 qapi_free_UserDefNativeListUnion(cvalue);
707 #define DOUBLE_STR_MAX 16
709 static void test_visitor_in_native_list_number(TestInputVisitorData *data,
710 const void *unused)
712 UserDefNativeListUnion *cvalue = NULL;
713 numberList *elem = NULL;
714 Visitor *v;
715 GString *gstr_list = g_string_new("");
716 GString *gstr_union = g_string_new("");
717 int i;
719 for (i = 0; i < 32; i++) {
720 g_string_append_printf(gstr_list, "%f", (double)i / 3);
721 if (i != 31) {
722 g_string_append(gstr_list, ", ");
725 g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }",
726 gstr_list->str);
727 v = visitor_input_test_init_raw(data, gstr_union->str);
729 visit_type_UserDefNativeListUnion(v, NULL, &cvalue, &error_abort);
730 g_assert(cvalue != NULL);
731 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
733 for (i = 0, elem = cvalue->u.number.data; elem; elem = elem->next, i++) {
734 GString *double_expected = g_string_new("");
735 GString *double_actual = g_string_new("");
737 g_string_printf(double_expected, "%.6f", (double)i / 3);
738 g_string_printf(double_actual, "%.6f", elem->value);
739 g_assert_cmpstr(double_expected->str, ==, double_actual->str);
741 g_string_free(double_expected, true);
742 g_string_free(double_actual, true);
745 g_string_free(gstr_union, true);
746 g_string_free(gstr_list, true);
747 qapi_free_UserDefNativeListUnion(cvalue);
750 static void input_visitor_test_add(const char *testpath,
751 const void *user_data,
752 void (*test_func)(TestInputVisitorData *data,
753 const void *user_data))
755 g_test_add(testpath, TestInputVisitorData, user_data, NULL, test_func,
756 visitor_input_teardown);
759 static void test_visitor_in_errors(TestInputVisitorData *data,
760 const void *unused)
762 TestStruct *p = NULL;
763 Error *err = NULL;
764 Visitor *v;
765 strList *q = NULL;
766 UserDefTwo *r = NULL;
767 WrapAlternate *s = NULL;
769 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', "
770 "'string': -42 }");
772 visit_type_TestStruct(v, NULL, &p, &err);
773 error_free_or_abort(&err);
774 g_assert(!p);
776 v = visitor_input_test_init(data, "[ '1', '2', false, '3' ]");
777 visit_type_strList(v, NULL, &q, &err);
778 error_free_or_abort(&err);
779 assert(!q);
781 v = visitor_input_test_init(data, "{ 'str':'hi' }");
782 visit_type_UserDefTwo(v, NULL, &r, &err);
783 error_free_or_abort(&err);
784 assert(!r);
786 v = visitor_input_test_init(data, "{ }");
787 visit_type_WrapAlternate(v, NULL, &s, &err);
788 error_free_or_abort(&err);
789 assert(!s);
792 static void test_visitor_in_wrong_type(TestInputVisitorData *data,
793 const void *unused)
795 TestStruct *p = NULL;
796 Visitor *v;
797 strList *q = NULL;
798 int64_t i;
799 Error *err = NULL;
801 /* Make sure arrays and structs cannot be confused */
803 v = visitor_input_test_init(data, "[]");
804 visit_type_TestStruct(v, NULL, &p, &err);
805 error_free_or_abort(&err);
806 g_assert(!p);
808 v = visitor_input_test_init(data, "{}");
809 visit_type_strList(v, NULL, &q, &err);
810 error_free_or_abort(&err);
811 assert(!q);
813 /* Make sure primitives and struct cannot be confused */
815 v = visitor_input_test_init(data, "1");
816 visit_type_TestStruct(v, NULL, &p, &err);
817 error_free_or_abort(&err);
818 g_assert(!p);
820 v = visitor_input_test_init(data, "{}");
821 visit_type_int(v, NULL, &i, &err);
822 error_free_or_abort(&err);
824 /* Make sure primitives and arrays cannot be confused */
826 v = visitor_input_test_init(data, "1");
827 visit_type_strList(v, NULL, &q, &err);
828 error_free_or_abort(&err);
829 assert(!q);
831 v = visitor_input_test_init(data, "[]");
832 visit_type_int(v, NULL, &i, &err);
833 error_free_or_abort(&err);
836 int main(int argc, char **argv)
838 g_test_init(&argc, &argv, NULL);
840 input_visitor_test_add("/visitor/input/int",
841 NULL, test_visitor_in_int);
842 input_visitor_test_add("/visitor/input/int_overflow",
843 NULL, test_visitor_in_int_overflow);
844 input_visitor_test_add("/visitor/input/bool",
845 NULL, test_visitor_in_bool);
846 input_visitor_test_add("/visitor/input/number",
847 NULL, test_visitor_in_number);
848 input_visitor_test_add("/visitor/input/string",
849 NULL, test_visitor_in_string);
850 input_visitor_test_add("/visitor/input/enum",
851 NULL, test_visitor_in_enum);
852 input_visitor_test_add("/visitor/input/struct",
853 NULL, test_visitor_in_struct);
854 input_visitor_test_add("/visitor/input/struct-nested",
855 NULL, test_visitor_in_struct_nested);
856 input_visitor_test_add("/visitor/input/list",
857 NULL, test_visitor_in_list);
858 input_visitor_test_add("/visitor/input/any",
859 NULL, test_visitor_in_any);
860 input_visitor_test_add("/visitor/input/null",
861 NULL, test_visitor_in_null);
862 input_visitor_test_add("/visitor/input/union-flat",
863 NULL, test_visitor_in_union_flat);
864 input_visitor_test_add("/visitor/input/alternate",
865 NULL, test_visitor_in_alternate);
866 input_visitor_test_add("/visitor/input/errors",
867 NULL, test_visitor_in_errors);
868 input_visitor_test_add("/visitor/input/wrong-type",
869 NULL, test_visitor_in_wrong_type);
870 input_visitor_test_add("/visitor/input/alternate-number",
871 NULL, test_visitor_in_alternate_number);
872 input_visitor_test_add("/visitor/input/native_list/int",
873 NULL, test_visitor_in_native_list_int);
874 input_visitor_test_add("/visitor/input/native_list/int8",
875 NULL, test_visitor_in_native_list_int8);
876 input_visitor_test_add("/visitor/input/native_list/int16",
877 NULL, test_visitor_in_native_list_int16);
878 input_visitor_test_add("/visitor/input/native_list/int32",
879 NULL, test_visitor_in_native_list_int32);
880 input_visitor_test_add("/visitor/input/native_list/int64",
881 NULL, test_visitor_in_native_list_int64);
882 input_visitor_test_add("/visitor/input/native_list/uint8",
883 NULL, test_visitor_in_native_list_uint8);
884 input_visitor_test_add("/visitor/input/native_list/uint16",
885 NULL, test_visitor_in_native_list_uint16);
886 input_visitor_test_add("/visitor/input/native_list/uint32",
887 NULL, test_visitor_in_native_list_uint32);
888 input_visitor_test_add("/visitor/input/native_list/uint64",
889 NULL, test_visitor_in_native_list_uint64);
890 input_visitor_test_add("/visitor/input/native_list/bool",
891 NULL, test_visitor_in_native_list_bool);
892 input_visitor_test_add("/visitor/input/native_list/str",
893 NULL, test_visitor_in_native_list_string);
894 input_visitor_test_add("/visitor/input/native_list/number",
895 NULL, test_visitor_in_native_list_number);
897 g_test_run();
899 return 0;