Merge remote-tracking branch 'remotes/stefanha/tags/block-pull-request' into staging
[qemu/ar7.git] / tests / test-qmp-input-visitor.c
blob8941963c8dca58106b94cbcd2331545a89db090b
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 /* 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
41 int main()) */
42 static GCC_FMT_ATTR(2, 3)
43 Visitor *visitor_input_test_init(TestInputVisitorData *data,
44 const char *json_string, ...)
46 Visitor *v;
47 va_list ap;
49 va_start(ap, json_string);
50 data->obj = qobject_from_jsonv(json_string, &ap);
51 va_end(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);
59 g_assert(v != NULL);
61 return v;
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
69 * string)
71 static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
72 const char *json_string)
74 Visitor *v;
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);
84 g_assert(v != NULL);
86 return v;
89 static void test_visitor_in_int(TestInputVisitorData *data,
90 const void *unused)
92 int64_t res = 0, value = -42;
93 Error *err = NULL;
94 Visitor *v;
96 v = visitor_input_test_init(data, "%" PRId64, value);
98 visit_type_int(v, &res, NULL, &err);
99 g_assert(!err);
100 g_assert_cmpint(res, ==, value);
103 static void test_visitor_in_int_overflow(TestInputVisitorData *data,
104 const void *unused)
106 int64_t res = 0;
107 Error *err = NULL;
108 Visitor *v;
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);
117 g_assert(err);
118 error_free(err);
121 static void test_visitor_in_bool(TestInputVisitorData *data,
122 const void *unused)
124 Error *err = NULL;
125 bool res = false;
126 Visitor *v;
128 v = visitor_input_test_init(data, "true");
130 visit_type_bool(v, &res, NULL, &err);
131 g_assert(!err);
132 g_assert_cmpint(res, ==, true);
135 static void test_visitor_in_number(TestInputVisitorData *data,
136 const void *unused)
138 double res = 0, value = 3.14;
139 Error *err = NULL;
140 Visitor *v;
142 v = visitor_input_test_init(data, "%f", value);
144 visit_type_number(v, &res, NULL, &err);
145 g_assert(!err);
146 g_assert_cmpfloat(res, ==, value);
149 static void test_visitor_in_string(TestInputVisitorData *data,
150 const void *unused)
152 char *res = NULL, *value = (char *) "Q E M U";
153 Error *err = NULL;
154 Visitor *v;
156 v = visitor_input_test_init(data, "%s", value);
158 visit_type_str(v, &res, NULL, &err);
159 g_assert(!err);
160 g_assert_cmpstr(res, ==, value);
162 g_free(res);
165 static void test_visitor_in_enum(TestInputVisitorData *data,
166 const void *unused)
168 Error *err = NULL;
169 Visitor *v;
170 EnumOne i;
172 for (i = 0; EnumOne_lookup[i]; i++) {
173 EnumOne res = -1;
175 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
177 visit_type_EnumOne(v, &res, NULL, &err);
178 g_assert(!err);
179 g_assert_cmpint(i, ==, res);
181 visitor_input_teardown(data, NULL);
184 data->obj = NULL;
185 data->qiv = NULL;
188 typedef struct TestStruct
190 int64_t integer;
191 bool boolean;
192 char *string;
193 } TestStruct;
195 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
196 const char *name, Error **errp)
198 Error *err = NULL;
200 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
201 &err);
202 if (err) {
203 goto out;
205 visit_type_int(v, &(*obj)->integer, "integer", &err);
206 if (err) {
207 goto out_end;
209 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
210 if (err) {
211 goto out_end;
213 visit_type_str(v, &(*obj)->string, "string", &err);
215 out_end:
216 error_propagate(errp, err);
217 err = NULL;
218 visit_end_struct(v, &err);
219 out:
220 error_propagate(errp, err);
223 static void test_visitor_in_struct(TestInputVisitorData *data,
224 const void *unused)
226 TestStruct *p = NULL;
227 Error *err = NULL;
228 Visitor *v;
230 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
232 visit_type_TestStruct(v, &p, NULL, &err);
233 g_assert(!err);
234 g_assert_cmpint(p->integer, ==, -42);
235 g_assert(p->boolean == true);
236 g_assert_cmpstr(p->string, ==, "foo");
238 g_free(p->string);
239 g_free(p);
242 static void check_and_free_str(char *str, const char *cmp)
244 g_assert_cmpstr(str, ==, cmp);
245 g_free(str);
248 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
249 const void *unused)
251 UserDefTwo *udp = NULL;
252 Error *err = NULL;
253 Visitor *v;
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);
261 g_assert(!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->base->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);
272 g_free(udp->dict1);
273 g_free(udp);
276 static void test_visitor_in_list(TestInputVisitorData *data,
277 const void *unused)
279 UserDefOneList *item, *head = NULL;
280 Error *err = NULL;
281 Visitor *v;
282 int i;
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);
287 g_assert(!err);
288 g_assert(head != NULL);
290 for (i = 0, item = head; item; item = item->next, i++) {
291 char string[12];
293 snprintf(string, sizeof(string), "string%d", i);
294 g_assert_cmpstr(item->value->string, ==, string);
295 g_assert_cmpint(item->value->base->integer, ==, 42 + i);
298 qapi_free_UserDefOneList(head);
301 static void test_visitor_in_any(TestInputVisitorData *data,
302 const void *unused)
304 QObject *res = NULL;
305 Error *err = NULL;
306 Visitor *v;
307 QInt *qint;
308 QBool *qbool;
309 QString *qstring;
310 QDict *qdict;
311 QObject *qobj;
313 v = visitor_input_test_init(data, "-42");
314 visit_type_any(v, &res, NULL, &err);
315 g_assert(!err);
316 qint = qobject_to_qint(res);
317 g_assert(qint);
318 g_assert_cmpint(qint_get_int(qint), ==, -42);
319 qobject_decref(res);
321 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
322 visit_type_any(v, &res, NULL, &err);
323 g_assert(!err);
324 qdict = qobject_to_qdict(res);
325 g_assert(qdict && qdict_size(qdict) == 3);
326 qobj = qdict_get(qdict, "integer");
327 g_assert(qobj);
328 qint = qobject_to_qint(qobj);
329 g_assert(qint);
330 g_assert_cmpint(qint_get_int(qint), ==, -42);
331 qobj = qdict_get(qdict, "boolean");
332 g_assert(qobj);
333 qbool = qobject_to_qbool(qobj);
334 g_assert(qbool);
335 g_assert(qbool_get_bool(qbool) == true);
336 qobj = qdict_get(qdict, "string");
337 g_assert(qobj);
338 qstring = qobject_to_qstring(qobj);
339 g_assert(qstring);
340 g_assert_cmpstr(qstring_get_str(qstring), ==, "foo");
341 qobject_decref(res);
344 static void test_visitor_in_union_flat(TestInputVisitorData *data,
345 const void *unused)
347 Visitor *v;
348 Error *err = NULL;
349 UserDefFlatUnion *tmp;
351 v = visitor_input_test_init(data,
352 "{ 'enum1': 'value1', "
353 "'integer': 41, "
354 "'string': 'str', "
355 "'boolean': true }");
357 visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
358 g_assert(err == NULL);
359 g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1);
360 g_assert_cmpstr(tmp->string, ==, "str");
361 g_assert_cmpint(tmp->integer, ==, 41);
362 g_assert_cmpint(tmp->value1->boolean, ==, true);
363 qapi_free_UserDefFlatUnion(tmp);
366 static void test_visitor_in_alternate(TestInputVisitorData *data,
367 const void *unused)
369 Visitor *v;
370 Error *err = NULL;
371 UserDefAlternate *tmp;
373 v = visitor_input_test_init(data, "42");
374 visit_type_UserDefAlternate(v, &tmp, NULL, &error_abort);
375 g_assert_cmpint(tmp->kind, ==, USER_DEF_ALTERNATE_KIND_I);
376 g_assert_cmpint(tmp->i, ==, 42);
377 qapi_free_UserDefAlternate(tmp);
378 visitor_input_teardown(data, NULL);
380 v = visitor_input_test_init(data, "'string'");
381 visit_type_UserDefAlternate(v, &tmp, NULL, &error_abort);
382 g_assert_cmpint(tmp->kind, ==, USER_DEF_ALTERNATE_KIND_S);
383 g_assert_cmpstr(tmp->s, ==, "string");
384 qapi_free_UserDefAlternate(tmp);
385 visitor_input_teardown(data, NULL);
387 v = visitor_input_test_init(data, "false");
388 visit_type_UserDefAlternate(v, &tmp, NULL, &err);
389 g_assert(err);
390 error_free(err);
391 err = NULL;
392 qapi_free_UserDefAlternate(tmp);
393 visitor_input_teardown(data, NULL);
396 static void test_visitor_in_alternate_number(TestInputVisitorData *data,
397 const void *unused)
399 Visitor *v;
400 Error *err = NULL;
401 AltStrBool *asb;
402 AltStrNum *asn;
403 AltNumStr *ans;
404 AltStrInt *asi;
405 AltIntNum *ain;
406 AltNumInt *ani;
408 /* Parsing an int */
410 v = visitor_input_test_init(data, "42");
411 visit_type_AltStrBool(v, &asb, NULL, &err);
412 g_assert(err);
413 error_free(err);
414 err = NULL;
415 qapi_free_AltStrBool(asb);
416 visitor_input_teardown(data, NULL);
418 /* FIXME: Order of alternate should not affect semantics; asn should
419 * parse the same as ans */
420 v = visitor_input_test_init(data, "42");
421 visit_type_AltStrNum(v, &asn, NULL, &err);
422 /* FIXME g_assert_cmpint(asn->kind, == ALT_STR_NUM_KIND_N); */
423 /* FIXME g_assert_cmpfloat(asn->n, ==, 42); */
424 g_assert(err);
425 error_free(err);
426 err = NULL;
427 qapi_free_AltStrNum(asn);
428 visitor_input_teardown(data, NULL);
430 v = visitor_input_test_init(data, "42");
431 visit_type_AltNumStr(v, &ans, NULL, &error_abort);
432 g_assert_cmpint(ans->kind, ==, ALT_NUM_STR_KIND_N);
433 g_assert_cmpfloat(ans->n, ==, 42);
434 qapi_free_AltNumStr(ans);
435 visitor_input_teardown(data, NULL);
437 v = visitor_input_test_init(data, "42");
438 visit_type_AltStrInt(v, &asi, NULL, &error_abort);
439 g_assert_cmpint(asi->kind, ==, ALT_STR_INT_KIND_I);
440 g_assert_cmpint(asi->i, ==, 42);
441 qapi_free_AltStrInt(asi);
442 visitor_input_teardown(data, NULL);
444 v = visitor_input_test_init(data, "42");
445 visit_type_AltIntNum(v, &ain, NULL, &error_abort);
446 g_assert_cmpint(ain->kind, ==, ALT_INT_NUM_KIND_I);
447 g_assert_cmpint(ain->i, ==, 42);
448 qapi_free_AltIntNum(ain);
449 visitor_input_teardown(data, NULL);
451 v = visitor_input_test_init(data, "42");
452 visit_type_AltNumInt(v, &ani, NULL, &error_abort);
453 g_assert_cmpint(ani->kind, ==, ALT_NUM_INT_KIND_I);
454 g_assert_cmpint(ani->i, ==, 42);
455 qapi_free_AltNumInt(ani);
456 visitor_input_teardown(data, NULL);
458 /* Parsing a double */
460 v = visitor_input_test_init(data, "42.5");
461 visit_type_AltStrBool(v, &asb, NULL, &err);
462 g_assert(err);
463 error_free(err);
464 err = NULL;
465 qapi_free_AltStrBool(asb);
466 visitor_input_teardown(data, NULL);
468 v = visitor_input_test_init(data, "42.5");
469 visit_type_AltStrNum(v, &asn, NULL, &error_abort);
470 g_assert_cmpint(asn->kind, ==, ALT_STR_NUM_KIND_N);
471 g_assert_cmpfloat(asn->n, ==, 42.5);
472 qapi_free_AltStrNum(asn);
473 visitor_input_teardown(data, NULL);
475 v = visitor_input_test_init(data, "42.5");
476 visit_type_AltNumStr(v, &ans, NULL, &error_abort);
477 g_assert_cmpint(ans->kind, ==, ALT_NUM_STR_KIND_N);
478 g_assert_cmpfloat(ans->n, ==, 42.5);
479 qapi_free_AltNumStr(ans);
480 visitor_input_teardown(data, NULL);
482 v = visitor_input_test_init(data, "42.5");
483 visit_type_AltStrInt(v, &asi, NULL, &err);
484 g_assert(err);
485 error_free(err);
486 err = NULL;
487 qapi_free_AltStrInt(asi);
488 visitor_input_teardown(data, NULL);
490 v = visitor_input_test_init(data, "42.5");
491 visit_type_AltIntNum(v, &ain, NULL, &error_abort);
492 g_assert_cmpint(ain->kind, ==, ALT_INT_NUM_KIND_N);
493 g_assert_cmpfloat(ain->n, ==, 42.5);
494 qapi_free_AltIntNum(ain);
495 visitor_input_teardown(data, NULL);
497 v = visitor_input_test_init(data, "42.5");
498 visit_type_AltNumInt(v, &ani, NULL, &error_abort);
499 g_assert_cmpint(ani->kind, ==, ALT_NUM_INT_KIND_N);
500 g_assert_cmpfloat(ani->n, ==, 42.5);
501 qapi_free_AltNumInt(ani);
502 visitor_input_teardown(data, NULL);
505 static void test_native_list_integer_helper(TestInputVisitorData *data,
506 const void *unused,
507 UserDefNativeListUnionKind kind)
509 UserDefNativeListUnion *cvalue = NULL;
510 Error *err = NULL;
511 Visitor *v;
512 GString *gstr_list = g_string_new("");
513 GString *gstr_union = g_string_new("");
514 int i;
516 for (i = 0; i < 32; i++) {
517 g_string_append_printf(gstr_list, "%d", i);
518 if (i != 31) {
519 g_string_append(gstr_list, ", ");
522 g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }",
523 UserDefNativeListUnionKind_lookup[kind],
524 gstr_list->str);
525 v = visitor_input_test_init_raw(data, gstr_union->str);
527 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
528 g_assert(err == NULL);
529 g_assert(cvalue != NULL);
530 g_assert_cmpint(cvalue->kind, ==, kind);
532 switch (kind) {
533 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
534 intList *elem = NULL;
535 for (i = 0, elem = cvalue->integer; elem; elem = elem->next, i++) {
536 g_assert_cmpint(elem->value, ==, i);
538 break;
540 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
541 int8List *elem = NULL;
542 for (i = 0, elem = cvalue->s8; elem; elem = elem->next, i++) {
543 g_assert_cmpint(elem->value, ==, i);
545 break;
547 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
548 int16List *elem = NULL;
549 for (i = 0, elem = cvalue->s16; elem; elem = elem->next, i++) {
550 g_assert_cmpint(elem->value, ==, i);
552 break;
554 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
555 int32List *elem = NULL;
556 for (i = 0, elem = cvalue->s32; elem; elem = elem->next, i++) {
557 g_assert_cmpint(elem->value, ==, i);
559 break;
561 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
562 int64List *elem = NULL;
563 for (i = 0, elem = cvalue->s64; elem; elem = elem->next, i++) {
564 g_assert_cmpint(elem->value, ==, i);
566 break;
568 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
569 uint8List *elem = NULL;
570 for (i = 0, elem = cvalue->u8; elem; elem = elem->next, i++) {
571 g_assert_cmpint(elem->value, ==, i);
573 break;
575 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
576 uint16List *elem = NULL;
577 for (i = 0, elem = cvalue->u16; elem; elem = elem->next, i++) {
578 g_assert_cmpint(elem->value, ==, i);
580 break;
582 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
583 uint32List *elem = NULL;
584 for (i = 0, elem = cvalue->u32; elem; elem = elem->next, i++) {
585 g_assert_cmpint(elem->value, ==, i);
587 break;
589 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
590 uint64List *elem = NULL;
591 for (i = 0, elem = cvalue->u64; elem; elem = elem->next, i++) {
592 g_assert_cmpint(elem->value, ==, i);
594 break;
596 default:
597 g_assert_not_reached();
600 g_string_free(gstr_union, true);
601 g_string_free(gstr_list, true);
602 qapi_free_UserDefNativeListUnion(cvalue);
605 static void test_visitor_in_native_list_int(TestInputVisitorData *data,
606 const void *unused)
608 test_native_list_integer_helper(data, unused,
609 USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
612 static void test_visitor_in_native_list_int8(TestInputVisitorData *data,
613 const void *unused)
615 test_native_list_integer_helper(data, unused,
616 USER_DEF_NATIVE_LIST_UNION_KIND_S8);
619 static void test_visitor_in_native_list_int16(TestInputVisitorData *data,
620 const void *unused)
622 test_native_list_integer_helper(data, unused,
623 USER_DEF_NATIVE_LIST_UNION_KIND_S16);
626 static void test_visitor_in_native_list_int32(TestInputVisitorData *data,
627 const void *unused)
629 test_native_list_integer_helper(data, unused,
630 USER_DEF_NATIVE_LIST_UNION_KIND_S32);
633 static void test_visitor_in_native_list_int64(TestInputVisitorData *data,
634 const void *unused)
636 test_native_list_integer_helper(data, unused,
637 USER_DEF_NATIVE_LIST_UNION_KIND_S64);
640 static void test_visitor_in_native_list_uint8(TestInputVisitorData *data,
641 const void *unused)
643 test_native_list_integer_helper(data, unused,
644 USER_DEF_NATIVE_LIST_UNION_KIND_U8);
647 static void test_visitor_in_native_list_uint16(TestInputVisitorData *data,
648 const void *unused)
650 test_native_list_integer_helper(data, unused,
651 USER_DEF_NATIVE_LIST_UNION_KIND_U16);
654 static void test_visitor_in_native_list_uint32(TestInputVisitorData *data,
655 const void *unused)
657 test_native_list_integer_helper(data, unused,
658 USER_DEF_NATIVE_LIST_UNION_KIND_U32);
661 static void test_visitor_in_native_list_uint64(TestInputVisitorData *data,
662 const void *unused)
664 test_native_list_integer_helper(data, unused,
665 USER_DEF_NATIVE_LIST_UNION_KIND_U64);
668 static void test_visitor_in_native_list_bool(TestInputVisitorData *data,
669 const void *unused)
671 UserDefNativeListUnion *cvalue = NULL;
672 boolList *elem = NULL;
673 Error *err = NULL;
674 Visitor *v;
675 GString *gstr_list = g_string_new("");
676 GString *gstr_union = g_string_new("");
677 int i;
679 for (i = 0; i < 32; i++) {
680 g_string_append_printf(gstr_list, "%s",
681 (i % 3 == 0) ? "true" : "false");
682 if (i != 31) {
683 g_string_append(gstr_list, ", ");
686 g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }",
687 gstr_list->str);
688 v = visitor_input_test_init_raw(data, gstr_union->str);
690 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
691 g_assert(err == NULL);
692 g_assert(cvalue != NULL);
693 g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
695 for (i = 0, elem = cvalue->boolean; elem; elem = elem->next, i++) {
696 g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0);
699 g_string_free(gstr_union, true);
700 g_string_free(gstr_list, true);
701 qapi_free_UserDefNativeListUnion(cvalue);
704 static void test_visitor_in_native_list_string(TestInputVisitorData *data,
705 const void *unused)
707 UserDefNativeListUnion *cvalue = NULL;
708 strList *elem = NULL;
709 Error *err = NULL;
710 Visitor *v;
711 GString *gstr_list = g_string_new("");
712 GString *gstr_union = g_string_new("");
713 int i;
715 for (i = 0; i < 32; i++) {
716 g_string_append_printf(gstr_list, "'%d'", i);
717 if (i != 31) {
718 g_string_append(gstr_list, ", ");
721 g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }",
722 gstr_list->str);
723 v = visitor_input_test_init_raw(data, gstr_union->str);
725 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
726 g_assert(err == NULL);
727 g_assert(cvalue != NULL);
728 g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
730 for (i = 0, elem = cvalue->string; elem; elem = elem->next, i++) {
731 gchar str[8];
732 sprintf(str, "%d", i);
733 g_assert_cmpstr(elem->value, ==, str);
736 g_string_free(gstr_union, true);
737 g_string_free(gstr_list, true);
738 qapi_free_UserDefNativeListUnion(cvalue);
741 #define DOUBLE_STR_MAX 16
743 static void test_visitor_in_native_list_number(TestInputVisitorData *data,
744 const void *unused)
746 UserDefNativeListUnion *cvalue = NULL;
747 numberList *elem = NULL;
748 Error *err = NULL;
749 Visitor *v;
750 GString *gstr_list = g_string_new("");
751 GString *gstr_union = g_string_new("");
752 int i;
754 for (i = 0; i < 32; i++) {
755 g_string_append_printf(gstr_list, "%f", (double)i / 3);
756 if (i != 31) {
757 g_string_append(gstr_list, ", ");
760 g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }",
761 gstr_list->str);
762 v = visitor_input_test_init_raw(data, gstr_union->str);
764 visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err);
765 g_assert(err == NULL);
766 g_assert(cvalue != NULL);
767 g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
769 for (i = 0, elem = cvalue->number; elem; elem = elem->next, i++) {
770 GString *double_expected = g_string_new("");
771 GString *double_actual = g_string_new("");
773 g_string_printf(double_expected, "%.6f", (double)i / 3);
774 g_string_printf(double_actual, "%.6f", elem->value);
775 g_assert_cmpstr(double_expected->str, ==, double_actual->str);
777 g_string_free(double_expected, true);
778 g_string_free(double_actual, true);
781 g_string_free(gstr_union, true);
782 g_string_free(gstr_list, true);
783 qapi_free_UserDefNativeListUnion(cvalue);
786 static void input_visitor_test_add(const char *testpath,
787 TestInputVisitorData *data,
788 void (*test_func)(TestInputVisitorData *data, const void *user_data))
790 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
791 visitor_input_teardown);
794 static void test_visitor_in_errors(TestInputVisitorData *data,
795 const void *unused)
797 TestStruct *p = NULL;
798 Error *err = NULL;
799 Visitor *v;
801 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
803 visit_type_TestStruct(v, &p, NULL, &err);
804 g_assert(err);
805 /* FIXME - a failed parse should not leave a partially-allocated p
806 * for us to clean up; this could cause callers to leak memory. */
807 g_assert(p->string == NULL);
809 error_free(err);
810 g_free(p->string);
811 g_free(p);
814 int main(int argc, char **argv)
816 TestInputVisitorData in_visitor_data;
818 g_test_init(&argc, &argv, NULL);
820 input_visitor_test_add("/visitor/input/int",
821 &in_visitor_data, test_visitor_in_int);
822 input_visitor_test_add("/visitor/input/int_overflow",
823 &in_visitor_data, test_visitor_in_int_overflow);
824 input_visitor_test_add("/visitor/input/bool",
825 &in_visitor_data, test_visitor_in_bool);
826 input_visitor_test_add("/visitor/input/number",
827 &in_visitor_data, test_visitor_in_number);
828 input_visitor_test_add("/visitor/input/string",
829 &in_visitor_data, test_visitor_in_string);
830 input_visitor_test_add("/visitor/input/enum",
831 &in_visitor_data, test_visitor_in_enum);
832 input_visitor_test_add("/visitor/input/struct",
833 &in_visitor_data, test_visitor_in_struct);
834 input_visitor_test_add("/visitor/input/struct-nested",
835 &in_visitor_data, test_visitor_in_struct_nested);
836 input_visitor_test_add("/visitor/input/list",
837 &in_visitor_data, test_visitor_in_list);
838 input_visitor_test_add("/visitor/input/any",
839 &in_visitor_data, test_visitor_in_any);
840 input_visitor_test_add("/visitor/input/union-flat",
841 &in_visitor_data, test_visitor_in_union_flat);
842 input_visitor_test_add("/visitor/input/alternate",
843 &in_visitor_data, test_visitor_in_alternate);
844 input_visitor_test_add("/visitor/input/errors",
845 &in_visitor_data, test_visitor_in_errors);
846 input_visitor_test_add("/visitor/input/alternate-number",
847 &in_visitor_data, test_visitor_in_alternate_number);
848 input_visitor_test_add("/visitor/input/native_list/int",
849 &in_visitor_data,
850 test_visitor_in_native_list_int);
851 input_visitor_test_add("/visitor/input/native_list/int8",
852 &in_visitor_data,
853 test_visitor_in_native_list_int8);
854 input_visitor_test_add("/visitor/input/native_list/int16",
855 &in_visitor_data,
856 test_visitor_in_native_list_int16);
857 input_visitor_test_add("/visitor/input/native_list/int32",
858 &in_visitor_data,
859 test_visitor_in_native_list_int32);
860 input_visitor_test_add("/visitor/input/native_list/int64",
861 &in_visitor_data,
862 test_visitor_in_native_list_int64);
863 input_visitor_test_add("/visitor/input/native_list/uint8",
864 &in_visitor_data,
865 test_visitor_in_native_list_uint8);
866 input_visitor_test_add("/visitor/input/native_list/uint16",
867 &in_visitor_data,
868 test_visitor_in_native_list_uint16);
869 input_visitor_test_add("/visitor/input/native_list/uint32",
870 &in_visitor_data,
871 test_visitor_in_native_list_uint32);
872 input_visitor_test_add("/visitor/input/native_list/uint64",
873 &in_visitor_data,
874 test_visitor_in_native_list_uint64);
875 input_visitor_test_add("/visitor/input/native_list/bool",
876 &in_visitor_data, test_visitor_in_native_list_bool);
877 input_visitor_test_add("/visitor/input/native_list/str",
878 &in_visitor_data,
879 test_visitor_in_native_list_string);
880 input_visitor_test_add("/visitor/input/native_list/number",
881 &in_visitor_data,
882 test_visitor_in_native_list_number);
884 g_test_run();
886 return 0;