cirrus: fix off-by-one in cirrus_bitblt_rop_bkwd_transp_*_16
[qemu.git] / tests / test-qmp-event.c
1 /*
2 * qapi event unit-tests.
3 *
4 * Copyright (c) 2014 Wenchao Xia
5 *
6 * Authors:
7 * Wenchao Xia <wenchaoqemu@gmail.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
14 #include "qemu/osdep.h"
15
16 #include "qemu-common.h"
17 #include "test-qapi-types.h"
18 #include "test-qapi-visit.h"
19 #include "test-qapi-event.h"
20 #include "qapi/qmp/types.h"
21 #include "qapi/qmp/qint.h"
22 #include "qapi/qmp/qobject.h"
23 #include "qapi/qmp-event.h"
24
25 typedef struct TestEventData {
26 QDict *expect;
27 } TestEventData;
28
29 typedef struct QDictCmpData {
30 QDict *expect;
31 bool result;
32 } QDictCmpData;
33
34 TestEventData *test_event_data;
35 static CompatGMutex test_event_lock;
36
37 /* Only compares bool, int, string */
38 static
39 void qdict_cmp_do_simple(const char *key, QObject *obj1, void *opaque)
40
41 {
42 QObject *obj2;
43 QDictCmpData d_new, *d = opaque;
44
45 if (!d->result) {
46 return;
47 }
48
49 obj2 = qdict_get(d->expect, key);
50 if (!obj2) {
51 d->result = false;
52 return;
53 }
54
55 if (qobject_type(obj1) != qobject_type(obj2)) {
56 d->result = false;
57 return;
58 }
59
60 switch (qobject_type(obj1)) {
61 case QTYPE_QBOOL:
62 d->result = (qbool_get_bool(qobject_to_qbool(obj1)) ==
63 qbool_get_bool(qobject_to_qbool(obj2)));
64 return;
65 case QTYPE_QINT:
66 d->result = (qint_get_int(qobject_to_qint(obj1)) ==
67 qint_get_int(qobject_to_qint(obj2)));
68 return;
69 case QTYPE_QSTRING:
70 d->result = g_strcmp0(qstring_get_str(qobject_to_qstring(obj1)),
71 qstring_get_str(qobject_to_qstring(obj2))) == 0;
72 return;
73 case QTYPE_QDICT:
74 d_new.expect = qobject_to_qdict(obj2);
75 d_new.result = true;
76 qdict_iter(qobject_to_qdict(obj1), qdict_cmp_do_simple, &d_new);
77 d->result = d_new.result;
78 return;
79 default:
80 abort();
81 }
82 }
83
84 static bool qdict_cmp_simple(QDict *a, QDict *b)
85 {
86 QDictCmpData d;
87
88 d.expect = b;
89 d.result = true;
90 qdict_iter(a, qdict_cmp_do_simple, &d);
91 return d.result;
92 }
93
94 /* This function is hooked as final emit function, which can verify the
95 correctness. */
96 static void event_test_emit(test_QAPIEvent event, QDict *d, Error **errp)
97 {
98 QDict *t;
99 int64_t s, ms;
100
101 /* Verify that we have timestamp, then remove it to compare other fields */
102 t = qdict_get_qdict(d, "timestamp");
103 g_assert(t);
104 s = qdict_get_try_int(t, "seconds", -2);
105 ms = qdict_get_try_int(t, "microseconds", -2);
106 if (s == -1) {
107 g_assert(ms == -1);
108 } else {
109 g_assert(s >= 0);
110 g_assert(ms >= 0 && ms <= 999999);
111 }
112 g_assert(qdict_size(t) == 2);
113
114 qdict_del(d, "timestamp");
115
116 g_assert(qdict_cmp_simple(d, test_event_data->expect));
117
118 }
119
120 static void event_prepare(TestEventData *data,
121 const void *unused)
122 {
123 /* Global variable test_event_data was used to pass the expectation, so
124 test cases can't be executed at same time. */
125 g_mutex_lock(&test_event_lock);
126
127 data->expect = qdict_new();
128 test_event_data = data;
129 }
130
131 static void event_teardown(TestEventData *data,
132 const void *unused)
133 {
134 QDECREF(data->expect);
135 test_event_data = NULL;
136
137 g_mutex_unlock(&test_event_lock);
138 }
139
140 static void event_test_add(const char *testpath,
141 void (*test_func)(TestEventData *data,
142 const void *user_data))
143 {
144 g_test_add(testpath, TestEventData, NULL, event_prepare, test_func,
145 event_teardown);
146 }
147
148
149 /* Test cases */
150
151 static void test_event_a(TestEventData *data,
152 const void *unused)
153 {
154 QDict *d;
155 d = data->expect;
156 qdict_put(d, "event", qstring_from_str("EVENT_A"));
157 qapi_event_send_event_a(&error_abort);
158 }
159
160 static void test_event_b(TestEventData *data,
161 const void *unused)
162 {
163 QDict *d;
164 d = data->expect;
165 qdict_put(d, "event", qstring_from_str("EVENT_B"));
166 qapi_event_send_event_b(&error_abort);
167 }
168
169 static void test_event_c(TestEventData *data,
170 const void *unused)
171 {
172 QDict *d, *d_data, *d_b;
173
174 UserDefOne b;
175 b.integer = 2;
176 b.string = g_strdup("test1");
177 b.has_enum1 = false;
178
179 d_b = qdict_new();
180 qdict_put(d_b, "integer", qint_from_int(2));
181 qdict_put(d_b, "string", qstring_from_str("test1"));
182
183 d_data = qdict_new();
184 qdict_put(d_data, "a", qint_from_int(1));
185 qdict_put(d_data, "b", d_b);
186 qdict_put(d_data, "c", qstring_from_str("test2"));
187
188 d = data->expect;
189 qdict_put(d, "event", qstring_from_str("EVENT_C"));
190 qdict_put(d, "data", d_data);
191
192 qapi_event_send_event_c(true, 1, true, &b, "test2", &error_abort);
193
194 g_free(b.string);
195 }
196
197 /* Complex type */
198 static void test_event_d(TestEventData *data,
199 const void *unused)
200 {
201 UserDefOne struct1;
202 EventStructOne a;
203 QDict *d, *d_data, *d_a, *d_struct1;
204
205 struct1.integer = 2;
206 struct1.string = g_strdup("test1");
207 struct1.has_enum1 = true;
208 struct1.enum1 = ENUM_ONE_VALUE1;
209
210 a.struct1 = &struct1;
211 a.string = g_strdup("test2");
212 a.has_enum2 = true;
213 a.enum2 = ENUM_ONE_VALUE2;
214
215 d_struct1 = qdict_new();
216 qdict_put(d_struct1, "integer", qint_from_int(2));
217 qdict_put(d_struct1, "string", qstring_from_str("test1"));
218 qdict_put(d_struct1, "enum1", qstring_from_str("value1"));
219
220 d_a = qdict_new();
221 qdict_put(d_a, "struct1", d_struct1);
222 qdict_put(d_a, "string", qstring_from_str("test2"));
223 qdict_put(d_a, "enum2", qstring_from_str("value2"));
224
225 d_data = qdict_new();
226 qdict_put(d_data, "a", d_a);
227 qdict_put(d_data, "b", qstring_from_str("test3"));
228 qdict_put(d_data, "enum3", qstring_from_str("value3"));
229
230 d = data->expect;
231 qdict_put(d, "event", qstring_from_str("EVENT_D"));
232 qdict_put(d, "data", d_data);
233
234 qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3,
235 &error_abort);
236
237 g_free(struct1.string);
238 g_free(a.string);
239 }
240
241 int main(int argc, char **argv)
242 {
243 #if !GLIB_CHECK_VERSION(2, 31, 0)
244 if (!g_thread_supported()) {
245 g_thread_init(NULL);
246 }
247 #endif
248
249 qmp_event_set_func_emit(event_test_emit);
250
251 g_test_init(&argc, &argv, NULL);
252
253 event_test_add("/event/event_a", test_event_a);
254 event_test_add("/event/event_b", test_event_b);
255 event_test_add("/event/event_c", test_event_c);
256 event_test_add("/event/event_d", test_event_d);
257 g_test_run();
258
259 return 0;
260 }