]>
Commit | Line | Data |
---|---|---|
d88f5fd1 LC |
1 | /* |
2 | * QMP Input Visitor unit-tests. | |
3 | * | |
805017b7 | 4 | * Copyright (C) 2011, 2015 Red Hat Inc. |
d88f5fd1 LC |
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 | #include <stdarg.h> | |
15 | ||
79ee7df8 | 16 | #include "qemu-common.h" |
d88f5fd1 LC |
17 | #include "qapi/qmp-input-visitor.h" |
18 | #include "test-qapi-types.h" | |
19 | #include "test-qapi-visit.h" | |
7b1b5d19 | 20 | #include "qapi/qmp/types.h" |
d88f5fd1 LC |
21 | |
22 | typedef struct TestInputVisitorData { | |
23 | QObject *obj; | |
24 | QmpInputVisitor *qiv; | |
25 | } TestInputVisitorData; | |
26 | ||
27 | static void visitor_input_teardown(TestInputVisitorData *data, | |
28 | const void *unused) | |
29 | { | |
30 | qobject_decref(data->obj); | |
31 | data->obj = NULL; | |
32 | ||
33 | if (data->qiv) { | |
34 | qmp_input_visitor_cleanup(data->qiv); | |
35 | data->qiv = NULL; | |
36 | } | |
37 | } | |
38 | ||
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()) */ | |
aba2107a SW |
42 | static GCC_FMT_ATTR(2, 3) |
43 | Visitor *visitor_input_test_init(TestInputVisitorData *data, | |
44 | const char *json_string, ...) | |
d88f5fd1 LC |
45 | { |
46 | Visitor *v; | |
47 | va_list ap; | |
48 | ||
49 | va_start(ap, json_string); | |
50 | data->obj = qobject_from_jsonv(json_string, &ap); | |
51 | va_end(ap); | |
52 | ||
53 | g_assert(data->obj != NULL); | |
54 | ||
55 | data->qiv = qmp_input_visitor_new(data->obj); | |
56 | g_assert(data->qiv != NULL); | |
57 | ||
58 | v = qmp_input_get_visitor(data->qiv); | |
59 | g_assert(v != NULL); | |
60 | ||
61 | return v; | |
62 | } | |
63 | ||
199e0f17 MR |
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) | |
70 | */ | |
71 | static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data, | |
72 | const char *json_string) | |
73 | { | |
74 | Visitor *v; | |
75 | ||
76 | data->obj = qobject_from_json(json_string); | |
77 | ||
78 | g_assert(data->obj != NULL); | |
79 | ||
80 | data->qiv = qmp_input_visitor_new(data->obj); | |
81 | g_assert(data->qiv != NULL); | |
82 | ||
83 | v = qmp_input_get_visitor(data->qiv); | |
84 | g_assert(v != NULL); | |
85 | ||
86 | return v; | |
87 | } | |
88 | ||
d88f5fd1 LC |
89 | static void test_visitor_in_int(TestInputVisitorData *data, |
90 | const void *unused) | |
91 | { | |
92 | int64_t res = 0, value = -42; | |
e940f543 | 93 | Error *err = NULL; |
d88f5fd1 LC |
94 | Visitor *v; |
95 | ||
aba2107a | 96 | v = visitor_input_test_init(data, "%" PRId64, value); |
d88f5fd1 | 97 | |
e940f543 MA |
98 | visit_type_int(v, &res, NULL, &err); |
99 | g_assert(!err); | |
d88f5fd1 LC |
100 | g_assert_cmpint(res, ==, value); |
101 | } | |
102 | ||
e92cfa0d MR |
103 | static void test_visitor_in_int_overflow(TestInputVisitorData *data, |
104 | const void *unused) | |
105 | { | |
106 | int64_t res = 0; | |
e940f543 | 107 | Error *err = NULL; |
e92cfa0d MR |
108 | Visitor *v; |
109 | ||
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. | |
113 | */ | |
114 | v = visitor_input_test_init(data, "%f", DBL_MAX); | |
115 | ||
e940f543 MA |
116 | visit_type_int(v, &res, NULL, &err); |
117 | g_assert(err); | |
118 | error_free(err); | |
e92cfa0d MR |
119 | } |
120 | ||
d88f5fd1 LC |
121 | static void test_visitor_in_bool(TestInputVisitorData *data, |
122 | const void *unused) | |
123 | { | |
e940f543 | 124 | Error *err = NULL; |
d88f5fd1 LC |
125 | bool res = false; |
126 | Visitor *v; | |
127 | ||
128 | v = visitor_input_test_init(data, "true"); | |
129 | ||
e940f543 MA |
130 | visit_type_bool(v, &res, NULL, &err); |
131 | g_assert(!err); | |
d88f5fd1 LC |
132 | g_assert_cmpint(res, ==, true); |
133 | } | |
134 | ||
135 | static void test_visitor_in_number(TestInputVisitorData *data, | |
136 | const void *unused) | |
137 | { | |
138 | double res = 0, value = 3.14; | |
e940f543 | 139 | Error *err = NULL; |
d88f5fd1 LC |
140 | Visitor *v; |
141 | ||
142 | v = visitor_input_test_init(data, "%f", value); | |
143 | ||
e940f543 MA |
144 | visit_type_number(v, &res, NULL, &err); |
145 | g_assert(!err); | |
d88f5fd1 LC |
146 | g_assert_cmpfloat(res, ==, value); |
147 | } | |
148 | ||
149 | static void test_visitor_in_string(TestInputVisitorData *data, | |
150 | const void *unused) | |
151 | { | |
152 | char *res = NULL, *value = (char *) "Q E M U"; | |
e940f543 | 153 | Error *err = NULL; |
d88f5fd1 LC |
154 | Visitor *v; |
155 | ||
156 | v = visitor_input_test_init(data, "%s", value); | |
157 | ||
e940f543 MA |
158 | visit_type_str(v, &res, NULL, &err); |
159 | g_assert(!err); | |
d88f5fd1 LC |
160 | g_assert_cmpstr(res, ==, value); |
161 | ||
162 | g_free(res); | |
163 | } | |
164 | ||
165 | static void test_visitor_in_enum(TestInputVisitorData *data, | |
166 | const void *unused) | |
167 | { | |
e940f543 | 168 | Error *err = NULL; |
d88f5fd1 LC |
169 | Visitor *v; |
170 | EnumOne i; | |
171 | ||
172 | for (i = 0; EnumOne_lookup[i]; i++) { | |
173 | EnumOne res = -1; | |
174 | ||
175 | v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]); | |
176 | ||
e940f543 MA |
177 | visit_type_EnumOne(v, &res, NULL, &err); |
178 | g_assert(!err); | |
d88f5fd1 LC |
179 | g_assert_cmpint(i, ==, res); |
180 | ||
181 | visitor_input_teardown(data, NULL); | |
182 | } | |
183 | ||
184 | data->obj = NULL; | |
185 | data->qiv = NULL; | |
186 | } | |
187 | ||
188 | typedef struct TestStruct | |
189 | { | |
190 | int64_t integer; | |
191 | bool boolean; | |
192 | char *string; | |
193 | } TestStruct; | |
194 | ||
195 | static void visit_type_TestStruct(Visitor *v, TestStruct **obj, | |
196 | const char *name, Error **errp) | |
197 | { | |
d195325b | 198 | Error *err = NULL; |
196857f8 MA |
199 | |
200 | visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct), | |
201 | &err); | |
297a3646 MA |
202 | if (err) { |
203 | goto out; | |
d195325b | 204 | } |
297a3646 MA |
205 | visit_type_int(v, &(*obj)->integer, "integer", &err); |
206 | if (err) { | |
207 | goto out_end; | |
208 | } | |
209 | visit_type_bool(v, &(*obj)->boolean, "boolean", &err); | |
210 | if (err) { | |
211 | goto out_end; | |
212 | } | |
213 | visit_type_str(v, &(*obj)->string, "string", &err); | |
214 | ||
215 | out_end: | |
216 | error_propagate(errp, err); | |
217 | err = NULL; | |
218 | visit_end_struct(v, &err); | |
219 | out: | |
196857f8 | 220 | error_propagate(errp, err); |
d88f5fd1 LC |
221 | } |
222 | ||
223 | static void test_visitor_in_struct(TestInputVisitorData *data, | |
224 | const void *unused) | |
225 | { | |
226 | TestStruct *p = NULL; | |
e940f543 | 227 | Error *err = NULL; |
d88f5fd1 LC |
228 | Visitor *v; |
229 | ||
230 | v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }"); | |
231 | ||
e940f543 MA |
232 | visit_type_TestStruct(v, &p, NULL, &err); |
233 | g_assert(!err); | |
d88f5fd1 LC |
234 | g_assert_cmpint(p->integer, ==, -42); |
235 | g_assert(p->boolean == true); | |
236 | g_assert_cmpstr(p->string, ==, "foo"); | |
237 | ||
238 | g_free(p->string); | |
239 | g_free(p); | |
240 | } | |
241 | ||
242 | static void check_and_free_str(char *str, const char *cmp) | |
243 | { | |
244 | g_assert_cmpstr(str, ==, cmp); | |
245 | g_free(str); | |
246 | } | |
247 | ||
248 | static void test_visitor_in_struct_nested(TestInputVisitorData *data, | |
249 | const void *unused) | |
250 | { | |
251 | UserDefNested *udp = NULL; | |
e940f543 | 252 | Error *err = NULL; |
d88f5fd1 LC |
253 | Visitor *v; |
254 | ||
255 | v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}"); | |
256 | ||
e940f543 MA |
257 | visit_type_UserDefNested(v, &udp, NULL, &err); |
258 | g_assert(!err); | |
d88f5fd1 LC |
259 | |
260 | check_and_free_str(udp->string0, "string0"); | |
261 | check_and_free_str(udp->dict1.string1, "string1"); | |
aabbd472 | 262 | g_assert_cmpint(udp->dict1.dict2.userdef1->base->integer, ==, 42); |
d88f5fd1 LC |
263 | check_and_free_str(udp->dict1.dict2.userdef1->string, "string"); |
264 | check_and_free_str(udp->dict1.dict2.string2, "string2"); | |
265 | g_assert(udp->dict1.has_dict3 == false); | |
266 | ||
267 | g_free(udp->dict1.dict2.userdef1); | |
268 | g_free(udp); | |
269 | } | |
270 | ||
271 | static void test_visitor_in_list(TestInputVisitorData *data, | |
272 | const void *unused) | |
273 | { | |
274 | UserDefOneList *item, *head = NULL; | |
e940f543 | 275 | Error *err = NULL; |
d88f5fd1 LC |
276 | Visitor *v; |
277 | int i; | |
278 | ||
279 | v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); | |
280 | ||
e940f543 MA |
281 | visit_type_UserDefOneList(v, &head, NULL, &err); |
282 | g_assert(!err); | |
d88f5fd1 LC |
283 | g_assert(head != NULL); |
284 | ||
285 | for (i = 0, item = head; item; item = item->next, i++) { | |
286 | char string[12]; | |
287 | ||
288 | snprintf(string, sizeof(string), "string%d", i); | |
289 | g_assert_cmpstr(item->value->string, ==, string); | |
aabbd472 | 290 | g_assert_cmpint(item->value->base->integer, ==, 42 + i); |
d88f5fd1 LC |
291 | } |
292 | ||
293 | qapi_free_UserDefOneList(head); | |
294 | } | |
295 | ||
2fc00432 MA |
296 | static void test_visitor_in_union_flat(TestInputVisitorData *data, |
297 | const void *unused) | |
298 | { | |
299 | Visitor *v; | |
300 | Error *err = NULL; | |
301 | UserDefFlatUnion *tmp; | |
302 | ||
5223070c WX |
303 | v = visitor_input_test_init(data, |
304 | "{ 'enum1': 'value1', " | |
305 | "'string': 'str', " | |
306 | "'boolean': true }"); | |
2fc00432 MA |
307 | /* TODO when generator bug is fixed, add 'integer': 41 */ |
308 | ||
309 | visit_type_UserDefFlatUnion(v, &tmp, NULL, &err); | |
310 | g_assert(err == NULL); | |
5223070c WX |
311 | g_assert_cmpint(tmp->kind, ==, ENUM_ONE_VALUE1); |
312 | g_assert_cmpstr(tmp->string, ==, "str"); | |
2fc00432 | 313 | /* TODO g_assert_cmpint(tmp->integer, ==, 41); */ |
5223070c | 314 | g_assert_cmpint(tmp->value1->boolean, ==, true); |
2fc00432 MA |
315 | qapi_free_UserDefFlatUnion(tmp); |
316 | } | |
317 | ||
2c38b600 MA |
318 | static void test_visitor_in_union_anon(TestInputVisitorData *data, |
319 | const void *unused) | |
320 | { | |
321 | Visitor *v; | |
322 | Error *err = NULL; | |
323 | UserDefAnonUnion *tmp; | |
324 | ||
325 | v = visitor_input_test_init(data, "42"); | |
326 | ||
327 | visit_type_UserDefAnonUnion(v, &tmp, NULL, &err); | |
328 | g_assert(err == NULL); | |
329 | g_assert_cmpint(tmp->kind, ==, USER_DEF_ANON_UNION_KIND_I); | |
330 | g_assert_cmpint(tmp->i, ==, 42); | |
331 | qapi_free_UserDefAnonUnion(tmp); | |
332 | } | |
333 | ||
199e0f17 MR |
334 | static void test_native_list_integer_helper(TestInputVisitorData *data, |
335 | const void *unused, | |
336 | UserDefNativeListUnionKind kind) | |
337 | { | |
338 | UserDefNativeListUnion *cvalue = NULL; | |
339 | Error *err = NULL; | |
340 | Visitor *v; | |
341 | GString *gstr_list = g_string_new(""); | |
342 | GString *gstr_union = g_string_new(""); | |
343 | int i; | |
344 | ||
345 | for (i = 0; i < 32; i++) { | |
346 | g_string_append_printf(gstr_list, "%d", i); | |
347 | if (i != 31) { | |
348 | g_string_append(gstr_list, ", "); | |
349 | } | |
350 | } | |
351 | g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }", | |
352 | UserDefNativeListUnionKind_lookup[kind], | |
353 | gstr_list->str); | |
354 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
355 | ||
356 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
357 | g_assert(err == NULL); | |
358 | g_assert(cvalue != NULL); | |
359 | g_assert_cmpint(cvalue->kind, ==, kind); | |
360 | ||
361 | switch (kind) { | |
362 | case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: { | |
363 | intList *elem = NULL; | |
364 | for (i = 0, elem = cvalue->integer; elem; elem = elem->next, i++) { | |
365 | g_assert_cmpint(elem->value, ==, i); | |
366 | } | |
367 | break; | |
368 | } | |
369 | case USER_DEF_NATIVE_LIST_UNION_KIND_S8: { | |
370 | int8List *elem = NULL; | |
371 | for (i = 0, elem = cvalue->s8; elem; elem = elem->next, i++) { | |
372 | g_assert_cmpint(elem->value, ==, i); | |
373 | } | |
374 | break; | |
375 | } | |
376 | case USER_DEF_NATIVE_LIST_UNION_KIND_S16: { | |
377 | int16List *elem = NULL; | |
378 | for (i = 0, elem = cvalue->s16; elem; elem = elem->next, i++) { | |
379 | g_assert_cmpint(elem->value, ==, i); | |
380 | } | |
381 | break; | |
382 | } | |
383 | case USER_DEF_NATIVE_LIST_UNION_KIND_S32: { | |
384 | int32List *elem = NULL; | |
385 | for (i = 0, elem = cvalue->s32; elem; elem = elem->next, i++) { | |
386 | g_assert_cmpint(elem->value, ==, i); | |
387 | } | |
388 | break; | |
389 | } | |
390 | case USER_DEF_NATIVE_LIST_UNION_KIND_S64: { | |
391 | int64List *elem = NULL; | |
392 | for (i = 0, elem = cvalue->s64; elem; elem = elem->next, i++) { | |
393 | g_assert_cmpint(elem->value, ==, i); | |
394 | } | |
395 | break; | |
396 | } | |
397 | case USER_DEF_NATIVE_LIST_UNION_KIND_U8: { | |
398 | uint8List *elem = NULL; | |
399 | for (i = 0, elem = cvalue->u8; elem; elem = elem->next, i++) { | |
400 | g_assert_cmpint(elem->value, ==, i); | |
401 | } | |
402 | break; | |
403 | } | |
404 | case USER_DEF_NATIVE_LIST_UNION_KIND_U16: { | |
405 | uint16List *elem = NULL; | |
406 | for (i = 0, elem = cvalue->u16; elem; elem = elem->next, i++) { | |
407 | g_assert_cmpint(elem->value, ==, i); | |
408 | } | |
409 | break; | |
410 | } | |
411 | case USER_DEF_NATIVE_LIST_UNION_KIND_U32: { | |
412 | uint32List *elem = NULL; | |
413 | for (i = 0, elem = cvalue->u32; elem; elem = elem->next, i++) { | |
414 | g_assert_cmpint(elem->value, ==, i); | |
415 | } | |
416 | break; | |
417 | } | |
418 | case USER_DEF_NATIVE_LIST_UNION_KIND_U64: { | |
419 | uint64List *elem = NULL; | |
420 | for (i = 0, elem = cvalue->u64; elem; elem = elem->next, i++) { | |
421 | g_assert_cmpint(elem->value, ==, i); | |
422 | } | |
423 | break; | |
424 | } | |
425 | default: | |
dfc6f865 | 426 | g_assert_not_reached(); |
199e0f17 MR |
427 | } |
428 | ||
429 | g_string_free(gstr_union, true); | |
430 | g_string_free(gstr_list, true); | |
431 | qapi_free_UserDefNativeListUnion(cvalue); | |
432 | } | |
433 | ||
434 | static void test_visitor_in_native_list_int(TestInputVisitorData *data, | |
435 | const void *unused) | |
436 | { | |
437 | test_native_list_integer_helper(data, unused, | |
438 | USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER); | |
439 | } | |
440 | ||
441 | static void test_visitor_in_native_list_int8(TestInputVisitorData *data, | |
442 | const void *unused) | |
443 | { | |
444 | test_native_list_integer_helper(data, unused, | |
445 | USER_DEF_NATIVE_LIST_UNION_KIND_S8); | |
446 | } | |
447 | ||
448 | static void test_visitor_in_native_list_int16(TestInputVisitorData *data, | |
449 | const void *unused) | |
450 | { | |
451 | test_native_list_integer_helper(data, unused, | |
452 | USER_DEF_NATIVE_LIST_UNION_KIND_S16); | |
453 | } | |
454 | ||
455 | static void test_visitor_in_native_list_int32(TestInputVisitorData *data, | |
456 | const void *unused) | |
457 | { | |
458 | test_native_list_integer_helper(data, unused, | |
459 | USER_DEF_NATIVE_LIST_UNION_KIND_S32); | |
460 | } | |
461 | ||
462 | static void test_visitor_in_native_list_int64(TestInputVisitorData *data, | |
463 | const void *unused) | |
464 | { | |
465 | test_native_list_integer_helper(data, unused, | |
466 | USER_DEF_NATIVE_LIST_UNION_KIND_S64); | |
467 | } | |
468 | ||
469 | static void test_visitor_in_native_list_uint8(TestInputVisitorData *data, | |
470 | const void *unused) | |
471 | { | |
472 | test_native_list_integer_helper(data, unused, | |
473 | USER_DEF_NATIVE_LIST_UNION_KIND_U8); | |
474 | } | |
475 | ||
476 | static void test_visitor_in_native_list_uint16(TestInputVisitorData *data, | |
477 | const void *unused) | |
478 | { | |
479 | test_native_list_integer_helper(data, unused, | |
480 | USER_DEF_NATIVE_LIST_UNION_KIND_U16); | |
481 | } | |
482 | ||
483 | static void test_visitor_in_native_list_uint32(TestInputVisitorData *data, | |
484 | const void *unused) | |
485 | { | |
486 | test_native_list_integer_helper(data, unused, | |
487 | USER_DEF_NATIVE_LIST_UNION_KIND_U32); | |
488 | } | |
489 | ||
490 | static void test_visitor_in_native_list_uint64(TestInputVisitorData *data, | |
491 | const void *unused) | |
492 | { | |
493 | test_native_list_integer_helper(data, unused, | |
494 | USER_DEF_NATIVE_LIST_UNION_KIND_U64); | |
495 | } | |
496 | ||
497 | static void test_visitor_in_native_list_bool(TestInputVisitorData *data, | |
498 | const void *unused) | |
499 | { | |
500 | UserDefNativeListUnion *cvalue = NULL; | |
501 | boolList *elem = NULL; | |
502 | Error *err = NULL; | |
503 | Visitor *v; | |
504 | GString *gstr_list = g_string_new(""); | |
505 | GString *gstr_union = g_string_new(""); | |
506 | int i; | |
507 | ||
508 | for (i = 0; i < 32; i++) { | |
509 | g_string_append_printf(gstr_list, "%s", | |
510 | (i % 3 == 0) ? "true" : "false"); | |
511 | if (i != 31) { | |
512 | g_string_append(gstr_list, ", "); | |
513 | } | |
514 | } | |
515 | g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }", | |
516 | gstr_list->str); | |
517 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
518 | ||
519 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
520 | g_assert(err == NULL); | |
521 | g_assert(cvalue != NULL); | |
522 | g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN); | |
523 | ||
524 | for (i = 0, elem = cvalue->boolean; elem; elem = elem->next, i++) { | |
525 | g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0); | |
526 | } | |
527 | ||
528 | g_string_free(gstr_union, true); | |
529 | g_string_free(gstr_list, true); | |
530 | qapi_free_UserDefNativeListUnion(cvalue); | |
531 | } | |
532 | ||
533 | static void test_visitor_in_native_list_string(TestInputVisitorData *data, | |
534 | const void *unused) | |
535 | { | |
536 | UserDefNativeListUnion *cvalue = NULL; | |
537 | strList *elem = NULL; | |
538 | Error *err = NULL; | |
539 | Visitor *v; | |
540 | GString *gstr_list = g_string_new(""); | |
541 | GString *gstr_union = g_string_new(""); | |
542 | int i; | |
543 | ||
544 | for (i = 0; i < 32; i++) { | |
545 | g_string_append_printf(gstr_list, "'%d'", i); | |
546 | if (i != 31) { | |
547 | g_string_append(gstr_list, ", "); | |
548 | } | |
549 | } | |
550 | g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }", | |
551 | gstr_list->str); | |
552 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
553 | ||
554 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
555 | g_assert(err == NULL); | |
556 | g_assert(cvalue != NULL); | |
557 | g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING); | |
558 | ||
559 | for (i = 0, elem = cvalue->string; elem; elem = elem->next, i++) { | |
560 | gchar str[8]; | |
561 | sprintf(str, "%d", i); | |
562 | g_assert_cmpstr(elem->value, ==, str); | |
563 | } | |
564 | ||
565 | g_string_free(gstr_union, true); | |
566 | g_string_free(gstr_list, true); | |
567 | qapi_free_UserDefNativeListUnion(cvalue); | |
568 | } | |
569 | ||
570 | #define DOUBLE_STR_MAX 16 | |
571 | ||
572 | static void test_visitor_in_native_list_number(TestInputVisitorData *data, | |
573 | const void *unused) | |
574 | { | |
575 | UserDefNativeListUnion *cvalue = NULL; | |
576 | numberList *elem = NULL; | |
577 | Error *err = NULL; | |
578 | Visitor *v; | |
579 | GString *gstr_list = g_string_new(""); | |
580 | GString *gstr_union = g_string_new(""); | |
581 | int i; | |
582 | ||
583 | for (i = 0; i < 32; i++) { | |
584 | g_string_append_printf(gstr_list, "%f", (double)i / 3); | |
585 | if (i != 31) { | |
586 | g_string_append(gstr_list, ", "); | |
587 | } | |
588 | } | |
589 | g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }", | |
590 | gstr_list->str); | |
591 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
592 | ||
593 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
594 | g_assert(err == NULL); | |
595 | g_assert(cvalue != NULL); | |
596 | g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER); | |
597 | ||
598 | for (i = 0, elem = cvalue->number; elem; elem = elem->next, i++) { | |
599 | GString *double_expected = g_string_new(""); | |
600 | GString *double_actual = g_string_new(""); | |
601 | ||
602 | g_string_printf(double_expected, "%.6f", (double)i / 3); | |
603 | g_string_printf(double_actual, "%.6f", elem->value); | |
604 | g_assert_cmpstr(double_expected->str, ==, double_actual->str); | |
605 | ||
606 | g_string_free(double_expected, true); | |
607 | g_string_free(double_actual, true); | |
608 | } | |
609 | ||
610 | g_string_free(gstr_union, true); | |
611 | g_string_free(gstr_list, true); | |
612 | qapi_free_UserDefNativeListUnion(cvalue); | |
613 | } | |
614 | ||
d88f5fd1 LC |
615 | static void input_visitor_test_add(const char *testpath, |
616 | TestInputVisitorData *data, | |
617 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) | |
618 | { | |
619 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, | |
620 | visitor_input_teardown); | |
621 | } | |
622 | ||
3dcf71f6 PB |
623 | static void test_visitor_in_errors(TestInputVisitorData *data, |
624 | const void *unused) | |
625 | { | |
626 | TestStruct *p = NULL; | |
e940f543 | 627 | Error *err = NULL; |
3dcf71f6 PB |
628 | Visitor *v; |
629 | ||
630 | v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }"); | |
631 | ||
e940f543 MA |
632 | visit_type_TestStruct(v, &p, NULL, &err); |
633 | g_assert(err); | |
3dcf71f6 PB |
634 | g_assert(p->string == NULL); |
635 | ||
e940f543 | 636 | error_free(err); |
3dcf71f6 PB |
637 | g_free(p->string); |
638 | g_free(p); | |
639 | } | |
640 | ||
d88f5fd1 LC |
641 | int main(int argc, char **argv) |
642 | { | |
643 | TestInputVisitorData in_visitor_data; | |
644 | ||
645 | g_test_init(&argc, &argv, NULL); | |
646 | ||
647 | input_visitor_test_add("/visitor/input/int", | |
648 | &in_visitor_data, test_visitor_in_int); | |
e92cfa0d MR |
649 | input_visitor_test_add("/visitor/input/int_overflow", |
650 | &in_visitor_data, test_visitor_in_int_overflow); | |
d88f5fd1 LC |
651 | input_visitor_test_add("/visitor/input/bool", |
652 | &in_visitor_data, test_visitor_in_bool); | |
653 | input_visitor_test_add("/visitor/input/number", | |
654 | &in_visitor_data, test_visitor_in_number); | |
655 | input_visitor_test_add("/visitor/input/string", | |
805017b7 | 656 | &in_visitor_data, test_visitor_in_string); |
d88f5fd1 | 657 | input_visitor_test_add("/visitor/input/enum", |
805017b7 | 658 | &in_visitor_data, test_visitor_in_enum); |
d88f5fd1 | 659 | input_visitor_test_add("/visitor/input/struct", |
805017b7 | 660 | &in_visitor_data, test_visitor_in_struct); |
d88f5fd1 | 661 | input_visitor_test_add("/visitor/input/struct-nested", |
805017b7 | 662 | &in_visitor_data, test_visitor_in_struct_nested); |
d88f5fd1 | 663 | input_visitor_test_add("/visitor/input/list", |
805017b7 | 664 | &in_visitor_data, test_visitor_in_list); |
2fc00432 | 665 | input_visitor_test_add("/visitor/input/union-flat", |
805017b7 | 666 | &in_visitor_data, test_visitor_in_union_flat); |
2c38b600 | 667 | input_visitor_test_add("/visitor/input/union-anon", |
805017b7 | 668 | &in_visitor_data, test_visitor_in_union_anon); |
3dcf71f6 | 669 | input_visitor_test_add("/visitor/input/errors", |
805017b7 | 670 | &in_visitor_data, test_visitor_in_errors); |
199e0f17 | 671 | input_visitor_test_add("/visitor/input/native_list/int", |
805017b7 EB |
672 | &in_visitor_data, |
673 | test_visitor_in_native_list_int); | |
199e0f17 | 674 | input_visitor_test_add("/visitor/input/native_list/int8", |
805017b7 EB |
675 | &in_visitor_data, |
676 | test_visitor_in_native_list_int8); | |
199e0f17 | 677 | input_visitor_test_add("/visitor/input/native_list/int16", |
805017b7 EB |
678 | &in_visitor_data, |
679 | test_visitor_in_native_list_int16); | |
199e0f17 | 680 | input_visitor_test_add("/visitor/input/native_list/int32", |
805017b7 EB |
681 | &in_visitor_data, |
682 | test_visitor_in_native_list_int32); | |
199e0f17 | 683 | input_visitor_test_add("/visitor/input/native_list/int64", |
805017b7 EB |
684 | &in_visitor_data, |
685 | test_visitor_in_native_list_int64); | |
199e0f17 | 686 | input_visitor_test_add("/visitor/input/native_list/uint8", |
805017b7 EB |
687 | &in_visitor_data, |
688 | test_visitor_in_native_list_uint8); | |
199e0f17 | 689 | input_visitor_test_add("/visitor/input/native_list/uint16", |
805017b7 EB |
690 | &in_visitor_data, |
691 | test_visitor_in_native_list_uint16); | |
199e0f17 | 692 | input_visitor_test_add("/visitor/input/native_list/uint32", |
805017b7 EB |
693 | &in_visitor_data, |
694 | test_visitor_in_native_list_uint32); | |
199e0f17 | 695 | input_visitor_test_add("/visitor/input/native_list/uint64", |
805017b7 EB |
696 | &in_visitor_data, |
697 | test_visitor_in_native_list_uint64); | |
199e0f17 | 698 | input_visitor_test_add("/visitor/input/native_list/bool", |
805017b7 | 699 | &in_visitor_data, test_visitor_in_native_list_bool); |
199e0f17 | 700 | input_visitor_test_add("/visitor/input/native_list/str", |
805017b7 EB |
701 | &in_visitor_data, |
702 | test_visitor_in_native_list_string); | |
199e0f17 | 703 | input_visitor_test_add("/visitor/input/native_list/number", |
805017b7 EB |
704 | &in_visitor_data, |
705 | test_visitor_in_native_list_number); | |
d88f5fd1 LC |
706 | |
707 | g_test_run(); | |
708 | ||
709 | return 0; | |
710 | } |