target/s390x: move cc_name() to helper.c
[qemu/ar7.git] / tests / test-string-input-visitor.c
blob79313a7f7a09927034ed0b4b0e18dfba62c7e9fe
1 /*
2 * String Input Visitor unit-tests.
4 * Copyright (C) 2012 Red Hat Inc.
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qobject-input-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 "qemu/osdep.h"
15 #include "qemu-common.h"
16 #include "qapi/error.h"
17 #include "qapi/string-input-visitor.h"
18 #include "test-qapi-types.h"
19 #include "test-qapi-visit.h"
20 #include "qapi/qmp/types.h"
22 typedef struct TestInputVisitorData {
23 Visitor *v;
24 } TestInputVisitorData;
26 static void visitor_input_teardown(TestInputVisitorData *data,
27 const void *unused)
29 if (data->v) {
30 visit_free(data->v);
31 data->v = NULL;
35 /* This is provided instead of a test setup function so that the JSON
36 string used by the tests are kept in the test functions (and not
37 int main()) */
38 static
39 Visitor *visitor_input_test_init(TestInputVisitorData *data,
40 const char *string)
42 visitor_input_teardown(data, NULL);
44 data->v = string_input_visitor_new(string);
45 g_assert(data->v);
46 return data->v;
49 static void test_visitor_in_int(TestInputVisitorData *data,
50 const void *unused)
52 int64_t res = 0, value = -42;
53 Error *err = NULL;
54 Visitor *v;
56 v = visitor_input_test_init(data, "-42");
58 visit_type_int(v, NULL, &res, &err);
59 g_assert(!err);
60 g_assert_cmpint(res, ==, value);
62 v = visitor_input_test_init(data, "not an int");
64 visit_type_int(v, NULL, &res, &err);
65 error_free_or_abort(&err);
67 v = visitor_input_test_init(data, "");
69 visit_type_int(v, NULL, &res, &err);
70 error_free_or_abort(&err);
73 static void check_ilist(Visitor *v, int64_t *expected, size_t n)
75 int64List *res = NULL;
76 int64List *tail;
77 int i;
79 visit_type_int64List(v, NULL, &res, &error_abort);
80 tail = res;
81 for (i = 0; i < n; i++) {
82 g_assert(tail);
83 g_assert_cmpint(tail->value, ==, expected[i]);
84 tail = tail->next;
86 g_assert(!tail);
88 qapi_free_int64List(res);
91 static void check_ulist(Visitor *v, uint64_t *expected, size_t n)
93 uint64List *res = NULL;
94 uint64List *tail;
95 int i;
97 /* BUG: unsigned numbers above INT64_MAX don't work */
98 for (i = 0; i < n; i++) {
99 if (expected[i] > INT64_MAX) {
100 Error *err = NULL;
101 visit_type_uint64List(v, NULL, &res, &err);
102 error_free_or_abort(&err);
103 return;
107 visit_type_uint64List(v, NULL, &res, &error_abort);
108 tail = res;
109 for (i = 0; i < n; i++) {
110 g_assert(tail);
111 g_assert_cmpuint(tail->value, ==, expected[i]);
112 tail = tail->next;
114 g_assert(!tail);
116 qapi_free_uint64List(res);
119 static void test_visitor_in_intList(TestInputVisitorData *data,
120 const void *unused)
122 /* Note: the visitor *sorts* ranges *unsigned* */
123 int64_t expect1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 };
124 int64_t expect2[] = { 32767, -32768, -32767 };
125 int64_t expect3[] = { INT64_MAX, INT64_MIN };
126 uint64_t expect4[] = { UINT64_MAX };
127 Error *err = NULL;
128 int64List *res = NULL;
129 int64List *tail;
130 Visitor *v;
131 int64_t val;
133 /* Valid lists */
135 v = visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8");
136 check_ilist(v, expect1, ARRAY_SIZE(expect1));
138 v = visitor_input_test_init(data, "32767,-32768--32767");
139 check_ilist(v, expect2, ARRAY_SIZE(expect2));
141 v = visitor_input_test_init(data,
142 "-9223372036854775808,9223372036854775807");
143 check_ilist(v, expect3, ARRAY_SIZE(expect3));
145 v = visitor_input_test_init(data, "18446744073709551615");
146 check_ulist(v, expect4, ARRAY_SIZE(expect4));
148 /* Empty list */
150 v = visitor_input_test_init(data, "");
151 visit_type_int64List(v, NULL, &res, &error_abort);
152 g_assert(!res);
154 /* Not a list */
156 v = visitor_input_test_init(data, "not an int list");
158 visit_type_int64List(v, NULL, &res, &err);
159 error_free_or_abort(&err);
160 g_assert(!res);
162 /* Unvisited list tail */
164 v = visitor_input_test_init(data, "0,2-3");
166 /* Would be simpler if the visitor genuinely supported virtual walks */
167 visit_start_list(v, NULL, (GenericList **)&res, sizeof(*res),
168 &error_abort);
169 tail = res;
170 visit_type_int64(v, NULL, &tail->value, &error_abort);
171 g_assert_cmpint(tail->value, ==, 0);
172 tail = (int64List *)visit_next_list(v, (GenericList *)tail, sizeof(*res));
173 g_assert(tail);
174 visit_type_int64(v, NULL, &tail->value, &error_abort);
175 g_assert_cmpint(tail->value, ==, 2);
176 tail = (int64List *)visit_next_list(v, (GenericList *)tail, sizeof(*res));
177 g_assert(tail);
179 visit_check_list(v, &err);
180 error_free_or_abort(&err);
181 visit_end_list(v, (void **)&res);
183 qapi_free_int64List(res);
185 /* Visit beyond end of list */
186 v = visitor_input_test_init(data, "0");
188 visit_start_list(v, NULL, (GenericList **)&res, sizeof(*res),
189 &error_abort);
190 tail = res;
191 visit_type_int64(v, NULL, &tail->value, &err);
192 g_assert_cmpint(tail->value, ==, 0);
193 visit_type_int64(v, NULL, &val, &err);
194 g_assert_cmpint(val, ==, 1); /* BUG */
195 visit_check_list(v, &error_abort);
196 visit_end_list(v, (void **)&res);
198 qapi_free_int64List(res);
201 static void test_visitor_in_bool(TestInputVisitorData *data,
202 const void *unused)
204 Error *err = NULL;
205 bool res = false;
206 Visitor *v;
208 v = visitor_input_test_init(data, "true");
210 visit_type_bool(v, NULL, &res, &err);
211 g_assert(!err);
212 g_assert_cmpint(res, ==, true);
214 v = visitor_input_test_init(data, "yes");
216 visit_type_bool(v, NULL, &res, &err);
217 g_assert(!err);
218 g_assert_cmpint(res, ==, true);
220 v = visitor_input_test_init(data, "on");
222 visit_type_bool(v, NULL, &res, &err);
223 g_assert(!err);
224 g_assert_cmpint(res, ==, true);
226 v = visitor_input_test_init(data, "false");
228 visit_type_bool(v, NULL, &res, &err);
229 g_assert(!err);
230 g_assert_cmpint(res, ==, false);
232 v = visitor_input_test_init(data, "no");
234 visit_type_bool(v, NULL, &res, &err);
235 g_assert(!err);
236 g_assert_cmpint(res, ==, false);
238 v = visitor_input_test_init(data, "off");
240 visit_type_bool(v, NULL, &res, &err);
241 g_assert(!err);
242 g_assert_cmpint(res, ==, false);
245 static void test_visitor_in_number(TestInputVisitorData *data,
246 const void *unused)
248 double res = 0, value = 3.14;
249 Error *err = NULL;
250 Visitor *v;
252 v = visitor_input_test_init(data, "3.14");
254 visit_type_number(v, NULL, &res, &err);
255 g_assert(!err);
256 g_assert_cmpfloat(res, ==, value);
259 static void test_visitor_in_string(TestInputVisitorData *data,
260 const void *unused)
262 char *res = NULL, *value = (char *) "Q E M U";
263 Error *err = NULL;
264 Visitor *v;
266 v = visitor_input_test_init(data, value);
268 visit_type_str(v, NULL, &res, &err);
269 g_assert(!err);
270 g_assert_cmpstr(res, ==, value);
272 g_free(res);
275 static void test_visitor_in_enum(TestInputVisitorData *data,
276 const void *unused)
278 Error *err = NULL;
279 Visitor *v;
280 EnumOne i;
282 for (i = 0; EnumOne_lookup[i]; i++) {
283 EnumOne res = -1;
285 v = visitor_input_test_init(data, EnumOne_lookup[i]);
287 visit_type_EnumOne(v, NULL, &res, &err);
288 g_assert(!err);
289 g_assert_cmpint(i, ==, res);
293 /* Try to crash the visitors */
294 static void test_visitor_in_fuzz(TestInputVisitorData *data,
295 const void *unused)
297 int64_t ires;
298 intList *ilres;
299 bool bres;
300 double nres;
301 char *sres;
302 EnumOne eres;
303 Visitor *v;
304 unsigned int i;
305 char buf[10000];
307 for (i = 0; i < 100; i++) {
308 unsigned int j;
310 j = g_test_rand_int_range(0, sizeof(buf) - 1);
312 buf[j] = '\0';
314 if (j != 0) {
315 for (j--; j != 0; j--) {
316 buf[j - 1] = (char)g_test_rand_int_range(0, 256);
320 v = visitor_input_test_init(data, buf);
321 visit_type_int(v, NULL, &ires, NULL);
323 v = visitor_input_test_init(data, buf);
324 visit_type_intList(v, NULL, &ilres, NULL);
325 qapi_free_intList(ilres);
327 v = visitor_input_test_init(data, buf);
328 visit_type_bool(v, NULL, &bres, NULL);
330 v = visitor_input_test_init(data, buf);
331 visit_type_number(v, NULL, &nres, NULL);
333 v = visitor_input_test_init(data, buf);
334 sres = NULL;
335 visit_type_str(v, NULL, &sres, NULL);
336 g_free(sres);
338 v = visitor_input_test_init(data, buf);
339 visit_type_EnumOne(v, NULL, &eres, NULL);
343 static void input_visitor_test_add(const char *testpath,
344 TestInputVisitorData *data,
345 void (*test_func)(TestInputVisitorData *data, const void *user_data))
347 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
348 visitor_input_teardown);
351 int main(int argc, char **argv)
353 TestInputVisitorData in_visitor_data;
355 g_test_init(&argc, &argv, NULL);
357 input_visitor_test_add("/string-visitor/input/int",
358 &in_visitor_data, test_visitor_in_int);
359 input_visitor_test_add("/string-visitor/input/intList",
360 &in_visitor_data, test_visitor_in_intList);
361 input_visitor_test_add("/string-visitor/input/bool",
362 &in_visitor_data, test_visitor_in_bool);
363 input_visitor_test_add("/string-visitor/input/number",
364 &in_visitor_data, test_visitor_in_number);
365 input_visitor_test_add("/string-visitor/input/string",
366 &in_visitor_data, test_visitor_in_string);
367 input_visitor_test_add("/string-visitor/input/enum",
368 &in_visitor_data, test_visitor_in_enum);
369 input_visitor_test_add("/string-visitor/input/fuzz",
370 &in_visitor_data, test_visitor_in_fuzz);
372 g_test_run();
374 return 0;