linux-user: Support for restarting system calls for OpenRISC targets
[qemu.git] / tests / test-string-output-visitor.c
1 /*
2 * String Output Visitor unit-tests.
3 *
4 * Copyright (C) 2012 Red Hat Inc.
5 *
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-output-visitor)
8 *
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.
11 */
12
13 #include "qemu/osdep.h"
14 #include <glib.h>
15
16 #include "qemu-common.h"
17 #include "qapi/error.h"
18 #include "qapi/string-output-visitor.h"
19 #include "test-qapi-types.h"
20 #include "test-qapi-visit.h"
21 #include "qapi/qmp/types.h"
22
23 typedef struct TestOutputVisitorData {
24 StringOutputVisitor *sov;
25 Visitor *ov;
26 bool human;
27 } TestOutputVisitorData;
28
29 static void visitor_output_setup(TestOutputVisitorData *data,
30 const void *unused)
31 {
32 data->human = false;
33 data->sov = string_output_visitor_new(data->human);
34 g_assert(data->sov != NULL);
35
36 data->ov = string_output_get_visitor(data->sov);
37 g_assert(data->ov != NULL);
38 }
39
40 static void visitor_output_setup_human(TestOutputVisitorData *data,
41 const void *unused)
42 {
43 data->human = true;
44 data->sov = string_output_visitor_new(data->human);
45 g_assert(data->sov != NULL);
46
47 data->ov = string_output_get_visitor(data->sov);
48 g_assert(data->ov != NULL);
49 }
50
51 static void visitor_output_teardown(TestOutputVisitorData *data,
52 const void *unused)
53 {
54 string_output_visitor_cleanup(data->sov);
55 data->sov = NULL;
56 data->ov = NULL;
57 }
58
59 static void test_visitor_out_int(TestOutputVisitorData *data,
60 const void *unused)
61 {
62 int64_t value = 42;
63 Error *err = NULL;
64 char *str;
65
66 visit_type_int(data->ov, NULL, &value, &err);
67 g_assert(!err);
68
69 str = string_output_get_string(data->sov);
70 g_assert(str != NULL);
71 if (data->human) {
72 g_assert_cmpstr(str, ==, "42 (0x2a)");
73 } else {
74 g_assert_cmpstr(str, ==, "42");
75 }
76 g_free(str);
77 }
78
79 static void test_visitor_out_intList(TestOutputVisitorData *data,
80 const void *unused)
81 {
82 int64_t value[] = {0, 1, 9, 10, 16, 15, 14,
83 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX - 1, INT64_MAX};
84 intList *list = NULL, **tmp = &list;
85 int i;
86 Error *err = NULL;
87 char *str;
88
89 for (i = 0; i < sizeof(value) / sizeof(value[0]); i++) {
90 *tmp = g_malloc0(sizeof(**tmp));
91 (*tmp)->value = value[i];
92 tmp = &(*tmp)->next;
93 }
94
95 visit_type_intList(data->ov, NULL, &list, &err);
96 g_assert(err == NULL);
97
98 str = string_output_get_string(data->sov);
99 g_assert(str != NULL);
100 if (data->human) {
101 g_assert_cmpstr(str, ==,
102 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
103 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
104 "0x7ffffffffffffffe-0x7fffffffffffffff)");
105 } else {
106 g_assert_cmpstr(str, ==,
107 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
108 }
109 g_free(str);
110 while (list) {
111 intList *tmp2;
112 tmp2 = list->next;
113 g_free(list);
114 list = tmp2;
115 }
116 }
117
118 static void test_visitor_out_bool(TestOutputVisitorData *data,
119 const void *unused)
120 {
121 Error *err = NULL;
122 bool value = true;
123 char *str;
124
125 visit_type_bool(data->ov, NULL, &value, &err);
126 g_assert(!err);
127
128 str = string_output_get_string(data->sov);
129 g_assert(str != NULL);
130 g_assert_cmpstr(str, ==, "true");
131 g_free(str);
132 }
133
134 static void test_visitor_out_number(TestOutputVisitorData *data,
135 const void *unused)
136 {
137 double value = 3.14;
138 Error *err = NULL;
139 char *str;
140
141 visit_type_number(data->ov, NULL, &value, &err);
142 g_assert(!err);
143
144 str = string_output_get_string(data->sov);
145 g_assert(str != NULL);
146 g_assert_cmpstr(str, ==, "3.140000");
147 g_free(str);
148 }
149
150 static void test_visitor_out_string(TestOutputVisitorData *data,
151 const void *unused)
152 {
153 char *string = (char *) "Q E M U";
154 const char *string_human = "\"Q E M U\"";
155 Error *err = NULL;
156 char *str;
157
158 visit_type_str(data->ov, NULL, &string, &err);
159 g_assert(!err);
160
161 str = string_output_get_string(data->sov);
162 g_assert(str != NULL);
163 if (data->human) {
164 g_assert_cmpstr(str, ==, string_human);
165 } else {
166 g_assert_cmpstr(str, ==, string);
167 }
168 g_free(str);
169 }
170
171 static void test_visitor_out_no_string(TestOutputVisitorData *data,
172 const void *unused)
173 {
174 char *string = NULL;
175 Error *err = NULL;
176 char *str;
177
178 /* A null string should return "" */
179 visit_type_str(data->ov, NULL, &string, &err);
180 g_assert(!err);
181
182 str = string_output_get_string(data->sov);
183 g_assert(str != NULL);
184 if (data->human) {
185 g_assert_cmpstr(str, ==, "<null>");
186 } else {
187 g_assert_cmpstr(str, ==, "");
188 }
189 g_free(str);
190 }
191
192 static void test_visitor_out_enum(TestOutputVisitorData *data,
193 const void *unused)
194 {
195 Error *err = NULL;
196 char *str;
197 EnumOne i;
198
199 for (i = 0; i < ENUM_ONE__MAX; i++) {
200 char *str_human;
201
202 visit_type_EnumOne(data->ov, "unused", &i, &err);
203 g_assert(!err);
204
205 str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]);
206
207 str = string_output_get_string(data->sov);
208 g_assert(str != NULL);
209 if (data->human) {
210 g_assert_cmpstr(str, ==, str_human);
211 } else {
212 g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
213 }
214 g_free(str_human);
215 g_free(str);
216 }
217 }
218
219 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
220 const void *unused)
221 {
222 EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 };
223 Error *err;
224
225 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
226 err = NULL;
227 visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err);
228 g_assert(err);
229 error_free(err);
230 }
231 }
232
233 static void
234 output_visitor_test_add(const char *testpath,
235 TestOutputVisitorData *data,
236 void (*test_func)(TestOutputVisitorData *data,
237 const void *user_data),
238 bool human)
239 {
240 g_test_add(testpath, TestOutputVisitorData, data,
241 human ? visitor_output_setup_human : visitor_output_setup,
242 test_func, visitor_output_teardown);
243 }
244
245 int main(int argc, char **argv)
246 {
247 TestOutputVisitorData out_visitor_data;
248
249 g_test_init(&argc, &argv, NULL);
250
251 output_visitor_test_add("/string-visitor/output/int",
252 &out_visitor_data, test_visitor_out_int, false);
253 output_visitor_test_add("/string-visitor/output/int-human",
254 &out_visitor_data, test_visitor_out_int, true);
255 output_visitor_test_add("/string-visitor/output/bool",
256 &out_visitor_data, test_visitor_out_bool, false);
257 output_visitor_test_add("/string-visitor/output/bool-human",
258 &out_visitor_data, test_visitor_out_bool, true);
259 output_visitor_test_add("/string-visitor/output/number",
260 &out_visitor_data, test_visitor_out_number, false);
261 output_visitor_test_add("/string-visitor/output/number-human",
262 &out_visitor_data, test_visitor_out_number, true);
263 output_visitor_test_add("/string-visitor/output/string",
264 &out_visitor_data, test_visitor_out_string, false);
265 output_visitor_test_add("/string-visitor/output/string-human",
266 &out_visitor_data, test_visitor_out_string, true);
267 output_visitor_test_add("/string-visitor/output/no-string",
268 &out_visitor_data, test_visitor_out_no_string,
269 false);
270 output_visitor_test_add("/string-visitor/output/no-string-human",
271 &out_visitor_data, test_visitor_out_no_string,
272 true);
273 output_visitor_test_add("/string-visitor/output/enum",
274 &out_visitor_data, test_visitor_out_enum, false);
275 output_visitor_test_add("/string-visitor/output/enum-human",
276 &out_visitor_data, test_visitor_out_enum, true);
277 output_visitor_test_add("/string-visitor/output/enum-errors",
278 &out_visitor_data, test_visitor_out_enum_errors,
279 false);
280 output_visitor_test_add("/string-visitor/output/enum-errors-human",
281 &out_visitor_data, test_visitor_out_enum_errors,
282 true);
283 output_visitor_test_add("/string-visitor/output/intList",
284 &out_visitor_data, test_visitor_out_intList, false);
285 output_visitor_test_add("/string-visitor/output/intList-human",
286 &out_visitor_data, test_visitor_out_intList, true);
287
288 g_test_run();
289
290 return 0;
291 }