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/qmp-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
22 typedef struct TestInputVisitorData
{
25 } TestInputVisitorData
;
27 static void visitor_input_teardown(TestInputVisitorData
*data
,
30 qobject_decref(data
->obj
);
34 qmp_input_visitor_cleanup(data
->qiv
);
39 /* The various test_init functions are provided instead of a test setup
40 function so that the JSON string used by the tests are kept in the test
41 functions (and not in main()). */
42 static Visitor
*visitor_input_test_init_internal(TestInputVisitorData
*data
,
43 const char *json_string
,
48 visitor_input_teardown(data
, NULL
);
50 data
->obj
= qobject_from_jsonv(json_string
, ap
);
53 data
->qiv
= qmp_input_visitor_new(data
->obj
);
56 v
= qmp_input_get_visitor(data
->qiv
);
62 static GCC_FMT_ATTR(2, 3)
63 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
64 const char *json_string
, ...)
69 va_start(ap
, json_string
);
70 v
= visitor_input_test_init_internal(data
, json_string
, &ap
);
75 /* similar to visitor_input_test_init(), but does not expect a string
76 * literal/format json_string argument and so can be used for
77 * programatically generated strings (and we can't pass in programatically
78 * generated strings via %s format parameters since qobject_from_jsonv()
79 * will wrap those in double-quotes and treat the entire object as a
82 static Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
83 const char *json_string
)
85 return visitor_input_test_init_internal(data
, json_string
, NULL
);
88 static void test_visitor_in_int(TestInputVisitorData
*data
,
91 int64_t res
= 0, value
= -42;
94 v
= visitor_input_test_init(data
, "%" PRId64
, value
);
96 visit_type_int(v
, NULL
, &res
, &error_abort
);
97 g_assert_cmpint(res
, ==, value
);
100 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
107 /* this will overflow a Qint/int64, so should be deserialized into
108 * a QFloat/double field instead, leading to an error if we pass it
109 * to visit_type_int. confirm this.
111 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
113 visit_type_int(v
, NULL
, &res
, &err
);
114 error_free_or_abort(&err
);
117 static void test_visitor_in_bool(TestInputVisitorData
*data
,
123 v
= visitor_input_test_init(data
, "true");
125 visit_type_bool(v
, NULL
, &res
, &error_abort
);
126 g_assert_cmpint(res
, ==, true);
129 static void test_visitor_in_number(TestInputVisitorData
*data
,
132 double res
= 0, value
= 3.14;
135 v
= visitor_input_test_init(data
, "%f", value
);
137 visit_type_number(v
, NULL
, &res
, &error_abort
);
138 g_assert_cmpfloat(res
, ==, value
);
141 static void test_visitor_in_string(TestInputVisitorData
*data
,
144 char *res
= NULL
, *value
= (char *) "Q E M U";
147 v
= visitor_input_test_init(data
, "%s", value
);
149 visit_type_str(v
, NULL
, &res
, &error_abort
);
150 g_assert_cmpstr(res
, ==, value
);
155 static void test_visitor_in_enum(TestInputVisitorData
*data
,
161 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
164 v
= visitor_input_test_init(data
, "%s", EnumOne_lookup
[i
]);
166 visit_type_EnumOne(v
, NULL
, &res
, &error_abort
);
167 g_assert_cmpint(i
, ==, res
);
172 static void test_visitor_in_struct(TestInputVisitorData
*data
,
175 TestStruct
*p
= NULL
;
178 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
180 visit_type_TestStruct(v
, NULL
, &p
, &error_abort
);
181 g_assert_cmpint(p
->integer
, ==, -42);
182 g_assert(p
->boolean
== true);
183 g_assert_cmpstr(p
->string
, ==, "foo");
189 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
192 UserDefTwo
*udp
= NULL
;
195 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
196 "'dict1': { 'string1': 'string1', "
197 "'dict2': { 'userdef': { 'integer': 42, "
198 "'string': 'string' }, 'string': 'string2'}}}");
200 visit_type_UserDefTwo(v
, NULL
, &udp
, &error_abort
);
202 g_assert_cmpstr(udp
->string0
, ==, "string0");
203 g_assert_cmpstr(udp
->dict1
->string1
, ==, "string1");
204 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
205 g_assert_cmpstr(udp
->dict1
->dict2
->userdef
->string
, ==, "string");
206 g_assert_cmpstr(udp
->dict1
->dict2
->string
, ==, "string2");
207 g_assert(udp
->dict1
->has_dict3
== false);
209 qapi_free_UserDefTwo(udp
);
212 static void test_visitor_in_list(TestInputVisitorData
*data
,
215 UserDefOneList
*item
, *head
= NULL
;
219 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
221 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
222 g_assert(head
!= NULL
);
224 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
227 snprintf(string
, sizeof(string
), "string%d", i
);
228 g_assert_cmpstr(item
->value
->string
, ==, string
);
229 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
232 qapi_free_UserDefOneList(head
);
235 /* An empty list is valid */
236 v
= visitor_input_test_init(data
, "[]");
237 visit_type_UserDefOneList(v
, NULL
, &head
, &error_abort
);
241 static void test_visitor_in_any(TestInputVisitorData
*data
,
252 v
= visitor_input_test_init(data
, "-42");
253 visit_type_any(v
, NULL
, &res
, &error_abort
);
254 qint
= qobject_to_qint(res
);
256 g_assert_cmpint(qint_get_int(qint
), ==, -42);
259 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
260 visit_type_any(v
, NULL
, &res
, &error_abort
);
261 qdict
= qobject_to_qdict(res
);
262 g_assert(qdict
&& qdict_size(qdict
) == 3);
263 qobj
= qdict_get(qdict
, "integer");
265 qint
= qobject_to_qint(qobj
);
267 g_assert_cmpint(qint_get_int(qint
), ==, -42);
268 qobj
= qdict_get(qdict
, "boolean");
270 qbool
= qobject_to_qbool(qobj
);
272 g_assert(qbool_get_bool(qbool
) == true);
273 qobj
= qdict_get(qdict
, "string");
275 qstring
= qobject_to_qstring(qobj
);
277 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
281 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
285 UserDefFlatUnion
*tmp
;
286 UserDefUnionBase
*base
;
288 v
= visitor_input_test_init(data
,
289 "{ 'enum1': 'value1', "
292 "'boolean': true }");
294 visit_type_UserDefFlatUnion(v
, NULL
, &tmp
, &error_abort
);
295 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
296 g_assert_cmpstr(tmp
->string
, ==, "str");
297 g_assert_cmpint(tmp
->integer
, ==, 41);
298 g_assert_cmpint(tmp
->u
.value1
.boolean
, ==, true);
300 base
= qapi_UserDefFlatUnion_base(tmp
);
301 g_assert(&base
->enum1
== &tmp
->enum1
);
303 qapi_free_UserDefFlatUnion(tmp
);
306 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
311 UserDefAlternate
*tmp
;
314 v
= visitor_input_test_init(data
, "42");
315 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
316 g_assert_cmpint(tmp
->type
, ==, QTYPE_QINT
);
317 g_assert_cmpint(tmp
->u
.i
, ==, 42);
318 qapi_free_UserDefAlternate(tmp
);
320 v
= visitor_input_test_init(data
, "'string'");
321 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
322 g_assert_cmpint(tmp
->type
, ==, QTYPE_QSTRING
);
323 g_assert_cmpstr(tmp
->u
.s
, ==, "string");
324 qapi_free_UserDefAlternate(tmp
);
326 v
= visitor_input_test_init(data
, "{'integer':1, 'string':'str', "
327 "'enum1':'value1', 'boolean':true}");
328 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &error_abort
);
329 g_assert_cmpint(tmp
->type
, ==, QTYPE_QDICT
);
330 g_assert_cmpint(tmp
->u
.udfu
.integer
, ==, 1);
331 g_assert_cmpstr(tmp
->u
.udfu
.string
, ==, "str");
332 g_assert_cmpint(tmp
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
333 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.boolean
, ==, true);
334 g_assert_cmpint(tmp
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
335 qapi_free_UserDefAlternate(tmp
);
337 v
= visitor_input_test_init(data
, "false");
338 visit_type_UserDefAlternate(v
, NULL
, &tmp
, &err
);
339 error_free_or_abort(&err
);
340 qapi_free_UserDefAlternate(tmp
);
342 v
= visitor_input_test_init(data
, "{ 'alt': 42 }");
343 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
344 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QINT
);
345 g_assert_cmpint(wrap
->alt
->u
.i
, ==, 42);
346 qapi_free_WrapAlternate(wrap
);
348 v
= visitor_input_test_init(data
, "{ 'alt': 'string' }");
349 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
350 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QSTRING
);
351 g_assert_cmpstr(wrap
->alt
->u
.s
, ==, "string");
352 qapi_free_WrapAlternate(wrap
);
354 v
= visitor_input_test_init(data
, "{ 'alt': {'integer':1, 'string':'str', "
355 "'enum1':'value1', 'boolean':true} }");
356 visit_type_WrapAlternate(v
, NULL
, &wrap
, &error_abort
);
357 g_assert_cmpint(wrap
->alt
->type
, ==, QTYPE_QDICT
);
358 g_assert_cmpint(wrap
->alt
->u
.udfu
.integer
, ==, 1);
359 g_assert_cmpstr(wrap
->alt
->u
.udfu
.string
, ==, "str");
360 g_assert_cmpint(wrap
->alt
->u
.udfu
.enum1
, ==, ENUM_ONE_VALUE1
);
361 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.boolean
, ==, true);
362 g_assert_cmpint(wrap
->alt
->u
.udfu
.u
.value1
.has_a_b
, ==, false);
363 qapi_free_WrapAlternate(wrap
);
366 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
380 v
= visitor_input_test_init(data
, "42");
381 visit_type_AltStrBool(v
, NULL
, &asb
, &err
);
382 error_free_or_abort(&err
);
383 qapi_free_AltStrBool(asb
);
385 v
= visitor_input_test_init(data
, "42");
386 visit_type_AltStrNum(v
, NULL
, &asn
, &error_abort
);
387 g_assert_cmpint(asn
->type
, ==, QTYPE_QFLOAT
);
388 g_assert_cmpfloat(asn
->u
.n
, ==, 42);
389 qapi_free_AltStrNum(asn
);
391 v
= visitor_input_test_init(data
, "42");
392 visit_type_AltNumStr(v
, NULL
, &ans
, &error_abort
);
393 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
394 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
395 qapi_free_AltNumStr(ans
);
397 v
= visitor_input_test_init(data
, "42");
398 visit_type_AltStrInt(v
, NULL
, &asi
, &error_abort
);
399 g_assert_cmpint(asi
->type
, ==, QTYPE_QINT
);
400 g_assert_cmpint(asi
->u
.i
, ==, 42);
401 qapi_free_AltStrInt(asi
);
403 v
= visitor_input_test_init(data
, "42");
404 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
405 g_assert_cmpint(ain
->type
, ==, QTYPE_QINT
);
406 g_assert_cmpint(ain
->u
.i
, ==, 42);
407 qapi_free_AltIntNum(ain
);
409 v
= visitor_input_test_init(data
, "42");
410 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
411 g_assert_cmpint(ani
->type
, ==, QTYPE_QINT
);
412 g_assert_cmpint(ani
->u
.i
, ==, 42);
413 qapi_free_AltNumInt(ani
);
415 /* Parsing a double */
417 v
= visitor_input_test_init(data
, "42.5");
418 visit_type_AltStrBool(v
, NULL
, &asb
, &err
);
419 error_free_or_abort(&err
);
420 qapi_free_AltStrBool(asb
);
422 v
= visitor_input_test_init(data
, "42.5");
423 visit_type_AltStrNum(v
, NULL
, &asn
, &error_abort
);
424 g_assert_cmpint(asn
->type
, ==, QTYPE_QFLOAT
);
425 g_assert_cmpfloat(asn
->u
.n
, ==, 42.5);
426 qapi_free_AltStrNum(asn
);
428 v
= visitor_input_test_init(data
, "42.5");
429 visit_type_AltNumStr(v
, NULL
, &ans
, &error_abort
);
430 g_assert_cmpint(ans
->type
, ==, QTYPE_QFLOAT
);
431 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
432 qapi_free_AltNumStr(ans
);
434 v
= visitor_input_test_init(data
, "42.5");
435 visit_type_AltStrInt(v
, NULL
, &asi
, &err
);
436 error_free_or_abort(&err
);
437 qapi_free_AltStrInt(asi
);
439 v
= visitor_input_test_init(data
, "42.5");
440 visit_type_AltIntNum(v
, NULL
, &ain
, &error_abort
);
441 g_assert_cmpint(ain
->type
, ==, QTYPE_QFLOAT
);
442 g_assert_cmpfloat(ain
->u
.n
, ==, 42.5);
443 qapi_free_AltIntNum(ain
);
445 v
= visitor_input_test_init(data
, "42.5");
446 visit_type_AltNumInt(v
, NULL
, &ani
, &error_abort
);
447 g_assert_cmpint(ani
->type
, ==, QTYPE_QFLOAT
);
448 g_assert_cmpfloat(ani
->u
.n
, ==, 42.5);
449 qapi_free_AltNumInt(ani
);
452 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
454 UserDefNativeListUnionKind kind
)
456 UserDefNativeListUnion
*cvalue
= NULL
;
458 GString
*gstr_list
= g_string_new("");
459 GString
*gstr_union
= g_string_new("");
462 for (i
= 0; i
< 32; i
++) {
463 g_string_append_printf(gstr_list
, "%d", i
);
465 g_string_append(gstr_list
, ", ");
468 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
469 UserDefNativeListUnionKind_lookup
[kind
],
471 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
473 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
474 g_assert(cvalue
!= NULL
);
475 g_assert_cmpint(cvalue
->type
, ==, kind
);
478 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
479 intList
*elem
= NULL
;
480 for (i
= 0, elem
= cvalue
->u
.integer
.data
;
481 elem
; elem
= elem
->next
, i
++) {
482 g_assert_cmpint(elem
->value
, ==, i
);
486 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
487 int8List
*elem
= NULL
;
488 for (i
= 0, elem
= cvalue
->u
.s8
.data
; elem
; elem
= elem
->next
, i
++) {
489 g_assert_cmpint(elem
->value
, ==, i
);
493 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
494 int16List
*elem
= NULL
;
495 for (i
= 0, elem
= cvalue
->u
.s16
.data
; elem
; elem
= elem
->next
, i
++) {
496 g_assert_cmpint(elem
->value
, ==, i
);
500 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
501 int32List
*elem
= NULL
;
502 for (i
= 0, elem
= cvalue
->u
.s32
.data
; elem
; elem
= elem
->next
, i
++) {
503 g_assert_cmpint(elem
->value
, ==, i
);
507 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
508 int64List
*elem
= NULL
;
509 for (i
= 0, elem
= cvalue
->u
.s64
.data
; elem
; elem
= elem
->next
, i
++) {
510 g_assert_cmpint(elem
->value
, ==, i
);
514 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
515 uint8List
*elem
= NULL
;
516 for (i
= 0, elem
= cvalue
->u
.u8
.data
; elem
; elem
= elem
->next
, i
++) {
517 g_assert_cmpint(elem
->value
, ==, i
);
521 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
522 uint16List
*elem
= NULL
;
523 for (i
= 0, elem
= cvalue
->u
.u16
.data
; elem
; elem
= elem
->next
, i
++) {
524 g_assert_cmpint(elem
->value
, ==, i
);
528 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
529 uint32List
*elem
= NULL
;
530 for (i
= 0, elem
= cvalue
->u
.u32
.data
; elem
; elem
= elem
->next
, i
++) {
531 g_assert_cmpint(elem
->value
, ==, i
);
535 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
536 uint64List
*elem
= NULL
;
537 for (i
= 0, elem
= cvalue
->u
.u64
.data
; elem
; elem
= elem
->next
, i
++) {
538 g_assert_cmpint(elem
->value
, ==, i
);
543 g_assert_not_reached();
546 g_string_free(gstr_union
, true);
547 g_string_free(gstr_list
, true);
548 qapi_free_UserDefNativeListUnion(cvalue
);
551 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
554 test_native_list_integer_helper(data
, unused
,
555 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
558 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
561 test_native_list_integer_helper(data
, unused
,
562 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
565 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
568 test_native_list_integer_helper(data
, unused
,
569 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
572 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
575 test_native_list_integer_helper(data
, unused
,
576 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
579 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
582 test_native_list_integer_helper(data
, unused
,
583 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
586 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
589 test_native_list_integer_helper(data
, unused
,
590 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
593 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
596 test_native_list_integer_helper(data
, unused
,
597 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
600 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
603 test_native_list_integer_helper(data
, unused
,
604 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
607 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
610 test_native_list_integer_helper(data
, unused
,
611 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
614 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
617 UserDefNativeListUnion
*cvalue
= NULL
;
618 boolList
*elem
= NULL
;
620 GString
*gstr_list
= g_string_new("");
621 GString
*gstr_union
= g_string_new("");
624 for (i
= 0; i
< 32; i
++) {
625 g_string_append_printf(gstr_list
, "%s",
626 (i
% 3 == 0) ? "true" : "false");
628 g_string_append(gstr_list
, ", ");
631 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
633 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
635 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
636 g_assert(cvalue
!= NULL
);
637 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
639 for (i
= 0, elem
= cvalue
->u
.boolean
.data
; elem
; elem
= elem
->next
, i
++) {
640 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
643 g_string_free(gstr_union
, true);
644 g_string_free(gstr_list
, true);
645 qapi_free_UserDefNativeListUnion(cvalue
);
648 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
651 UserDefNativeListUnion
*cvalue
= NULL
;
652 strList
*elem
= NULL
;
654 GString
*gstr_list
= g_string_new("");
655 GString
*gstr_union
= g_string_new("");
658 for (i
= 0; i
< 32; i
++) {
659 g_string_append_printf(gstr_list
, "'%d'", i
);
661 g_string_append(gstr_list
, ", ");
664 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
666 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
668 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
669 g_assert(cvalue
!= NULL
);
670 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
672 for (i
= 0, elem
= cvalue
->u
.string
.data
; elem
; elem
= elem
->next
, i
++) {
674 sprintf(str
, "%d", i
);
675 g_assert_cmpstr(elem
->value
, ==, str
);
678 g_string_free(gstr_union
, true);
679 g_string_free(gstr_list
, true);
680 qapi_free_UserDefNativeListUnion(cvalue
);
683 #define DOUBLE_STR_MAX 16
685 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
688 UserDefNativeListUnion
*cvalue
= NULL
;
689 numberList
*elem
= NULL
;
691 GString
*gstr_list
= g_string_new("");
692 GString
*gstr_union
= g_string_new("");
695 for (i
= 0; i
< 32; i
++) {
696 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
698 g_string_append(gstr_list
, ", ");
701 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
703 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
705 visit_type_UserDefNativeListUnion(v
, NULL
, &cvalue
, &error_abort
);
706 g_assert(cvalue
!= NULL
);
707 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
709 for (i
= 0, elem
= cvalue
->u
.number
.data
; elem
; elem
= elem
->next
, i
++) {
710 GString
*double_expected
= g_string_new("");
711 GString
*double_actual
= g_string_new("");
713 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
714 g_string_printf(double_actual
, "%.6f", elem
->value
);
715 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
717 g_string_free(double_expected
, true);
718 g_string_free(double_actual
, true);
721 g_string_free(gstr_union
, true);
722 g_string_free(gstr_list
, true);
723 qapi_free_UserDefNativeListUnion(cvalue
);
726 static void input_visitor_test_add(const char *testpath
,
727 TestInputVisitorData
*data
,
728 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
730 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
731 visitor_input_teardown
);
734 static void test_visitor_in_errors(TestInputVisitorData
*data
,
737 TestStruct
*p
= NULL
;
742 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', "
745 visit_type_TestStruct(v
, NULL
, &p
, &err
);
746 error_free_or_abort(&err
);
747 /* FIXME - a failed parse should not leave a partially-allocated p
748 * for us to clean up; this could cause callers to leak memory. */
749 g_assert(p
->string
== NULL
);
754 v
= visitor_input_test_init(data
, "[ '1', '2', false, '3' ]");
755 visit_type_strList(v
, NULL
, &q
, &err
);
756 error_free_or_abort(&err
);
758 qapi_free_strList(q
);
761 static void test_visitor_in_wrong_type(TestInputVisitorData
*data
,
764 TestStruct
*p
= NULL
;
770 /* Make sure arrays and structs cannot be confused */
772 v
= visitor_input_test_init(data
, "[]");
773 visit_type_TestStruct(v
, NULL
, &p
, &err
);
774 error_free_or_abort(&err
);
777 v
= visitor_input_test_init(data
, "{}");
778 visit_type_strList(v
, NULL
, &q
, &err
);
779 error_free_or_abort(&err
);
782 /* Make sure primitives and struct cannot be confused */
784 v
= visitor_input_test_init(data
, "1");
785 visit_type_TestStruct(v
, NULL
, &p
, &err
);
786 error_free_or_abort(&err
);
789 v
= visitor_input_test_init(data
, "{}");
790 visit_type_int(v
, NULL
, &i
, &err
);
791 error_free_or_abort(&err
);
793 /* Make sure primitives and arrays cannot be confused */
795 v
= visitor_input_test_init(data
, "1");
796 visit_type_strList(v
, NULL
, &q
, &err
);
797 error_free_or_abort(&err
);
800 v
= visitor_input_test_init(data
, "[]");
801 visit_type_int(v
, NULL
, &i
, &err
);
802 error_free_or_abort(&err
);
805 int main(int argc
, char **argv
)
807 TestInputVisitorData in_visitor_data
;
809 g_test_init(&argc
, &argv
, NULL
);
811 input_visitor_test_add("/visitor/input/int",
812 &in_visitor_data
, test_visitor_in_int
);
813 input_visitor_test_add("/visitor/input/int_overflow",
814 &in_visitor_data
, test_visitor_in_int_overflow
);
815 input_visitor_test_add("/visitor/input/bool",
816 &in_visitor_data
, test_visitor_in_bool
);
817 input_visitor_test_add("/visitor/input/number",
818 &in_visitor_data
, test_visitor_in_number
);
819 input_visitor_test_add("/visitor/input/string",
820 &in_visitor_data
, test_visitor_in_string
);
821 input_visitor_test_add("/visitor/input/enum",
822 &in_visitor_data
, test_visitor_in_enum
);
823 input_visitor_test_add("/visitor/input/struct",
824 &in_visitor_data
, test_visitor_in_struct
);
825 input_visitor_test_add("/visitor/input/struct-nested",
826 &in_visitor_data
, test_visitor_in_struct_nested
);
827 input_visitor_test_add("/visitor/input/list",
828 &in_visitor_data
, test_visitor_in_list
);
829 input_visitor_test_add("/visitor/input/any",
830 &in_visitor_data
, test_visitor_in_any
);
831 input_visitor_test_add("/visitor/input/union-flat",
832 &in_visitor_data
, test_visitor_in_union_flat
);
833 input_visitor_test_add("/visitor/input/alternate",
834 &in_visitor_data
, test_visitor_in_alternate
);
835 input_visitor_test_add("/visitor/input/errors",
836 &in_visitor_data
, test_visitor_in_errors
);
837 input_visitor_test_add("/visitor/input/wrong-type",
838 &in_visitor_data
, test_visitor_in_wrong_type
);
839 input_visitor_test_add("/visitor/input/alternate-number",
840 &in_visitor_data
, test_visitor_in_alternate_number
);
841 input_visitor_test_add("/visitor/input/native_list/int",
843 test_visitor_in_native_list_int
);
844 input_visitor_test_add("/visitor/input/native_list/int8",
846 test_visitor_in_native_list_int8
);
847 input_visitor_test_add("/visitor/input/native_list/int16",
849 test_visitor_in_native_list_int16
);
850 input_visitor_test_add("/visitor/input/native_list/int32",
852 test_visitor_in_native_list_int32
);
853 input_visitor_test_add("/visitor/input/native_list/int64",
855 test_visitor_in_native_list_int64
);
856 input_visitor_test_add("/visitor/input/native_list/uint8",
858 test_visitor_in_native_list_uint8
);
859 input_visitor_test_add("/visitor/input/native_list/uint16",
861 test_visitor_in_native_list_uint16
);
862 input_visitor_test_add("/visitor/input/native_list/uint32",
864 test_visitor_in_native_list_uint32
);
865 input_visitor_test_add("/visitor/input/native_list/uint64",
867 test_visitor_in_native_list_uint64
);
868 input_visitor_test_add("/visitor/input/native_list/bool",
869 &in_visitor_data
, test_visitor_in_native_list_bool
);
870 input_visitor_test_add("/visitor/input/native_list/str",
872 test_visitor_in_native_list_string
);
873 input_visitor_test_add("/visitor/input/native_list/number",
875 test_visitor_in_native_list_number
);