2
0

check-qobject.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Generic QObject unit-tests.
  3. *
  4. * Copyright (C) 2017 Red Hat Inc.
  5. *
  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"
  10. #include "block/qdict.h"
  11. #include "qapi/qmp/qbool.h"
  12. #include "qapi/qmp/qdict.h"
  13. #include "qapi/qmp/qlist.h"
  14. #include "qapi/qmp/qnull.h"
  15. #include "qapi/qmp/qnum.h"
  16. #include "qapi/qmp/qstring.h"
  17. #include "qemu-common.h"
  18. #include <math.h>
  19. /* Marks the end of the test_equality() argument list.
  20. * We cannot use NULL there because that is a valid argument. */
  21. static QObject test_equality_end_of_arguments;
  22. /**
  23. * Test whether all variadic QObject *arguments are equal (@expected
  24. * is true) or whether they are all not equal (@expected is false).
  25. * Every QObject is tested to be equal to itself (to test
  26. * reflexivity), all tests are done both ways (to test symmetry), and
  27. * transitivity is not assumed but checked (each object is compared to
  28. * every other one).
  29. *
  30. * Note that qobject_is_equal() is not really an equivalence relation,
  31. * so this function may not be used for all objects (reflexivity is
  32. * not guaranteed, e.g. in the case of a QNum containing NaN).
  33. *
  34. * The @_ argument is required because a boolean may not be the last
  35. * argument before a variadic argument list (C11 7.16.1.4 para. 4).
  36. */
  37. static void do_test_equality(bool expected, int _, ...)
  38. {
  39. va_list ap_count, ap_extract;
  40. QObject **args;
  41. int arg_count = 0;
  42. int i, j;
  43. va_start(ap_count, _);
  44. va_copy(ap_extract, ap_count);
  45. while (va_arg(ap_count, QObject *) != &test_equality_end_of_arguments) {
  46. arg_count++;
  47. }
  48. 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 *);
  52. }
  53. va_end(ap_extract);
  54. for (i = 0; i < arg_count; i++) {
  55. g_assert(qobject_is_equal(args[i], args[i]) == true);
  56. for (j = i + 1; j < arg_count; j++) {
  57. g_assert(qobject_is_equal(args[i], args[j]) == expected);
  58. }
  59. }
  60. g_free(args);
  61. }
  62. #define check_equal(...) \
  63. do_test_equality(true, 0, __VA_ARGS__, &test_equality_end_of_arguments)
  64. #define check_unequal(...) \
  65. do_test_equality(false, 0, __VA_ARGS__, &test_equality_end_of_arguments)
  66. static void do_free_all(int _, ...)
  67. {
  68. va_list ap;
  69. QObject *obj;
  70. va_start(ap, _);
  71. while ((obj = va_arg(ap, QObject *)) != NULL) {
  72. qobject_unref(obj);
  73. }
  74. va_end(ap);
  75. }
  76. #define free_all(...) \
  77. do_free_all(0, __VA_ARGS__, NULL)
  78. static void qobject_is_equal_null_test(void)
  79. {
  80. check_unequal(qnull(), NULL);
  81. }
  82. static void qobject_is_equal_num_test(void)
  83. {
  84. QNum *u0, *i0, *d0, *dnan, *um42, *im42, *dm42;
  85. u0 = qnum_from_uint(0u);
  86. i0 = qnum_from_int(0);
  87. d0 = qnum_from_double(0.0);
  88. dnan = qnum_from_double(NAN);
  89. um42 = qnum_from_uint((uint64_t)-42);
  90. im42 = qnum_from_int(-42);
  91. dm42 = qnum_from_double(-42.0);
  92. /* Integers representing a mathematically equal number should
  93. * compare equal */
  94. check_equal(u0, i0);
  95. /* Doubles, however, are always unequal to integers */
  96. check_unequal(u0, d0);
  97. check_unequal(i0, d0);
  98. /* Do not assume any object is equal to itself -- note however
  99. * that NaN cannot occur in a JSON object anyway. */
  100. g_assert(qobject_is_equal(QOBJECT(dnan), QOBJECT(dnan)) == false);
  101. /* No unsigned overflow */
  102. check_unequal(um42, im42);
  103. check_unequal(um42, dm42);
  104. check_unequal(im42, dm42);
  105. free_all(u0, i0, d0, dnan, um42, im42, dm42);
  106. }
  107. static void qobject_is_equal_bool_test(void)
  108. {
  109. QBool *btrue_0, *btrue_1, *bfalse_0, *bfalse_1;
  110. btrue_0 = qbool_from_bool(true);
  111. btrue_1 = qbool_from_bool(true);
  112. bfalse_0 = qbool_from_bool(false);
  113. bfalse_1 = qbool_from_bool(false);
  114. check_equal(btrue_0, btrue_1);
  115. check_equal(bfalse_0, bfalse_1);
  116. check_unequal(btrue_0, bfalse_0);
  117. free_all(btrue_0, btrue_1, bfalse_0, bfalse_1);
  118. }
  119. static void qobject_is_equal_string_test(void)
  120. {
  121. QString *str_base, *str_whitespace_0, *str_whitespace_1, *str_whitespace_2;
  122. QString *str_whitespace_3, *str_case, *str_built;
  123. str_base = qstring_from_str("foo");
  124. str_whitespace_0 = qstring_from_str(" foo");
  125. str_whitespace_1 = qstring_from_str("foo ");
  126. str_whitespace_2 = qstring_from_str("foo\b");
  127. str_whitespace_3 = qstring_from_str("fooo\b");
  128. str_case = qstring_from_str("Foo");
  129. /* Should yield "foo" */
  130. str_built = qstring_from_substr("form", 0, 2);
  131. qstring_append_chr(str_built, 'o');
  132. check_unequal(str_base, str_whitespace_0, str_whitespace_1,
  133. str_whitespace_2, str_whitespace_3, str_case);
  134. check_equal(str_base, str_built);
  135. free_all(str_base, str_whitespace_0, str_whitespace_1, str_whitespace_2,
  136. str_whitespace_3, str_case, str_built);
  137. }
  138. static void qobject_is_equal_list_test(void)
  139. {
  140. QList *list_0, *list_1, *list_cloned;
  141. QList *list_reordered, *list_longer, *list_shorter;
  142. list_0 = qlist_new();
  143. list_1 = qlist_new();
  144. list_reordered = qlist_new();
  145. list_longer = qlist_new();
  146. list_shorter = qlist_new();
  147. qlist_append_int(list_0, 1);
  148. qlist_append_int(list_0, 2);
  149. qlist_append_int(list_0, 3);
  150. qlist_append_int(list_1, 1);
  151. qlist_append_int(list_1, 2);
  152. qlist_append_int(list_1, 3);
  153. qlist_append_int(list_reordered, 1);
  154. qlist_append_int(list_reordered, 3);
  155. qlist_append_int(list_reordered, 2);
  156. qlist_append_int(list_longer, 1);
  157. qlist_append_int(list_longer, 2);
  158. qlist_append_int(list_longer, 3);
  159. qlist_append_null(list_longer);
  160. qlist_append_int(list_shorter, 1);
  161. qlist_append_int(list_shorter, 2);
  162. list_cloned = qlist_copy(list_0);
  163. check_equal(list_0, list_1, list_cloned);
  164. check_unequal(list_0, list_reordered, list_longer, list_shorter);
  165. /* With a NaN in it, the list should no longer compare equal to
  166. * itself */
  167. qlist_append(list_0, qnum_from_double(NAN));
  168. g_assert(qobject_is_equal(QOBJECT(list_0), QOBJECT(list_0)) == false);
  169. free_all(list_0, list_1, list_cloned, list_reordered, list_longer,
  170. list_shorter);
  171. }
  172. static void qobject_is_equal_dict_test(void)
  173. {
  174. Error *local_err = NULL;
  175. QDict *dict_0, *dict_1, *dict_cloned;
  176. QDict *dict_different_key, *dict_different_value, *dict_different_null_key;
  177. QDict *dict_longer, *dict_shorter, *dict_nested;
  178. QDict *dict_crumpled;
  179. dict_0 = qdict_new();
  180. dict_1 = qdict_new();
  181. dict_different_key = qdict_new();
  182. dict_different_value = qdict_new();
  183. dict_different_null_key = qdict_new();
  184. dict_longer = qdict_new();
  185. dict_shorter = qdict_new();
  186. dict_nested = qdict_new();
  187. qdict_put_int(dict_0, "f.o", 1);
  188. qdict_put_int(dict_0, "bar", 2);
  189. qdict_put_int(dict_0, "baz", 3);
  190. qdict_put_null(dict_0, "null");
  191. qdict_put_int(dict_1, "f.o", 1);
  192. qdict_put_int(dict_1, "bar", 2);
  193. qdict_put_int(dict_1, "baz", 3);
  194. qdict_put_null(dict_1, "null");
  195. qdict_put_int(dict_different_key, "F.o", 1);
  196. qdict_put_int(dict_different_key, "bar", 2);
  197. qdict_put_int(dict_different_key, "baz", 3);
  198. qdict_put_null(dict_different_key, "null");
  199. qdict_put_int(dict_different_value, "f.o", 42);
  200. qdict_put_int(dict_different_value, "bar", 2);
  201. qdict_put_int(dict_different_value, "baz", 3);
  202. qdict_put_null(dict_different_value, "null");
  203. qdict_put_int(dict_different_null_key, "f.o", 1);
  204. qdict_put_int(dict_different_null_key, "bar", 2);
  205. qdict_put_int(dict_different_null_key, "baz", 3);
  206. qdict_put_null(dict_different_null_key, "none");
  207. qdict_put_int(dict_longer, "f.o", 1);
  208. qdict_put_int(dict_longer, "bar", 2);
  209. qdict_put_int(dict_longer, "baz", 3);
  210. qdict_put_int(dict_longer, "xyz", 4);
  211. qdict_put_null(dict_longer, "null");
  212. qdict_put_int(dict_shorter, "f.o", 1);
  213. qdict_put_int(dict_shorter, "bar", 2);
  214. qdict_put_int(dict_shorter, "baz", 3);
  215. qdict_put(dict_nested, "f", qdict_new());
  216. qdict_put_int(qdict_get_qdict(dict_nested, "f"), "o", 1);
  217. qdict_put_int(dict_nested, "bar", 2);
  218. qdict_put_int(dict_nested, "baz", 3);
  219. qdict_put_null(dict_nested, "null");
  220. dict_cloned = qdict_clone_shallow(dict_0);
  221. check_equal(dict_0, dict_1, dict_cloned);
  222. check_unequal(dict_0, dict_different_key, dict_different_value,
  223. dict_different_null_key, dict_longer, dict_shorter,
  224. dict_nested);
  225. dict_crumpled = qobject_to(QDict, qdict_crumple(dict_1, &local_err));
  226. g_assert(!local_err);
  227. check_equal(dict_crumpled, dict_nested);
  228. qdict_flatten(dict_nested);
  229. check_equal(dict_0, dict_nested);
  230. /* Containing an NaN value will make this dict compare unequal to
  231. * itself */
  232. qdict_put(dict_0, "NaN", qnum_from_double(NAN));
  233. g_assert(qobject_is_equal(QOBJECT(dict_0), QOBJECT(dict_0)) == false);
  234. free_all(dict_0, dict_1, dict_cloned, dict_different_key,
  235. dict_different_value, dict_different_null_key, dict_longer,
  236. dict_shorter, dict_nested, dict_crumpled);
  237. }
  238. static void qobject_is_equal_conversion_test(void)
  239. {
  240. QNum *u0, *i0, *d0;
  241. QString *s0, *s_empty;
  242. QBool *bfalse;
  243. u0 = qnum_from_uint(0u);
  244. i0 = qnum_from_int(0);
  245. d0 = qnum_from_double(0.0);
  246. s0 = qstring_from_str("0");
  247. s_empty = qstring_new();
  248. bfalse = qbool_from_bool(false);
  249. /* No automatic type conversion */
  250. check_unequal(u0, s0, s_empty, bfalse, qnull(), NULL);
  251. check_unequal(i0, s0, s_empty, bfalse, qnull(), NULL);
  252. check_unequal(d0, s0, s_empty, bfalse, qnull(), NULL);
  253. free_all(u0, i0, d0, s0, s_empty, bfalse);
  254. }
  255. int main(int argc, char **argv)
  256. {
  257. g_test_init(&argc, &argv, NULL);
  258. g_test_add_func("/public/qobject_is_equal_null",
  259. qobject_is_equal_null_test);
  260. g_test_add_func("/public/qobject_is_equal_num", qobject_is_equal_num_test);
  261. g_test_add_func("/public/qobject_is_equal_bool",
  262. qobject_is_equal_bool_test);
  263. g_test_add_func("/public/qobject_is_equal_string",
  264. qobject_is_equal_string_test);
  265. g_test_add_func("/public/qobject_is_equal_list",
  266. qobject_is_equal_list_test);
  267. g_test_add_func("/public/qobject_is_equal_dict",
  268. qobject_is_equal_dict_test);
  269. g_test_add_func("/public/qobject_is_equal_conversion",
  270. qobject_is_equal_conversion_test);
  271. return g_test_run();
  272. }