block/qcow: Add blkdebug events
[qemu/ar7.git] / tests / check-qobject.c
blob710f9e6b0ab2a94a34742b33d4cd44c3a862d4f5
1 /*
2 * Generic QObject unit-tests.
4 * Copyright (C) 2017 Red Hat Inc.
6 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
7 * See the COPYING.LIB file in the top-level directory.
8 */
9 #include "qemu/osdep.h"
11 #include "qapi/qmp/types.h"
12 #include "qemu-common.h"
14 #include <math.h>
16 /* Marks the end of the test_equality() argument list.
17 * We cannot use NULL there because that is a valid argument. */
18 static QObject test_equality_end_of_arguments;
20 /**
21 * Test whether all variadic QObject *arguments are equal (@expected
22 * is true) or whether they are all not equal (@expected is false).
23 * Every QObject is tested to be equal to itself (to test
24 * reflexivity), all tests are done both ways (to test symmetry), and
25 * transitivity is not assumed but checked (each object is compared to
26 * every other one).
28 * Note that qobject_is_equal() is not really an equivalence relation,
29 * so this function may not be used for all objects (reflexivity is
30 * not guaranteed, e.g. in the case of a QNum containing NaN).
32 * The @_ argument is required because a boolean may not be the last
33 * argument before a variadic argument list (C11 7.16.1.4 para. 4).
35 static void do_test_equality(bool expected, int _, ...)
37 va_list ap_count, ap_extract;
38 QObject **args;
39 int arg_count = 0;
40 int i, j;
42 va_start(ap_count, _);
43 va_copy(ap_extract, ap_count);
44 while (va_arg(ap_count, QObject *) != &test_equality_end_of_arguments) {
45 arg_count++;
47 va_end(ap_count);
49 args = g_new(QObject *, arg_count);
50 for (i = 0; i < arg_count; i++) {
51 args[i] = va_arg(ap_extract, QObject *);
53 va_end(ap_extract);
55 for (i = 0; i < arg_count; i++) {
56 g_assert(qobject_is_equal(args[i], args[i]) == true);
58 for (j = i + 1; j < arg_count; j++) {
59 g_assert(qobject_is_equal(args[i], args[j]) == expected);
63 g_free(args);
66 #define check_equal(...) \
67 do_test_equality(true, 0, __VA_ARGS__, &test_equality_end_of_arguments)
68 #define check_unequal(...) \
69 do_test_equality(false, 0, __VA_ARGS__, &test_equality_end_of_arguments)
71 static void do_free_all(int _, ...)
73 va_list ap;
74 QObject *obj;
76 va_start(ap, _);
77 while ((obj = va_arg(ap, QObject *)) != NULL) {
78 qobject_decref(obj);
80 va_end(ap);
83 #define free_all(...) \
84 do_free_all(0, __VA_ARGS__, NULL)
86 static void qobject_is_equal_null_test(void)
88 check_unequal(qnull(), NULL);
91 static void qobject_is_equal_num_test(void)
93 QNum *u0, *i0, *d0, *dnan, *um42, *im42, *dm42;
95 u0 = qnum_from_uint(0u);
96 i0 = qnum_from_int(0);
97 d0 = qnum_from_double(0.0);
98 dnan = qnum_from_double(NAN);
99 um42 = qnum_from_uint((uint64_t)-42);
100 im42 = qnum_from_int(-42);
101 dm42 = qnum_from_double(-42.0);
103 /* Integers representing a mathematically equal number should
104 * compare equal */
105 check_equal(u0, i0);
106 /* Doubles, however, are always unequal to integers */
107 check_unequal(u0, d0);
108 check_unequal(i0, d0);
110 /* Do not assume any object is equal to itself -- note however
111 * that NaN cannot occur in a JSON object anyway. */
112 g_assert(qobject_is_equal(QOBJECT(dnan), QOBJECT(dnan)) == false);
114 /* No unsigned overflow */
115 check_unequal(um42, im42);
116 check_unequal(um42, dm42);
117 check_unequal(im42, dm42);
119 free_all(u0, i0, d0, dnan, um42, im42, dm42);
122 static void qobject_is_equal_bool_test(void)
124 QBool *btrue_0, *btrue_1, *bfalse_0, *bfalse_1;
126 btrue_0 = qbool_from_bool(true);
127 btrue_1 = qbool_from_bool(true);
128 bfalse_0 = qbool_from_bool(false);
129 bfalse_1 = qbool_from_bool(false);
131 check_equal(btrue_0, btrue_1);
132 check_equal(bfalse_0, bfalse_1);
133 check_unequal(btrue_0, bfalse_0);
135 free_all(btrue_0, btrue_1, bfalse_0, bfalse_1);
138 static void qobject_is_equal_string_test(void)
140 QString *str_base, *str_whitespace_0, *str_whitespace_1, *str_whitespace_2;
141 QString *str_whitespace_3, *str_case, *str_built;
143 str_base = qstring_from_str("foo");
144 str_whitespace_0 = qstring_from_str(" foo");
145 str_whitespace_1 = qstring_from_str("foo ");
146 str_whitespace_2 = qstring_from_str("foo\b");
147 str_whitespace_3 = qstring_from_str("fooo\b");
148 str_case = qstring_from_str("Foo");
150 /* Should yield "foo" */
151 str_built = qstring_from_substr("form", 0, 1);
152 qstring_append_chr(str_built, 'o');
154 check_unequal(str_base, str_whitespace_0, str_whitespace_1,
155 str_whitespace_2, str_whitespace_3, str_case);
157 check_equal(str_base, str_built);
159 free_all(str_base, str_whitespace_0, str_whitespace_1, str_whitespace_2,
160 str_whitespace_3, str_case, str_built);
163 static void qobject_is_equal_list_test(void)
165 QList *list_0, *list_1, *list_cloned;
166 QList *list_reordered, *list_longer, *list_shorter;
168 list_0 = qlist_new();
169 list_1 = qlist_new();
170 list_reordered = qlist_new();
171 list_longer = qlist_new();
172 list_shorter = qlist_new();
174 qlist_append_int(list_0, 1);
175 qlist_append_int(list_0, 2);
176 qlist_append_int(list_0, 3);
178 qlist_append_int(list_1, 1);
179 qlist_append_int(list_1, 2);
180 qlist_append_int(list_1, 3);
182 qlist_append_int(list_reordered, 1);
183 qlist_append_int(list_reordered, 3);
184 qlist_append_int(list_reordered, 2);
186 qlist_append_int(list_longer, 1);
187 qlist_append_int(list_longer, 2);
188 qlist_append_int(list_longer, 3);
189 qlist_append_null(list_longer);
191 qlist_append_int(list_shorter, 1);
192 qlist_append_int(list_shorter, 2);
194 list_cloned = qlist_copy(list_0);
196 check_equal(list_0, list_1, list_cloned);
197 check_unequal(list_0, list_reordered, list_longer, list_shorter);
199 /* With a NaN in it, the list should no longer compare equal to
200 * itself */
201 qlist_append(list_0, qnum_from_double(NAN));
202 g_assert(qobject_is_equal(QOBJECT(list_0), QOBJECT(list_0)) == false);
204 free_all(list_0, list_1, list_cloned, list_reordered, list_longer,
205 list_shorter);
208 static void qobject_is_equal_dict_test(void)
210 Error *local_err = NULL;
211 QDict *dict_0, *dict_1, *dict_cloned;
212 QDict *dict_different_key, *dict_different_value, *dict_different_null_key;
213 QDict *dict_longer, *dict_shorter, *dict_nested;
214 QDict *dict_crumpled;
216 dict_0 = qdict_new();
217 dict_1 = qdict_new();
218 dict_different_key = qdict_new();
219 dict_different_value = qdict_new();
220 dict_different_null_key = qdict_new();
221 dict_longer = qdict_new();
222 dict_shorter = qdict_new();
223 dict_nested = qdict_new();
225 qdict_put_int(dict_0, "f.o", 1);
226 qdict_put_int(dict_0, "bar", 2);
227 qdict_put_int(dict_0, "baz", 3);
228 qdict_put_null(dict_0, "null");
230 qdict_put_int(dict_1, "f.o", 1);
231 qdict_put_int(dict_1, "bar", 2);
232 qdict_put_int(dict_1, "baz", 3);
233 qdict_put_null(dict_1, "null");
235 qdict_put_int(dict_different_key, "F.o", 1);
236 qdict_put_int(dict_different_key, "bar", 2);
237 qdict_put_int(dict_different_key, "baz", 3);
238 qdict_put_null(dict_different_key, "null");
240 qdict_put_int(dict_different_value, "f.o", 42);
241 qdict_put_int(dict_different_value, "bar", 2);
242 qdict_put_int(dict_different_value, "baz", 3);
243 qdict_put_null(dict_different_value, "null");
245 qdict_put_int(dict_different_null_key, "f.o", 1);
246 qdict_put_int(dict_different_null_key, "bar", 2);
247 qdict_put_int(dict_different_null_key, "baz", 3);
248 qdict_put_null(dict_different_null_key, "none");
250 qdict_put_int(dict_longer, "f.o", 1);
251 qdict_put_int(dict_longer, "bar", 2);
252 qdict_put_int(dict_longer, "baz", 3);
253 qdict_put_int(dict_longer, "xyz", 4);
254 qdict_put_null(dict_longer, "null");
256 qdict_put_int(dict_shorter, "f.o", 1);
257 qdict_put_int(dict_shorter, "bar", 2);
258 qdict_put_int(dict_shorter, "baz", 3);
260 qdict_put(dict_nested, "f", qdict_new());
261 qdict_put_int(qdict_get_qdict(dict_nested, "f"), "o", 1);
262 qdict_put_int(dict_nested, "bar", 2);
263 qdict_put_int(dict_nested, "baz", 3);
264 qdict_put_null(dict_nested, "null");
266 dict_cloned = qdict_clone_shallow(dict_0);
268 check_equal(dict_0, dict_1, dict_cloned);
269 check_unequal(dict_0, dict_different_key, dict_different_value,
270 dict_different_null_key, dict_longer, dict_shorter,
271 dict_nested);
273 dict_crumpled = qobject_to_qdict(qdict_crumple(dict_1, &local_err));
274 g_assert(!local_err);
275 check_equal(dict_crumpled, dict_nested);
277 qdict_flatten(dict_nested);
278 check_equal(dict_0, dict_nested);
280 /* Containing an NaN value will make this dict compare unequal to
281 * itself */
282 qdict_put(dict_0, "NaN", qnum_from_double(NAN));
283 g_assert(qobject_is_equal(QOBJECT(dict_0), QOBJECT(dict_0)) == false);
285 free_all(dict_0, dict_1, dict_cloned, dict_different_key,
286 dict_different_value, dict_different_null_key, dict_longer,
287 dict_shorter, dict_nested, dict_crumpled);
290 static void qobject_is_equal_conversion_test(void)
292 QNum *u0, *i0, *d0;
293 QString *s0, *s_empty;
294 QBool *bfalse;
296 u0 = qnum_from_uint(0u);
297 i0 = qnum_from_int(0);
298 d0 = qnum_from_double(0.0);
299 s0 = qstring_from_str("0");
300 s_empty = qstring_new();
301 bfalse = qbool_from_bool(false);
303 /* No automatic type conversion */
304 check_unequal(u0, s0, s_empty, bfalse, qnull(), NULL);
305 check_unequal(i0, s0, s_empty, bfalse, qnull(), NULL);
306 check_unequal(d0, s0, s_empty, bfalse, qnull(), NULL);
308 free_all(u0, i0, d0, s0, s_empty, bfalse);
311 int main(int argc, char **argv)
313 g_test_init(&argc, &argv, NULL);
315 g_test_add_func("/public/qobject_is_equal_null",
316 qobject_is_equal_null_test);
317 g_test_add_func("/public/qobject_is_equal_num", qobject_is_equal_num_test);
318 g_test_add_func("/public/qobject_is_equal_bool",
319 qobject_is_equal_bool_test);
320 g_test_add_func("/public/qobject_is_equal_string",
321 qobject_is_equal_string_test);
322 g_test_add_func("/public/qobject_is_equal_list",
323 qobject_is_equal_list_test);
324 g_test_add_func("/public/qobject_is_equal_dict",
325 qobject_is_equal_dict_test);
326 g_test_add_func("/public/qobject_is_equal_conversion",
327 qobject_is_equal_conversion_test);
329 return g_test_run();