qapi: Clean up after recent conversions to QAPISchemaVisitor
[qemu/ar7.git] / tests / test-opts-visitor.c
blob1c753d982431c9504ff0fc4d0d2d3bd5449b1bc6
1 /*
2 * Options Visitor unit-tests.
4 * Copyright (C) 2013 Red Hat, Inc.
6 * Authors:
7 * Laszlo Ersek <lersek@redhat.com> (based on test-string-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/config-file.h" /* qemu_add_opts() */
16 #include "qemu/option.h" /* qemu_opts_parse() */
17 #include "qapi/opts-visitor.h" /* opts_visitor_new() */
18 #include "test-qapi-visit.h" /* visit_type_UserDefOptions() */
19 #include "qapi/dealloc-visitor.h" /* qapi_dealloc_visitor_new() */
21 static QemuOptsList userdef_opts = {
22 .name = "userdef",
23 .head = QTAILQ_HEAD_INITIALIZER(userdef_opts.head),
24 .desc = { { 0 } } /* validated with OptsVisitor */
27 /* fixture (= glib test case context) and test case manipulation */
29 typedef struct OptsVisitorFixture {
30 UserDefOptions *userdef;
31 Error *err;
32 } OptsVisitorFixture;
35 static void
36 setup_fixture(OptsVisitorFixture *f, gconstpointer test_data)
38 const char *opts_string = test_data;
39 QemuOpts *opts;
40 OptsVisitor *ov;
42 opts = qemu_opts_parse(qemu_find_opts("userdef"), opts_string, false,
43 NULL);
44 g_assert(opts != NULL);
46 ov = opts_visitor_new(opts);
47 visit_type_UserDefOptions(opts_get_visitor(ov), &f->userdef, NULL,
48 &f->err);
49 opts_visitor_cleanup(ov);
50 qemu_opts_del(opts);
54 static void
55 teardown_fixture(OptsVisitorFixture *f, gconstpointer test_data)
57 if (f->userdef != NULL) {
58 QapiDeallocVisitor *dv;
60 dv = qapi_dealloc_visitor_new();
61 visit_type_UserDefOptions(qapi_dealloc_get_visitor(dv), &f->userdef,
62 NULL, NULL);
63 qapi_dealloc_visitor_cleanup(dv);
65 error_free(f->err);
69 static void
70 add_test(const char *testpath,
71 void (*test_func)(OptsVisitorFixture *f, gconstpointer test_data),
72 gconstpointer test_data)
74 g_test_add(testpath, OptsVisitorFixture, test_data, setup_fixture,
75 test_func, teardown_fixture);
78 /* test output evaluation */
80 static void
81 expect_ok(OptsVisitorFixture *f, gconstpointer test_data)
83 g_assert(f->err == NULL);
84 g_assert(f->userdef != NULL);
88 static void
89 expect_fail(OptsVisitorFixture *f, gconstpointer test_data)
91 g_assert(f->err != NULL);
93 /* The error message is printed when this test utility is invoked directly
94 * (ie. without gtester) and the --verbose flag is passed:
96 * tests/test-opts-visitor --verbose
98 g_test_message("'%s': %s", (const char *)test_data,
99 error_get_pretty(f->err));
103 static void
104 test_value(OptsVisitorFixture *f, gconstpointer test_data)
106 uint64_t magic, bitval;
107 intList *i64;
108 uint64List *u64;
109 uint16List *u16;
111 expect_ok(f, test_data);
113 magic = 0;
114 for (i64 = f->userdef->i64; i64 != NULL; i64 = i64->next) {
115 g_assert(-16 <= i64->value && i64->value < 64-16);
116 bitval = 1ull << (i64->value + 16);
117 g_assert((magic & bitval) == 0);
118 magic |= bitval;
120 g_assert(magic == 0xDEADBEEF);
122 magic = 0;
123 for (u64 = f->userdef->u64; u64 != NULL; u64 = u64->next) {
124 g_assert(u64->value < 64);
125 bitval = 1ull << u64->value;
126 g_assert((magic & bitval) == 0);
127 magic |= bitval;
129 g_assert(magic == 0xBADC0FFEE0DDF00DULL);
131 magic = 0;
132 for (u16 = f->userdef->u16; u16 != NULL; u16 = u16->next) {
133 g_assert(u16->value < 64);
134 bitval = 1ull << u16->value;
135 g_assert((magic & bitval) == 0);
136 magic |= bitval;
138 g_assert(magic == 0xD15EA5E);
142 static void
143 expect_i64_min(OptsVisitorFixture *f, gconstpointer test_data)
145 expect_ok(f, test_data);
146 g_assert(f->userdef->has_i64);
147 g_assert(f->userdef->i64->next == NULL);
148 g_assert(f->userdef->i64->value == INT64_MIN);
152 static void
153 expect_i64_max(OptsVisitorFixture *f, gconstpointer test_data)
155 expect_ok(f, test_data);
156 g_assert(f->userdef->has_i64);
157 g_assert(f->userdef->i64->next == NULL);
158 g_assert(f->userdef->i64->value == INT64_MAX);
162 static void
163 expect_zero(OptsVisitorFixture *f, gconstpointer test_data)
165 expect_ok(f, test_data);
166 g_assert(f->userdef->has_u64);
167 g_assert(f->userdef->u64->next == NULL);
168 g_assert(f->userdef->u64->value == 0);
172 static void
173 expect_u64_max(OptsVisitorFixture *f, gconstpointer test_data)
175 expect_ok(f, test_data);
176 g_assert(f->userdef->has_u64);
177 g_assert(f->userdef->u64->next == NULL);
178 g_assert(f->userdef->u64->value == UINT64_MAX);
181 /* test cases */
184 main(int argc, char **argv)
186 g_test_init(&argc, &argv, NULL);
188 qemu_add_opts(&userdef_opts);
190 /* Three hexadecimal magic numbers, "dead beef", "bad coffee, odd food" and
191 * "disease", from
192 * <http://en.wikipedia.org/wiki/Magic_number_%28programming%29>, were
193 * converted to binary and dissected into bit ranges. Each magic number is
194 * going to be recomposed using the lists called "i64", "u64" and "u16",
195 * respectively.
197 * (Note that these types pertain to the individual bit shift counts, not
198 * the magic numbers themselves; the intent is to exercise opts_type_int()
199 * and opts_type_uint64().)
201 * The "i64" shift counts have been decreased by 16 (decimal) in order to
202 * test negative values as well. Finally, the full list of QemuOpt elements
203 * has been permuted with "shuf".
205 * Both "i64" and "u64" have some (distinct) single-element ranges
206 * represented as both "a" and "a-a". "u16" is a special case of "i64" (see
207 * visit_type_uint16()), so it wouldn't add a separate test in this regard.
210 add_test("/visitor/opts/flatten/value", &test_value,
211 "i64=-1-0,u64=12-16,u64=2-3,i64=-11--9,u64=57,u16=9,i64=5-5,"
212 "u16=1-4,u16=20,u64=63-63,i64=-16--13,u64=50-52,i64=14-15,u16=11,"
213 "i64=7,u16=18,i64=2-3,u16=6,u64=54-55,u64=0,u64=18-20,u64=33-43,"
214 "i64=9-12,u16=26-27,u64=59-61,u16=13-16,u64=29-31,u64=22-23,"
215 "u16=24,i64=-7--3");
217 add_test("/visitor/opts/i64/val1/errno", &expect_fail,
218 "i64=0x8000000000000000");
219 add_test("/visitor/opts/i64/val1/empty", &expect_fail, "i64=");
220 add_test("/visitor/opts/i64/val1/trailing", &expect_fail, "i64=5z");
221 add_test("/visitor/opts/i64/nonlist", &expect_fail, "i64x=5-6");
222 add_test("/visitor/opts/i64/val2/errno", &expect_fail,
223 "i64=0x7fffffffffffffff-0x8000000000000000");
224 add_test("/visitor/opts/i64/val2/empty", &expect_fail, "i64=5-");
225 add_test("/visitor/opts/i64/val2/trailing", &expect_fail, "i64=5-6z");
226 add_test("/visitor/opts/i64/range/empty", &expect_fail, "i64=6-5");
227 add_test("/visitor/opts/i64/range/minval", &expect_i64_min,
228 "i64=-0x8000000000000000--0x8000000000000000");
229 add_test("/visitor/opts/i64/range/maxval", &expect_i64_max,
230 "i64=0x7fffffffffffffff-0x7fffffffffffffff");
232 add_test("/visitor/opts/u64/val1/errno", &expect_fail, "u64=-1");
233 add_test("/visitor/opts/u64/val1/empty", &expect_fail, "u64=");
234 add_test("/visitor/opts/u64/val1/trailing", &expect_fail, "u64=5z");
235 add_test("/visitor/opts/u64/nonlist", &expect_fail, "u64x=5-6");
236 add_test("/visitor/opts/u64/val2/errno", &expect_fail,
237 "u64=0xffffffffffffffff-0x10000000000000000");
238 add_test("/visitor/opts/u64/val2/empty", &expect_fail, "u64=5-");
239 add_test("/visitor/opts/u64/val2/trailing", &expect_fail, "u64=5-6z");
240 add_test("/visitor/opts/u64/range/empty", &expect_fail, "u64=6-5");
241 add_test("/visitor/opts/u64/range/minval", &expect_zero, "u64=0-0");
242 add_test("/visitor/opts/u64/range/maxval", &expect_u64_max,
243 "u64=0xffffffffffffffff-0xffffffffffffffff");
245 /* Test maximum range sizes. The macro value is open-coded here
246 * *intentionally*; the test case must use concrete values by design. If
247 * OPTS_VISITOR_RANGE_MAX is changed, the following values need to be
248 * recalculated as well. The assert and this comment should help with it.
250 g_assert(OPTS_VISITOR_RANGE_MAX == 65536);
252 /* The unsigned case is simple, a u64-u64 difference can always be
253 * represented as a u64.
255 add_test("/visitor/opts/u64/range/max", &expect_ok, "u64=0-65535");
256 add_test("/visitor/opts/u64/range/2big", &expect_fail, "u64=0-65536");
258 /* The same cannot be said about an i64-i64 difference. */
259 add_test("/visitor/opts/i64/range/max/pos/a", &expect_ok,
260 "i64=0x7fffffffffff0000-0x7fffffffffffffff");
261 add_test("/visitor/opts/i64/range/max/pos/b", &expect_ok,
262 "i64=0x7ffffffffffeffff-0x7ffffffffffffffe");
263 add_test("/visitor/opts/i64/range/2big/pos", &expect_fail,
264 "i64=0x7ffffffffffeffff-0x7fffffffffffffff");
265 add_test("/visitor/opts/i64/range/max/neg/a", &expect_ok,
266 "i64=-0x8000000000000000--0x7fffffffffff0001");
267 add_test("/visitor/opts/i64/range/max/neg/b", &expect_ok,
268 "i64=-0x7fffffffffffffff--0x7fffffffffff0000");
269 add_test("/visitor/opts/i64/range/2big/neg", &expect_fail,
270 "i64=-0x8000000000000000--0x7fffffffffff0000");
271 add_test("/visitor/opts/i64/range/2big/full", &expect_fail,
272 "i64=-0x8000000000000000-0x7fffffffffffffff");
274 g_test_run();
275 return 0;