linux-user: improve print_fcntl()
[qemu/cris-port.git] / tests / test-qmp-input-visitor.c
blob955a4c0b0aff16b8aac4afffb6ef472773eb0172
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 "qemu-common.h"
17 #include "qapi/qmp-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
22 typedef struct TestInputVisitorData {
23 QObject *obj;
24 QmpInputVisitor *qiv;
25 } TestInputVisitorData;
27 static void visitor_input_teardown(TestInputVisitorData *data,
28 const void *unused)
30 qobject_decref(data->obj);
31 data->obj = NULL;
33 if (data->qiv) {
34 qmp_input_visitor_cleanup(data->qiv);
35 data->qiv = NULL;
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()) */
42 static GCC_FMT_ATTR(2, 3)
43 Visitor *visitor_input_test_init(TestInputVisitorData *data,
44 const char *json_string, ...)
46 Visitor *v;
47 va_list ap;
49 va_start(ap, json_string);
50 data->obj = qobject_from_jsonv(json_string, &ap);
51 va_end(ap);
53 g_assert(data->obj != NULL);
55 data->qiv = qmp_input_visitor_new(data->obj);
56 g_assert(data->qiv != NULL);
58 v = qmp_input_get_visitor(data->qiv);
59 g_assert(v != NULL);
61 return v;
64 static void test_visitor_in_int(TestInputVisitorData *data,
65 const void *unused)
67 int64_t res = 0, value = -42;
68 Error *errp = NULL;
69 Visitor *v;
71 v = visitor_input_test_init(data, "%" PRId64, value);
73 visit_type_int(v, &res, NULL, &errp);
74 g_assert(!error_is_set(&errp));
75 g_assert_cmpint(res, ==, value);
78 static void test_visitor_in_bool(TestInputVisitorData *data,
79 const void *unused)
81 Error *errp = NULL;
82 bool res = false;
83 Visitor *v;
85 v = visitor_input_test_init(data, "true");
87 visit_type_bool(v, &res, NULL, &errp);
88 g_assert(!error_is_set(&errp));
89 g_assert_cmpint(res, ==, true);
92 static void test_visitor_in_number(TestInputVisitorData *data,
93 const void *unused)
95 double res = 0, value = 3.14;
96 Error *errp = NULL;
97 Visitor *v;
99 v = visitor_input_test_init(data, "%f", value);
101 visit_type_number(v, &res, NULL, &errp);
102 g_assert(!error_is_set(&errp));
103 g_assert_cmpfloat(res, ==, value);
106 static void test_visitor_in_string(TestInputVisitorData *data,
107 const void *unused)
109 char *res = NULL, *value = (char *) "Q E M U";
110 Error *errp = NULL;
111 Visitor *v;
113 v = visitor_input_test_init(data, "%s", value);
115 visit_type_str(v, &res, NULL, &errp);
116 g_assert(!error_is_set(&errp));
117 g_assert_cmpstr(res, ==, value);
119 g_free(res);
122 static void test_visitor_in_enum(TestInputVisitorData *data,
123 const void *unused)
125 Error *errp = NULL;
126 Visitor *v;
127 EnumOne i;
129 for (i = 0; EnumOne_lookup[i]; i++) {
130 EnumOne res = -1;
132 v = visitor_input_test_init(data, "%s", EnumOne_lookup[i]);
134 visit_type_EnumOne(v, &res, NULL, &errp);
135 g_assert(!error_is_set(&errp));
136 g_assert_cmpint(i, ==, res);
138 visitor_input_teardown(data, NULL);
141 data->obj = NULL;
142 data->qiv = NULL;
145 typedef struct TestStruct
147 int64_t integer;
148 bool boolean;
149 char *string;
150 } TestStruct;
152 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
153 const char *name, Error **errp)
155 Error *err = NULL;
156 if (!error_is_set(errp)) {
157 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
158 &err);
159 if (!err) {
160 visit_type_int(v, &(*obj)->integer, "integer", &err);
161 visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
162 visit_type_str(v, &(*obj)->string, "string", &err);
164 /* Always call end_struct if start_struct succeeded. */
165 error_propagate(errp, err);
166 err = NULL;
167 visit_end_struct(v, &err);
169 error_propagate(errp, err);
173 static void test_visitor_in_struct(TestInputVisitorData *data,
174 const void *unused)
176 TestStruct *p = NULL;
177 Error *errp = NULL;
178 Visitor *v;
180 v = visitor_input_test_init(data, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
182 visit_type_TestStruct(v, &p, NULL, &errp);
183 g_assert(!error_is_set(&errp));
184 g_assert_cmpint(p->integer, ==, -42);
185 g_assert(p->boolean == true);
186 g_assert_cmpstr(p->string, ==, "foo");
188 g_free(p->string);
189 g_free(p);
192 static void check_and_free_str(char *str, const char *cmp)
194 g_assert_cmpstr(str, ==, cmp);
195 g_free(str);
198 static void test_visitor_in_struct_nested(TestInputVisitorData *data,
199 const void *unused)
201 UserDefNested *udp = NULL;
202 Error *errp = NULL;
203 Visitor *v;
205 v = visitor_input_test_init(data, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
207 visit_type_UserDefNested(v, &udp, NULL, &errp);
208 g_assert(!error_is_set(&errp));
210 check_and_free_str(udp->string0, "string0");
211 check_and_free_str(udp->dict1.string1, "string1");
212 g_assert_cmpint(udp->dict1.dict2.userdef1->integer, ==, 42);
213 check_and_free_str(udp->dict1.dict2.userdef1->string, "string");
214 check_and_free_str(udp->dict1.dict2.string2, "string2");
215 g_assert(udp->dict1.has_dict3 == false);
217 g_free(udp->dict1.dict2.userdef1);
218 g_free(udp);
221 static void test_visitor_in_list(TestInputVisitorData *data,
222 const void *unused)
224 UserDefOneList *item, *head = NULL;
225 Error *errp = NULL;
226 Visitor *v;
227 int i;
229 v = visitor_input_test_init(data, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
231 visit_type_UserDefOneList(v, &head, NULL, &errp);
232 g_assert(!error_is_set(&errp));
233 g_assert(head != NULL);
235 for (i = 0, item = head; item; item = item->next, i++) {
236 char string[12];
238 snprintf(string, sizeof(string), "string%d", i);
239 g_assert_cmpstr(item->value->string, ==, string);
240 g_assert_cmpint(item->value->integer, ==, 42 + i);
243 qapi_free_UserDefOneList(head);
246 static void test_visitor_in_union(TestInputVisitorData *data,
247 const void *unused)
249 Visitor *v;
250 Error *err = NULL;
251 UserDefUnion *tmp;
253 v = visitor_input_test_init(data, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
255 visit_type_UserDefUnion(v, &tmp, NULL, &err);
256 g_assert(err == NULL);
257 g_assert_cmpint(tmp->kind, ==, USER_DEF_UNION_KIND_B);
258 g_assert_cmpint(tmp->b->integer, ==, 42);
259 qapi_free_UserDefUnion(tmp);
262 static void input_visitor_test_add(const char *testpath,
263 TestInputVisitorData *data,
264 void (*test_func)(TestInputVisitorData *data, const void *user_data))
266 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
267 visitor_input_teardown);
270 static void test_visitor_in_errors(TestInputVisitorData *data,
271 const void *unused)
273 TestStruct *p = NULL;
274 Error *errp = NULL;
275 Visitor *v;
277 v = visitor_input_test_init(data, "{ 'integer': false, 'boolean': 'foo', 'string': -42 }");
279 visit_type_TestStruct(v, &p, NULL, &errp);
280 g_assert(error_is_set(&errp));
281 g_assert(p->string == NULL);
283 g_free(p->string);
284 g_free(p);
287 int main(int argc, char **argv)
289 TestInputVisitorData in_visitor_data;
291 g_test_init(&argc, &argv, NULL);
293 input_visitor_test_add("/visitor/input/int",
294 &in_visitor_data, test_visitor_in_int);
295 input_visitor_test_add("/visitor/input/bool",
296 &in_visitor_data, test_visitor_in_bool);
297 input_visitor_test_add("/visitor/input/number",
298 &in_visitor_data, test_visitor_in_number);
299 input_visitor_test_add("/visitor/input/string",
300 &in_visitor_data, test_visitor_in_string);
301 input_visitor_test_add("/visitor/input/enum",
302 &in_visitor_data, test_visitor_in_enum);
303 input_visitor_test_add("/visitor/input/struct",
304 &in_visitor_data, test_visitor_in_struct);
305 input_visitor_test_add("/visitor/input/struct-nested",
306 &in_visitor_data, test_visitor_in_struct_nested);
307 input_visitor_test_add("/visitor/input/list",
308 &in_visitor_data, test_visitor_in_list);
309 input_visitor_test_add("/visitor/input/union",
310 &in_visitor_data, test_visitor_in_union);
311 input_visitor_test_add("/visitor/input/errors",
312 &in_visitor_data, test_visitor_in_errors);
314 g_test_run();
316 return 0;