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