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 Visitor
*visitor_input_test_init(TestInputVisitorData
*data
,
42 const char *json_string
, ...)
47 va_start(ap
, json_string
);
48 data
->obj
= qobject_from_jsonv(json_string
, &ap
);
51 g_assert(data
->obj
!= NULL
);
53 data
->qiv
= qmp_input_visitor_new(data
->obj
);
54 g_assert(data
->qiv
!= NULL
);
56 v
= qmp_input_get_visitor(data
->qiv
);
62 static void test_visitor_in_int(TestInputVisitorData
*data
,
65 int64_t res
= 0, value
= -42;
69 v
= visitor_input_test_init(data
, "%d", value
);
71 visit_type_int(v
, &res
, NULL
, &errp
);
72 g_assert(!error_is_set(&errp
));
73 g_assert_cmpint(res
, ==, value
);
76 static void test_visitor_in_bool(TestInputVisitorData
*data
,
83 v
= visitor_input_test_init(data
, "true");
85 visit_type_bool(v
, &res
, NULL
, &errp
);
86 g_assert(!error_is_set(&errp
));
87 g_assert_cmpint(res
, ==, true);
90 static void test_visitor_in_number(TestInputVisitorData
*data
,
93 double res
= 0, value
= 3.14;
97 v
= visitor_input_test_init(data
, "%f", value
);
99 visit_type_number(v
, &res
, NULL
, &errp
);
100 g_assert(!error_is_set(&errp
));
101 g_assert_cmpfloat(res
, ==, value
);
104 static void test_visitor_in_string(TestInputVisitorData
*data
,
107 char *res
= NULL
, *value
= (char *) "Q E M U";
111 v
= visitor_input_test_init(data
, "%s", value
);
113 visit_type_str(v
, &res
, NULL
, &errp
);
114 g_assert(!error_is_set(&errp
));
115 g_assert_cmpstr(res
, ==, value
);
120 static void test_visitor_in_enum(TestInputVisitorData
*data
,
127 for (i
= 0; EnumOne_lookup
[i
]; i
++) {
130 v
= visitor_input_test_init(data
, "%s", EnumOne_lookup
[i
]);
132 visit_type_EnumOne(v
, &res
, NULL
, &errp
);
133 g_assert(!error_is_set(&errp
));
134 g_assert_cmpint(i
, ==, res
);
136 visitor_input_teardown(data
, NULL
);
143 typedef struct TestStruct
150 static void visit_type_TestStruct(Visitor
*v
, TestStruct
**obj
,
151 const char *name
, Error
**errp
)
153 visit_start_struct(v
, (void **)obj
, "TestStruct", name
, sizeof(TestStruct
),
156 visit_type_int(v
, &(*obj
)->integer
, "integer", errp
);
157 visit_type_bool(v
, &(*obj
)->boolean
, "boolean", errp
);
158 visit_type_str(v
, &(*obj
)->string
, "string", errp
);
160 visit_end_struct(v
, errp
);
163 static void test_visitor_in_struct(TestInputVisitorData
*data
,
166 TestStruct
*p
= NULL
;
170 v
= visitor_input_test_init(data
, "{ 'integer': -42, 'boolean': true, 'string': 'foo' }");
172 visit_type_TestStruct(v
, &p
, NULL
, &errp
);
173 g_assert(!error_is_set(&errp
));
174 g_assert_cmpint(p
->integer
, ==, -42);
175 g_assert(p
->boolean
== true);
176 g_assert_cmpstr(p
->string
, ==, "foo");
182 static void check_and_free_str(char *str
, const char *cmp
)
184 g_assert_cmpstr(str
, ==, cmp
);
188 static void test_visitor_in_struct_nested(TestInputVisitorData
*data
,
191 UserDefNested
*udp
= NULL
;
195 v
= visitor_input_test_init(data
, "{ 'string0': 'string0', 'dict1': { 'string1': 'string1', 'dict2': { 'userdef1': { 'integer': 42, 'string': 'string' }, 'string2': 'string2'}}}");
197 visit_type_UserDefNested(v
, &udp
, NULL
, &errp
);
198 g_assert(!error_is_set(&errp
));
200 check_and_free_str(udp
->string0
, "string0");
201 check_and_free_str(udp
->dict1
.string1
, "string1");
202 g_assert_cmpint(udp
->dict1
.dict2
.userdef1
->integer
, ==, 42);
203 check_and_free_str(udp
->dict1
.dict2
.userdef1
->string
, "string");
204 check_and_free_str(udp
->dict1
.dict2
.string2
, "string2");
205 g_assert(udp
->dict1
.has_dict3
== false);
207 g_free(udp
->dict1
.dict2
.userdef1
);
211 static void test_visitor_in_list(TestInputVisitorData
*data
,
214 UserDefOneList
*item
, *head
= NULL
;
219 v
= visitor_input_test_init(data
, "[ { 'string': 'string0', 'integer': 42 }, { 'string': 'string1', 'integer': 43 }, { 'string': 'string2', 'integer': 44 } ]");
221 visit_type_UserDefOneList(v
, &head
, NULL
, &errp
);
222 g_assert(!error_is_set(&errp
));
223 g_assert(head
!= NULL
);
225 for (i
= 0, item
= head
; item
; item
= item
->next
, i
++) {
228 snprintf(string
, sizeof(string
), "string%d", i
);
229 g_assert_cmpstr(item
->value
->string
, ==, string
);
230 g_assert_cmpint(item
->value
->integer
, ==, 42 + i
);
233 qapi_free_UserDefOneList(head
);
236 static void input_visitor_test_add(const char *testpath
,
237 TestInputVisitorData
*data
,
238 void (*test_func
)(TestInputVisitorData
*data
, const void *user_data
))
240 g_test_add(testpath
, TestInputVisitorData
, data
, NULL
, test_func
,
241 visitor_input_teardown
);
244 int main(int argc
, char **argv
)
246 TestInputVisitorData in_visitor_data
;
248 g_test_init(&argc
, &argv
, NULL
);
250 input_visitor_test_add("/visitor/input/int",
251 &in_visitor_data
, test_visitor_in_int
);
252 input_visitor_test_add("/visitor/input/bool",
253 &in_visitor_data
, test_visitor_in_bool
);
254 input_visitor_test_add("/visitor/input/number",
255 &in_visitor_data
, test_visitor_in_number
);
256 input_visitor_test_add("/visitor/input/string",
257 &in_visitor_data
, test_visitor_in_string
);
258 input_visitor_test_add("/visitor/input/enum",
259 &in_visitor_data
, test_visitor_in_enum
);
260 input_visitor_test_add("/visitor/input/struct",
261 &in_visitor_data
, test_visitor_in_struct
);
262 input_visitor_test_add("/visitor/input/struct-nested",
263 &in_visitor_data
, test_visitor_in_struct_nested
);
264 input_visitor_test_add("/visitor/input/list",
265 &in_visitor_data
, test_visitor_in_list
);