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