2 * QMP Input Visitor unit-tests.
4 * Copyright (C) 2011 Red Hat Inc.
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.
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
{
24 } TestInputVisitorData
;
26 static void visitor_input_teardown(TestInputVisitorData
*data
,
29 qobject_decref(data
->obj
);
33 qmp_input_visitor_cleanup(data
->qiv
);
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
41 static GCC_FMT_ATTR(2, 3)
42 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
43 const char *json_string
, ...)
48 va_start(ap
, json_string
);
49 data
->obj
= qobject_from_jsonv(json_string
, &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
);
63 static void test_visitor_in_int(TestInputVisitorData
*data
,
66 int64_t res
= 0, value
= -42;
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
,
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
,
94 double res
= 0, value
= 3.14;
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
,
108 char *res
= NULL
, *value
= (char *) "Q E M U";
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
);
121 static void test_visitor_in_enum(TestInputVisitorData
*data
,
128 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
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
);
144 typedef struct TestStruct
151 static void visit_type_TestStruct(Visitor
*v
, TestStruct
**obj
,
152 const char *name
, Error
**errp
)
154 visit_start_struct(v
, (void **)obj
, "TestStruct", name
, sizeof(TestStruct
),
157 visit_type_int(v
, &(*obj
)->integer
, "integer", errp
);
158 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", errp
);
159 visit_type_str(v
, &(*obj
)->string
, "string", errp
);
161 visit_end_struct(v
, errp
);
164 static void test_visitor_in_struct(TestInputVisitorData
*data
,
167 TestStruct
*p
= NULL
;
171 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
173 visit_type_TestStruct(v
, &p
, NULL
, &errp
);
174 g_assert(!error_is_set(&errp
));
175 g_assert_cmpint(p
->integer
, ==, -42);
176 g_assert(p
->boolean
== true);
177 g_assert_cmpstr(p
->string
, ==, "foo");
183 static void check_and_free_str(char *str
, const char *cmp
)
185 g_assert_cmpstr(str
, ==, cmp
);
189 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
192 UserDefNested
*udp
= NULL
;
196 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
198 visit_type_UserDefNested(v
, &udp
, NULL
, &errp
);
199 g_assert(!error_is_set(&errp
));
201 check_and_free_str(udp
->string0
, "string0");
202 check_and_free_str(udp
->dict1
.string1
, "string1");
203 g_assert_cmpint(udp
->dict1
.dict2
.userdef1
->integer
, ==, 42);
204 check_and_free_str(udp
->dict1
.dict2
.userdef1
->string
, "string");
205 check_and_free_str(udp
->dict1
.dict2
.string2
, "string2");
206 g_assert(udp
->dict1
.has_dict3
== false);
208 g_free(udp
->dict1
.dict2
.userdef1
);
212 static void test_visitor_in_list(TestInputVisitorData
*data
,
215 UserDefOneList
*item
, *head
= NULL
;
220 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
222 visit_type_UserDefOneList(v
, &head
, NULL
, &errp
);
223 g_assert(!error_is_set(&errp
));
224 g_assert(head
!= NULL
);
226 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
229 snprintf(string
, sizeof(string
), "string%d", i
);
230 g_assert_cmpstr(item
->value
->string
, ==, string
);
231 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
234 qapi_free_UserDefOneList(head
);
237 static void test_visitor_in_union(TestInputVisitorData
*data
,
244 v
= visitor_input_test_init(data
, "{ 'type': 'b', 'data' : { 'integer': 42 } }");
246 visit_type_UserDefUnion(v
, &tmp
, NULL
, &err
);
247 g_assert(err
== NULL
);
248 g_assert_cmpint(tmp
->kind
, ==, USER_DEF_UNION_KIND_B
);
249 g_assert_cmpint(tmp
->b
->integer
, ==, 42);
250 qapi_free_UserDefUnion(tmp
);
253 static void input_visitor_test_add(const char *testpath
,
254 TestInputVisitorData
*data
,
255 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
257 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
258 visitor_input_teardown
);
261 int main(int argc
, char **argv
)
263 TestInputVisitorData in_visitor_data
;
265 g_test_init(&argc
, &argv
, NULL
);
267 input_visitor_test_add("/visitor/input/int",
268 &in_visitor_data
, test_visitor_in_int
);
269 input_visitor_test_add("/visitor/input/bool",
270 &in_visitor_data
, test_visitor_in_bool
);
271 input_visitor_test_add("/visitor/input/number",
272 &in_visitor_data
, test_visitor_in_number
);
273 input_visitor_test_add("/visitor/input/string",
274 &in_visitor_data
, test_visitor_in_string
);
275 input_visitor_test_add("/visitor/input/enum",
276 &in_visitor_data
, test_visitor_in_enum
);
277 input_visitor_test_add("/visitor/input/struct",
278 &in_visitor_data
, test_visitor_in_struct
);
279 input_visitor_test_add("/visitor/input/struct-nested",
280 &in_visitor_data
, test_visitor_in_struct_nested
);
281 input_visitor_test_add("/visitor/input/list",
282 &in_visitor_data
, test_visitor_in_list
);
283 input_visitor_test_add("/visitor/input/union",
284 &in_visitor_data
, test_visitor_in_union
);