tmp105: Split out I2C message constants from header
[qemu/opensuse.git] / tests / test-qmp-output-visitor.c
blob71367e6efa6644893fa6d798ac2989dee67214ae
1 /*
2 * QMP Output Visitor unit-tests.
4 * Copyright (C) 2011 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
13 #include <glib.h>
15 #include "qemu-common.h"
16 #include "qapi/qmp-output-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qapi/qmp/types.h"
21 typedef struct TestOutputVisitorData {
22 QmpOutputVisitor *qov;
23 Visitor *ov;
24 } TestOutputVisitorData;
26 static void visitor_output_setup(TestOutputVisitorData *data,
27 const void *unused)
29 data->qov = qmp_output_visitor_new();
30 g_assert(data->qov != NULL);
32 data->ov = qmp_output_get_visitor(data->qov);
33 g_assert(data->ov != NULL);
36 static void visitor_output_teardown(TestOutputVisitorData *data,
37 const void *unused)
39 qmp_output_visitor_cleanup(data->qov);
40 data->qov = NULL;
41 data->ov = NULL;
44 static void test_visitor_out_int(TestOutputVisitorData *data,
45 const void *unused)
47 int64_t value = -42;
48 Error *errp = NULL;
49 QObject *obj;
51 visit_type_int(data->ov, &value, NULL, &errp);
52 g_assert(error_is_set(&errp) == 0);
54 obj = qmp_output_get_qobject(data->qov);
55 g_assert(obj != NULL);
56 g_assert(qobject_type(obj) == QTYPE_QINT);
57 g_assert_cmpint(qint_get_int(qobject_to_qint(obj)), ==, value);
59 qobject_decref(obj);
62 static void test_visitor_out_bool(TestOutputVisitorData *data,
63 const void *unused)
65 Error *errp = NULL;
66 bool value = true;
67 QObject *obj;
69 visit_type_bool(data->ov, &value, NULL, &errp);
70 g_assert(error_is_set(&errp) == 0);
72 obj = qmp_output_get_qobject(data->qov);
73 g_assert(obj != NULL);
74 g_assert(qobject_type(obj) == QTYPE_QBOOL);
75 g_assert(qbool_get_int(qobject_to_qbool(obj)) == value);
77 qobject_decref(obj);
80 static void test_visitor_out_number(TestOutputVisitorData *data,
81 const void *unused)
83 double value = 3.14;
84 Error *errp = NULL;
85 QObject *obj;
87 visit_type_number(data->ov, &value, NULL, &errp);
88 g_assert(error_is_set(&errp) == 0);
90 obj = qmp_output_get_qobject(data->qov);
91 g_assert(obj != NULL);
92 g_assert(qobject_type(obj) == QTYPE_QFLOAT);
93 g_assert(qfloat_get_double(qobject_to_qfloat(obj)) == value);
95 qobject_decref(obj);
98 static void test_visitor_out_string(TestOutputVisitorData *data,
99 const void *unused)
101 char *string = (char *) "Q E M U";
102 Error *errp = NULL;
103 QObject *obj;
105 visit_type_str(data->ov, &string, NULL, &errp);
106 g_assert(error_is_set(&errp) == 0);
108 obj = qmp_output_get_qobject(data->qov);
109 g_assert(obj != NULL);
110 g_assert(qobject_type(obj) == QTYPE_QSTRING);
111 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, string);
113 qobject_decref(obj);
116 static void test_visitor_out_no_string(TestOutputVisitorData *data,
117 const void *unused)
119 char *string = NULL;
120 Error *errp = NULL;
121 QObject *obj;
123 /* A null string should return "" */
124 visit_type_str(data->ov, &string, NULL, &errp);
125 g_assert(error_is_set(&errp) == 0);
127 obj = qmp_output_get_qobject(data->qov);
128 g_assert(obj != NULL);
129 g_assert(qobject_type(obj) == QTYPE_QSTRING);
130 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==, "");
132 qobject_decref(obj);
135 static void test_visitor_out_enum(TestOutputVisitorData *data,
136 const void *unused)
138 Error *errp = NULL;
139 QObject *obj;
140 EnumOne i;
142 for (i = 0; i < ENUM_ONE_MAX; i++) {
143 visit_type_EnumOne(data->ov, &i, "unused", &errp);
144 g_assert(!error_is_set(&errp));
146 obj = qmp_output_get_qobject(data->qov);
147 g_assert(obj != NULL);
148 g_assert(qobject_type(obj) == QTYPE_QSTRING);
149 g_assert_cmpstr(qstring_get_str(qobject_to_qstring(obj)), ==,
150 EnumOne_lookup[i]);
151 qobject_decref(obj);
155 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
156 const void *unused)
158 EnumOne i, bad_values[] = { ENUM_ONE_MAX, -1 };
159 Error *errp;
161 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
162 errp = NULL;
163 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &errp);
164 g_assert(error_is_set(&errp) == true);
165 error_free(errp);
169 typedef struct TestStruct
171 int64_t integer;
172 bool boolean;
173 char *string;
174 } TestStruct;
176 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
177 const char *name, Error **errp)
179 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
180 errp);
182 visit_type_int(v, &(*obj)->integer, "integer", errp);
183 visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
184 visit_type_str(v, &(*obj)->string, "string", errp);
186 visit_end_struct(v, errp);
189 static void test_visitor_out_struct(TestOutputVisitorData *data,
190 const void *unused)
192 TestStruct test_struct = { .integer = 42,
193 .boolean = false,
194 .string = (char *) "foo"};
195 TestStruct *p = &test_struct;
196 Error *errp = NULL;
197 QObject *obj;
198 QDict *qdict;
200 visit_type_TestStruct(data->ov, &p, NULL, &errp);
201 g_assert(!error_is_set(&errp));
203 obj = qmp_output_get_qobject(data->qov);
204 g_assert(obj != NULL);
205 g_assert(qobject_type(obj) == QTYPE_QDICT);
207 qdict = qobject_to_qdict(obj);
208 g_assert_cmpint(qdict_size(qdict), ==, 3);
209 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, 42);
210 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, 0);
211 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, "foo");
213 QDECREF(qdict);
216 static void test_visitor_out_struct_nested(TestOutputVisitorData *data,
217 const void *unused)
219 int64_t value = 42;
220 Error *errp = NULL;
221 UserDefNested *ud2;
222 QObject *obj;
223 QDict *qdict, *dict1, *dict2, *dict3, *userdef;
224 const char *string = "user def string";
225 const char *strings[] = { "forty two", "forty three", "forty four",
226 "forty five" };
228 ud2 = g_malloc0(sizeof(*ud2));
229 ud2->string0 = g_strdup(strings[0]);
231 ud2->dict1.string1 = g_strdup(strings[1]);
232 ud2->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
233 ud2->dict1.dict2.userdef1->string = g_strdup(string);
234 ud2->dict1.dict2.userdef1->integer = value;
235 ud2->dict1.dict2.string2 = g_strdup(strings[2]);
237 ud2->dict1.has_dict3 = true;
238 ud2->dict1.dict3.userdef2 = g_malloc0(sizeof(UserDefOne));
239 ud2->dict1.dict3.userdef2->string = g_strdup(string);
240 ud2->dict1.dict3.userdef2->integer = value;
241 ud2->dict1.dict3.string3 = g_strdup(strings[3]);
243 visit_type_UserDefNested(data->ov, &ud2, "unused", &errp);
244 g_assert(!error_is_set(&errp));
246 obj = qmp_output_get_qobject(data->qov);
247 g_assert(obj != NULL);
248 g_assert(qobject_type(obj) == QTYPE_QDICT);
250 qdict = qobject_to_qdict(obj);
251 g_assert_cmpint(qdict_size(qdict), ==, 2);
252 g_assert_cmpstr(qdict_get_str(qdict, "string0"), ==, strings[0]);
254 dict1 = qdict_get_qdict(qdict, "dict1");
255 g_assert_cmpint(qdict_size(dict1), ==, 3);
256 g_assert_cmpstr(qdict_get_str(dict1, "string1"), ==, strings[1]);
258 dict2 = qdict_get_qdict(dict1, "dict2");
259 g_assert_cmpint(qdict_size(dict2), ==, 2);
260 g_assert_cmpstr(qdict_get_str(dict2, "string2"), ==, strings[2]);
261 userdef = qdict_get_qdict(dict2, "userdef1");
262 g_assert_cmpint(qdict_size(userdef), ==, 2);
263 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
264 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
266 dict3 = qdict_get_qdict(dict1, "dict3");
267 g_assert_cmpint(qdict_size(dict3), ==, 2);
268 g_assert_cmpstr(qdict_get_str(dict3, "string3"), ==, strings[3]);
269 userdef = qdict_get_qdict(dict3, "userdef2");
270 g_assert_cmpint(qdict_size(userdef), ==, 2);
271 g_assert_cmpint(qdict_get_int(userdef, "integer"), ==, value);
272 g_assert_cmpstr(qdict_get_str(userdef, "string"), ==, string);
274 QDECREF(qdict);
275 qapi_free_UserDefNested(ud2);
278 static void test_visitor_out_struct_errors(TestOutputVisitorData *data,
279 const void *unused)
281 EnumOne bad_values[] = { ENUM_ONE_MAX, -1 };
282 UserDefOne u = { 0 }, *pu = &u;
283 Error *errp;
284 int i;
286 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
287 errp = NULL;
288 u.has_enum1 = true;
289 u.enum1 = bad_values[i];
290 visit_type_UserDefOne(data->ov, &pu, "unused", &errp);
291 g_assert(error_is_set(&errp) == true);
292 error_free(errp);
296 typedef struct TestStructList
298 TestStruct *value;
299 struct TestStructList *next;
300 } TestStructList;
302 static void visit_type_TestStructList(Visitor *v, TestStructList **obj,
303 const char *name, Error **errp)
305 GenericList *i, **head = (GenericList **)obj;
307 visit_start_list(v, name, errp);
309 for (*head = i = visit_next_list(v, head, errp); i; i = visit_next_list(v, &i, errp)) {
310 TestStructList *native_i = (TestStructList *)i;
311 visit_type_TestStruct(v, &native_i->value, NULL, errp);
314 visit_end_list(v, errp);
317 static void test_visitor_out_list(TestOutputVisitorData *data,
318 const void *unused)
320 char *value_str = (char *) "list value";
321 TestStructList *p, *head = NULL;
322 const int max_items = 10;
323 bool value_bool = true;
324 int value_int = 10;
325 Error *errp = NULL;
326 QListEntry *entry;
327 QObject *obj;
328 QList *qlist;
329 int i;
331 for (i = 0; i < max_items; i++) {
332 p = g_malloc0(sizeof(*p));
333 p->value = g_malloc0(sizeof(*p->value));
334 p->value->integer = value_int;
335 p->value->boolean = value_bool;
336 p->value->string = value_str;
338 p->next = head;
339 head = p;
342 visit_type_TestStructList(data->ov, &head, NULL, &errp);
343 g_assert(!error_is_set(&errp));
345 obj = qmp_output_get_qobject(data->qov);
346 g_assert(obj != NULL);
347 g_assert(qobject_type(obj) == QTYPE_QLIST);
349 qlist = qobject_to_qlist(obj);
350 g_assert(!qlist_empty(qlist));
352 i = 0;
353 QLIST_FOREACH_ENTRY(qlist, entry) {
354 QDict *qdict;
356 g_assert(qobject_type(entry->value) == QTYPE_QDICT);
357 qdict = qobject_to_qdict(entry->value);
358 g_assert_cmpint(qdict_size(qdict), ==, 3);
359 g_assert_cmpint(qdict_get_int(qdict, "integer"), ==, value_int);
360 g_assert_cmpint(qdict_get_bool(qdict, "boolean"), ==, value_bool);
361 g_assert_cmpstr(qdict_get_str(qdict, "string"), ==, value_str);
362 i++;
364 g_assert_cmpint(i, ==, max_items);
366 QDECREF(qlist);
368 for (p = head; p;) {
369 TestStructList *tmp = p->next;
370 g_free(p->value);
371 g_free(p);
372 p = tmp;
376 static void test_visitor_out_list_qapi_free(TestOutputVisitorData *data,
377 const void *unused)
379 UserDefNestedList *p, *head = NULL;
380 const char string[] = "foo bar";
381 int i, max_count = 1024;
383 for (i = 0; i < max_count; i++) {
384 p = g_malloc0(sizeof(*p));
385 p->value = g_malloc0(sizeof(*p->value));
387 p->value->string0 = g_strdup(string);
388 p->value->dict1.string1 = g_strdup(string);
389 p->value->dict1.dict2.userdef1 = g_malloc0(sizeof(UserDefOne));
390 p->value->dict1.dict2.userdef1->string = g_strdup(string);
391 p->value->dict1.dict2.userdef1->integer = 42;
392 p->value->dict1.dict2.string2 = g_strdup(string);
393 p->value->dict1.has_dict3 = false;
395 p->next = head;
396 head = p;
399 qapi_free_UserDefNestedList(head);
402 static void test_visitor_out_union(TestOutputVisitorData *data,
403 const void *unused)
405 QObject *arg, *qvalue;
406 QDict *qdict, *value;
408 Error *err = NULL;
410 UserDefUnion *tmp = g_malloc0(sizeof(UserDefUnion));
411 tmp->kind = USER_DEF_UNION_KIND_A;
412 tmp->a = g_malloc0(sizeof(UserDefA));
413 tmp->a->boolean = true;
415 visit_type_UserDefUnion(data->ov, &tmp, NULL, &err);
416 g_assert(err == NULL);
417 arg = qmp_output_get_qobject(data->qov);
419 g_assert(qobject_type(arg) == QTYPE_QDICT);
420 qdict = qobject_to_qdict(arg);
422 g_assert_cmpstr(qdict_get_str(qdict, "type"), ==, "a");
424 qvalue = qdict_get(qdict, "data");
425 g_assert(data != NULL);
426 g_assert(qobject_type(qvalue) == QTYPE_QDICT);
427 value = qobject_to_qdict(qvalue);
428 g_assert_cmpint(qdict_get_bool(value, "boolean"), ==, true);
430 qapi_free_UserDefUnion(tmp);
431 QDECREF(qdict);
434 static void output_visitor_test_add(const char *testpath,
435 TestOutputVisitorData *data,
436 void (*test_func)(TestOutputVisitorData *data, const void *user_data))
438 g_test_add(testpath, TestOutputVisitorData, data, visitor_output_setup,
439 test_func, visitor_output_teardown);
442 int main(int argc, char **argv)
444 TestOutputVisitorData out_visitor_data;
446 g_test_init(&argc, &argv, NULL);
448 output_visitor_test_add("/visitor/output/int",
449 &out_visitor_data, test_visitor_out_int);
450 output_visitor_test_add("/visitor/output/bool",
451 &out_visitor_data, test_visitor_out_bool);
452 output_visitor_test_add("/visitor/output/number",
453 &out_visitor_data, test_visitor_out_number);
454 output_visitor_test_add("/visitor/output/string",
455 &out_visitor_data, test_visitor_out_string);
456 output_visitor_test_add("/visitor/output/no-string",
457 &out_visitor_data, test_visitor_out_no_string);
458 output_visitor_test_add("/visitor/output/enum",
459 &out_visitor_data, test_visitor_out_enum);
460 output_visitor_test_add("/visitor/output/enum-errors",
461 &out_visitor_data, test_visitor_out_enum_errors);
462 output_visitor_test_add("/visitor/output/struct",
463 &out_visitor_data, test_visitor_out_struct);
464 output_visitor_test_add("/visitor/output/struct-nested",
465 &out_visitor_data, test_visitor_out_struct_nested);
466 output_visitor_test_add("/visitor/output/struct-errors",
467 &out_visitor_data, test_visitor_out_struct_errors);
468 output_visitor_test_add("/visitor/output/list",
469 &out_visitor_data, test_visitor_out_list);
470 output_visitor_test_add("/visitor/output/list-qapi-free",
471 &out_visitor_data, test_visitor_out_list_qapi_free);
472 output_visitor_test_add("/visitor/output/union",
473 &out_visitor_data, test_visitor_out_union);
475 g_test_run();
477 return 0;