]>
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 | { | |
b6fcf32d | 251 | UserDefTwo *udp = NULL; |
e940f543 | 252 | Error *err = NULL; |
d88f5fd1 LC |
253 | Visitor *v; |
254 | ||
b6fcf32d EB |
255 | v = visitor_input_test_init(data, "{ 'string0': 'string0', " |
256 | "'dict1': { 'string1': 'string1', " | |
257 | "'dict2': { 'userdef': { 'integer': 42, " | |
258 | "'string': 'string' }, 'string': 'string2'}}}"); | |
d88f5fd1 | 259 | |
b6fcf32d | 260 | visit_type_UserDefTwo(v, &udp, NULL, &err); |
e940f543 | 261 | g_assert(!err); |
d88f5fd1 LC |
262 | |
263 | check_and_free_str(udp->string0, "string0"); | |
6446a592 EB |
264 | check_and_free_str(udp->dict1->string1, "string1"); |
265 | g_assert_cmpint(udp->dict1->dict2->userdef->base->integer, ==, 42); | |
266 | check_and_free_str(udp->dict1->dict2->userdef->string, "string"); | |
267 | check_and_free_str(udp->dict1->dict2->string, "string2"); | |
268 | g_assert(udp->dict1->has_dict3 == false); | |
269 | ||
270 | g_free(udp->dict1->dict2->userdef); | |
271 | g_free(udp->dict1->dict2); | |
272 | g_free(udp->dict1); | |
d88f5fd1 LC |
273 | g_free(udp); |
274 | } | |
275 | ||
276 | static void test_visitor_in_list(TestInputVisitorData *data, | |
277 | const void *unused) | |
278 | { | |
279 | UserDefOneList *item, *head = NULL; | |
e940f543 | 280 | Error *err = NULL; |
d88f5fd1 LC |
281 | Visitor *v; |
282 | int i; | |
283 | ||
284 | v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]"); | |
285 | ||
e940f543 MA |
286 | visit_type_UserDefOneList(v, &head, NULL, &err); |
287 | g_assert(!err); | |
d88f5fd1 LC |
288 | g_assert(head != NULL); |
289 | ||
290 | for (i = 0, item = head; item; item = item->next, i++) { | |
291 | char string[12]; | |
292 | ||
293 | snprintf(string, sizeof(string), "string%d", i); | |
294 | g_assert_cmpstr(item->value->string, ==, string); | |
aabbd472 | 295 | g_assert_cmpint(item->value->base->integer, ==, 42 + i); |
d88f5fd1 LC |
296 | } |
297 | ||
298 | qapi_free_UserDefOneList(head); | |
299 | } | |
300 | ||
2fc00432 MA |
301 | static void test_visitor_in_union_flat(TestInputVisitorData *data, |
302 | const void *unused) | |
303 | { | |
304 | Visitor *v; | |
305 | Error *err = NULL; | |
306 | UserDefFlatUnion *tmp; | |
307 | ||
5223070c WX |
308 | v = visitor_input_test_init(data, |
309 | "{ 'enum1': 'value1', " | |
310 | "'string': 'str', " | |
311 | "'boolean': true }"); | |
2fc00432 MA |
312 | /* TODO when generator bug is fixed, add 'integer': 41 */ |
313 | ||
314 | visit_type_UserDefFlatUnion(v, &tmp, NULL, &err); | |
315 | g_assert(err == NULL); | |
0f61af3e | 316 | g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1); |
5223070c | 317 | g_assert_cmpstr(tmp->string, ==, "str"); |
2fc00432 | 318 | /* TODO g_assert_cmpint(tmp->integer, ==, 41); */ |
5223070c | 319 | g_assert_cmpint(tmp->value1->boolean, ==, true); |
2fc00432 MA |
320 | qapi_free_UserDefFlatUnion(tmp); |
321 | } | |
322 | ||
ab045267 EB |
323 | static void test_visitor_in_alternate(TestInputVisitorData *data, |
324 | const void *unused) | |
2c38b600 MA |
325 | { |
326 | Visitor *v; | |
327 | Error *err = NULL; | |
ab045267 | 328 | UserDefAlternate *tmp; |
2c38b600 MA |
329 | |
330 | v = visitor_input_test_init(data, "42"); | |
331 | ||
ab045267 | 332 | visit_type_UserDefAlternate(v, &tmp, NULL, &err); |
2c38b600 | 333 | g_assert(err == NULL); |
ab045267 | 334 | g_assert_cmpint(tmp->kind, ==, USER_DEF_ALTERNATE_KIND_I); |
2c38b600 | 335 | g_assert_cmpint(tmp->i, ==, 42); |
ab045267 | 336 | qapi_free_UserDefAlternate(tmp); |
2c38b600 MA |
337 | } |
338 | ||
199e0f17 MR |
339 | static void test_native_list_integer_helper(TestInputVisitorData *data, |
340 | const void *unused, | |
341 | UserDefNativeListUnionKind kind) | |
342 | { | |
343 | UserDefNativeListUnion *cvalue = NULL; | |
344 | Error *err = NULL; | |
345 | Visitor *v; | |
346 | GString *gstr_list = g_string_new(""); | |
347 | GString *gstr_union = g_string_new(""); | |
348 | int i; | |
349 | ||
350 | for (i = 0; i < 32; i++) { | |
351 | g_string_append_printf(gstr_list, "%d", i); | |
352 | if (i != 31) { | |
353 | g_string_append(gstr_list, ", "); | |
354 | } | |
355 | } | |
356 | g_string_append_printf(gstr_union, "{ 'type': '%s', 'data': [ %s ] }", | |
357 | UserDefNativeListUnionKind_lookup[kind], | |
358 | gstr_list->str); | |
359 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
360 | ||
361 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
362 | g_assert(err == NULL); | |
363 | g_assert(cvalue != NULL); | |
364 | g_assert_cmpint(cvalue->kind, ==, kind); | |
365 | ||
366 | switch (kind) { | |
367 | case USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER: { | |
368 | intList *elem = NULL; | |
369 | for (i = 0, elem = cvalue->integer; elem; elem = elem->next, i++) { | |
370 | g_assert_cmpint(elem->value, ==, i); | |
371 | } | |
372 | break; | |
373 | } | |
374 | case USER_DEF_NATIVE_LIST_UNION_KIND_S8: { | |
375 | int8List *elem = NULL; | |
376 | for (i = 0, elem = cvalue->s8; elem; elem = elem->next, i++) { | |
377 | g_assert_cmpint(elem->value, ==, i); | |
378 | } | |
379 | break; | |
380 | } | |
381 | case USER_DEF_NATIVE_LIST_UNION_KIND_S16: { | |
382 | int16List *elem = NULL; | |
383 | for (i = 0, elem = cvalue->s16; elem; elem = elem->next, i++) { | |
384 | g_assert_cmpint(elem->value, ==, i); | |
385 | } | |
386 | break; | |
387 | } | |
388 | case USER_DEF_NATIVE_LIST_UNION_KIND_S32: { | |
389 | int32List *elem = NULL; | |
390 | for (i = 0, elem = cvalue->s32; elem; elem = elem->next, i++) { | |
391 | g_assert_cmpint(elem->value, ==, i); | |
392 | } | |
393 | break; | |
394 | } | |
395 | case USER_DEF_NATIVE_LIST_UNION_KIND_S64: { | |
396 | int64List *elem = NULL; | |
397 | for (i = 0, elem = cvalue->s64; elem; elem = elem->next, i++) { | |
398 | g_assert_cmpint(elem->value, ==, i); | |
399 | } | |
400 | break; | |
401 | } | |
402 | case USER_DEF_NATIVE_LIST_UNION_KIND_U8: { | |
403 | uint8List *elem = NULL; | |
404 | for (i = 0, elem = cvalue->u8; elem; elem = elem->next, i++) { | |
405 | g_assert_cmpint(elem->value, ==, i); | |
406 | } | |
407 | break; | |
408 | } | |
409 | case USER_DEF_NATIVE_LIST_UNION_KIND_U16: { | |
410 | uint16List *elem = NULL; | |
411 | for (i = 0, elem = cvalue->u16; elem; elem = elem->next, i++) { | |
412 | g_assert_cmpint(elem->value, ==, i); | |
413 | } | |
414 | break; | |
415 | } | |
416 | case USER_DEF_NATIVE_LIST_UNION_KIND_U32: { | |
417 | uint32List *elem = NULL; | |
418 | for (i = 0, elem = cvalue->u32; elem; elem = elem->next, i++) { | |
419 | g_assert_cmpint(elem->value, ==, i); | |
420 | } | |
421 | break; | |
422 | } | |
423 | case USER_DEF_NATIVE_LIST_UNION_KIND_U64: { | |
424 | uint64List *elem = NULL; | |
425 | for (i = 0, elem = cvalue->u64; elem; elem = elem->next, i++) { | |
426 | g_assert_cmpint(elem->value, ==, i); | |
427 | } | |
428 | break; | |
429 | } | |
430 | default: | |
dfc6f865 | 431 | g_assert_not_reached(); |
199e0f17 MR |
432 | } |
433 | ||
434 | g_string_free(gstr_union, true); | |
435 | g_string_free(gstr_list, true); | |
436 | qapi_free_UserDefNativeListUnion(cvalue); | |
437 | } | |
438 | ||
439 | static void test_visitor_in_native_list_int(TestInputVisitorData *data, | |
440 | const void *unused) | |
441 | { | |
442 | test_native_list_integer_helper(data, unused, | |
443 | USER_DEF_NATIVE_LIST_UNION_KIND_INTEGER); | |
444 | } | |
445 | ||
446 | static void test_visitor_in_native_list_int8(TestInputVisitorData *data, | |
447 | const void *unused) | |
448 | { | |
449 | test_native_list_integer_helper(data, unused, | |
450 | USER_DEF_NATIVE_LIST_UNION_KIND_S8); | |
451 | } | |
452 | ||
453 | static void test_visitor_in_native_list_int16(TestInputVisitorData *data, | |
454 | const void *unused) | |
455 | { | |
456 | test_native_list_integer_helper(data, unused, | |
457 | USER_DEF_NATIVE_LIST_UNION_KIND_S16); | |
458 | } | |
459 | ||
460 | static void test_visitor_in_native_list_int32(TestInputVisitorData *data, | |
461 | const void *unused) | |
462 | { | |
463 | test_native_list_integer_helper(data, unused, | |
464 | USER_DEF_NATIVE_LIST_UNION_KIND_S32); | |
465 | } | |
466 | ||
467 | static void test_visitor_in_native_list_int64(TestInputVisitorData *data, | |
468 | const void *unused) | |
469 | { | |
470 | test_native_list_integer_helper(data, unused, | |
471 | USER_DEF_NATIVE_LIST_UNION_KIND_S64); | |
472 | } | |
473 | ||
474 | static void test_visitor_in_native_list_uint8(TestInputVisitorData *data, | |
475 | const void *unused) | |
476 | { | |
477 | test_native_list_integer_helper(data, unused, | |
478 | USER_DEF_NATIVE_LIST_UNION_KIND_U8); | |
479 | } | |
480 | ||
481 | static void test_visitor_in_native_list_uint16(TestInputVisitorData *data, | |
482 | const void *unused) | |
483 | { | |
484 | test_native_list_integer_helper(data, unused, | |
485 | USER_DEF_NATIVE_LIST_UNION_KIND_U16); | |
486 | } | |
487 | ||
488 | static void test_visitor_in_native_list_uint32(TestInputVisitorData *data, | |
489 | const void *unused) | |
490 | { | |
491 | test_native_list_integer_helper(data, unused, | |
492 | USER_DEF_NATIVE_LIST_UNION_KIND_U32); | |
493 | } | |
494 | ||
495 | static void test_visitor_in_native_list_uint64(TestInputVisitorData *data, | |
496 | const void *unused) | |
497 | { | |
498 | test_native_list_integer_helper(data, unused, | |
499 | USER_DEF_NATIVE_LIST_UNION_KIND_U64); | |
500 | } | |
501 | ||
502 | static void test_visitor_in_native_list_bool(TestInputVisitorData *data, | |
503 | const void *unused) | |
504 | { | |
505 | UserDefNativeListUnion *cvalue = NULL; | |
506 | boolList *elem = NULL; | |
507 | Error *err = NULL; | |
508 | Visitor *v; | |
509 | GString *gstr_list = g_string_new(""); | |
510 | GString *gstr_union = g_string_new(""); | |
511 | int i; | |
512 | ||
513 | for (i = 0; i < 32; i++) { | |
514 | g_string_append_printf(gstr_list, "%s", | |
515 | (i % 3 == 0) ? "true" : "false"); | |
516 | if (i != 31) { | |
517 | g_string_append(gstr_list, ", "); | |
518 | } | |
519 | } | |
520 | g_string_append_printf(gstr_union, "{ 'type': 'boolean', 'data': [ %s ] }", | |
521 | gstr_list->str); | |
522 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
523 | ||
524 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
525 | g_assert(err == NULL); | |
526 | g_assert(cvalue != NULL); | |
527 | g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_BOOLEAN); | |
528 | ||
529 | for (i = 0, elem = cvalue->boolean; elem; elem = elem->next, i++) { | |
530 | g_assert_cmpint(elem->value, ==, (i % 3 == 0) ? 1 : 0); | |
531 | } | |
532 | ||
533 | g_string_free(gstr_union, true); | |
534 | g_string_free(gstr_list, true); | |
535 | qapi_free_UserDefNativeListUnion(cvalue); | |
536 | } | |
537 | ||
538 | static void test_visitor_in_native_list_string(TestInputVisitorData *data, | |
539 | const void *unused) | |
540 | { | |
541 | UserDefNativeListUnion *cvalue = NULL; | |
542 | strList *elem = NULL; | |
543 | Error *err = NULL; | |
544 | Visitor *v; | |
545 | GString *gstr_list = g_string_new(""); | |
546 | GString *gstr_union = g_string_new(""); | |
547 | int i; | |
548 | ||
549 | for (i = 0; i < 32; i++) { | |
550 | g_string_append_printf(gstr_list, "'%d'", i); | |
551 | if (i != 31) { | |
552 | g_string_append(gstr_list, ", "); | |
553 | } | |
554 | } | |
555 | g_string_append_printf(gstr_union, "{ 'type': 'string', 'data': [ %s ] }", | |
556 | gstr_list->str); | |
557 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
558 | ||
559 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
560 | g_assert(err == NULL); | |
561 | g_assert(cvalue != NULL); | |
562 | g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_STRING); | |
563 | ||
564 | for (i = 0, elem = cvalue->string; elem; elem = elem->next, i++) { | |
565 | gchar str[8]; | |
566 | sprintf(str, "%d", i); | |
567 | g_assert_cmpstr(elem->value, ==, str); | |
568 | } | |
569 | ||
570 | g_string_free(gstr_union, true); | |
571 | g_string_free(gstr_list, true); | |
572 | qapi_free_UserDefNativeListUnion(cvalue); | |
573 | } | |
574 | ||
575 | #define DOUBLE_STR_MAX 16 | |
576 | ||
577 | static void test_visitor_in_native_list_number(TestInputVisitorData *data, | |
578 | const void *unused) | |
579 | { | |
580 | UserDefNativeListUnion *cvalue = NULL; | |
581 | numberList *elem = NULL; | |
582 | Error *err = NULL; | |
583 | Visitor *v; | |
584 | GString *gstr_list = g_string_new(""); | |
585 | GString *gstr_union = g_string_new(""); | |
586 | int i; | |
587 | ||
588 | for (i = 0; i < 32; i++) { | |
589 | g_string_append_printf(gstr_list, "%f", (double)i / 3); | |
590 | if (i != 31) { | |
591 | g_string_append(gstr_list, ", "); | |
592 | } | |
593 | } | |
594 | g_string_append_printf(gstr_union, "{ 'type': 'number', 'data': [ %s ] }", | |
595 | gstr_list->str); | |
596 | v = visitor_input_test_init_raw(data, gstr_union->str); | |
597 | ||
598 | visit_type_UserDefNativeListUnion(v, &cvalue, NULL, &err); | |
599 | g_assert(err == NULL); | |
600 | g_assert(cvalue != NULL); | |
601 | g_assert_cmpint(cvalue->kind, ==, USER_DEF_NATIVE_LIST_UNION_KIND_NUMBER); | |
602 | ||
603 | for (i = 0, elem = cvalue->number; elem; elem = elem->next, i++) { | |
604 | GString *double_expected = g_string_new(""); | |
605 | GString *double_actual = g_string_new(""); | |
606 | ||
607 | g_string_printf(double_expected, "%.6f", (double)i / 3); | |
608 | g_string_printf(double_actual, "%.6f", elem->value); | |
609 | g_assert_cmpstr(double_expected->str, ==, double_actual->str); | |
610 | ||
611 | g_string_free(double_expected, true); | |
612 | g_string_free(double_actual, true); | |
613 | } | |
614 | ||
615 | g_string_free(gstr_union, true); | |
616 | g_string_free(gstr_list, true); | |
617 | qapi_free_UserDefNativeListUnion(cvalue); | |
618 | } | |
619 | ||
d88f5fd1 LC |
620 | static void input_visitor_test_add(const char *testpath, |
621 | TestInputVisitorData *data, | |
622 | void (*test_func)(TestInputVisitorData *data, const void *user_data)) | |
623 | { | |
624 | g_test_add(testpath, TestInputVisitorData, data, NULL, test_func, | |
625 | visitor_input_teardown); | |
626 | } | |
627 | ||
3dcf71f6 PB |
628 | static void test_visitor_in_errors(TestInputVisitorData *data, |
629 | const void *unused) | |
630 | { | |
631 | TestStruct *p = NULL; | |
e940f543 | 632 | Error *err = NULL; |
3dcf71f6 PB |
633 | Visitor *v; |
634 | ||
635 | v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }"); | |
636 | ||
e940f543 MA |
637 | visit_type_TestStruct(v, &p, NULL, &err); |
638 | g_assert(err); | |
2f52e205 EB |
639 | /* FIXME - a failed parse should not leave a partially-allocated p |
640 | * for us to clean up; this could cause callers to leak memory. */ | |
3dcf71f6 PB |
641 | g_assert(p->string == NULL); |
642 | ||
e940f543 | 643 | error_free(err); |
3dcf71f6 PB |
644 | g_free(p->string); |
645 | g_free(p); | |
646 | } | |
647 | ||
d88f5fd1 LC |
648 | int main(int argc, char **argv) |
649 | { | |
650 | TestInputVisitorData in_visitor_data; | |
651 | ||
652 | g_test_init(&argc, &argv, NULL); | |
653 | ||
654 | input_visitor_test_add("/visitor/input/int", | |
655 | &in_visitor_data, test_visitor_in_int); | |
e92cfa0d MR |
656 | input_visitor_test_add("/visitor/input/int_overflow", |
657 | &in_visitor_data, test_visitor_in_int_overflow); | |
d88f5fd1 LC |
658 | input_visitor_test_add("/visitor/input/bool", |
659 | &in_visitor_data, test_visitor_in_bool); | |
660 | input_visitor_test_add("/visitor/input/number", | |
661 | &in_visitor_data, test_visitor_in_number); | |
662 | input_visitor_test_add("/visitor/input/string", | |
805017b7 | 663 | &in_visitor_data, test_visitor_in_string); |
d88f5fd1 | 664 | input_visitor_test_add("/visitor/input/enum", |
805017b7 | 665 | &in_visitor_data, test_visitor_in_enum); |
d88f5fd1 | 666 | input_visitor_test_add("/visitor/input/struct", |
805017b7 | 667 | &in_visitor_data, test_visitor_in_struct); |
d88f5fd1 | 668 | input_visitor_test_add("/visitor/input/struct-nested", |
805017b7 | 669 | &in_visitor_data, test_visitor_in_struct_nested); |
d88f5fd1 | 670 | input_visitor_test_add("/visitor/input/list", |
805017b7 | 671 | &in_visitor_data, test_visitor_in_list); |
2fc00432 | 672 | input_visitor_test_add("/visitor/input/union-flat", |
805017b7 | 673 | &in_visitor_data, test_visitor_in_union_flat); |
ab045267 EB |
674 | input_visitor_test_add("/visitor/input/alternate", |
675 | &in_visitor_data, test_visitor_in_alternate); | |
3dcf71f6 | 676 | input_visitor_test_add("/visitor/input/errors", |
805017b7 | 677 | &in_visitor_data, test_visitor_in_errors); |
199e0f17 | 678 | input_visitor_test_add("/visitor/input/native_list/int", |
805017b7 EB |
679 | &in_visitor_data, |
680 | test_visitor_in_native_list_int); | |
199e0f17 | 681 | input_visitor_test_add("/visitor/input/native_list/int8", |
805017b7 EB |
682 | &in_visitor_data, |
683 | test_visitor_in_native_list_int8); | |
199e0f17 | 684 | input_visitor_test_add("/visitor/input/native_list/int16", |
805017b7 EB |
685 | &in_visitor_data, |
686 | test_visitor_in_native_list_int16); | |
199e0f17 | 687 | input_visitor_test_add("/visitor/input/native_list/int32", |
805017b7 EB |
688 | &in_visitor_data, |
689 | test_visitor_in_native_list_int32); | |
199e0f17 | 690 | input_visitor_test_add("/visitor/input/native_list/int64", |
805017b7 EB |
691 | &in_visitor_data, |
692 | test_visitor_in_native_list_int64); | |
199e0f17 | 693 | input_visitor_test_add("/visitor/input/native_list/uint8", |
805017b7 EB |
694 | &in_visitor_data, |
695 | test_visitor_in_native_list_uint8); | |
199e0f17 | 696 | input_visitor_test_add("/visitor/input/native_list/uint16", |
805017b7 EB |
697 | &in_visitor_data, |
698 | test_visitor_in_native_list_uint16); | |
199e0f17 | 699 | input_visitor_test_add("/visitor/input/native_list/uint32", |
805017b7 EB |
700 | &in_visitor_data, |
701 | test_visitor_in_native_list_uint32); | |
199e0f17 | 702 | input_visitor_test_add("/visitor/input/native_list/uint64", |
805017b7 EB |
703 | &in_visitor_data, |
704 | test_visitor_in_native_list_uint64); | |
199e0f17 | 705 | input_visitor_test_add("/visitor/input/native_list/bool", |
805017b7 | 706 | &in_visitor_data, test_visitor_in_native_list_bool); |
199e0f17 | 707 | input_visitor_test_add("/visitor/input/native_list/str", |
805017b7 EB |
708 | &in_visitor_data, |
709 | test_visitor_in_native_list_string); | |
199e0f17 | 710 | input_visitor_test_add("/visitor/input/native_list/number", |
805017b7 EB |
711 | &in_visitor_data, |
712 | test_visitor_in_native_list_number); | |
d88f5fd1 LC |
713 | |
714 | g_test_run(); | |
715 | ||
716 | return 0; | |
717 | } |