2 * QMP Input Visitor unit-tests.
4 * Copyright (C) 2011, 2015 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.
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 /* This is provided instead of a test setup function so that the JSON
40 string used by the tests are kept in the test functions (and not
42 static GCC_FMT_ATTR(2, 3)
43 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
44 const char *json_string
, ...)
49 va_start(ap
, json_string
);
50 data
->obj
= qobject_from_jsonv(json_string
, &ap
);
53 g_assert(data
->obj
!= NULL
);
55 data
->qiv
= qmp_input_visitor_new(data
->obj
);
56 g_assert(data
->qiv
!= NULL
);
58 v
= qmp_input_get_visitor(data
->qiv
);
64 /* similar to visitor_input_test_init(), but does not expect a string
65 * literal/format json_string argument and so can be used for
66 * programatically generated strings (and we can't pass in programatically
67 * generated strings via %s format parameters since qobject_from_jsonv()
68 * will wrap those in double-quotes and treat the entire object as a
71 static Visitor
*visitor_input_test_init_raw(TestInputVisitorData
*data
,
72 const char *json_string
)
76 data
->obj
= qobject_from_json(json_string
);
78 g_assert(data
->obj
!= NULL
);
80 data
->qiv
= qmp_input_visitor_new(data
->obj
);
81 g_assert(data
->qiv
!= NULL
);
83 v
= qmp_input_get_visitor(data
->qiv
);
89 static void test_visitor_in_int(TestInputVisitorData
*data
,
92 int64_t res
= 0, value
= -42;
96 v
= visitor_input_test_init(data
, "%" PRId64
, value
);
98 visit_type_int(v
, &res
, NULL
, &err
);
100 g_assert_cmpint(res
, ==, value
);
103 static void test_visitor_in_int_overflow(TestInputVisitorData
*data
,
110 /* this will overflow a Qint/int64, so should be deserialized into
111 * a QFloat/double field instead, leading to an error if we pass it
112 * to visit_type_int. confirm this.
114 v
= visitor_input_test_init(data
, "%f", DBL_MAX
);
116 visit_type_int(v
, &res
, NULL
, &err
);
121 static void test_visitor_in_bool(TestInputVisitorData
*data
,
128 v
= visitor_input_test_init(data
, "true");
130 visit_type_bool(v
, &res
, NULL
, &err
);
132 g_assert_cmpint(res
, ==, true);
135 static void test_visitor_in_number(TestInputVisitorData
*data
,
138 double res
= 0, value
= 3.14;
142 v
= visitor_input_test_init(data
, "%f", value
);
144 visit_type_number(v
, &res
, NULL
, &err
);
146 g_assert_cmpfloat(res
, ==, value
);
149 static void test_visitor_in_string(TestInputVisitorData
*data
,
152 char *res
= NULL
, *value
= (char *) "Q E M U";
156 v
= visitor_input_test_init(data
, "%s", value
);
158 visit_type_str(v
, &res
, NULL
, &err
);
160 g_assert_cmpstr(res
, ==, value
);
165 static void test_visitor_in_enum(TestInputVisitorData
*data
,
172 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
175 v
= visitor_input_test_init(data
, "%s", EnumOne_lookup
[i
]);
177 visit_type_EnumOne(v
, &res
, NULL
, &err
);
179 g_assert_cmpint(i
, ==, res
);
181 visitor_input_teardown(data
, NULL
);
188 typedef struct TestStruct
195 static void visit_type_TestStruct(Visitor
*v
, TestStruct
**obj
,
196 const char *name
, Error
**errp
)
200 visit_start_struct(v
, (void **)obj
, "TestStruct", name
, sizeof(TestStruct
),
205 visit_type_int(v
, &(*obj
)->integer
, "integer", &err
);
209 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", &err
);
213 visit_type_str(v
, &(*obj
)->string
, "string", &err
);
216 error_propagate(errp
, err
);
218 visit_end_struct(v
, &err
);
220 error_propagate(errp
, err
);
223 static void test_visitor_in_struct(TestInputVisitorData
*data
,
226 TestStruct
*p
= NULL
;
230 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
232 visit_type_TestStruct(v
, &p
, NULL
, &err
);
234 g_assert_cmpint(p
->integer
, ==, -42);
235 g_assert(p
->boolean
== true);
236 g_assert_cmpstr(p
->string
, ==, "foo");
242 static void check_and_free_str(char *str
, const char *cmp
)
244 g_assert_cmpstr(str
, ==, cmp
);
248 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
251 UserDefTwo
*udp
= NULL
;
255 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', "
256 "'dict1': { 'string1': 'string1', "
257 "'dict2': { 'userdef': { 'integer': 42, "
258 "'string': 'string' }, 'string': 'string2'}}}");
260 visit_type_UserDefTwo(v
, &udp
, NULL
, &err
);
263 check_and_free_str(udp
->string0
, "string0");
264 check_and_free_str(udp
->dict1
->string1
, "string1");
265 g_assert_cmpint(udp
->dict1
->dict2
->userdef
->integer
, ==, 42);
266 check_and_free_str(udp
->dict1
->dict2
->userdef
->string
, "string");
267 check_and_free_str(udp
->dict1
->dict2
->string
, "string2");
268 g_assert(udp
->dict1
->has_dict3
== false);
270 g_free(udp
->dict1
->dict2
->userdef
);
271 g_free(udp
->dict1
->dict2
);
276 static void test_visitor_in_list(TestInputVisitorData
*data
,
279 UserDefOneList
*item
, *head
= NULL
;
284 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
286 visit_type_UserDefOneList(v
, &head
, NULL
, &err
);
288 g_assert(head
!= NULL
);
290 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
293 snprintf(string
, sizeof(string
), "string%d", i
);
294 g_assert_cmpstr(item
->value
->string
, ==, string
);
295 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
298 qapi_free_UserDefOneList(head
);
301 static void test_visitor_in_any(TestInputVisitorData
*data
,
313 v
= visitor_input_test_init(data
, "-42");
314 visit_type_any(v
, &res
, NULL
, &err
);
316 qint
= qobject_to_qint(res
);
318 g_assert_cmpint(qint_get_int(qint
), ==, -42);
321 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
322 visit_type_any(v
, &res
, NULL
, &err
);
324 qdict
= qobject_to_qdict(res
);
325 g_assert(qdict
&& qdict_size(qdict
) == 3);
326 qobj
= qdict_get(qdict
, "integer");
328 qint
= qobject_to_qint(qobj
);
330 g_assert_cmpint(qint_get_int(qint
), ==, -42);
331 qobj
= qdict_get(qdict
, "boolean");
333 qbool
= qobject_to_qbool(qobj
);
335 g_assert(qbool_get_bool(qbool
) == true);
336 qobj
= qdict_get(qdict
, "string");
338 qstring
= qobject_to_qstring(qobj
);
340 g_assert_cmpstr(qstring_get_str(qstring
), ==, "foo");
344 static void test_visitor_in_union_flat(TestInputVisitorData
*data
,
349 UserDefFlatUnion
*tmp
;
350 UserDefUnionBase
*base
;
352 v
= visitor_input_test_init(data
,
353 "{ 'enum1': 'value1', "
356 "'boolean': true }");
358 visit_type_UserDefFlatUnion(v
, &tmp
, NULL
, &err
);
359 g_assert(err
== NULL
);
360 g_assert_cmpint(tmp
->enum1
, ==, ENUM_ONE_VALUE1
);
361 g_assert_cmpstr(tmp
->string
, ==, "str");
362 g_assert_cmpint(tmp
->integer
, ==, 41);
363 g_assert_cmpint(tmp
->u
.value1
->boolean
, ==, true);
365 base
= qapi_UserDefFlatUnion_base(tmp
);
366 g_assert(&base
->enum1
== &tmp
->enum1
);
368 qapi_free_UserDefFlatUnion(tmp
);
371 static void test_visitor_in_alternate(TestInputVisitorData
*data
,
376 UserDefAlternate
*tmp
;
378 v
= visitor_input_test_init(data
, "42");
379 visit_type_UserDefAlternate(v
, &tmp
, NULL
, &error_abort
);
380 g_assert_cmpint(tmp
->type
, ==, USER_DEF_ALTERNATE_KIND_I
);
381 g_assert_cmpint(tmp
->u
.i
, ==, 42);
382 qapi_free_UserDefAlternate(tmp
);
383 visitor_input_teardown(data
, NULL
);
385 v
= visitor_input_test_init(data
, "'string'");
386 visit_type_UserDefAlternate(v
, &tmp
, NULL
, &error_abort
);
387 g_assert_cmpint(tmp
->type
, ==, USER_DEF_ALTERNATE_KIND_S
);
388 g_assert_cmpstr(tmp
->u
.s
, ==, "string");
389 qapi_free_UserDefAlternate(tmp
);
390 visitor_input_teardown(data
, NULL
);
392 v
= visitor_input_test_init(data
, "false");
393 visit_type_UserDefAlternate(v
, &tmp
, NULL
, &err
);
397 qapi_free_UserDefAlternate(tmp
);
398 visitor_input_teardown(data
, NULL
);
401 static void test_visitor_in_alternate_number(TestInputVisitorData
*data
,
415 v
= visitor_input_test_init(data
, "42");
416 visit_type_AltStrBool(v
, &asb
, NULL
, &err
);
420 qapi_free_AltStrBool(asb
);
421 visitor_input_teardown(data
, NULL
);
423 /* FIXME: Order of alternate should not affect semantics; asn should
424 * parse the same as ans */
425 v
= visitor_input_test_init(data
, "42");
426 visit_type_AltStrNum(v
, &asn
, NULL
, &err
);
427 /* FIXME g_assert_cmpint(asn->type, == ALT_STR_NUM_KIND_N); */
428 /* FIXME g_assert_cmpfloat(asn->u.n, ==, 42); */
432 qapi_free_AltStrNum(asn
);
433 visitor_input_teardown(data
, NULL
);
435 v
= visitor_input_test_init(data
, "42");
436 visit_type_AltNumStr(v
, &ans
, NULL
, &error_abort
);
437 g_assert_cmpint(ans
->type
, ==, ALT_NUM_STR_KIND_N
);
438 g_assert_cmpfloat(ans
->u
.n
, ==, 42);
439 qapi_free_AltNumStr(ans
);
440 visitor_input_teardown(data
, NULL
);
442 v
= visitor_input_test_init(data
, "42");
443 visit_type_AltStrInt(v
, &asi
, NULL
, &error_abort
);
444 g_assert_cmpint(asi
->type
, ==, ALT_STR_INT_KIND_I
);
445 g_assert_cmpint(asi
->u
.i
, ==, 42);
446 qapi_free_AltStrInt(asi
);
447 visitor_input_teardown(data
, NULL
);
449 v
= visitor_input_test_init(data
, "42");
450 visit_type_AltIntNum(v
, &ain
, NULL
, &error_abort
);
451 g_assert_cmpint(ain
->type
, ==, ALT_INT_NUM_KIND_I
);
452 g_assert_cmpint(ain
->u
.i
, ==, 42);
453 qapi_free_AltIntNum(ain
);
454 visitor_input_teardown(data
, NULL
);
456 v
= visitor_input_test_init(data
, "42");
457 visit_type_AltNumInt(v
, &ani
, NULL
, &error_abort
);
458 g_assert_cmpint(ani
->type
, ==, ALT_NUM_INT_KIND_I
);
459 g_assert_cmpint(ani
->u
.i
, ==, 42);
460 qapi_free_AltNumInt(ani
);
461 visitor_input_teardown(data
, NULL
);
463 /* Parsing a double */
465 v
= visitor_input_test_init(data
, "42.5");
466 visit_type_AltStrBool(v
, &asb
, NULL
, &err
);
470 qapi_free_AltStrBool(asb
);
471 visitor_input_teardown(data
, NULL
);
473 v
= visitor_input_test_init(data
, "42.5");
474 visit_type_AltStrNum(v
, &asn
, NULL
, &error_abort
);
475 g_assert_cmpint(asn
->type
, ==, ALT_STR_NUM_KIND_N
);
476 g_assert_cmpfloat(asn
->u
.n
, ==, 42.5);
477 qapi_free_AltStrNum(asn
);
478 visitor_input_teardown(data
, NULL
);
480 v
= visitor_input_test_init(data
, "42.5");
481 visit_type_AltNumStr(v
, &ans
, NULL
, &error_abort
);
482 g_assert_cmpint(ans
->type
, ==, ALT_NUM_STR_KIND_N
);
483 g_assert_cmpfloat(ans
->u
.n
, ==, 42.5);
484 qapi_free_AltNumStr(ans
);
485 visitor_input_teardown(data
, NULL
);
487 v
= visitor_input_test_init(data
, "42.5");
488 visit_type_AltStrInt(v
, &asi
, NULL
, &err
);
492 qapi_free_AltStrInt(asi
);
493 visitor_input_teardown(data
, NULL
);
495 v
= visitor_input_test_init(data
, "42.5");
496 visit_type_AltIntNum(v
, &ain
, NULL
, &error_abort
);
497 g_assert_cmpint(ain
->type
, ==, ALT_INT_NUM_KIND_N
);
498 g_assert_cmpfloat(ain
->u
.n
, ==, 42.5);
499 qapi_free_AltIntNum(ain
);
500 visitor_input_teardown(data
, NULL
);
502 v
= visitor_input_test_init(data
, "42.5");
503 visit_type_AltNumInt(v
, &ani
, NULL
, &error_abort
);
504 g_assert_cmpint(ani
->type
, ==, ALT_NUM_INT_KIND_N
);
505 g_assert_cmpfloat(ani
->u
.n
, ==, 42.5);
506 qapi_free_AltNumInt(ani
);
507 visitor_input_teardown(data
, NULL
);
510 static void test_native_list_integer_helper(TestInputVisitorData
*data
,
512 UserDefNativeListUnionKind kind
)
514 UserDefNativeListUnion
*cvalue
= NULL
;
517 GString
*gstr_list
= g_string_new("");
518 GString
*gstr_union
= g_string_new("");
521 for (i
= 0; i
< 32; i
++) {
522 g_string_append_printf(gstr_list
, "%d", i
);
524 g_string_append(gstr_list
, ", ");
527 g_string_append_printf(gstr_union
, "{ 'type': '%s', 'data': [ %s ] }",
528 UserDefNativeListUnionKind_lookup
[kind
],
530 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
532 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
533 g_assert(err
== NULL
);
534 g_assert(cvalue
!= NULL
);
535 g_assert_cmpint(cvalue
->type
, ==, kind
);
538 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
: {
539 intList
*elem
= NULL
;
540 for (i
= 0, elem
= cvalue
->u
.integer
; elem
; elem
= elem
->next
, i
++) {
541 g_assert_cmpint(elem
->value
, ==, i
);
545 case USER_DEF_NATIVE_LIST_UNION_KIND_S8
: {
546 int8List
*elem
= NULL
;
547 for (i
= 0, elem
= cvalue
->u
.s8
; elem
; elem
= elem
->next
, i
++) {
548 g_assert_cmpint(elem
->value
, ==, i
);
552 case USER_DEF_NATIVE_LIST_UNION_KIND_S16
: {
553 int16List
*elem
= NULL
;
554 for (i
= 0, elem
= cvalue
->u
.s16
; elem
; elem
= elem
->next
, i
++) {
555 g_assert_cmpint(elem
->value
, ==, i
);
559 case USER_DEF_NATIVE_LIST_UNION_KIND_S32
: {
560 int32List
*elem
= NULL
;
561 for (i
= 0, elem
= cvalue
->u
.s32
; elem
; elem
= elem
->next
, i
++) {
562 g_assert_cmpint(elem
->value
, ==, i
);
566 case USER_DEF_NATIVE_LIST_UNION_KIND_S64
: {
567 int64List
*elem
= NULL
;
568 for (i
= 0, elem
= cvalue
->u
.s64
; elem
; elem
= elem
->next
, i
++) {
569 g_assert_cmpint(elem
->value
, ==, i
);
573 case USER_DEF_NATIVE_LIST_UNION_KIND_U8
: {
574 uint8List
*elem
= NULL
;
575 for (i
= 0, elem
= cvalue
->u
.u8
; elem
; elem
= elem
->next
, i
++) {
576 g_assert_cmpint(elem
->value
, ==, i
);
580 case USER_DEF_NATIVE_LIST_UNION_KIND_U16
: {
581 uint16List
*elem
= NULL
;
582 for (i
= 0, elem
= cvalue
->u
.u16
; elem
; elem
= elem
->next
, i
++) {
583 g_assert_cmpint(elem
->value
, ==, i
);
587 case USER_DEF_NATIVE_LIST_UNION_KIND_U32
: {
588 uint32List
*elem
= NULL
;
589 for (i
= 0, elem
= cvalue
->u
.u32
; elem
; elem
= elem
->next
, i
++) {
590 g_assert_cmpint(elem
->value
, ==, i
);
594 case USER_DEF_NATIVE_LIST_UNION_KIND_U64
: {
595 uint64List
*elem
= NULL
;
596 for (i
= 0, elem
= cvalue
->u
.u64
; elem
; elem
= elem
->next
, i
++) {
597 g_assert_cmpint(elem
->value
, ==, i
);
602 g_assert_not_reached();
605 g_string_free(gstr_union
, true);
606 g_string_free(gstr_list
, true);
607 qapi_free_UserDefNativeListUnion(cvalue
);
610 static void test_visitor_in_native_list_int(TestInputVisitorData
*data
,
613 test_native_list_integer_helper(data
, unused
,
614 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER
);
617 static void test_visitor_in_native_list_int8(TestInputVisitorData
*data
,
620 test_native_list_integer_helper(data
, unused
,
621 USER_DEF_NATIVE_LIST_UNION_KIND_S8
);
624 static void test_visitor_in_native_list_int16(TestInputVisitorData
*data
,
627 test_native_list_integer_helper(data
, unused
,
628 USER_DEF_NATIVE_LIST_UNION_KIND_S16
);
631 static void test_visitor_in_native_list_int32(TestInputVisitorData
*data
,
634 test_native_list_integer_helper(data
, unused
,
635 USER_DEF_NATIVE_LIST_UNION_KIND_S32
);
638 static void test_visitor_in_native_list_int64(TestInputVisitorData
*data
,
641 test_native_list_integer_helper(data
, unused
,
642 USER_DEF_NATIVE_LIST_UNION_KIND_S64
);
645 static void test_visitor_in_native_list_uint8(TestInputVisitorData
*data
,
648 test_native_list_integer_helper(data
, unused
,
649 USER_DEF_NATIVE_LIST_UNION_KIND_U8
);
652 static void test_visitor_in_native_list_uint16(TestInputVisitorData
*data
,
655 test_native_list_integer_helper(data
, unused
,
656 USER_DEF_NATIVE_LIST_UNION_KIND_U16
);
659 static void test_visitor_in_native_list_uint32(TestInputVisitorData
*data
,
662 test_native_list_integer_helper(data
, unused
,
663 USER_DEF_NATIVE_LIST_UNION_KIND_U32
);
666 static void test_visitor_in_native_list_uint64(TestInputVisitorData
*data
,
669 test_native_list_integer_helper(data
, unused
,
670 USER_DEF_NATIVE_LIST_UNION_KIND_U64
);
673 static void test_visitor_in_native_list_bool(TestInputVisitorData
*data
,
676 UserDefNativeListUnion
*cvalue
= NULL
;
677 boolList
*elem
= NULL
;
680 GString
*gstr_list
= g_string_new("");
681 GString
*gstr_union
= g_string_new("");
684 for (i
= 0; i
< 32; i
++) {
685 g_string_append_printf(gstr_list
, "%s",
686 (i
% 3 == 0) ? "true" : "false");
688 g_string_append(gstr_list
, ", ");
691 g_string_append_printf(gstr_union
, "{ 'type': 'boolean', 'data': [ %s ] }",
693 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
695 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
696 g_assert(err
== NULL
);
697 g_assert(cvalue
!= NULL
);
698 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN
);
700 for (i
= 0, elem
= cvalue
->u
.boolean
; elem
; elem
= elem
->next
, i
++) {
701 g_assert_cmpint(elem
->value
, ==, (i
% 3 == 0) ? 1 : 0);
704 g_string_free(gstr_union
, true);
705 g_string_free(gstr_list
, true);
706 qapi_free_UserDefNativeListUnion(cvalue
);
709 static void test_visitor_in_native_list_string(TestInputVisitorData
*data
,
712 UserDefNativeListUnion
*cvalue
= NULL
;
713 strList
*elem
= NULL
;
716 GString
*gstr_list
= g_string_new("");
717 GString
*gstr_union
= g_string_new("");
720 for (i
= 0; i
< 32; i
++) {
721 g_string_append_printf(gstr_list
, "'%d'", i
);
723 g_string_append(gstr_list
, ", ");
726 g_string_append_printf(gstr_union
, "{ 'type': 'string', 'data': [ %s ] }",
728 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
730 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
731 g_assert(err
== NULL
);
732 g_assert(cvalue
!= NULL
);
733 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING
);
735 for (i
= 0, elem
= cvalue
->u
.string
; elem
; elem
= elem
->next
, i
++) {
737 sprintf(str
, "%d", i
);
738 g_assert_cmpstr(elem
->value
, ==, str
);
741 g_string_free(gstr_union
, true);
742 g_string_free(gstr_list
, true);
743 qapi_free_UserDefNativeListUnion(cvalue
);
746 #define DOUBLE_STR_MAX 16
748 static void test_visitor_in_native_list_number(TestInputVisitorData
*data
,
751 UserDefNativeListUnion
*cvalue
= NULL
;
752 numberList
*elem
= NULL
;
755 GString
*gstr_list
= g_string_new("");
756 GString
*gstr_union
= g_string_new("");
759 for (i
= 0; i
< 32; i
++) {
760 g_string_append_printf(gstr_list
, "%f", (double)i
/ 3);
762 g_string_append(gstr_list
, ", ");
765 g_string_append_printf(gstr_union
, "{ 'type': 'number', 'data': [ %s ] }",
767 v
= visitor_input_test_init_raw(data
, gstr_union
->str
);
769 visit_type_UserDefNativeListUnion(v
, &cvalue
, NULL
, &err
);
770 g_assert(err
== NULL
);
771 g_assert(cvalue
!= NULL
);
772 g_assert_cmpint(cvalue
->type
, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER
);
774 for (i
= 0, elem
= cvalue
->u
.number
; elem
; elem
= elem
->next
, i
++) {
775 GString
*double_expected
= g_string_new("");
776 GString
*double_actual
= g_string_new("");
778 g_string_printf(double_expected
, "%.6f", (double)i
/ 3);
779 g_string_printf(double_actual
, "%.6f", elem
->value
);
780 g_assert_cmpstr(double_expected
->str
, ==, double_actual
->str
);
782 g_string_free(double_expected
, true);
783 g_string_free(double_actual
, true);
786 g_string_free(gstr_union
, true);
787 g_string_free(gstr_list
, true);
788 qapi_free_UserDefNativeListUnion(cvalue
);
791 static void input_visitor_test_add(const char *testpath
,
792 TestInputVisitorData
*data
,
793 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
795 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
796 visitor_input_teardown
);
799 static void test_visitor_in_errors(TestInputVisitorData
*data
,
802 TestStruct
*p
= NULL
;
806 v
= visitor_input_test_init(data
, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
808 visit_type_TestStruct(v
, &p
, NULL
, &err
);
810 /* FIXME - a failed parse should not leave a partially-allocated p
811 * for us to clean up; this could cause callers to leak memory. */
812 g_assert(p
->string
== NULL
);
819 int main(int argc
, char **argv
)
821 TestInputVisitorData in_visitor_data
;
823 g_test_init(&argc
, &argv
, NULL
);
825 input_visitor_test_add("/visitor/input/int",
826 &in_visitor_data
, test_visitor_in_int
);
827 input_visitor_test_add("/visitor/input/int_overflow",
828 &in_visitor_data
, test_visitor_in_int_overflow
);
829 input_visitor_test_add("/visitor/input/bool",
830 &in_visitor_data
, test_visitor_in_bool
);
831 input_visitor_test_add("/visitor/input/number",
832 &in_visitor_data
, test_visitor_in_number
);
833 input_visitor_test_add("/visitor/input/string",
834 &in_visitor_data
, test_visitor_in_string
);
835 input_visitor_test_add("/visitor/input/enum",
836 &in_visitor_data
, test_visitor_in_enum
);
837 input_visitor_test_add("/visitor/input/struct",
838 &in_visitor_data
, test_visitor_in_struct
);
839 input_visitor_test_add("/visitor/input/struct-nested",
840 &in_visitor_data
, test_visitor_in_struct_nested
);
841 input_visitor_test_add("/visitor/input/list",
842 &in_visitor_data
, test_visitor_in_list
);
843 input_visitor_test_add("/visitor/input/any",
844 &in_visitor_data
, test_visitor_in_any
);
845 input_visitor_test_add("/visitor/input/union-flat",
846 &in_visitor_data
, test_visitor_in_union_flat
);
847 input_visitor_test_add("/visitor/input/alternate",
848 &in_visitor_data
, test_visitor_in_alternate
);
849 input_visitor_test_add("/visitor/input/errors",
850 &in_visitor_data
, test_visitor_in_errors
);
851 input_visitor_test_add("/visitor/input/alternate-number",
852 &in_visitor_data
, test_visitor_in_alternate_number
);
853 input_visitor_test_add("/visitor/input/native_list/int",
855 test_visitor_in_native_list_int
);
856 input_visitor_test_add("/visitor/input/native_list/int8",
858 test_visitor_in_native_list_int8
);
859 input_visitor_test_add("/visitor/input/native_list/int16",
861 test_visitor_in_native_list_int16
);
862 input_visitor_test_add("/visitor/input/native_list/int32",
864 test_visitor_in_native_list_int32
);
865 input_visitor_test_add("/visitor/input/native_list/int64",
867 test_visitor_in_native_list_int64
);
868 input_visitor_test_add("/visitor/input/native_list/uint8",
870 test_visitor_in_native_list_uint8
);
871 input_visitor_test_add("/visitor/input/native_list/uint16",
873 test_visitor_in_native_list_uint16
);
874 input_visitor_test_add("/visitor/input/native_list/uint32",
876 test_visitor_in_native_list_uint32
);
877 input_visitor_test_add("/visitor/input/native_list/uint64",
879 test_visitor_in_native_list_uint64
);
880 input_visitor_test_add("/visitor/input/native_list/bool",
881 &in_visitor_data
, test_visitor_in_native_list_bool
);
882 input_visitor_test_add("/visitor/input/native_list/str",
884 test_visitor_in_native_list_string
);
885 input_visitor_test_add("/visitor/input/native_list/number",
887 test_visitor_in_native_list_number
);