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