slirp: Refactor if_start
[qemu.git] / test-string-input-visitor.c
blob5370e32041a14b55d8db82537c80c897a69891de
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-qmp-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 <glib.h>
14 #include <stdarg.h>
16 #include "qapi/string-input-visitor.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "qemu-objects.h"
21 typedef struct TestInputVisitorData {
22 StringInputVisitor *siv;
23 } TestInputVisitorData;
25 static void visitor_input_teardown(TestInputVisitorData *data,
26 const void *unused)
28 if (data->siv) {
29 string_input_visitor_cleanup(data->siv);
30 data->siv = NULL;
34 /* This is provided instead of a test setup function so that the JSON
35 string used by the tests are kept in the test functions (and not
36 int main()) */
37 static
38 Visitor *visitor_input_test_init(TestInputVisitorData *data,
39 const char *string)
41 Visitor *v;
43 data->siv = string_input_visitor_new(string);
44 g_assert(data->siv != NULL);
46 v = string_input_get_visitor(data->siv);
47 g_assert(v != NULL);
49 return v;
52 static void test_visitor_in_int(TestInputVisitorData *data,
53 const void *unused)
55 int64_t res = 0, value = -42;
56 Error *errp = NULL;
57 Visitor *v;
59 v = visitor_input_test_init(data, "-42");
61 visit_type_int(v, &res, NULL, &errp);
62 g_assert(!error_is_set(&errp));
63 g_assert_cmpint(res, ==, value);
66 static void test_visitor_in_bool(TestInputVisitorData *data,
67 const void *unused)
69 Error *errp = NULL;
70 bool res = false;
71 Visitor *v;
73 v = visitor_input_test_init(data, "true");
75 visit_type_bool(v, &res, NULL, &errp);
76 g_assert(!error_is_set(&errp));
77 g_assert_cmpint(res, ==, true);
78 visitor_input_teardown(data, unused);
80 v = visitor_input_test_init(data, "yes");
82 visit_type_bool(v, &res, NULL, &errp);
83 g_assert(!error_is_set(&errp));
84 g_assert_cmpint(res, ==, true);
85 visitor_input_teardown(data, unused);
87 v = visitor_input_test_init(data, "on");
89 visit_type_bool(v, &res, NULL, &errp);
90 g_assert(!error_is_set(&errp));
91 g_assert_cmpint(res, ==, true);
92 visitor_input_teardown(data, unused);
94 v = visitor_input_test_init(data, "false");
96 visit_type_bool(v, &res, NULL, &errp);
97 g_assert(!error_is_set(&errp));
98 g_assert_cmpint(res, ==, false);
99 visitor_input_teardown(data, unused);
101 v = visitor_input_test_init(data, "no");
103 visit_type_bool(v, &res, NULL, &errp);
104 g_assert(!error_is_set(&errp));
105 g_assert_cmpint(res, ==, false);
106 visitor_input_teardown(data, unused);
108 v = visitor_input_test_init(data, "off");
110 visit_type_bool(v, &res, NULL, &errp);
111 g_assert(!error_is_set(&errp));
112 g_assert_cmpint(res, ==, false);
115 static void test_visitor_in_number(TestInputVisitorData *data,
116 const void *unused)
118 double res = 0, value = 3.14;
119 Error *errp = NULL;
120 Visitor *v;
122 v = visitor_input_test_init(data, "3.14");
124 visit_type_number(v, &res, NULL, &errp);
125 g_assert(!error_is_set(&errp));
126 g_assert_cmpfloat(res, ==, value);
129 static void test_visitor_in_string(TestInputVisitorData *data,
130 const void *unused)
132 char *res = NULL, *value = (char *) "Q E M U";
133 Error *errp = NULL;
134 Visitor *v;
136 v = visitor_input_test_init(data, value);
138 visit_type_str(v, &res, NULL, &errp);
139 g_assert(!error_is_set(&errp));
140 g_assert_cmpstr(res, ==, value);
142 g_free(res);
145 static void test_visitor_in_enum(TestInputVisitorData *data,
146 const void *unused)
148 Error *errp = NULL;
149 Visitor *v;
150 EnumOne i;
152 for (i = 0; EnumOne_lookup[i]; i++) {
153 EnumOne res = -1;
155 v = visitor_input_test_init(data, EnumOne_lookup[i]);
157 visit_type_EnumOne(v, &res, NULL, &errp);
158 g_assert(!error_is_set(&errp));
159 g_assert_cmpint(i, ==, res);
161 visitor_input_teardown(data, NULL);
164 data->siv = NULL;
167 static void input_visitor_test_add(const char *testpath,
168 TestInputVisitorData *data,
169 void (*test_func)(TestInputVisitorData *data, const void *user_data))
171 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
172 visitor_input_teardown);
175 int main(int argc, char **argv)
177 TestInputVisitorData in_visitor_data;
179 g_test_init(&argc, &argv, NULL);
181 input_visitor_test_add("/string-visitor/input/int",
182 &in_visitor_data, test_visitor_in_int);
183 input_visitor_test_add("/string-visitor/input/bool",
184 &in_visitor_data, test_visitor_in_bool);
185 input_visitor_test_add("/string-visitor/input/number",
186 &in_visitor_data, test_visitor_in_number);
187 input_visitor_test_add("/string-visitor/input/string",
188 &in_visitor_data, test_visitor_in_string);
189 input_visitor_test_add("/string-visitor/input/enum",
190 &in_visitor_data, test_visitor_in_enum);
192 g_test_run();
194 return 0;