target-arm: kvm - implement software breakpoints
[qemu/ar7.git] / tests / test-qmp-input-visitor.c
blobb4a5bee6a55df6dad3e2cbf45ac8923e6de2a3a5
1 /*
2 * QMP Input Visitor unit-tests.
4 * Copyright (C) 2011, 2015 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 <glib.h>
14 #include <stdarg.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 {
23 QObject *obj;
24 QmpInputVisitor *qiv;
25 } TestInputVisitorData;
27 static void visitor_input_teardown(TestInputVisitorData *data,
28 const void *unused)
30 qobject_decref(data->obj);
31 data->obj = NULL;
33 if (data->qiv) {
34 qmp_input_visitor_cleanup(data->qiv);
35 data->qiv = NULL;
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,
44 va_list *ap)
46 Visitor *v;
48 visitor_input_teardown(data, NULL);
50 data->obj = qobject_from_jsonv(json_string, ap);
51 g_assert(data->obj);
53 data->qiv = qmp_input_visitor_new(data->obj);
54 g_assert(data->qiv);
56 v = qmp_input_get_visitor(data->qiv);
57 g_assert(v);
59 return v;
62 static GCC_FMT_ATTR(2, 3)
63 Visitor *visitor_input_test_init(TestInputVisitorData *data,
64 const char *json_string, ...)
66 Visitor *v;
67 va_list ap;
69 va_start(ap, json_string);
70 v = visitor_input_test_init_internal(data, json_string, &ap);
71 va_end(ap);
72 return v;
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
80 * string)
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,
89 const void *unused)
91 int64_t res = 0, value = -42;
92 Visitor *v;
94 v = visitor_input_test_init(data, "%" PRId64, value);
96 visit_type_int(v, &res, NULL, &error_abort);
97 g_assert_cmpint(res, ==, value);
100 static void test_visitor_in_int_overflow(TestInputVisitorData *data,
101 const void *unused)
103 int64_t res = 0;
104 Error *err = NULL;
105 Visitor *v;
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, &res, NULL, &err);
114 error_free_or_abort(&err);
117 static void test_visitor_in_bool(TestInputVisitorData *data,
118 const void *unused)
120 bool res = false;
121 Visitor *v;
123 v = visitor_input_test_init(data, "true");
125 visit_type_bool(v, &res, NULL, &error_abort);
126 g_assert_cmpint(res, ==, true);
129 static void test_visitor_in_number(TestInputVisitorData *data,
130 const void *unused)
132 double res = 0, value = 3.14;
133 Visitor *v;
135 v = visitor_input_test_init(data, "%f", value);
137 visit_type_number(v, &res, NULL, &error_abort);
138 g_assert_cmpfloat(res, ==, value);
141 static void test_visitor_in_string(TestInputVisitorData *data,
142 const void *unused)
144 char *res = NULL, *value = (char *) "Q E M U";
145 Visitor *v;
147 v = visitor_input_test_init(data, "%s", value);
149 visit_type_str(v, &res, NULL, &error_abort);
150 g_assert_cmpstr(res, ==, value);
152 g_free(res);
155 static void test_visitor_in_enum(TestInputVisitorData *data,
156 const void *unused)
158 Visitor *v;
159 EnumOne i;
161 for (i = 0; EnumOne_lookup[i]; i++) {
162 EnumOne res = -1;
164 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
166 visit_type_EnumOne(v, &res, NULL, &error_abort);
167 g_assert_cmpint(i, ==, res);
172 static void test_visitor_in_struct(TestInputVisitorData *data,
173 const void *unused)
175 TestStruct *p = NULL;
176 Visitor *v;
178 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
180 visit_type_TestStruct(v, &p, NULL, &error_abort);
181 g_assert_cmpint(p->integer, ==, -42);
182 g_assert(p->boolean == true);
183 g_assert_cmpstr(p->string, ==, "foo");
185 g_free(p->string);
186 g_free(p);
189 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
190 const void *unused)
192 UserDefTwo *udp = NULL;
193 Visitor *v;
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, &udp, NULL, &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,
213 const void *unused)
215 UserDefOneList *item, *head = NULL;
216 Visitor *v;
217 int i;
219 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
221 visit_type_UserDefOneList(v, &head, NULL, &error_abort);
222 g_assert(head != NULL);
224 for (i = 0, item = head; item; item = item->next, i++) {
225 char string[12];
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);
233 head = NULL;
235 /* An empty list is valid */
236 v = visitor_input_test_init(data, "[]");
237 visit_type_UserDefOneList(v, &head, NULL, &error_abort);
238 g_assert(!head);
241 static void test_visitor_in_any(TestInputVisitorData *data,
242 const void *unused)
244 QObject *res = NULL;
245 Visitor *v;
246 QInt *qint;
247 QBool *qbool;
248 QString *qstring;
249 QDict *qdict;
250 QObject *qobj;
252 v = visitor_input_test_init(data, "-42");
253 visit_type_any(v, &res, NULL, &error_abort);
254 qint = qobject_to_qint(res);
255 g_assert(qint);
256 g_assert_cmpint(qint_get_int(qint), ==, -42);
257 qobject_decref(res);
259 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
260 visit_type_any(v, &res, NULL, &error_abort);
261 qdict = qobject_to_qdict(res);
262 g_assert(qdict && qdict_size(qdict) == 3);
263 qobj = qdict_get(qdict, "integer");
264 g_assert(qobj);
265 qint = qobject_to_qint(qobj);
266 g_assert(qint);
267 g_assert_cmpint(qint_get_int(qint), ==, -42);
268 qobj = qdict_get(qdict, "boolean");
269 g_assert(qobj);
270 qbool = qobject_to_qbool(qobj);
271 g_assert(qbool);
272 g_assert(qbool_get_bool(qbool) == true);
273 qobj = qdict_get(qdict, "string");
274 g_assert(qobj);
275 qstring = qobject_to_qstring(qobj);
276 g_assert(qstring);
277 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
278 qobject_decref(res);
281 static void test_visitor_in_union_flat(TestInputVisitorData *data,
282 const void *unused)
284 Visitor *v;
285 UserDefFlatUnion *tmp;
286 UserDefUnionBase *base;
288 v = visitor_input_test_init(data,
289 "{ 'enum1': 'value1', "
290 "'integer': 41, "
291 "'string': 'str', "
292 "'boolean': true }");
294 visit_type_UserDefFlatUnion(v, &tmp, NULL, &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,
307 const void *unused)
309 Visitor *v;
310 Error *err = NULL;
311 UserDefAlternate *tmp;
313 v = visitor_input_test_init(data, "42");
314 visit_type_UserDefAlternate(v, &tmp, NULL, &error_abort);
315 g_assert_cmpint(tmp->type, ==, QTYPE_QINT);
316 g_assert_cmpint(tmp->u.i, ==, 42);
317 qapi_free_UserDefAlternate(tmp);
319 v = visitor_input_test_init(data, "'string'");
320 visit_type_UserDefAlternate(v, &tmp, NULL, &error_abort);
321 g_assert_cmpint(tmp->type, ==, QTYPE_QSTRING);
322 g_assert_cmpstr(tmp->u.s, ==, "string");
323 qapi_free_UserDefAlternate(tmp);
325 v = visitor_input_test_init(data, "false");
326 visit_type_UserDefAlternate(v, &tmp, NULL, &err);
327 error_free_or_abort(&err);
328 qapi_free_UserDefAlternate(tmp);
331 static void test_visitor_in_alternate_number(TestInputVisitorData *data,
332 const void *unused)
334 Visitor *v;
335 Error *err = NULL;
336 AltStrBool *asb;
337 AltStrNum *asn;
338 AltNumStr *ans;
339 AltStrInt *asi;
340 AltIntNum *ain;
341 AltNumInt *ani;
343 /* Parsing an int */
345 v = visitor_input_test_init(data, "42");
346 visit_type_AltStrBool(v, &asb, NULL, &err);
347 error_free_or_abort(&err);
348 qapi_free_AltStrBool(asb);
350 v = visitor_input_test_init(data, "42");
351 visit_type_AltStrNum(v, &asn, NULL, &error_abort);
352 g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT);
353 g_assert_cmpfloat(asn->u.n, ==, 42);
354 qapi_free_AltStrNum(asn);
356 v = visitor_input_test_init(data, "42");
357 visit_type_AltNumStr(v, &ans, NULL, &error_abort);
358 g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT);
359 g_assert_cmpfloat(ans->u.n, ==, 42);
360 qapi_free_AltNumStr(ans);
362 v = visitor_input_test_init(data, "42");
363 visit_type_AltStrInt(v, &asi, NULL, &error_abort);
364 g_assert_cmpint(asi->type, ==, QTYPE_QINT);
365 g_assert_cmpint(asi->u.i, ==, 42);
366 qapi_free_AltStrInt(asi);
368 v = visitor_input_test_init(data, "42");
369 visit_type_AltIntNum(v, &ain, NULL, &error_abort);
370 g_assert_cmpint(ain->type, ==, QTYPE_QINT);
371 g_assert_cmpint(ain->u.i, ==, 42);
372 qapi_free_AltIntNum(ain);
374 v = visitor_input_test_init(data, "42");
375 visit_type_AltNumInt(v, &ani, NULL, &error_abort);
376 g_assert_cmpint(ani->type, ==, QTYPE_QINT);
377 g_assert_cmpint(ani->u.i, ==, 42);
378 qapi_free_AltNumInt(ani);
380 /* Parsing a double */
382 v = visitor_input_test_init(data, "42.5");
383 visit_type_AltStrBool(v, &asb, NULL, &err);
384 error_free_or_abort(&err);
385 qapi_free_AltStrBool(asb);
387 v = visitor_input_test_init(data, "42.5");
388 visit_type_AltStrNum(v, &asn, NULL, &error_abort);
389 g_assert_cmpint(asn->type, ==, QTYPE_QFLOAT);
390 g_assert_cmpfloat(asn->u.n, ==, 42.5);
391 qapi_free_AltStrNum(asn);
393 v = visitor_input_test_init(data, "42.5");
394 visit_type_AltNumStr(v, &ans, NULL, &error_abort);
395 g_assert_cmpint(ans->type, ==, QTYPE_QFLOAT);
396 g_assert_cmpfloat(ans->u.n, ==, 42.5);
397 qapi_free_AltNumStr(ans);
399 v = visitor_input_test_init(data, "42.5");
400 visit_type_AltStrInt(v, &asi, NULL, &err);
401 error_free_or_abort(&err);
402 qapi_free_AltStrInt(asi);
404 v = visitor_input_test_init(data, "42.5");
405 visit_type_AltIntNum(v, &ain, NULL, &error_abort);
406 g_assert_cmpint(ain->type, ==, QTYPE_QFLOAT);
407 g_assert_cmpfloat(ain->u.n, ==, 42.5);
408 qapi_free_AltIntNum(ain);
410 v = visitor_input_test_init(data, "42.5");
411 visit_type_AltNumInt(v, &ani, NULL, &error_abort);
412 g_assert_cmpint(ani->type, ==, QTYPE_QFLOAT);
413 g_assert_cmpfloat(ani->u.n, ==, 42.5);
414 qapi_free_AltNumInt(ani);
417 static void test_native_list_integer_helper(TestInputVisitorData *data,
418 const void *unused,
419 UserDefNativeListUnionKind kind)
421 UserDefNativeListUnion *cvalue = NULL;
422 Visitor *v;
423 GString *gstr_list = g_string_new("");
424 GString *gstr_union = g_string_new("");
425 int i;
427 for (i = 0; i < 32; i++) {
428 g_string_append_printf(gstr_list, "%d", i);
429 if (i != 31) {
430 g_string_append(gstr_list, ", ");
433 g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }",
434 UserDefNativeListUnionKind_lookup[kind],
435 gstr_list->str);
436 v = visitor_input_test_init_raw(data, gstr_union->str);
438 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &error_abort);
439 g_assert(cvalue != NULL);
440 g_assert_cmpint(cvalue->type, ==, kind);
442 switch (kind) {
443 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
444 intList *elem = NULL;
445 for (i = 0, elem = cvalue->u.integer; elem; elem = elem->next, i++) {
446 g_assert_cmpint(elem->value, ==, i);
448 break;
450 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
451 int8List *elem = NULL;
452 for (i = 0, elem = cvalue->u.s8; elem; elem = elem->next, i++) {
453 g_assert_cmpint(elem->value, ==, i);
455 break;
457 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
458 int16List *elem = NULL;
459 for (i = 0, elem = cvalue->u.s16; elem; elem = elem->next, i++) {
460 g_assert_cmpint(elem->value, ==, i);
462 break;
464 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
465 int32List *elem = NULL;
466 for (i = 0, elem = cvalue->u.s32; elem; elem = elem->next, i++) {
467 g_assert_cmpint(elem->value, ==, i);
469 break;
471 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
472 int64List *elem = NULL;
473 for (i = 0, elem = cvalue->u.s64; elem; elem = elem->next, i++) {
474 g_assert_cmpint(elem->value, ==, i);
476 break;
478 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
479 uint8List *elem = NULL;
480 for (i = 0, elem = cvalue->u.u8; elem; elem = elem->next, i++) {
481 g_assert_cmpint(elem->value, ==, i);
483 break;
485 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
486 uint16List *elem = NULL;
487 for (i = 0, elem = cvalue->u.u16; elem; elem = elem->next, i++) {
488 g_assert_cmpint(elem->value, ==, i);
490 break;
492 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
493 uint32List *elem = NULL;
494 for (i = 0, elem = cvalue->u.u32; elem; elem = elem->next, i++) {
495 g_assert_cmpint(elem->value, ==, i);
497 break;
499 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
500 uint64List *elem = NULL;
501 for (i = 0, elem = cvalue->u.u64; elem; elem = elem->next, i++) {
502 g_assert_cmpint(elem->value, ==, i);
504 break;
506 default:
507 g_assert_not_reached();
510 g_string_free(gstr_union, true);
511 g_string_free(gstr_list, true);
512 qapi_free_UserDefNativeListUnion(cvalue);
515 static void test_visitor_in_native_list_int(TestInputVisitorData *data,
516 const void *unused)
518 test_native_list_integer_helper(data, unused,
519 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
522 static void test_visitor_in_native_list_int8(TestInputVisitorData *data,
523 const void *unused)
525 test_native_list_integer_helper(data, unused,
526 USER_DEF_NATIVE_LIST_UNION_KIND_S8);
529 static void test_visitor_in_native_list_int16(TestInputVisitorData *data,
530 const void *unused)
532 test_native_list_integer_helper(data, unused,
533 USER_DEF_NATIVE_LIST_UNION_KIND_S16);
536 static void test_visitor_in_native_list_int32(TestInputVisitorData *data,
537 const void *unused)
539 test_native_list_integer_helper(data, unused,
540 USER_DEF_NATIVE_LIST_UNION_KIND_S32);
543 static void test_visitor_in_native_list_int64(TestInputVisitorData *data,
544 const void *unused)
546 test_native_list_integer_helper(data, unused,
547 USER_DEF_NATIVE_LIST_UNION_KIND_S64);
550 static void test_visitor_in_native_list_uint8(TestInputVisitorData *data,
551 const void *unused)
553 test_native_list_integer_helper(data, unused,
554 USER_DEF_NATIVE_LIST_UNION_KIND_U8);
557 static void test_visitor_in_native_list_uint16(TestInputVisitorData *data,
558 const void *unused)
560 test_native_list_integer_helper(data, unused,
561 USER_DEF_NATIVE_LIST_UNION_KIND_U16);
564 static void test_visitor_in_native_list_uint32(TestInputVisitorData *data,
565 const void *unused)
567 test_native_list_integer_helper(data, unused,
568 USER_DEF_NATIVE_LIST_UNION_KIND_U32);
571 static void test_visitor_in_native_list_uint64(TestInputVisitorData *data,
572 const void *unused)
574 test_native_list_integer_helper(data, unused,
575 USER_DEF_NATIVE_LIST_UNION_KIND_U64);
578 static void test_visitor_in_native_list_bool(TestInputVisitorData *data,
579 const void *unused)
581 UserDefNativeListUnion *cvalue = NULL;
582 boolList *elem = NULL;
583 Visitor *v;
584 GString *gstr_list = g_string_new("");
585 GString *gstr_union = g_string_new("");
586 int i;
588 for (i = 0; i < 32; i++) {
589 g_string_append_printf(gstr_list, "%s",
590 (i % 3 == 0) ? "true" : "false");
591 if (i != 31) {
592 g_string_append(gstr_list, ", ");
595 g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }",
596 gstr_list->str);
597 v = visitor_input_test_init_raw(data, gstr_union->str);
599 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &error_abort);
600 g_assert(cvalue != NULL);
601 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
603 for (i = 0, elem = cvalue->u.boolean; elem; elem = elem->next, i++) {
604 g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0);
607 g_string_free(gstr_union, true);
608 g_string_free(gstr_list, true);
609 qapi_free_UserDefNativeListUnion(cvalue);
612 static void test_visitor_in_native_list_string(TestInputVisitorData *data,
613 const void *unused)
615 UserDefNativeListUnion *cvalue = NULL;
616 strList *elem = NULL;
617 Visitor *v;
618 GString *gstr_list = g_string_new("");
619 GString *gstr_union = g_string_new("");
620 int i;
622 for (i = 0; i < 32; i++) {
623 g_string_append_printf(gstr_list, "'%d'", i);
624 if (i != 31) {
625 g_string_append(gstr_list, ", ");
628 g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }",
629 gstr_list->str);
630 v = visitor_input_test_init_raw(data, gstr_union->str);
632 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &error_abort);
633 g_assert(cvalue != NULL);
634 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
636 for (i = 0, elem = cvalue->u.string; elem; elem = elem->next, i++) {
637 gchar str[8];
638 sprintf(str, "%d", i);
639 g_assert_cmpstr(elem->value, ==, str);
642 g_string_free(gstr_union, true);
643 g_string_free(gstr_list, true);
644 qapi_free_UserDefNativeListUnion(cvalue);
647 #define DOUBLE_STR_MAX 16
649 static void test_visitor_in_native_list_number(TestInputVisitorData *data,
650 const void *unused)
652 UserDefNativeListUnion *cvalue = NULL;
653 numberList *elem = NULL;
654 Visitor *v;
655 GString *gstr_list = g_string_new("");
656 GString *gstr_union = g_string_new("");
657 int i;
659 for (i = 0; i < 32; i++) {
660 g_string_append_printf(gstr_list, "%f", (double)i / 3);
661 if (i != 31) {
662 g_string_append(gstr_list, ", ");
665 g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }",
666 gstr_list->str);
667 v = visitor_input_test_init_raw(data, gstr_union->str);
669 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &error_abort);
670 g_assert(cvalue != NULL);
671 g_assert_cmpint(cvalue->type, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
673 for (i = 0, elem = cvalue->u.number; elem; elem = elem->next, i++) {
674 GString *double_expected = g_string_new("");
675 GString *double_actual = g_string_new("");
677 g_string_printf(double_expected, "%.6f", (double)i / 3);
678 g_string_printf(double_actual, "%.6f", elem->value);
679 g_assert_cmpstr(double_expected->str, ==, double_actual->str);
681 g_string_free(double_expected, true);
682 g_string_free(double_actual, true);
685 g_string_free(gstr_union, true);
686 g_string_free(gstr_list, true);
687 qapi_free_UserDefNativeListUnion(cvalue);
690 static void input_visitor_test_add(const char *testpath,
691 TestInputVisitorData *data,
692 void (*test_func)(TestInputVisitorData *data, const void *user_data))
694 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
695 visitor_input_teardown);
698 static void test_visitor_in_errors(TestInputVisitorData *data,
699 const void *unused)
701 TestStruct *p = NULL;
702 Error *err = NULL;
703 Visitor *v;
704 strList *q = NULL;
706 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', "
707 "'string': -42 }");
709 visit_type_TestStruct(v, &p, NULL, &err);
710 error_free_or_abort(&err);
711 /* FIXME - a failed parse should not leave a partially-allocated p
712 * for us to clean up; this could cause callers to leak memory. */
713 g_assert(p->string == NULL);
715 g_free(p->string);
716 g_free(p);
718 v = visitor_input_test_init(data, "[ '1', '2', false, '3' ]");
719 visit_type_strList(v, &q, NULL, &err);
720 error_free_or_abort(&err);
721 assert(q);
722 qapi_free_strList(q);
725 static void test_visitor_in_wrong_type(TestInputVisitorData *data,
726 const void *unused)
728 TestStruct *p = NULL;
729 Visitor *v;
730 strList *q = NULL;
731 int64_t i;
732 Error *err = NULL;
734 /* Make sure arrays and structs cannot be confused */
736 v = visitor_input_test_init(data, "[]");
737 visit_type_TestStruct(v, &p, NULL, &err);
738 error_free_or_abort(&err);
739 g_assert(!p);
741 v = visitor_input_test_init(data, "{}");
742 visit_type_strList(v, &q, NULL, &err);
743 error_free_or_abort(&err);
744 assert(!q);
746 /* Make sure primitives and struct cannot be confused */
748 v = visitor_input_test_init(data, "1");
749 visit_type_TestStruct(v, &p, NULL, &err);
750 error_free_or_abort(&err);
751 g_assert(!p);
753 v = visitor_input_test_init(data, "{}");
754 visit_type_int(v, &i, NULL, &err);
755 error_free_or_abort(&err);
757 /* Make sure primitives and arrays cannot be confused */
759 v = visitor_input_test_init(data, "1");
760 visit_type_strList(v, &q, NULL, &err);
761 error_free_or_abort(&err);
762 assert(!q);
764 v = visitor_input_test_init(data, "[]");
765 visit_type_int(v, &i, NULL, &err);
766 error_free_or_abort(&err);
769 int main(int argc, char **argv)
771 TestInputVisitorData in_visitor_data;
773 g_test_init(&argc, &argv, NULL);
775 input_visitor_test_add("/visitor/input/int",
776 &in_visitor_data, test_visitor_in_int);
777 input_visitor_test_add("/visitor/input/int_overflow",
778 &in_visitor_data, test_visitor_in_int_overflow);
779 input_visitor_test_add("/visitor/input/bool",
780 &in_visitor_data, test_visitor_in_bool);
781 input_visitor_test_add("/visitor/input/number",
782 &in_visitor_data, test_visitor_in_number);
783 input_visitor_test_add("/visitor/input/string",
784 &in_visitor_data, test_visitor_in_string);
785 input_visitor_test_add("/visitor/input/enum",
786 &in_visitor_data, test_visitor_in_enum);
787 input_visitor_test_add("/visitor/input/struct",
788 &in_visitor_data, test_visitor_in_struct);
789 input_visitor_test_add("/visitor/input/struct-nested",
790 &in_visitor_data, test_visitor_in_struct_nested);
791 input_visitor_test_add("/visitor/input/list",
792 &in_visitor_data, test_visitor_in_list);
793 input_visitor_test_add("/visitor/input/any",
794 &in_visitor_data, test_visitor_in_any);
795 input_visitor_test_add("/visitor/input/union-flat",
796 &in_visitor_data, test_visitor_in_union_flat);
797 input_visitor_test_add("/visitor/input/alternate",
798 &in_visitor_data, test_visitor_in_alternate);
799 input_visitor_test_add("/visitor/input/errors",
800 &in_visitor_data, test_visitor_in_errors);
801 input_visitor_test_add("/visitor/input/wrong-type",
802 &in_visitor_data, test_visitor_in_wrong_type);
803 input_visitor_test_add("/visitor/input/alternate-number",
804 &in_visitor_data, test_visitor_in_alternate_number);
805 input_visitor_test_add("/visitor/input/native_list/int",
806 &in_visitor_data,
807 test_visitor_in_native_list_int);
808 input_visitor_test_add("/visitor/input/native_list/int8",
809 &in_visitor_data,
810 test_visitor_in_native_list_int8);
811 input_visitor_test_add("/visitor/input/native_list/int16",
812 &in_visitor_data,
813 test_visitor_in_native_list_int16);
814 input_visitor_test_add("/visitor/input/native_list/int32",
815 &in_visitor_data,
816 test_visitor_in_native_list_int32);
817 input_visitor_test_add("/visitor/input/native_list/int64",
818 &in_visitor_data,
819 test_visitor_in_native_list_int64);
820 input_visitor_test_add("/visitor/input/native_list/uint8",
821 &in_visitor_data,
822 test_visitor_in_native_list_uint8);
823 input_visitor_test_add("/visitor/input/native_list/uint16",
824 &in_visitor_data,
825 test_visitor_in_native_list_uint16);
826 input_visitor_test_add("/visitor/input/native_list/uint32",
827 &in_visitor_data,
828 test_visitor_in_native_list_uint32);
829 input_visitor_test_add("/visitor/input/native_list/uint64",
830 &in_visitor_data,
831 test_visitor_in_native_list_uint64);
832 input_visitor_test_add("/visitor/input/native_list/bool",
833 &in_visitor_data, test_visitor_in_native_list_bool);
834 input_visitor_test_add("/visitor/input/native_list/str",
835 &in_visitor_data,
836 test_visitor_in_native_list_string);
837 input_visitor_test_add("/visitor/input/native_list/number",
838 &in_visitor_data,
839 test_visitor_in_native_list_number);
841 g_test_run();
843 return 0;