coroutine: add test-coroutine --benchmark-lifecycle
[qemu/stefanha.git] / test-visitor.c
blob5133ad6b198af2ca22fe28ccf30377dad7fd4903
1 #include <glib.h>
2 #include "qapi/qmp-output-visitor.h"
3 #include "qapi/qmp-input-visitor.h"
4 #include "test-qapi-types.h"
5 #include "test-qapi-visit.h"
6 #include "qemu-objects.h"
8 typedef struct TestStruct
10 int64_t x;
11 int64_t y;
12 } TestStruct;
14 typedef struct TestStructList
16 TestStruct *value;
17 struct TestStructList *next;
18 } TestStructList;
20 static void visit_type_TestStruct(Visitor *v, TestStruct **obj, const char *name, Error **errp)
22 visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct), errp);
23 visit_type_int(v, &(*obj)->x, "x", errp);
24 visit_type_int(v, &(*obj)->y, "y", errp);
25 visit_end_struct(v, errp);
28 static void visit_type_TestStructList(Visitor *m, TestStructList ** obj, const char *name, Error **errp)
30 GenericList *i;
32 visit_start_list(m, name, errp);
34 for (i = visit_next_list(m, (GenericList **)obj, errp); i; i = visit_next_list(m, &i, errp)) {
35 TestStructList *native_i = (TestStructList *)i;
36 visit_type_TestStruct(m, &native_i->value, NULL, errp);
39 visit_end_list(m, errp);
42 /* test core visitor methods */
43 static void test_visitor_core(void)
45 QmpOutputVisitor *mo;
46 QmpInputVisitor *mi;
47 Visitor *v;
48 TestStruct ts = { 42, 82 };
49 TestStruct *pts = &ts;
50 TestStructList *lts = NULL;
51 Error *err = NULL;
52 QObject *obj;
53 QString *str;
54 int64_t value = 0;
56 mo = qmp_output_visitor_new();
57 v = qmp_output_get_visitor(mo);
59 visit_type_TestStruct(v, &pts, NULL, &err);
61 obj = qmp_output_get_qobject(mo);
63 str = qobject_to_json(obj);
65 printf("%s\n", qstring_get_str(str));
67 QDECREF(str);
69 obj = QOBJECT(qint_from_int(0x42));
71 mi = qmp_input_visitor_new(obj);
72 v = qmp_input_get_visitor(mi);
74 visit_type_int(v, &value, NULL, &err);
75 if (err) {
76 g_error("%s", error_get_pretty(err));
79 g_assert(value == 0x42);
81 qobject_decref(obj);
83 obj = qobject_from_json("{'x': 42, 'y': 84}");
84 mi = qmp_input_visitor_new(obj);
85 v = qmp_input_get_visitor(mi);
87 pts = NULL;
89 visit_type_TestStruct(v, &pts, NULL, &err);
90 if (err) {
91 g_error("%s", error_get_pretty(err));
94 g_assert(pts != NULL);
95 g_assert(pts->x == 42);
96 g_assert(pts->y == 84);
98 qobject_decref(obj);
100 obj = qobject_from_json("[{'x': 42, 'y': 84}, {'x': 12, 'y': 24}]");
101 mi = qmp_input_visitor_new(obj);
102 v = qmp_input_get_visitor(mi);
104 visit_type_TestStructList(v, &lts, NULL, &err);
105 if (err) {
106 g_error("%s", error_get_pretty(err));
109 g_assert(lts != NULL);
110 g_assert(lts->value->x == 42);
111 g_assert(lts->value->y == 84);
113 lts = lts->next;
114 g_assert(lts != NULL);
115 g_assert(lts->value->x == 12);
116 g_assert(lts->value->y == 24);
118 g_assert(lts->next == NULL);
120 qobject_decref(obj);
123 /* test deep nesting with refs to other user-defined types */
124 static void test_nested_structs(void)
126 QmpOutputVisitor *mo;
127 QmpInputVisitor *mi;
128 Visitor *v;
129 UserDefOne ud1;
130 UserDefOne *ud1_p = &ud1, *ud1c_p = NULL;
131 UserDefTwo ud2;
132 UserDefTwo *ud2_p = &ud2, *ud2c_p = NULL;
133 Error *err = NULL;
134 QObject *obj;
135 QString *str;
137 ud1.integer = 42;
138 ud1.string = strdup("fourty two");
140 /* sanity check */
141 mo = qmp_output_visitor_new();
142 v = qmp_output_get_visitor(mo);
143 visit_type_UserDefOne(v, &ud1_p, "o_O", &err);
144 if (err) {
145 g_error("%s", error_get_pretty(err));
147 obj = qmp_output_get_qobject(mo);
148 g_assert(obj);
149 qobject_decref(obj);
151 ud2.string = strdup("fourty three");
152 ud2.dict.string = strdup("fourty four");
153 ud2.dict.dict.userdef = ud1_p;
154 ud2.dict.dict.string = strdup("fourty five");
155 ud2.dict.has_dict2 = true;
156 ud2.dict.dict2.userdef = ud1_p;
157 ud2.dict.dict2.string = strdup("fourty six");
159 /* c type -> qobject */
160 mo = qmp_output_visitor_new();
161 v = qmp_output_get_visitor(mo);
162 visit_type_UserDefTwo(v, &ud2_p, "unused", &err);
163 if (err) {
164 g_error("%s", error_get_pretty(err));
166 obj = qmp_output_get_qobject(mo);
167 g_assert(obj);
168 str = qobject_to_json_pretty(obj);
169 g_print("%s\n", qstring_get_str(str));
170 QDECREF(str);
172 /* qobject -> c type, should match original struct */
173 mi = qmp_input_visitor_new(obj);
174 v = qmp_input_get_visitor(mi);
175 visit_type_UserDefTwo(v, &ud2c_p, NULL, &err);
176 if (err) {
177 g_error("%s", error_get_pretty(err));
180 g_assert(!g_strcmp0(ud2c_p->string, ud2.string));
181 g_assert(!g_strcmp0(ud2c_p->dict.string, ud2.dict.string));
183 ud1c_p = ud2c_p->dict.dict.userdef;
184 g_assert(ud1c_p->integer == ud1_p->integer);
185 g_assert(!g_strcmp0(ud1c_p->string, ud1_p->string));
187 g_assert(!g_strcmp0(ud2c_p->dict.dict.string, ud2.dict.dict.string));
189 ud1c_p = ud2c_p->dict.dict2.userdef;
190 g_assert(ud1c_p->integer == ud1_p->integer);
191 g_assert(!g_strcmp0(ud1c_p->string, ud1_p->string));
193 g_assert(!g_strcmp0(ud2c_p->dict.dict2.string, ud2.dict.dict2.string));
194 qemu_free(ud1.string);
195 qemu_free(ud2.string);
196 qemu_free(ud2.dict.string);
197 qemu_free(ud2.dict.dict.string);
198 qemu_free(ud2.dict.dict2.string);
200 qapi_free_UserDefTwo(ud2c_p);
202 qobject_decref(obj);
205 /* test enum values */
206 static void test_enums(void)
208 QmpOutputVisitor *mo;
209 QmpInputVisitor *mi;
210 Visitor *v;
211 EnumOne enum1 = ENUM_ONE_VALUE2, enum1_cpy = ENUM_ONE_VALUE1;
212 Error *err = NULL;
213 QObject *obj;
214 QString *str;
216 /* C type -> QObject */
217 mo = qmp_output_visitor_new();
218 v = qmp_output_get_visitor(mo);
219 visit_type_EnumOne(v, &enum1, "unused", &err);
220 if (err) {
221 g_error("%s", error_get_pretty(err));
223 obj = qmp_output_get_qobject(mo);
224 g_assert(obj);
225 str = qobject_to_json_pretty(obj);
226 g_print("%s\n", qstring_get_str(str));
227 QDECREF(str);
228 g_assert(g_strcmp0(qstring_get_str(qobject_to_qstring(obj)), "value2") == 0);
230 /* QObject -> C type */
231 mi = qmp_input_visitor_new(obj);
232 v = qmp_input_get_visitor(mi);
233 visit_type_EnumOne(v, &enum1_cpy, "unused", &err);
234 if (err) {
235 g_error("%s", error_get_pretty(err));
237 g_debug("enum1_cpy, enum1: %d, %d", enum1_cpy, enum1);
238 g_assert(enum1_cpy == enum1);
240 qobject_decref(obj);
243 /* test enum values nested in schema-defined structs */
244 static void test_nested_enums(void)
246 QmpOutputVisitor *mo;
247 QmpInputVisitor *mi;
248 Visitor *v;
249 NestedEnumsOne *nested_enums, *nested_enums_cpy = NULL;
250 Error *err = NULL;
251 QObject *obj;
252 QString *str;
254 nested_enums = qemu_mallocz(sizeof(NestedEnumsOne));
255 nested_enums->enum1 = ENUM_ONE_VALUE1;
256 nested_enums->enum2 = ENUM_ONE_VALUE2;
257 nested_enums->enum3 = ENUM_ONE_VALUE3;
258 nested_enums->enum4 = ENUM_ONE_VALUE3;
259 nested_enums->has_enum2 = false;
260 nested_enums->has_enum4 = true;
262 /* C type -> QObject */
263 mo = qmp_output_visitor_new();
264 v = qmp_output_get_visitor(mo);
265 visit_type_NestedEnumsOne(v, &nested_enums, NULL, &err);
266 if (err) {
267 g_error("%s", error_get_pretty(err));
269 obj = qmp_output_get_qobject(mo);
270 g_assert(obj);
271 str = qobject_to_json_pretty(obj);
272 g_print("%s\n", qstring_get_str(str));
273 QDECREF(str);
275 /* QObject -> C type */
276 mi = qmp_input_visitor_new(obj);
277 v = qmp_input_get_visitor(mi);
278 visit_type_NestedEnumsOne(v, &nested_enums_cpy, NULL, &err);
279 if (err) {
280 g_error("%s", error_get_pretty(err));
282 g_assert(nested_enums_cpy);
283 g_assert(nested_enums_cpy->enum1 == nested_enums->enum1);
284 g_assert(nested_enums_cpy->enum3 == nested_enums->enum3);
285 g_assert(nested_enums_cpy->enum4 == nested_enums->enum4);
286 g_assert(nested_enums_cpy->has_enum2 == false);
287 g_assert(nested_enums_cpy->has_enum4 == true);
289 qobject_decref(obj);
290 qapi_free_NestedEnumsOne(nested_enums);
291 qapi_free_NestedEnumsOne(nested_enums_cpy);
294 int main(int argc, char **argv)
296 g_test_init(&argc, &argv, NULL);
298 g_test_add_func("/0.15/visitor_core", test_visitor_core);
299 g_test_add_func("/0.15/nested_structs", test_nested_structs);
300 g_test_add_func("/0.15/enums", test_enums);
301 g_test_add_func("/0.15/nested_enums", test_nested_enums);
303 g_test_run();
305 return 0;