]> git.proxmox.com Git - qemu.git/blame - test-string-input-visitor.c
qemu-iotests: Test rebase with short backing file
[qemu.git] / test-string-input-visitor.c
CommitLineData
2d7799f2
PB
1/*
2 * String Input Visitor unit-tests.
3 *
4 * Copyright (C) 2012 Red Hat Inc.
5 *
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com> (based on test-qmp-input-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
13#include <glib.h>
14#include <stdarg.h>
15
16#include "qapi/string-input-visitor.h"
17#include "test-qapi-types.h"
18#include "test-qapi-visit.h"
19#include "qemu-objects.h"
20
21typedef struct TestInputVisitorData {
22 StringInputVisitor *siv;
23} TestInputVisitorData;
24
25static void visitor_input_teardown(TestInputVisitorData *data,
26 const void *unused)
27{
28 if (data->siv) {
29 string_input_visitor_cleanup(data->siv);
30 data->siv = NULL;
31 }
32}
33
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()) */
37static
38Visitor *visitor_input_test_init(TestInputVisitorData *data,
39 const char *string)
40{
41 Visitor *v;
42
43 data->siv = string_input_visitor_new(string);
44 g_assert(data->siv != NULL);
45
46 v = string_input_get_visitor(data->siv);
47 g_assert(v != NULL);
48
49 return v;
50}
51
52static void test_visitor_in_int(TestInputVisitorData *data,
53 const void *unused)
54{
55 int64_t res = 0, value = -42;
56 Error *errp = NULL;
57 Visitor *v;
58
59 v = visitor_input_test_init(data, "-42");
60
61 visit_type_int(v, &res, NULL, &errp);
62 g_assert(!error_is_set(&errp));
63 g_assert_cmpint(res, ==, value);
64}
65
66static void test_visitor_in_bool(TestInputVisitorData *data,
67 const void *unused)
68{
69 Error *errp = NULL;
70 bool res = false;
71 Visitor *v;
72
73 v = visitor_input_test_init(data, "true");
74
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);
79
80 v = visitor_input_test_init(data, "yes");
81
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);
86
87 v = visitor_input_test_init(data, "on");
88
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);
93
94 v = visitor_input_test_init(data, "false");
95
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);
100
101 v = visitor_input_test_init(data, "no");
102
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);
107
108 v = visitor_input_test_init(data, "off");
109
110 visit_type_bool(v, &res, NULL, &errp);
111 g_assert(!error_is_set(&errp));
112 g_assert_cmpint(res, ==, false);
113}
114
115static void test_visitor_in_number(TestInputVisitorData *data,
116 const void *unused)
117{
118 double res = 0, value = 3.14;
119 Error *errp = NULL;
120 Visitor *v;
121
122 v = visitor_input_test_init(data, "3.14");
123
124 visit_type_number(v, &res, NULL, &errp);
125 g_assert(!error_is_set(&errp));
126 g_assert_cmpfloat(res, ==, value);
127}
128
129static void test_visitor_in_string(TestInputVisitorData *data,
130 const void *unused)
131{
132 char *res = NULL, *value = (char *) "Q E M U";
133 Error *errp = NULL;
134 Visitor *v;
135
136 v = visitor_input_test_init(data, value);
137
138 visit_type_str(v, &res, NULL, &errp);
139 g_assert(!error_is_set(&errp));
140 g_assert_cmpstr(res, ==, value);
141
142 g_free(res);
143}
144
145static void test_visitor_in_enum(TestInputVisitorData *data,
146 const void *unused)
147{
148 Error *errp = NULL;
149 Visitor *v;
150 EnumOne i;
151
152 for (i = 0; EnumOne_lookup[i]; i++) {
153 EnumOne res = -1;
154
155 v = visitor_input_test_init(data, EnumOne_lookup[i]);
156
157 visit_type_EnumOne(v, &res, NULL, &errp);
158 g_assert(!error_is_set(&errp));
159 g_assert_cmpint(i, ==, res);
160
161 visitor_input_teardown(data, NULL);
162 }
163
164 data->siv = NULL;
165}
166
167static void input_visitor_test_add(const char *testpath,
168 TestInputVisitorData *data,
169 void (*test_func)(TestInputVisitorData *data, const void *user_data))
170{
171 g_test_add(testpath, TestInputVisitorData, data, NULL, test_func,
172 visitor_input_teardown);
173}
174
175int main(int argc, char **argv)
176{
177 TestInputVisitorData in_visitor_data;
178
179 g_test_init(&argc, &argv, NULL);
180
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);
191
192 g_test_run();
193
194 return 0;
195}