]>
Commit | Line | Data |
---|---|---|
2d7799f2 PB |
1 | /* |
2 | * String Output Visitor unit-tests. | |
3 | * | |
4 | * Copyright (C) 2012 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Paolo Bonzini <[email protected]> (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 | ||
681c28a3 | 13 | #include "qemu/osdep.h" |
2d7799f2 PB |
14 | #include <glib.h> |
15 | ||
79ee7df8 | 16 | #include "qemu-common.h" |
da34e65c | 17 | #include "qapi/error.h" |
2d7799f2 PB |
18 | #include "qapi/string-output-visitor.h" |
19 | #include "test-qapi-types.h" | |
20 | #include "test-qapi-visit.h" | |
7b1b5d19 | 21 | #include "qapi/qmp/types.h" |
2d7799f2 PB |
22 | |
23 | typedef struct TestOutputVisitorData { | |
24 | StringOutputVisitor *sov; | |
25 | Visitor *ov; | |
b4900c0e | 26 | bool human; |
2d7799f2 PB |
27 | } TestOutputVisitorData; |
28 | ||
29 | static void visitor_output_setup(TestOutputVisitorData *data, | |
30 | const void *unused) | |
31 | { | |
b4900c0e HT |
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); | |
2d7799f2 PB |
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 | { | |
69e25563 | 62 | int64_t value = 42; |
e940f543 | 63 | Error *err = NULL; |
2d7799f2 PB |
64 | char *str; |
65 | ||
51e72bc1 | 66 | visit_type_int(data->ov, NULL, &value, &err); |
e940f543 | 67 | g_assert(!err); |
2d7799f2 PB |
68 | |
69 | str = string_output_get_string(data->sov); | |
70 | g_assert(str != NULL); | |
b4900c0e HT |
71 | if (data->human) { |
72 | g_assert_cmpstr(str, ==, "42 (0x2a)"); | |
73 | } else { | |
74 | g_assert_cmpstr(str, ==, "42"); | |
75 | } | |
2d7799f2 PB |
76 | g_free(str); |
77 | } | |
78 | ||
69e25563 HT |
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; | |
533fdaed | 86 | Error *err = NULL; |
69e25563 HT |
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 | ||
51e72bc1 | 95 | visit_type_intList(data->ov, NULL, &list, &err); |
533fdaed | 96 | g_assert(err == NULL); |
69e25563 HT |
97 | |
98 | str = string_output_get_string(data->sov); | |
99 | g_assert(str != NULL); | |
b4900c0e HT |
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 | } | |
69e25563 HT |
109 | g_free(str); |
110 | while (list) { | |
111 | intList *tmp2; | |
112 | tmp2 = list->next; | |
113 | g_free(list); | |
114 | list = tmp2; | |
115 | } | |
116 | } | |
117 | ||
2d7799f2 PB |
118 | static void test_visitor_out_bool(TestOutputVisitorData *data, |
119 | const void *unused) | |
120 | { | |
e940f543 | 121 | Error *err = NULL; |
2d7799f2 PB |
122 | bool value = true; |
123 | char *str; | |
124 | ||
51e72bc1 | 125 | visit_type_bool(data->ov, NULL, &value, &err); |
e940f543 | 126 | g_assert(!err); |
2d7799f2 PB |
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; | |
e940f543 | 138 | Error *err = NULL; |
2d7799f2 PB |
139 | char *str; |
140 | ||
51e72bc1 | 141 | visit_type_number(data->ov, NULL, &value, &err); |
e940f543 | 142 | g_assert(!err); |
2d7799f2 PB |
143 | |
144 | str = string_output_get_string(data->sov); | |
145 | g_assert(str != NULL); | |
173bbb75 | 146 | g_assert_cmpstr(str, ==, "3.140000"); |
2d7799f2 PB |
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"; | |
b4900c0e | 154 | const char *string_human = "\"Q E M U\""; |
e940f543 | 155 | Error *err = NULL; |
2d7799f2 PB |
156 | char *str; |
157 | ||
51e72bc1 | 158 | visit_type_str(data->ov, NULL, &string, &err); |
e940f543 | 159 | g_assert(!err); |
2d7799f2 PB |
160 | |
161 | str = string_output_get_string(data->sov); | |
162 | g_assert(str != NULL); | |
b4900c0e HT |
163 | if (data->human) { |
164 | g_assert_cmpstr(str, ==, string_human); | |
165 | } else { | |
166 | g_assert_cmpstr(str, ==, string); | |
167 | } | |
2d7799f2 PB |
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; | |
e940f543 | 175 | Error *err = NULL; |
2d7799f2 PB |
176 | char *str; |
177 | ||
178 | /* A null string should return "" */ | |
51e72bc1 | 179 | visit_type_str(data->ov, NULL, &string, &err); |
e940f543 | 180 | g_assert(!err); |
2d7799f2 PB |
181 | |
182 | str = string_output_get_string(data->sov); | |
183 | g_assert(str != NULL); | |
b4900c0e HT |
184 | if (data->human) { |
185 | g_assert_cmpstr(str, ==, "<null>"); | |
186 | } else { | |
187 | g_assert_cmpstr(str, ==, ""); | |
188 | } | |
2d7799f2 PB |
189 | g_free(str); |
190 | } | |
191 | ||
192 | static void test_visitor_out_enum(TestOutputVisitorData *data, | |
193 | const void *unused) | |
194 | { | |
e940f543 | 195 | Error *err = NULL; |
2d7799f2 PB |
196 | char *str; |
197 | EnumOne i; | |
198 | ||
7fb1cf16 | 199 | for (i = 0; i < ENUM_ONE__MAX; i++) { |
b4900c0e | 200 | char *str_human; |
b4900c0e | 201 | |
51e72bc1 | 202 | visit_type_EnumOne(data->ov, "unused", &i, &err); |
e940f543 | 203 | g_assert(!err); |
2d7799f2 | 204 | |
9e99c5fd | 205 | str_human = g_strdup_printf("\"%s\"", EnumOne_lookup[i]); |
b4900c0e | 206 | |
2d7799f2 PB |
207 | str = string_output_get_string(data->sov); |
208 | g_assert(str != NULL); | |
b4900c0e HT |
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); | |
2d7799f2 PB |
215 | g_free(str); |
216 | } | |
217 | } | |
218 | ||
219 | static void test_visitor_out_enum_errors(TestOutputVisitorData *data, | |
220 | const void *unused) | |
221 | { | |
7fb1cf16 | 222 | EnumOne i, bad_values[] = { ENUM_ONE__MAX, -1 }; |
e940f543 | 223 | Error *err; |
2d7799f2 PB |
224 | |
225 | for (i = 0; i < ARRAY_SIZE(bad_values) ; i++) { | |
e940f543 | 226 | err = NULL; |
51e72bc1 | 227 | visit_type_EnumOne(data->ov, "unused", &bad_values[i], &err); |
e940f543 MA |
228 | g_assert(err); |
229 | error_free(err); | |
2d7799f2 PB |
230 | } |
231 | } | |
232 | ||
b4900c0e HT |
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) | |
2d7799f2 | 239 | { |
b4900c0e HT |
240 | g_test_add(testpath, TestOutputVisitorData, data, |
241 | human ? visitor_output_setup_human : visitor_output_setup, | |
2d7799f2 PB |
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", | |
b4900c0e | 252 | &out_visitor_data, test_visitor_out_int, false); |
deb847bf | 253 | output_visitor_test_add("/string-visitor/output/int-human", |
b4900c0e HT |
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); | |
deb847bf | 257 | output_visitor_test_add("/string-visitor/output/bool-human", |
b4900c0e HT |
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); | |
deb847bf | 261 | output_visitor_test_add("/string-visitor/output/number-human", |
b4900c0e | 262 | &out_visitor_data, test_visitor_out_number, true); |
2d7799f2 | 263 | output_visitor_test_add("/string-visitor/output/string", |
b4900c0e | 264 | &out_visitor_data, test_visitor_out_string, false); |
deb847bf | 265 | output_visitor_test_add("/string-visitor/output/string-human", |
b4900c0e HT |
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); | |
deb847bf | 270 | output_visitor_test_add("/string-visitor/output/no-string-human", |
b4900c0e HT |
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); | |
deb847bf | 275 | output_visitor_test_add("/string-visitor/output/enum-human", |
b4900c0e | 276 | &out_visitor_data, test_visitor_out_enum, true); |
2d7799f2 | 277 | output_visitor_test_add("/string-visitor/output/enum-errors", |
b4900c0e HT |
278 | &out_visitor_data, test_visitor_out_enum_errors, |
279 | false); | |
deb847bf | 280 | output_visitor_test_add("/string-visitor/output/enum-errors-human", |
b4900c0e HT |
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); | |
deb847bf | 285 | output_visitor_test_add("/string-visitor/output/intList-human", |
b4900c0e | 286 | &out_visitor_data, test_visitor_out_intList, true); |
2d7799f2 PB |
287 | |
288 | g_test_run(); | |
289 | ||
290 | return 0; | |
291 | } |