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.
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "qapi/qmp/qbool.h"
13 #include "qapi/qmp/qdict.h"
14 #include "qapi/qmp/qlist.h"
15 #include "qapi/qmp/qnull.h"
16 #include "qapi/qmp/qnum.h"
17 #include "qapi/qmp/qstring.h"
18 #include "qemu-common.h"
22 /* Marks the end of the test_equality() argument list.
23 * We cannot use NULL there because that is a valid argument. */
24 static QObject test_equality_end_of_arguments
;
27 * Test whether all variadic QObject *arguments are equal (@expected
28 * is true) or whether they are all not equal (@expected is false).
29 * Every QObject is tested to be equal to itself (to test
30 * reflexivity), all tests are done both ways (to test symmetry), and
31 * transitivity is not assumed but checked (each object is compared to
34 * Note that qobject_is_equal() is not really an equivalence relation,
35 * so this function may not be used for all objects (reflexivity is
36 * not guaranteed, e.g. in the case of a QNum containing NaN).
38 * The @_ argument is required because a boolean may not be the last
39 * argument before a variadic argument list (C11 7.16.1.4 para. 4).
41 static void do_test_equality(bool expected
, int _
, ...)
43 va_list ap_count
, ap_extract
;
48 va_start(ap_count
, _
);
49 va_copy(ap_extract
, ap_count
);
50 while (va_arg(ap_count
, QObject
*) != &test_equality_end_of_arguments
) {
55 args
= g_new(QObject
*, arg_count
);
56 for (i
= 0; i
< arg_count
; i
++) {
57 args
[i
] = va_arg(ap_extract
, QObject
*);
61 for (i
= 0; i
< arg_count
; i
++) {
62 g_assert(qobject_is_equal(args
[i
], args
[i
]) == true);
64 for (j
= i
+ 1; j
< arg_count
; j
++) {
65 g_assert(qobject_is_equal(args
[i
], args
[j
]) == expected
);
72 #define check_equal(...) \
73 do_test_equality(true, 0, __VA_ARGS__, &test_equality_end_of_arguments)
74 #define check_unequal(...) \
75 do_test_equality(false, 0, __VA_ARGS__, &test_equality_end_of_arguments)
77 static void do_free_all(int _
, ...)
83 while ((obj
= va_arg(ap
, QObject
*)) != NULL
) {
89 #define free_all(...) \
90 do_free_all(0, __VA_ARGS__, NULL)
92 static void qobject_is_equal_null_test(void)
94 check_unequal(qnull(), NULL
);
97 static void qobject_is_equal_num_test(void)
99 QNum
*u0
, *i0
, *d0
, *dnan
, *um42
, *im42
, *dm42
;
101 u0
= qnum_from_uint(0u);
102 i0
= qnum_from_int(0);
103 d0
= qnum_from_double(0.0);
104 dnan
= qnum_from_double(NAN
);
105 um42
= qnum_from_uint((uint64_t)-42);
106 im42
= qnum_from_int(-42);
107 dm42
= qnum_from_double(-42.0);
109 /* Integers representing a mathematically equal number should
112 /* Doubles, however, are always unequal to integers */
113 check_unequal(u0
, d0
);
114 check_unequal(i0
, d0
);
116 /* Do not assume any object is equal to itself -- note however
117 * that NaN cannot occur in a JSON object anyway. */
118 g_assert(qobject_is_equal(QOBJECT(dnan
), QOBJECT(dnan
)) == false);
120 /* No unsigned overflow */
121 check_unequal(um42
, im42
);
122 check_unequal(um42
, dm42
);
123 check_unequal(im42
, dm42
);
125 free_all(u0
, i0
, d0
, dnan
, um42
, im42
, dm42
);
128 static void qobject_is_equal_bool_test(void)
130 QBool
*btrue_0
, *btrue_1
, *bfalse_0
, *bfalse_1
;
132 btrue_0
= qbool_from_bool(true);
133 btrue_1
= qbool_from_bool(true);
134 bfalse_0
= qbool_from_bool(false);
135 bfalse_1
= qbool_from_bool(false);
137 check_equal(btrue_0
, btrue_1
);
138 check_equal(bfalse_0
, bfalse_1
);
139 check_unequal(btrue_0
, bfalse_0
);
141 free_all(btrue_0
, btrue_1
, bfalse_0
, bfalse_1
);
144 static void qobject_is_equal_string_test(void)
146 QString
*str_base
, *str_whitespace_0
, *str_whitespace_1
, *str_whitespace_2
;
147 QString
*str_whitespace_3
, *str_case
, *str_built
;
149 str_base
= qstring_from_str("foo");
150 str_whitespace_0
= qstring_from_str(" foo");
151 str_whitespace_1
= qstring_from_str("foo ");
152 str_whitespace_2
= qstring_from_str("foo\b");
153 str_whitespace_3
= qstring_from_str("fooo\b");
154 str_case
= qstring_from_str("Foo");
156 /* Should yield "foo" */
157 str_built
= qstring_from_substr("buffoon", 3, 6);
159 check_unequal(str_base
, str_whitespace_0
, str_whitespace_1
,
160 str_whitespace_2
, str_whitespace_3
, str_case
);
162 check_equal(str_base
, str_built
);
164 free_all(str_base
, str_whitespace_0
, str_whitespace_1
, str_whitespace_2
,
165 str_whitespace_3
, str_case
, str_built
);
168 static void qobject_is_equal_list_test(void)
170 QList
*list_0
, *list_1
, *list_cloned
;
171 QList
*list_reordered
, *list_longer
, *list_shorter
;
173 list_0
= qlist_new();
174 list_1
= qlist_new();
175 list_reordered
= qlist_new();
176 list_longer
= qlist_new();
177 list_shorter
= qlist_new();
179 qlist_append_int(list_0
, 1);
180 qlist_append_int(list_0
, 2);
181 qlist_append_int(list_0
, 3);
183 qlist_append_int(list_1
, 1);
184 qlist_append_int(list_1
, 2);
185 qlist_append_int(list_1
, 3);
187 qlist_append_int(list_reordered
, 1);
188 qlist_append_int(list_reordered
, 3);
189 qlist_append_int(list_reordered
, 2);
191 qlist_append_int(list_longer
, 1);
192 qlist_append_int(list_longer
, 2);
193 qlist_append_int(list_longer
, 3);
194 qlist_append_null(list_longer
);
196 qlist_append_int(list_shorter
, 1);
197 qlist_append_int(list_shorter
, 2);
199 list_cloned
= qlist_copy(list_0
);
201 check_equal(list_0
, list_1
, list_cloned
);
202 check_unequal(list_0
, list_reordered
, list_longer
, list_shorter
);
204 /* With a NaN in it, the list should no longer compare equal to
206 qlist_append(list_0
, qnum_from_double(NAN
));
207 g_assert(qobject_is_equal(QOBJECT(list_0
), QOBJECT(list_0
)) == false);
209 free_all(list_0
, list_1
, list_cloned
, list_reordered
, list_longer
,
213 static void qobject_is_equal_dict_test(void)
215 QDict
*dict_0
, *dict_1
, *dict_cloned
;
216 QDict
*dict_different_key
, *dict_different_value
, *dict_different_null_key
;
217 QDict
*dict_longer
, *dict_shorter
, *dict_nested
;
218 QDict
*dict_crumpled
;
220 dict_0
= qdict_new();
221 dict_1
= qdict_new();
222 dict_different_key
= qdict_new();
223 dict_different_value
= qdict_new();
224 dict_different_null_key
= qdict_new();
225 dict_longer
= qdict_new();
226 dict_shorter
= qdict_new();
227 dict_nested
= qdict_new();
229 qdict_put_int(dict_0
, "f.o", 1);
230 qdict_put_int(dict_0
, "bar", 2);
231 qdict_put_int(dict_0
, "baz", 3);
232 qdict_put_null(dict_0
, "null");
234 qdict_put_int(dict_1
, "f.o", 1);
235 qdict_put_int(dict_1
, "bar", 2);
236 qdict_put_int(dict_1
, "baz", 3);
237 qdict_put_null(dict_1
, "null");
239 qdict_put_int(dict_different_key
, "F.o", 1);
240 qdict_put_int(dict_different_key
, "bar", 2);
241 qdict_put_int(dict_different_key
, "baz", 3);
242 qdict_put_null(dict_different_key
, "null");
244 qdict_put_int(dict_different_value
, "f.o", 42);
245 qdict_put_int(dict_different_value
, "bar", 2);
246 qdict_put_int(dict_different_value
, "baz", 3);
247 qdict_put_null(dict_different_value
, "null");
249 qdict_put_int(dict_different_null_key
, "f.o", 1);
250 qdict_put_int(dict_different_null_key
, "bar", 2);
251 qdict_put_int(dict_different_null_key
, "baz", 3);
252 qdict_put_null(dict_different_null_key
, "none");
254 qdict_put_int(dict_longer
, "f.o", 1);
255 qdict_put_int(dict_longer
, "bar", 2);
256 qdict_put_int(dict_longer
, "baz", 3);
257 qdict_put_int(dict_longer
, "xyz", 4);
258 qdict_put_null(dict_longer
, "null");
260 qdict_put_int(dict_shorter
, "f.o", 1);
261 qdict_put_int(dict_shorter
, "bar", 2);
262 qdict_put_int(dict_shorter
, "baz", 3);
264 qdict_put(dict_nested
, "f", qdict_new());
265 qdict_put_int(qdict_get_qdict(dict_nested
, "f"), "o", 1);
266 qdict_put_int(dict_nested
, "bar", 2);
267 qdict_put_int(dict_nested
, "baz", 3);
268 qdict_put_null(dict_nested
, "null");
270 dict_cloned
= qdict_clone_shallow(dict_0
);
272 check_equal(dict_0
, dict_1
, dict_cloned
);
273 check_unequal(dict_0
, dict_different_key
, dict_different_value
,
274 dict_different_null_key
, dict_longer
, dict_shorter
,
277 dict_crumpled
= qobject_to(QDict
, qdict_crumple(dict_1
, &error_abort
));
278 check_equal(dict_crumpled
, dict_nested
);
280 qdict_flatten(dict_nested
);
281 check_equal(dict_0
, dict_nested
);
283 /* Containing an NaN value will make this dict compare unequal to
285 qdict_put(dict_0
, "NaN", qnum_from_double(NAN
));
286 g_assert(qobject_is_equal(QOBJECT(dict_0
), QOBJECT(dict_0
)) == false);
288 free_all(dict_0
, dict_1
, dict_cloned
, dict_different_key
,
289 dict_different_value
, dict_different_null_key
, dict_longer
,
290 dict_shorter
, dict_nested
, dict_crumpled
);
293 static void qobject_is_equal_conversion_test(void)
296 QString
*s0
, *s_empty
;
299 u0
= qnum_from_uint(0u);
300 i0
= qnum_from_int(0);
301 d0
= qnum_from_double(0.0);
302 s0
= qstring_from_str("0");
303 s_empty
= qstring_new();
304 bfalse
= qbool_from_bool(false);
306 /* No automatic type conversion */
307 check_unequal(u0
, s0
, s_empty
, bfalse
, qnull(), NULL
);
308 check_unequal(i0
, s0
, s_empty
, bfalse
, qnull(), NULL
);
309 check_unequal(d0
, s0
, s_empty
, bfalse
, qnull(), NULL
);
311 free_all(u0
, i0
, d0
, s0
, s_empty
, bfalse
);
314 int main(int argc
, char **argv
)
316 g_test_init(&argc
, &argv
, NULL
);
318 g_test_add_func("/public/qobject_is_equal_null",
319 qobject_is_equal_null_test
);
320 g_test_add_func("/public/qobject_is_equal_num", qobject_is_equal_num_test
);
321 g_test_add_func("/public/qobject_is_equal_bool",
322 qobject_is_equal_bool_test
);
323 g_test_add_func("/public/qobject_is_equal_string",
324 qobject_is_equal_string_test
);
325 g_test_add_func("/public/qobject_is_equal_list",
326 qobject_is_equal_list_test
);
327 g_test_add_func("/public/qobject_is_equal_dict",
328 qobject_is_equal_dict_test
);
329 g_test_add_func("/public/qobject_is_equal_conversion",
330 qobject_is_equal_conversion_test
);