]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-qmp-output-visitor.c
tests: Don't call visit_end_struct() after visit_start_struct() fails
[mirror_qemu.git] / tests / test-qmp-output-visitor.c
1 /*
2 * QMP Output Visitor unit-tests.
3 *
4 * Copyright (C) 2011 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
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.
11 */
12
13 #include <glib.h>
14
15 #include "qemu-common.h"
16 #include "qapi/qmp-output-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qapi/qmp/types.h"
20
21 typedef struct TestOutputVisitorData {
22 QmpOutputVisitor *qov;
23 Visitor *ov;
24 } TestOutputVisitorData;
25
26 static void visitor_output_setup(TestOutputVisitorData *data,
27 const void *unused)
28 {
29 data->qov = qmp_output_visitor_new();
30 g_assert(data->qov != NULL);
31
32 data->ov = qmp_output_get_visitor(data->qov);
33 g_assert(data->ov != NULL);
34 }
35
36 static void visitor_output_teardown(TestOutputVisitorData *data,
37 const void *unused)
38 {
39 qmp_output_visitor_cleanup(data->qov);
40 data->qov = NULL;
41 data->ov = NULL;
42 }
43
44 static void test_visitor_out_int(TestOutputVisitorData *data,
45 const void *unused)
46 {
47 int64_t value = -42;
48 Error *err = NULL;
49 QObject *obj;
50
51 visit_type_int(data->ov, &value, NULL, &err);
52 g_assert(!err);
53
54 obj = qmp_output_get_qobject(data->qov);
55 g_assert(obj != NULL);
56 g_assert(qobject_type(obj) == QTYPE_QINT);
57 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value);
58
59 qobject_decref(obj);
60 }
61
62 static void test_visitor_out_bool(TestOutputVisitorData *data,
63 const void *unused)
64 {
65 Error *err = NULL;
66 bool value = true;
67 QObject *obj;
68
69 visit_type_bool(data->ov, &value, NULL, &err);
70 g_assert(!err);
71
72 obj = qmp_output_get_qobject(data->qov);
73 g_assert(obj != NULL);
74 g_assert(qobject_type(obj) == QTYPE_QBOOL);
75 g_assert(qbool_get_int(qobject_to_qbool(obj)) == value);
76
77 qobject_decref(obj);
78 }
79
80 static void test_visitor_out_number(TestOutputVisitorData *data,
81 const void *unused)
82 {
83 double value = 3.14;
84 Error *err = NULL;
85 QObject *obj;
86
87 visit_type_number(data->ov, &value, NULL, &err);
88 g_assert(!err);
89
90 obj = qmp_output_get_qobject(data->qov);
91 g_assert(obj != NULL);
92 g_assert(qobject_type(obj) == QTYPE_QFLOAT);
93 g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value);
94
95 qobject_decref(obj);
96 }
97
98 static void test_visitor_out_string(TestOutputVisitorData *data,
99 const void *unused)
100 {
101 char *string = (char *) "Q E M U";
102 Error *err = NULL;
103 QObject *obj;
104
105 visit_type_str(data->ov, &string, NULL, &err);
106 g_assert(!err);
107
108 obj = qmp_output_get_qobject(data->qov);
109 g_assert(obj != NULL);
110 g_assert(qobject_type(obj) == QTYPE_QSTRING);
111 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string);
112
113 qobject_decref(obj);
114 }
115
116 static void test_visitor_out_no_string(TestOutputVisitorData *data,
117 const void *unused)
118 {
119 char *string = NULL;
120 Error *err = NULL;
121 QObject *obj;
122
123 /* A null string should return "" */
124 visit_type_str(data->ov, &string, NULL, &err);
125 g_assert(!err);
126
127 obj = qmp_output_get_qobject(data->qov);
128 g_assert(obj != NULL);
129 g_assert(qobject_type(obj) == QTYPE_QSTRING);
130 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, "");
131
132 qobject_decref(obj);
133 }
134
135 static void test_visitor_out_enum(TestOutputVisitorData *data,
136 const void *unused)
137 {
138 Error *err = NULL;
139 QObject *obj;
140 EnumOne i;
141
142 for (i = 0; i < ENUM_ONE_MAX; i++) {
143 visit_type_EnumOne(data->ov, &i, "unused", &err);
144 g_assert(!err);
145
146 obj = qmp_output_get_qobject(data->qov);
147 g_assert(obj != NULL);
148 g_assert(qobject_type(obj) == QTYPE_QSTRING);
149 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==,
150 EnumOne_lookup[i]);
151 qobject_decref(obj);
152 }
153 }
154
155 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
156 const void *unused)
157 {
158 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
159 Error *err;
160
161 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
162 err = NULL;
163 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &err);
164 g_assert(err);
165 error_free(err);
166 }
167 }
168
169 typedef struct TestStruct
170 {
171 int64_t integer;
172 bool boolean;
173 char *string;
174 } TestStruct;
175
176 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
177 const char *name, Error **errp)
178 {
179 Error *err = NULL;
180
181 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
182 &err);
183 if (err) {
184 goto out;
185 }
186
187 visit_type_int(v, &(*obj)->integer, "integer", &err);
188 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
189 visit_type_str(v, &(*obj)->string, "string", &err);
190
191 visit_end_struct(v, &err);
192
193 out:
194 error_propagate(errp, err);
195 }
196
197 static void test_visitor_out_struct(TestOutputVisitorData *data,
198 const void *unused)
199 {
200 TestStruct test_struct = { .integer = 42,
201 .boolean = false,
202 .string = (char *) "foo"};
203 TestStruct *p = &test_struct;
204 Error *err = NULL;
205 QObject *obj;
206 QDict *qdict;
207
208 visit_type_TestStruct(data->ov, &p, NULL, &err);
209 g_assert(!err);
210
211 obj = qmp_output_get_qobject(data->qov);
212 g_assert(obj != NULL);
213 g_assert(qobject_type(obj) == QTYPE_QDICT);
214
215 qdict = qobject_to_qdict(obj);
216 g_assert_cmpint(qdict_size(qdict), ==, 3);
217 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
218 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, 0);
219 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
220
221 QDECREF(qdict);
222 }
223
224 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
225 const void *unused)
226 {
227 int64_t value = 42;
228 Error *err = NULL;
229 UserDefNested *ud2;
230 QObject *obj;
231 QDict *qdict, *dict1, *dict2, *dict3, *userdef;
232 const char *string = "user def string";
233 const char *strings[] = { "forty two", "forty three", "forty four",
234 "forty five" };
235
236 ud2 = g_malloc0(sizeof(*ud2));
237 ud2->string0 = g_strdup(strings[0]);
238
239 ud2->dict1.string1 = g_strdup(strings[1]);
240 ud2->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
241 ud2->dict1.dict2.userdef1->string = g_strdup(string);
242 ud2->dict1.dict2.userdef1->base = g_new0(UserDefZero, 1);
243 ud2->dict1.dict2.userdef1->base->integer = value;
244 ud2->dict1.dict2.string2 = g_strdup(strings[2]);
245
246 ud2->dict1.has_dict3 = true;
247 ud2->dict1.dict3.userdef2 = g_malloc0(sizeof(UserDefOne));
248 ud2->dict1.dict3.userdef2->string = g_strdup(string);
249 ud2->dict1.dict3.userdef2->base = g_new0(UserDefZero, 1);
250 ud2->dict1.dict3.userdef2->base->integer = value;
251 ud2->dict1.dict3.string3 = g_strdup(strings[3]);
252
253 visit_type_UserDefNested(data->ov, &ud2, "unused", &err);
254 g_assert(!err);
255
256 obj = qmp_output_get_qobject(data->qov);
257 g_assert(obj != NULL);
258 g_assert(qobject_type(obj) == QTYPE_QDICT);
259
260 qdict = qobject_to_qdict(obj);
261 g_assert_cmpint(qdict_size(qdict), ==, 2);
262 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
263
264 dict1 = qdict_get_qdict(qdict, "dict1");
265 g_assert_cmpint(qdict_size(dict1), ==, 3);
266 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
267
268 dict2 = qdict_get_qdict(dict1, "dict2");
269 g_assert_cmpint(qdict_size(dict2), ==, 2);
270 g_assert_cmpstr(qdict_get_str(dict2, "string2"), ==, strings[2]);
271 userdef = qdict_get_qdict(dict2, "userdef1");
272 g_assert_cmpint(qdict_size(userdef), ==, 2);
273 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
274 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
275
276 dict3 = qdict_get_qdict(dict1, "dict3");
277 g_assert_cmpint(qdict_size(dict3), ==, 2);
278 g_assert_cmpstr(qdict_get_str(dict3, "string3"), ==, strings[3]);
279 userdef = qdict_get_qdict(dict3, "userdef2");
280 g_assert_cmpint(qdict_size(userdef), ==, 2);
281 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
282 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
283
284 QDECREF(qdict);
285 qapi_free_UserDefNested(ud2);
286 }
287
288 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
289 const void *unused)
290 {
291 EnumOne bad_values[] = { ENUM_ONE_MAX, -1 };
292 UserDefZero b;
293 UserDefOne u = { .base = &b }, *pu = &u;
294 Error *err;
295 int i;
296
297 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
298 err = NULL;
299 u.has_enum1 = true;
300 u.enum1 = bad_values[i];
301 visit_type_UserDefOne(data->ov, &pu, "unused", &err);
302 g_assert(err);
303 error_free(err);
304 }
305 }
306
307 typedef struct TestStructList
308 {
309 union {
310 TestStruct *value;
311 uint64_t padding;
312 };
313 struct TestStructList *next;
314 } TestStructList;
315
316 static void visit_type_TestStructList(Visitor *v, TestStructList **obj,
317 const char *name, Error **errp)
318 {
319 GenericList *i, **head = (GenericList **)obj;
320
321 visit_start_list(v, name, errp);
322
323 for (*head = i = visit_next_list(v, head, errp); i; i = visit_next_list(v, &i, errp)) {
324 TestStructList *native_i = (TestStructList *)i;
325 visit_type_TestStruct(v, &native_i->value, NULL, errp);
326 }
327
328 visit_end_list(v, errp);
329 }
330
331 static void test_visitor_out_list(TestOutputVisitorData *data,
332 const void *unused)
333 {
334 char *value_str = (char *) "list value";
335 TestStructList *p, *head = NULL;
336 const int max_items = 10;
337 bool value_bool = true;
338 int value_int = 10;
339 Error *err = NULL;
340 QListEntry *entry;
341 QObject *obj;
342 QList *qlist;
343 int i;
344
345 for (i = 0; i < max_items; i++) {
346 p = g_malloc0(sizeof(*p));
347 p->value = g_malloc0(sizeof(*p->value));
348 p->value->integer = value_int;
349 p->value->boolean = value_bool;
350 p->value->string = value_str;
351
352 p->next = head;
353 head = p;
354 }
355
356 visit_type_TestStructList(data->ov, &head, NULL, &err);
357 g_assert(!err);
358
359 obj = qmp_output_get_qobject(data->qov);
360 g_assert(obj != NULL);
361 g_assert(qobject_type(obj) == QTYPE_QLIST);
362
363 qlist = qobject_to_qlist(obj);
364 g_assert(!qlist_empty(qlist));
365
366 i = 0;
367 QLIST_FOREACH_ENTRY(qlist, entry) {
368 QDict *qdict;
369
370 g_assert(qobject_type(entry->value) == QTYPE_QDICT);
371 qdict = qobject_to_qdict(entry->value);
372 g_assert_cmpint(qdict_size(qdict), ==, 3);
373 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int);
374 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
375 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
376 i++;
377 }
378 g_assert_cmpint(i, ==, max_items);
379
380 QDECREF(qlist);
381
382 for (p = head; p;) {
383 TestStructList *tmp = p->next;
384 g_free(p->value);
385 g_free(p);
386 p = tmp;
387 }
388 }
389
390 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
391 const void *unused)
392 {
393 UserDefNestedList *p, *head = NULL;
394 const char string[] = "foo bar";
395 int i, max_count = 1024;
396
397 for (i = 0; i < max_count; i++) {
398 p = g_malloc0(sizeof(*p));
399 p->value = g_malloc0(sizeof(*p->value));
400
401 p->value->string0 = g_strdup(string);
402 p->value->dict1.string1 = g_strdup(string);
403 p->value->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
404 p->value->dict1.dict2.userdef1->string = g_strdup(string);
405 p->value->dict1.dict2.userdef1->base = g_new0(UserDefZero, 1);
406 p->value->dict1.dict2.userdef1->base->integer = 42;
407 p->value->dict1.dict2.string2 = g_strdup(string);
408 p->value->dict1.has_dict3 = false;
409
410 p->next = head;
411 head = p;
412 }
413
414 qapi_free_UserDefNestedList(head);
415 }
416
417 static void test_visitor_out_union(TestOutputVisitorData *data,
418 const void *unused)
419 {
420 QObject *arg, *qvalue;
421 QDict *qdict, *value;
422
423 Error *err = NULL;
424
425 UserDefUnion *tmp = g_malloc0(sizeof(UserDefUnion));
426 tmp->kind = USER_DEF_UNION_KIND_A;
427 tmp->integer = 41;
428 tmp->a = g_malloc0(sizeof(UserDefA));
429 tmp->a->boolean = true;
430
431 visit_type_UserDefUnion(data->ov, &tmp, NULL, &err);
432 g_assert(err == NULL);
433 arg = qmp_output_get_qobject(data->qov);
434
435 g_assert(qobject_type(arg) == QTYPE_QDICT);
436 qdict = qobject_to_qdict(arg);
437
438 g_assert_cmpstr(qdict_get_str(qdict, "type"), ==, "a");
439 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41);
440
441 qvalue = qdict_get(qdict, "data");
442 g_assert(data != NULL);
443 g_assert(qobject_type(qvalue) == QTYPE_QDICT);
444 value = qobject_to_qdict(qvalue);
445 g_assert_cmpint(qdict_get_bool(value, "boolean"), ==, true);
446
447 qapi_free_UserDefUnion(tmp);
448 QDECREF(qdict);
449 }
450
451 static void test_visitor_out_union_flat(TestOutputVisitorData *data,
452 const void *unused)
453 {
454 QObject *arg;
455 QDict *qdict;
456
457 Error *err = NULL;
458
459 UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
460 tmp->kind = ENUM_ONE_VALUE1;
461 tmp->string = g_strdup("str");
462 tmp->value1 = g_malloc0(sizeof(UserDefA));
463 /* TODO when generator bug is fixed: tmp->integer = 41; */
464 tmp->value1->boolean = true;
465
466 visit_type_UserDefFlatUnion(data->ov, &tmp, NULL, &err);
467 g_assert(err == NULL);
468 arg = qmp_output_get_qobject(data->qov);
469
470 g_assert(qobject_type(arg) == QTYPE_QDICT);
471 qdict = qobject_to_qdict(arg);
472
473 g_assert_cmpstr(qdict_get_str(qdict, "enum1"), ==, "value1");
474 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "str");
475 /* TODO g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 41); */
476 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, true);
477
478 qapi_free_UserDefFlatUnion(tmp);
479 QDECREF(qdict);
480 }
481
482 static void test_visitor_out_union_anon(TestOutputVisitorData *data,
483 const void *unused)
484 {
485 QObject *arg;
486 Error *err = NULL;
487
488 UserDefAnonUnion *tmp = g_malloc0(sizeof(UserDefAnonUnion));
489 tmp->kind = USER_DEF_ANON_UNION_KIND_I;
490 tmp->i = 42;
491
492 visit_type_UserDefAnonUnion(data->ov, &tmp, NULL, &err);
493 g_assert(err == NULL);
494 arg = qmp_output_get_qobject(data->qov);
495
496 g_assert(qobject_type(arg) == QTYPE_QINT);
497 g_assert_cmpint(qint_get_int(qobject_to_qint(arg)), ==, 42);
498
499 qapi_free_UserDefAnonUnion(tmp);
500 }
501
502 static void init_native_list(UserDefNativeListUnion *cvalue)
503 {
504 int i;
505 switch (cvalue->kind) {
506 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: {
507 intList **list = &cvalue->integer;
508 for (i = 0; i < 32; i++) {
509 *list = g_new0(intList, 1);
510 (*list)->value = i;
511 (*list)->next = NULL;
512 list = &(*list)->next;
513 }
514 break;
515 }
516 case USER_DEF_NATIVE_LIST_UNION_KIND_S8: {
517 int8List **list = &cvalue->s8;
518 for (i = 0; i < 32; i++) {
519 *list = g_new0(int8List, 1);
520 (*list)->value = i;
521 (*list)->next = NULL;
522 list = &(*list)->next;
523 }
524 break;
525 }
526 case USER_DEF_NATIVE_LIST_UNION_KIND_S16: {
527 int16List **list = &cvalue->s16;
528 for (i = 0; i < 32; i++) {
529 *list = g_new0(int16List, 1);
530 (*list)->value = i;
531 (*list)->next = NULL;
532 list = &(*list)->next;
533 }
534 break;
535 }
536 case USER_DEF_NATIVE_LIST_UNION_KIND_S32: {
537 int32List **list = &cvalue->s32;
538 for (i = 0; i < 32; i++) {
539 *list = g_new0(int32List, 1);
540 (*list)->value = i;
541 (*list)->next = NULL;
542 list = &(*list)->next;
543 }
544 break;
545 }
546 case USER_DEF_NATIVE_LIST_UNION_KIND_S64: {
547 int64List **list = &cvalue->s64;
548 for (i = 0; i < 32; i++) {
549 *list = g_new0(int64List, 1);
550 (*list)->value = i;
551 (*list)->next = NULL;
552 list = &(*list)->next;
553 }
554 break;
555 }
556 case USER_DEF_NATIVE_LIST_UNION_KIND_U8: {
557 uint8List **list = &cvalue->u8;
558 for (i = 0; i < 32; i++) {
559 *list = g_new0(uint8List, 1);
560 (*list)->value = i;
561 (*list)->next = NULL;
562 list = &(*list)->next;
563 }
564 break;
565 }
566 case USER_DEF_NATIVE_LIST_UNION_KIND_U16: {
567 uint16List **list = &cvalue->u16;
568 for (i = 0; i < 32; i++) {
569 *list = g_new0(uint16List, 1);
570 (*list)->value = i;
571 (*list)->next = NULL;
572 list = &(*list)->next;
573 }
574 break;
575 }
576 case USER_DEF_NATIVE_LIST_UNION_KIND_U32: {
577 uint32List **list = &cvalue->u32;
578 for (i = 0; i < 32; i++) {
579 *list = g_new0(uint32List, 1);
580 (*list)->value = i;
581 (*list)->next = NULL;
582 list = &(*list)->next;
583 }
584 break;
585 }
586 case USER_DEF_NATIVE_LIST_UNION_KIND_U64: {
587 uint64List **list = &cvalue->u64;
588 for (i = 0; i < 32; i++) {
589 *list = g_new0(uint64List, 1);
590 (*list)->value = i;
591 (*list)->next = NULL;
592 list = &(*list)->next;
593 }
594 break;
595 }
596 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN: {
597 boolList **list = &cvalue->boolean;
598 for (i = 0; i < 32; i++) {
599 *list = g_new0(boolList, 1);
600 (*list)->value = (i % 3 == 0);
601 (*list)->next = NULL;
602 list = &(*list)->next;
603 }
604 break;
605 }
606 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING: {
607 strList **list = &cvalue->string;
608 for (i = 0; i < 32; i++) {
609 *list = g_new0(strList, 1);
610 (*list)->value = g_strdup_printf("%d", i);
611 (*list)->next = NULL;
612 list = &(*list)->next;
613 }
614 break;
615 }
616 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER: {
617 numberList **list = &cvalue->number;
618 for (i = 0; i < 32; i++) {
619 *list = g_new0(numberList, 1);
620 (*list)->value = (double)i / 3;
621 (*list)->next = NULL;
622 list = &(*list)->next;
623 }
624 break;
625 }
626 default:
627 g_assert_not_reached();
628 }
629 }
630
631 static void check_native_list(QObject *qobj,
632 UserDefNativeListUnionKind kind)
633 {
634 QDict *qdict;
635 QList *qlist;
636 int i;
637
638 g_assert(qobj);
639 g_assert(qobject_type(qobj) == QTYPE_QDICT);
640 qdict = qobject_to_qdict(qobj);
641 g_assert(qdict);
642 g_assert(qdict_haskey(qdict, "data"));
643 qlist = qlist_copy(qobject_to_qlist(qdict_get(qdict, "data")));
644
645 switch (kind) {
646 case USER_DEF_NATIVE_LIST_UNION_KIND_S8:
647 case USER_DEF_NATIVE_LIST_UNION_KIND_S16:
648 case USER_DEF_NATIVE_LIST_UNION_KIND_S32:
649 case USER_DEF_NATIVE_LIST_UNION_KIND_S64:
650 case USER_DEF_NATIVE_LIST_UNION_KIND_U8:
651 case USER_DEF_NATIVE_LIST_UNION_KIND_U16:
652 case USER_DEF_NATIVE_LIST_UNION_KIND_U32:
653 case USER_DEF_NATIVE_LIST_UNION_KIND_U64:
654 /* all integer elements in JSON arrays get stored into QInts when
655 * we convert to QObjects, so we can check them all in the same
656 * fashion, so simply fall through here
657 */
658 case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER:
659 for (i = 0; i < 32; i++) {
660 QObject *tmp;
661 QInt *qvalue;
662 tmp = qlist_peek(qlist);
663 g_assert(tmp);
664 qvalue = qobject_to_qint(tmp);
665 g_assert_cmpint(qint_get_int(qvalue), ==, i);
666 qobject_decref(qlist_pop(qlist));
667 }
668 break;
669 case USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN:
670 for (i = 0; i < 32; i++) {
671 QObject *tmp;
672 QBool *qvalue;
673 tmp = qlist_peek(qlist);
674 g_assert(tmp);
675 qvalue = qobject_to_qbool(tmp);
676 g_assert_cmpint(qbool_get_int(qvalue), ==, (i % 3 == 0) ? 1 : 0);
677 qobject_decref(qlist_pop(qlist));
678 }
679 break;
680 case USER_DEF_NATIVE_LIST_UNION_KIND_STRING:
681 for (i = 0; i < 32; i++) {
682 QObject *tmp;
683 QString *qvalue;
684 gchar str[8];
685 tmp = qlist_peek(qlist);
686 g_assert(tmp);
687 qvalue = qobject_to_qstring(tmp);
688 sprintf(str, "%d", i);
689 g_assert_cmpstr(qstring_get_str(qvalue), ==, str);
690 qobject_decref(qlist_pop(qlist));
691 }
692 break;
693 case USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER:
694 for (i = 0; i < 32; i++) {
695 QObject *tmp;
696 QFloat *qvalue;
697 GString *double_expected = g_string_new("");
698 GString *double_actual = g_string_new("");
699
700 tmp = qlist_peek(qlist);
701 g_assert(tmp);
702 qvalue = qobject_to_qfloat(tmp);
703 g_string_printf(double_expected, "%.6f", (double)i / 3);
704 g_string_printf(double_actual, "%.6f", qfloat_get_double(qvalue));
705 g_assert_cmpstr(double_actual->str, ==, double_expected->str);
706
707 qobject_decref(qlist_pop(qlist));
708 g_string_free(double_expected, true);
709 g_string_free(double_actual, true);
710 }
711 break;
712 default:
713 g_assert_not_reached();
714 }
715 QDECREF(qlist);
716 }
717
718 static void test_native_list(TestOutputVisitorData *data,
719 const void *unused,
720 UserDefNativeListUnionKind kind)
721 {
722 UserDefNativeListUnion *cvalue = g_new0(UserDefNativeListUnion, 1);
723 Error *err = NULL;
724 QObject *obj;
725
726 cvalue->kind = kind;
727 init_native_list(cvalue);
728
729 visit_type_UserDefNativeListUnion(data->ov, &cvalue, NULL, &err);
730 g_assert(err == NULL);
731
732 obj = qmp_output_get_qobject(data->qov);
733 check_native_list(obj, cvalue->kind);
734 qapi_free_UserDefNativeListUnion(cvalue);
735 qobject_decref(obj);
736 }
737
738 static void test_visitor_out_native_list_int(TestOutputVisitorData *data,
739 const void *unused)
740 {
741 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER);
742 }
743
744 static void test_visitor_out_native_list_int8(TestOutputVisitorData *data,
745 const void *unused)
746 {
747 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S8);
748 }
749
750 static void test_visitor_out_native_list_int16(TestOutputVisitorData *data,
751 const void *unused)
752 {
753 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S16);
754 }
755
756 static void test_visitor_out_native_list_int32(TestOutputVisitorData *data,
757 const void *unused)
758 {
759 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S32);
760 }
761
762 static void test_visitor_out_native_list_int64(TestOutputVisitorData *data,
763 const void *unused)
764 {
765 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_S64);
766 }
767
768 static void test_visitor_out_native_list_uint8(TestOutputVisitorData *data,
769 const void *unused)
770 {
771 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U8);
772 }
773
774 static void test_visitor_out_native_list_uint16(TestOutputVisitorData *data,
775 const void *unused)
776 {
777 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U16);
778 }
779
780 static void test_visitor_out_native_list_uint32(TestOutputVisitorData *data,
781 const void *unused)
782 {
783 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U32);
784 }
785
786 static void test_visitor_out_native_list_uint64(TestOutputVisitorData *data,
787 const void *unused)
788 {
789 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_U64);
790 }
791
792 static void test_visitor_out_native_list_bool(TestOutputVisitorData *data,
793 const void *unused)
794 {
795 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN);
796 }
797
798 static void test_visitor_out_native_list_str(TestOutputVisitorData *data,
799 const void *unused)
800 {
801 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_STRING);
802 }
803
804 static void test_visitor_out_native_list_number(TestOutputVisitorData *data,
805 const void *unused)
806 {
807 test_native_list(data, unused, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER);
808 }
809
810 static void output_visitor_test_add(const char *testpath,
811 TestOutputVisitorData *data,
812 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
813 {
814 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
815 test_func, visitor_output_teardown);
816 }
817
818 int main(int argc, char **argv)
819 {
820 TestOutputVisitorData out_visitor_data;
821
822 g_test_init(&argc, &argv, NULL);
823
824 output_visitor_test_add("/visitor/output/int",
825 &out_visitor_data, test_visitor_out_int);
826 output_visitor_test_add("/visitor/output/bool",
827 &out_visitor_data, test_visitor_out_bool);
828 output_visitor_test_add("/visitor/output/number",
829 &out_visitor_data, test_visitor_out_number);
830 output_visitor_test_add("/visitor/output/string",
831 &out_visitor_data, test_visitor_out_string);
832 output_visitor_test_add("/visitor/output/no-string",
833 &out_visitor_data, test_visitor_out_no_string);
834 output_visitor_test_add("/visitor/output/enum",
835 &out_visitor_data, test_visitor_out_enum);
836 output_visitor_test_add("/visitor/output/enum-errors",
837 &out_visitor_data, test_visitor_out_enum_errors);
838 output_visitor_test_add("/visitor/output/struct",
839 &out_visitor_data, test_visitor_out_struct);
840 output_visitor_test_add("/visitor/output/struct-nested",
841 &out_visitor_data, test_visitor_out_struct_nested);
842 output_visitor_test_add("/visitor/output/struct-errors",
843 &out_visitor_data, test_visitor_out_struct_errors);
844 output_visitor_test_add("/visitor/output/list",
845 &out_visitor_data, test_visitor_out_list);
846 output_visitor_test_add("/visitor/output/list-qapi-free",
847 &out_visitor_data, test_visitor_out_list_qapi_free);
848 output_visitor_test_add("/visitor/output/union",
849 &out_visitor_data, test_visitor_out_union);
850 output_visitor_test_add("/visitor/output/union-flat",
851 &out_visitor_data, test_visitor_out_union_flat);
852 output_visitor_test_add("/visitor/output/union-anon",
853 &out_visitor_data, test_visitor_out_union_anon);
854 output_visitor_test_add("/visitor/output/native_list/int",
855 &out_visitor_data, test_visitor_out_native_list_int);
856 output_visitor_test_add("/visitor/output/native_list/int8",
857 &out_visitor_data, test_visitor_out_native_list_int8);
858 output_visitor_test_add("/visitor/output/native_list/int16",
859 &out_visitor_data, test_visitor_out_native_list_int16);
860 output_visitor_test_add("/visitor/output/native_list/int32",
861 &out_visitor_data, test_visitor_out_native_list_int32);
862 output_visitor_test_add("/visitor/output/native_list/int64",
863 &out_visitor_data, test_visitor_out_native_list_int64);
864 output_visitor_test_add("/visitor/output/native_list/uint8",
865 &out_visitor_data, test_visitor_out_native_list_uint8);
866 output_visitor_test_add("/visitor/output/native_list/uint16",
867 &out_visitor_data, test_visitor_out_native_list_uint16);
868 output_visitor_test_add("/visitor/output/native_list/uint32",
869 &out_visitor_data, test_visitor_out_native_list_uint32);
870 output_visitor_test_add("/visitor/output/native_list/uint64",
871 &out_visitor_data, test_visitor_out_native_list_uint64);
872 output_visitor_test_add("/visitor/output/native_list/bool",
873 &out_visitor_data, test_visitor_out_native_list_bool);
874 output_visitor_test_add("/visitor/output/native_list/string",
875 &out_visitor_data, test_visitor_out_native_list_str);
876 output_visitor_test_add("/visitor/output/native_list/number",
877 &out_visitor_data, test_visitor_out_native_list_number);
878
879 g_test_run();
880
881 return 0;
882 }