target-ppc: Rework storage of VPA registration state
[qemu/opensuse.git] / tests / test-qmp-input-visitor.c
blob8f5a50958213751d9bcc481afafa5891a47e89c2
1 /*
2 * QMP Input 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>
14 #include <stdarg.h>
16 #include "qapi/qmp-input-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qemu-objects.h"
21 typedef struct TestInputVisitorData {
22 QObject *obj;
23 QmpInputVisitor *qiv;
24 } TestInputVisitorData;
26 static void visitor_input_teardown(TestInputVisitorData *data,
27 const void *unused)
29 qobject_decref(data->obj);
30 data->obj = NULL;
32 if (data->qiv) {
33 qmp_input_visitor_cleanup(data->qiv);
34 data->qiv = NULL;
38 /* This is provided instead of a test setup function so that the JSON
39 string used by the tests are kept in the test functions (and not
40 int main()) */
41 static GCC_FMT_ATTR(2, 3)
42 Visitor *visitor_input_test_init(TestInputVisitorData *data,
43 const char *json_string, ...)
45 Visitor *v;
46 va_list ap;
48 va_start(ap, json_string);
49 data->obj = qobject_from_jsonv(json_string, &ap);
50 va_end(ap);
52 g_assert(data->obj != NULL);
54 data->qiv = qmp_input_visitor_new(data->obj);
55 g_assert(data->qiv != NULL);
57 v = qmp_input_get_visitor(data->qiv);
58 g_assert(v != NULL);
60 return v;
63 static void test_visitor_in_int(TestInputVisitorData *data,
64 const void *unused)
66 int64_t res = 0, value = -42;
67 Error *errp = NULL;
68 Visitor *v;
70 v = visitor_input_test_init(data, "%" PRId64, value);
72 visit_type_int(v, &res, NULL, &errp);
73 g_assert(!error_is_set(&errp));
74 g_assert_cmpint(res, ==, value);
77 static void test_visitor_in_bool(TestInputVisitorData *data,
78 const void *unused)
80 Error *errp = NULL;
81 bool res = false;
82 Visitor *v;
84 v = visitor_input_test_init(data, "true");
86 visit_type_bool(v, &res, NULL, &errp);
87 g_assert(!error_is_set(&errp));
88 g_assert_cmpint(res, ==, true);
91 static void test_visitor_in_number(TestInputVisitorData *data,
92 const void *unused)
94 double res = 0, value = 3.14;
95 Error *errp = NULL;
96 Visitor *v;
98 v = visitor_input_test_init(data, "%f", value);
100 visit_type_number(v, &res, NULL, &errp);
101 g_assert(!error_is_set(&errp));
102 g_assert_cmpfloat(res, ==, value);
105 static void test_visitor_in_string(TestInputVisitorData *data,
106 const void *unused)
108 char *res = NULL, *value = (char *) "Q E M U";
109 Error *errp = NULL;
110 Visitor *v;
112 v = visitor_input_test_init(data, "%s", value);
114 visit_type_str(v, &res, NULL, &errp);
115 g_assert(!error_is_set(&errp));
116 g_assert_cmpstr(res, ==, value);
118 g_free(res);
121 static void test_visitor_in_enum(TestInputVisitorData *data,
122 const void *unused)
124 Error *errp = NULL;
125 Visitor *v;
126 EnumOne i;
128 for (i = 0; EnumOne_lookup[i]; i++) {
129 EnumOne res = -1;
131 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
133 visit_type_EnumOne(v, &res, NULL, &errp);
134 g_assert(!error_is_set(&errp));
135 g_assert_cmpint(i, ==, res);
137 visitor_input_teardown(data, NULL);
140 data->obj = NULL;
141 data->qiv = NULL;
144 typedef struct TestStruct
146 int64_t integer;
147 bool boolean;
148 char *string;
149 } TestStruct;
151 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
152 const char *name, Error **errp)
154 Error *err = NULL;
155 if (!error_is_set(errp)) {
156 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
157 &err);
158 if (!err) {
159 visit_type_int(v, &(*obj)->integer, "integer", &err);
160 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
161 visit_type_str(v, &(*obj)->string, "string", &err);
163 /* Always call end_struct if start_struct succeeded. */
164 error_propagate(errp, err);
165 err = NULL;
166 visit_end_struct(v, &err);
168 error_propagate(errp, err);
172 static void test_visitor_in_struct(TestInputVisitorData *data,
173 const void *unused)
175 TestStruct *p = NULL;
176 Error *errp = NULL;
177 Visitor *v;
179 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
181 visit_type_TestStruct(v, &p, NULL, &errp);
182 g_assert(!error_is_set(&errp));
183 g_assert_cmpint(p->integer, ==, -42);
184 g_assert(p->boolean == true);
185 g_assert_cmpstr(p->string, ==, "foo");
187 g_free(p->string);
188 g_free(p);
191 static void check_and_free_str(char *str, const char *cmp)
193 g_assert_cmpstr(str, ==, cmp);
194 g_free(str);
197 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
198 const void *unused)
200 UserDefNested *udp = NULL;
201 Error *errp = NULL;
202 Visitor *v;
204 v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
206 visit_type_UserDefNested(v, &udp, NULL, &errp);
207 g_assert(!error_is_set(&errp));
209 check_and_free_str(udp->string0, "string0");
210 check_and_free_str(udp->dict1.string1, "string1");
211 g_assert_cmpint(udp->dict1.dict2.userdef1->integer, ==, 42);
212 check_and_free_str(udp->dict1.dict2.userdef1->string, "string");
213 check_and_free_str(udp->dict1.dict2.string2, "string2");
214 g_assert(udp->dict1.has_dict3 == false);
216 g_free(udp->dict1.dict2.userdef1);
217 g_free(udp);
220 static void test_visitor_in_list(TestInputVisitorData *data,
221 const void *unused)
223 UserDefOneList *item, *head = NULL;
224 Error *errp = NULL;
225 Visitor *v;
226 int i;
228 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
230 visit_type_UserDefOneList(v, &head, NULL, &errp);
231 g_assert(!error_is_set(&errp));
232 g_assert(head != NULL);
234 for (i = 0, item = head; item; item = item->next, i++) {
235 char string[12];
237 snprintf(string, sizeof(string), "string%d", i);
238 g_assert_cmpstr(item->value->string, ==, string);
239 g_assert_cmpint(item->value->integer, ==, 42 + i);
242 qapi_free_UserDefOneList(head);
245 static void test_visitor_in_union(TestInputVisitorData *data,
246 const void *unused)
248 Visitor *v;
249 Error *err = NULL;
250 UserDefUnion *tmp;
252 v = visitor_input_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
254 visit_type_UserDefUnion(v, &tmp, NULL, &err);
255 g_assert(err == NULL);
256 g_assert_cmpint(tmp->kind, ==, USER_DEF_UNION_KIND_B);
257 g_assert_cmpint(tmp->b->integer, ==, 42);
258 qapi_free_UserDefUnion(tmp);
261 static void input_visitor_test_add(const char *testpath,
262 TestInputVisitorData *data,
263 void (*test_func)(TestInputVisitorData *data, const void *user_data))
265 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
266 visitor_input_teardown);
269 static void test_visitor_in_errors(TestInputVisitorData *data,
270 const void *unused)
272 TestStruct *p = NULL;
273 Error *errp = NULL;
274 Visitor *v;
276 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
278 visit_type_TestStruct(v, &p, NULL, &errp);
279 g_assert(error_is_set(&errp));
280 g_assert(p->string == NULL);
282 g_free(p->string);
283 g_free(p);
286 int main(int argc, char **argv)
288 TestInputVisitorData in_visitor_data;
290 g_test_init(&argc, &argv, NULL);
292 input_visitor_test_add("/visitor/input/int",
293 &in_visitor_data, test_visitor_in_int);
294 input_visitor_test_add("/visitor/input/bool",
295 &in_visitor_data, test_visitor_in_bool);
296 input_visitor_test_add("/visitor/input/number",
297 &in_visitor_data, test_visitor_in_number);
298 input_visitor_test_add("/visitor/input/string",
299 &in_visitor_data, test_visitor_in_string);
300 input_visitor_test_add("/visitor/input/enum",
301 &in_visitor_data, test_visitor_in_enum);
302 input_visitor_test_add("/visitor/input/struct",
303 &in_visitor_data, test_visitor_in_struct);
304 input_visitor_test_add("/visitor/input/struct-nested",
305 &in_visitor_data, test_visitor_in_struct_nested);
306 input_visitor_test_add("/visitor/input/list",
307 &in_visitor_data, test_visitor_in_list);
308 input_visitor_test_add("/visitor/input/union",
309 &in_visitor_data, test_visitor_in_union);
310 input_visitor_test_add("/visitor/input/errors",
311 &in_visitor_data, test_visitor_in_errors);
313 g_test_run();
315 return 0;