qemu-doc: Do not promote deprecated -smb and -redir options
[qemu/ar7.git] / tests / test-string-output-visitor.c
blob7aecdfcefb8c8ceea2c0990b3c945e462ab267db
1 /*
2 * String Output Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-output-visitor)
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>
15 #include "qemu-common.h"
16 #include "qapi/string-output-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qapi/qmp/types.h"
21 typedef struct TestOutputVisitorData {
22 StringOutputVisitor *sov;
23 Visitor *ov;
24 bool human;
25 } TestOutputVisitorData;
27 static void visitor_output_setup(TestOutputVisitorData *data,
28 const void *unused)
30 data->human = false;
31 data->sov = string_output_visitor_new(data->human);
32 g_assert(data->sov != NULL);
34 data->ov = string_output_get_visitor(data->sov);
35 g_assert(data->ov != NULL);
38 static void visitor_output_setup_human(TestOutputVisitorData *data,
39 const void *unused)
41 data->human = true;
42 data->sov = string_output_visitor_new(data->human);
43 g_assert(data->sov != NULL);
45 data->ov = string_output_get_visitor(data->sov);
46 g_assert(data->ov != NULL);
49 static void visitor_output_teardown(TestOutputVisitorData *data,
50 const void *unused)
52 string_output_visitor_cleanup(data->sov);
53 data->sov = NULL;
54 data->ov = NULL;
57 static void test_visitor_out_int(TestOutputVisitorData *data,
58 const void *unused)
60 int64_t value = 42;
61 Error *err = NULL;
62 char *str;
64 visit_type_int(data->ov, &value, NULL, &err);
65 g_assert(!err);
67 str = string_output_get_string(data->sov);
68 g_assert(str != NULL);
69 if (data->human) {
70 g_assert_cmpstr(str, ==, "42 (0x2a)");
71 } else {
72 g_assert_cmpstr(str, ==, "42");
74 g_free(str);
77 static void test_visitor_out_intList(TestOutputVisitorData *data,
78 const void *unused)
80 int64_t value[] = {0, 1, 9, 10, 16, 15, 14,
81 3, 4, 5, 6, 11, 12, 13, 21, 22, INT64_MAX - 1, INT64_MAX};
82 intList *list = NULL, **tmp = &list;
83 int i;
84 Error *err = NULL;
85 char *str;
87 for (i = 0; i < sizeof(value) / sizeof(value[0]); i++) {
88 *tmp = g_malloc0(sizeof(**tmp));
89 (*tmp)->value = value[i];
90 tmp = &(*tmp)->next;
93 visit_type_intList(data->ov, &list, NULL, &err);
94 g_assert(err == NULL);
96 str = string_output_get_string(data->sov);
97 g_assert(str != NULL);
98 if (data->human) {
99 g_assert_cmpstr(str, ==,
100 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807 "
101 "(0x0-0x1,0x3-0x6,0x9-0x10,0x15-0x16,"
102 "0x7ffffffffffffffe-0x7fffffffffffffff)");
103 } else {
104 g_assert_cmpstr(str, ==,
105 "0-1,3-6,9-16,21-22,9223372036854775806-9223372036854775807");
107 g_free(str);
108 while (list) {
109 intList *tmp2;
110 tmp2 = list->next;
111 g_free(list);
112 list = tmp2;
116 static void test_visitor_out_bool(TestOutputVisitorData *data,
117 const void *unused)
119 Error *err = NULL;
120 bool value = true;
121 char *str;
123 visit_type_bool(data->ov, &value, NULL, &err);
124 g_assert(!err);
126 str = string_output_get_string(data->sov);
127 g_assert(str != NULL);
128 g_assert_cmpstr(str, ==, "true");
129 g_free(str);
132 static void test_visitor_out_number(TestOutputVisitorData *data,
133 const void *unused)
135 double value = 3.14;
136 Error *err = NULL;
137 char *str;
139 visit_type_number(data->ov, &value, NULL, &err);
140 g_assert(!err);
142 str = string_output_get_string(data->sov);
143 g_assert(str != NULL);
144 g_assert_cmpstr(str, ==, "3.140000");
145 g_free(str);
148 static void test_visitor_out_string(TestOutputVisitorData *data,
149 const void *unused)
151 char *string = (char *) "Q E M U";
152 const char *string_human = "\"Q E M U\"";
153 Error *err = NULL;
154 char *str;
156 visit_type_str(data->ov, &string, NULL, &err);
157 g_assert(!err);
159 str = string_output_get_string(data->sov);
160 g_assert(str != NULL);
161 if (data->human) {
162 g_assert_cmpstr(str, ==, string_human);
163 } else {
164 g_assert_cmpstr(str, ==, string);
166 g_free(str);
169 static void test_visitor_out_no_string(TestOutputVisitorData *data,
170 const void *unused)
172 char *string = NULL;
173 Error *err = NULL;
174 char *str;
176 /* A null string should return "" */
177 visit_type_str(data->ov, &string, NULL, &err);
178 g_assert(!err);
180 str = string_output_get_string(data->sov);
181 g_assert(str != NULL);
182 if (data->human) {
183 g_assert_cmpstr(str, ==, "<null>");
184 } else {
185 g_assert_cmpstr(str, ==, "");
187 g_free(str);
190 static void test_visitor_out_enum(TestOutputVisitorData *data,
191 const void *unused)
193 Error *err = NULL;
194 char *str;
195 EnumOne i;
197 for (i = 0; i < ENUM_ONE__MAX; i++) {
198 char *str_human;
200 visit_type_EnumOne(data->ov, &i, "unused", &err);
201 g_assert(!err);
203 str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]);
205 str = string_output_get_string(data->sov);
206 g_assert(str != NULL);
207 if (data->human) {
208 g_assert_cmpstr(str, ==, str_human);
209 } else {
210 g_assert_cmpstr(str, ==, EnumOne_lookup[i]);
212 g_free(str_human);
213 g_free(str);
217 static void test_visitor_out_enum_errors(TestOutputVisitorData *data,
218 const void *unused)
220 EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 };
221 Error *err;
223 for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) {
224 err = NULL;
225 visit_type_EnumOne(data->ov, &bad_values[i], "unused", &err);
226 g_assert(err);
227 error_free(err);
231 static void
232 output_visitor_test_add(const char *testpath,
233 TestOutputVisitorData *data,
234 void (*test_func)(TestOutputVisitorData *data,
235 const void *user_data),
236 bool human)
238 g_test_add(testpath, TestOutputVisitorData, data,
239 human ? visitor_output_setup_human : visitor_output_setup,
240 test_func, visitor_output_teardown);
243 int main(int argc, char **argv)
245 TestOutputVisitorData out_visitor_data;
247 g_test_init(&argc, &argv, NULL);
249 output_visitor_test_add("/string-visitor/output/int",
250 &out_visitor_data, test_visitor_out_int, false);
251 output_visitor_test_add("/string-visitor/output/int-human",
252 &out_visitor_data, test_visitor_out_int, true);
253 output_visitor_test_add("/string-visitor/output/bool",
254 &out_visitor_data, test_visitor_out_bool, false);
255 output_visitor_test_add("/string-visitor/output/bool-human",
256 &out_visitor_data, test_visitor_out_bool, true);
257 output_visitor_test_add("/string-visitor/output/number",
258 &out_visitor_data, test_visitor_out_number, false);
259 output_visitor_test_add("/string-visitor/output/number-human",
260 &out_visitor_data, test_visitor_out_number, true);
261 output_visitor_test_add("/string-visitor/output/string",
262 &out_visitor_data, test_visitor_out_string, false);
263 output_visitor_test_add("/string-visitor/output/string-human",
264 &out_visitor_data, test_visitor_out_string, true);
265 output_visitor_test_add("/string-visitor/output/no-string",
266 &out_visitor_data, test_visitor_out_no_string,
267 false);
268 output_visitor_test_add("/string-visitor/output/no-string-human",
269 &out_visitor_data, test_visitor_out_no_string,
270 true);
271 output_visitor_test_add("/string-visitor/output/enum",
272 &out_visitor_data, test_visitor_out_enum, false);
273 output_visitor_test_add("/string-visitor/output/enum-human",
274 &out_visitor_data, test_visitor_out_enum, true);
275 output_visitor_test_add("/string-visitor/output/enum-errors",
276 &out_visitor_data, test_visitor_out_enum_errors,
277 false);
278 output_visitor_test_add("/string-visitor/output/enum-errors-human",
279 &out_visitor_data, test_visitor_out_enum_errors,
280 true);
281 output_visitor_test_add("/string-visitor/output/intList",
282 &out_visitor_data, test_visitor_out_intList, false);
283 output_visitor_test_add("/string-visitor/output/intList-human",
284 &out_visitor_data, test_visitor_out_intList, true);
286 g_test_run();
288 return 0;