2 * QMP Input Visitor unit-tests.
4 * Copyright (C) 2011-2016 Red Hat Inc.
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"
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
{
26 } TestInputVisitorData
;
28 static void visitor_input_teardown(TestInputVisitorData
*data
,
31 qobject_decref(data
->obj
);
35 qmp_input_visitor_cleanup(data
->qiv
);
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
,
49 visitor_input_teardown(data
, NULL
);
51 data
->obj
= qobject_from_jsonv(json_string
, ap
);
54 data
->qiv
= qmp_input_visitor_new(data
->obj
);
57 v
= qmp_input_get_visitor(data
->qiv
);
63 static GCC_FMT_ATTR(2, 3)
64 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
65 const char *json_string
, ...)
70 va_start(ap
, json_string
);
71 v
= visitor_input_test_init_internal(data
, json_string
, &ap
);
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
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
,
92 int64_t res
= 0, value
= -42;
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
,
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
,
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
,
133 double res
= 0, value
= 3.14;
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
,
145 char *res
= NULL
, *value
= (char *) "Q E M U";
148 v
= visitor_input_test_init(data
, "%s", value
);
150 visit_type_str(v
, NULL
, &res
, &error_abort
);
151 g_assert_cmpstr(res
, ==, value
);
156 static void test_visitor_in_enum(TestInputVisitorData
*data
,
162 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
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
,
176 TestStruct
*p
= NULL
;
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");
190 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
193 UserDefTwo
*udp
= NULL
;
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
,
216 UserDefOneList
*item
, *head
= NULL
;
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
++) {
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
);
236 /* An empty list is valid */
237 v
= visitor_input_test_init(data
, "[]");
238 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
242 static void test_visitor_in_any(TestInputVisitorData
*data
,
253 v
= visitor_input_test_init(data
, "-42");
254 visit_type_any(v
, NULL
, &res
, &error_abort
);
255 qint
= qobject_to_qint(res
);
257 g_assert_cmpint(qint_get_int(qint
), ==, -42);
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");
266 qint
= qobject_to_qint(qobj
);
268 g_assert_cmpint(qint_get_int(qint
), ==, -42);
269 qobj
= qdict_get(qdict
, "boolean");
271 qbool
= qobject_to_qbool(qobj
);
273 g_assert(qbool_get_bool(qbool
) == true);
274 qobj
= qdict_get(qdict
, "string");
276 qstring
= qobject_to_qstring(qobj
);
278 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
282 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
286 UserDefFlatUnion
*tmp
;
287 UserDefUnionBase
*base
;
289 v
= visitor_input_test_init(data
,
290 "{ 'enum1': 'value1', "
293 "'boolean': true }");
295 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
296 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
297 g_assert_cmpstr(tmp
->string
, ==, "str");
298 g_assert_cmpint(tmp
->integer
, ==, 41);
299 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
301 base
= qapi_UserDefFlatUnion_base(tmp
);
302 g_assert(&base
->enum1
== &tmp
->enum1
);
304 qapi_free_UserDefFlatUnion(tmp
);
307 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
312 UserDefAlternate
*tmp
;
315 v
= visitor_input_test_init(data
, "42");
316 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
317 g_assert_cmpint(tmp
->type
, ==, QTYPE_QINT
);
318 g_assert_cmpint(tmp
->u
.i
, ==, 42);
319 qapi_free_UserDefAlternate(tmp
);
321 v
= visitor_input_test_init(data
, "'string'");
322 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
323 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
324 g_assert_cmpstr(tmp
->u
.s
, ==, "string");
325 qapi_free_UserDefAlternate(tmp
);
327 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
328 "'enum1':'value1', 'boolean':true}");
329 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
330 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
331 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
332 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
333 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
334 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
335 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
336 qapi_free_UserDefAlternate(tmp
);
338 v
= visitor_input_test_init(data
, "false");
339 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
340 error_free_or_abort(&err
);
341 qapi_free_UserDefAlternate(tmp
);
343 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
344 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
345 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QINT
);
346 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
347 qapi_free_WrapAlternate(wrap
);
349 v
= visitor_input_test_init(data
, "{ 'alt': 'string' }");
350 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
351 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
352 g_assert_cmpstr(wrap
->alt
->u
.s
, ==, "string");
353 qapi_free_WrapAlternate(wrap
);
355 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
356 "'enum1':'value1', 'boolean':true} }");
357 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
358 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
359 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
360 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
361 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
362 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
363 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
364 qapi_free_WrapAlternate(wrap
);
367 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
381 v
= visitor_input_test_init(data
, "42");
382 visit_type_AltStrBool(v
, NULL
, &asb
, &err
);
383 error_free_or_abort(&err
);
384 qapi_free_AltStrBool(asb
);
386 v
= visitor_input_test_init(data
, "42");
387 visit_type_AltStrNum(v
, NULL
, &asn
, &error_abort
);
388 g_assert_cmpint(asn
->type
, ==, QTYPE_QFLOAT
);
389 g_assert_cmpfloat(asn
->u
.n
, ==, 42);
390 qapi_free_AltStrNum(asn
);
392 v
= visitor_input_test_init(data
, "42");
393 visit_type_AltNumStr(v
, NULL
, &ans
, &error_abort
);
394 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
395 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
396 qapi_free_AltNumStr(ans
);
398 v
= visitor_input_test_init(data
, "42");
399 visit_type_AltStrInt(v
, NULL
, &asi
, &error_abort
);
400 g_assert_cmpint(asi
->type
, ==, QTYPE_QINT
);
401 g_assert_cmpint(asi
->u
.i
, ==, 42);
402 qapi_free_AltStrInt(asi
);
404 v
= visitor_input_test_init(data
, "42");
405 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
406 g_assert_cmpint(ain
->type
, ==, QTYPE_QINT
);
407 g_assert_cmpint(ain
->u
.i
, ==, 42);
408 qapi_free_AltIntNum(ain
);
410 v
= visitor_input_test_init(data
, "42");
411 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
412 g_assert_cmpint(ani
->type
, ==, QTYPE_QINT
);
413 g_assert_cmpint(ani
->u
.i
, ==, 42);
414 qapi_free_AltNumInt(ani
);
416 /* Parsing a double */
418 v
= visitor_input_test_init(data
, "42.5");
419 visit_type_AltStrBool(v
, NULL
, &asb
, &err
);
420 error_free_or_abort(&err
);
421 qapi_free_AltStrBool(asb
);
423 v
= visitor_input_test_init(data
, "42.5");
424 visit_type_AltStrNum(v
, NULL
, &asn
, &error_abort
);
425 g_assert_cmpint(asn
->type
, ==, QTYPE_QFLOAT
);
426 g_assert_cmpfloat(asn
->u
.n
, ==, 42.5);
427 qapi_free_AltStrNum(asn
);
429 v
= visitor_input_test_init(data
, "42.5");
430 visit_type_AltNumStr(v
, NULL
, &ans
, &error_abort
);
431 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
432 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
433 qapi_free_AltNumStr(ans
);
435 v
= visitor_input_test_init(data
, "42.5");
436 visit_type_AltStrInt(v
, NULL
, &asi
, &err
);
437 error_free_or_abort(&err
);
438 qapi_free_AltStrInt(asi
);
440 v
= visitor_input_test_init(data
, "42.5");
441 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
442 g_assert_cmpint(ain
->type
, ==, QTYPE_QFLOAT
);
443 g_assert_cmpfloat(ain
->u
.n
, ==, 42.5);
444 qapi_free_AltIntNum(ain
);
446 v
= visitor_input_test_init(data
, "42.5");
447 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
448 g_assert_cmpint(ani
->type
, ==, QTYPE_QFLOAT
);
449 g_assert_cmpfloat(ani
->u
.n
, ==, 42.5);
450 qapi_free_AltNumInt(ani
);
453 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
455 UserDefNativeListUnionKind kind
)
457 UserDefNativeListUnion
*cvalue
= NULL
;
459 GString
*gstr_list
= g_string_new("");
460 GString
*gstr_union
= g_string_new("");
463 for (i
= 0; i
< 32; i
++) {
464 g_string_append_printf(gstr_list
, "%d", i
);
466 g_string_append(gstr_list
, ", ");
469 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
470 UserDefNativeListUnionKind_lookup
[kind
],
472 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
474 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
475 g_assert(cvalue
!= NULL
);
476 g_assert_cmpint(cvalue
->type
, ==, kind
);
479 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
480 intList
*elem
= NULL
;
481 for (i
= 0, elem
= cvalue
->u
.integer
.data
;
482 elem
; elem
= elem
->next
, i
++) {
483 g_assert_cmpint(elem
->value
, ==, i
);
487 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
488 int8List
*elem
= NULL
;
489 for (i
= 0, elem
= cvalue
->u
.s8
.data
; elem
; elem
= elem
->next
, i
++) {
490 g_assert_cmpint(elem
->value
, ==, i
);
494 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
495 int16List
*elem
= NULL
;
496 for (i
= 0, elem
= cvalue
->u
.s16
.data
; elem
; elem
= elem
->next
, i
++) {
497 g_assert_cmpint(elem
->value
, ==, i
);
501 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
502 int32List
*elem
= NULL
;
503 for (i
= 0, elem
= cvalue
->u
.s32
.data
; elem
; elem
= elem
->next
, i
++) {
504 g_assert_cmpint(elem
->value
, ==, i
);
508 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
509 int64List
*elem
= NULL
;
510 for (i
= 0, elem
= cvalue
->u
.s64
.data
; elem
; elem
= elem
->next
, i
++) {
511 g_assert_cmpint(elem
->value
, ==, i
);
515 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
516 uint8List
*elem
= NULL
;
517 for (i
= 0, elem
= cvalue
->u
.u8
.data
; elem
; elem
= elem
->next
, i
++) {
518 g_assert_cmpint(elem
->value
, ==, i
);
522 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
523 uint16List
*elem
= NULL
;
524 for (i
= 0, elem
= cvalue
->u
.u16
.data
; elem
; elem
= elem
->next
, i
++) {
525 g_assert_cmpint(elem
->value
, ==, i
);
529 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
530 uint32List
*elem
= NULL
;
531 for (i
= 0, elem
= cvalue
->u
.u32
.data
; elem
; elem
= elem
->next
, i
++) {
532 g_assert_cmpint(elem
->value
, ==, i
);
536 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
537 uint64List
*elem
= NULL
;
538 for (i
= 0, elem
= cvalue
->u
.u64
.data
; elem
; elem
= elem
->next
, i
++) {
539 g_assert_cmpint(elem
->value
, ==, i
);
544 g_assert_not_reached();
547 g_string_free(gstr_union
, true);
548 g_string_free(gstr_list
, true);
549 qapi_free_UserDefNativeListUnion(cvalue
);
552 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
555 test_native_list_integer_helper(data
, unused
,
556 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
559 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
562 test_native_list_integer_helper(data
, unused
,
563 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
566 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
569 test_native_list_integer_helper(data
, unused
,
570 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
573 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
576 test_native_list_integer_helper(data
, unused
,
577 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
580 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
583 test_native_list_integer_helper(data
, unused
,
584 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
587 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
590 test_native_list_integer_helper(data
, unused
,
591 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
594 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
597 test_native_list_integer_helper(data
, unused
,
598 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
601 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
604 test_native_list_integer_helper(data
, unused
,
605 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
608 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
611 test_native_list_integer_helper(data
, unused
,
612 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
615 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
618 UserDefNativeListUnion
*cvalue
= NULL
;
619 boolList
*elem
= NULL
;
621 GString
*gstr_list
= g_string_new("");
622 GString
*gstr_union
= g_string_new("");
625 for (i
= 0; i
< 32; i
++) {
626 g_string_append_printf(gstr_list
, "%s",
627 (i
% 3 == 0) ? "true" : "false");
629 g_string_append(gstr_list
, ", ");
632 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
634 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
636 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
637 g_assert(cvalue
!= NULL
);
638 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
640 for (i
= 0, elem
= cvalue
->u
.boolean
.data
; elem
; elem
= elem
->next
, i
++) {
641 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
644 g_string_free(gstr_union
, true);
645 g_string_free(gstr_list
, true);
646 qapi_free_UserDefNativeListUnion(cvalue
);
649 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
652 UserDefNativeListUnion
*cvalue
= NULL
;
653 strList
*elem
= NULL
;
655 GString
*gstr_list
= g_string_new("");
656 GString
*gstr_union
= g_string_new("");
659 for (i
= 0; i
< 32; i
++) {
660 g_string_append_printf(gstr_list
, "'%d'", i
);
662 g_string_append(gstr_list
, ", ");
665 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
667 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
669 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
670 g_assert(cvalue
!= NULL
);
671 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
673 for (i
= 0, elem
= cvalue
->u
.string
.data
; elem
; elem
= elem
->next
, i
++) {
675 sprintf(str
, "%d", i
);
676 g_assert_cmpstr(elem
->value
, ==, str
);
679 g_string_free(gstr_union
, true);
680 g_string_free(gstr_list
, true);
681 qapi_free_UserDefNativeListUnion(cvalue
);
684 #define DOUBLE_STR_MAX 16
686 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
689 UserDefNativeListUnion
*cvalue
= NULL
;
690 numberList
*elem
= NULL
;
692 GString
*gstr_list
= g_string_new("");
693 GString
*gstr_union
= g_string_new("");
696 for (i
= 0; i
< 32; i
++) {
697 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
699 g_string_append(gstr_list
, ", ");
702 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
704 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
706 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
707 g_assert(cvalue
!= NULL
);
708 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
710 for (i
= 0, elem
= cvalue
->u
.number
.data
; elem
; elem
= elem
->next
, i
++) {
711 GString
*double_expected
= g_string_new("");
712 GString
*double_actual
= g_string_new("");
714 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
715 g_string_printf(double_actual
, "%.6f", elem
->value
);
716 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
718 g_string_free(double_expected
, true);
719 g_string_free(double_actual
, true);
722 g_string_free(gstr_union
, true);
723 g_string_free(gstr_list
, true);
724 qapi_free_UserDefNativeListUnion(cvalue
);
727 static void input_visitor_test_add(const char *testpath
,
728 TestInputVisitorData
*data
,
729 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
731 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
732 visitor_input_teardown
);
735 static void test_visitor_in_errors(TestInputVisitorData
*data
,
738 TestStruct
*p
= NULL
;
743 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
746 visit_type_TestStruct(v
, NULL
, &p
, &err
);
747 error_free_or_abort(&err
);
748 /* FIXME - a failed parse should not leave a partially-allocated p
749 * for us to clean up; this could cause callers to leak memory. */
750 g_assert(p
->string
== NULL
);
755 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
756 visit_type_strList(v
, NULL
, &q
, &err
);
757 error_free_or_abort(&err
);
759 qapi_free_strList(q
);
762 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
765 TestStruct
*p
= NULL
;
771 /* Make sure arrays and structs cannot be confused */
773 v
= visitor_input_test_init(data
, "[]");
774 visit_type_TestStruct(v
, NULL
, &p
, &err
);
775 error_free_or_abort(&err
);
778 v
= visitor_input_test_init(data
, "{}");
779 visit_type_strList(v
, NULL
, &q
, &err
);
780 error_free_or_abort(&err
);
783 /* Make sure primitives and struct cannot be confused */
785 v
= visitor_input_test_init(data
, "1");
786 visit_type_TestStruct(v
, NULL
, &p
, &err
);
787 error_free_or_abort(&err
);
790 v
= visitor_input_test_init(data
, "{}");
791 visit_type_int(v
, NULL
, &i
, &err
);
792 error_free_or_abort(&err
);
794 /* Make sure primitives and arrays cannot be confused */
796 v
= visitor_input_test_init(data
, "1");
797 visit_type_strList(v
, NULL
, &q
, &err
);
798 error_free_or_abort(&err
);
801 v
= visitor_input_test_init(data
, "[]");
802 visit_type_int(v
, NULL
, &i
, &err
);
803 error_free_or_abort(&err
);
806 int main(int argc
, char **argv
)
808 TestInputVisitorData in_visitor_data
;
810 g_test_init(&argc
, &argv
, NULL
);
812 input_visitor_test_add("/visitor/input/int",
813 &in_visitor_data
, test_visitor_in_int
);
814 input_visitor_test_add("/visitor/input/int_overflow",
815 &in_visitor_data
, test_visitor_in_int_overflow
);
816 input_visitor_test_add("/visitor/input/bool",
817 &in_visitor_data
, test_visitor_in_bool
);
818 input_visitor_test_add("/visitor/input/number",
819 &in_visitor_data
, test_visitor_in_number
);
820 input_visitor_test_add("/visitor/input/string",
821 &in_visitor_data
, test_visitor_in_string
);
822 input_visitor_test_add("/visitor/input/enum",
823 &in_visitor_data
, test_visitor_in_enum
);
824 input_visitor_test_add("/visitor/input/struct",
825 &in_visitor_data
, test_visitor_in_struct
);
826 input_visitor_test_add("/visitor/input/struct-nested",
827 &in_visitor_data
, test_visitor_in_struct_nested
);
828 input_visitor_test_add("/visitor/input/list",
829 &in_visitor_data
, test_visitor_in_list
);
830 input_visitor_test_add("/visitor/input/any",
831 &in_visitor_data
, test_visitor_in_any
);
832 input_visitor_test_add("/visitor/input/union-flat",
833 &in_visitor_data
, test_visitor_in_union_flat
);
834 input_visitor_test_add("/visitor/input/alternate",
835 &in_visitor_data
, test_visitor_in_alternate
);
836 input_visitor_test_add("/visitor/input/errors",
837 &in_visitor_data
, test_visitor_in_errors
);
838 input_visitor_test_add("/visitor/input/wrong-type",
839 &in_visitor_data
, test_visitor_in_wrong_type
);
840 input_visitor_test_add("/visitor/input/alternate-number",
841 &in_visitor_data
, test_visitor_in_alternate_number
);
842 input_visitor_test_add("/visitor/input/native_list/int",
844 test_visitor_in_native_list_int
);
845 input_visitor_test_add("/visitor/input/native_list/int8",
847 test_visitor_in_native_list_int8
);
848 input_visitor_test_add("/visitor/input/native_list/int16",
850 test_visitor_in_native_list_int16
);
851 input_visitor_test_add("/visitor/input/native_list/int32",
853 test_visitor_in_native_list_int32
);
854 input_visitor_test_add("/visitor/input/native_list/int64",
856 test_visitor_in_native_list_int64
);
857 input_visitor_test_add("/visitor/input/native_list/uint8",
859 test_visitor_in_native_list_uint8
);
860 input_visitor_test_add("/visitor/input/native_list/uint16",
862 test_visitor_in_native_list_uint16
);
863 input_visitor_test_add("/visitor/input/native_list/uint32",
865 test_visitor_in_native_list_uint32
);
866 input_visitor_test_add("/visitor/input/native_list/uint64",
868 test_visitor_in_native_list_uint64
);
869 input_visitor_test_add("/visitor/input/native_list/bool",
870 &in_visitor_data
, test_visitor_in_native_list_bool
);
871 input_visitor_test_add("/visitor/input/native_list/str",
873 test_visitor_in_native_list_string
);
874 input_visitor_test_add("/visitor/input/native_list/number",
876 test_visitor_in_native_list_number
);