check-qlit.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * QLit unit-tests.
  3. *
  4. * Copyright (C) 2017 Red Hat Inc.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qapi/qmp/qbool.h"
  11. #include "qapi/qmp/qdict.h"
  12. #include "qapi/qmp/qlist.h"
  13. #include "qapi/qmp/qlit.h"
  14. #include "qapi/qmp/qnum.h"
  15. #include "qapi/qmp/qstring.h"
  16. static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) {
  17. { "foo", QLIT_QNUM(42) },
  18. { "bar", QLIT_QSTR("hello world") },
  19. { "baz", QLIT_QNULL },
  20. { "bee", QLIT_QLIST(((QLitObject[]) {
  21. QLIT_QNUM(43),
  22. QLIT_QNUM(44),
  23. QLIT_QBOOL(true),
  24. { },
  25. }))},
  26. { },
  27. }));
  28. static QLitObject qlit_foo = QLIT_QDICT(((QLitDictEntry[]) {
  29. { "foo", QLIT_QNUM(42) },
  30. { },
  31. }));
  32. static QObject *make_qobject(void)
  33. {
  34. QDict *qdict = qdict_new();
  35. QList *list = qlist_new();
  36. qdict_put_int(qdict, "foo", 42);
  37. qdict_put_str(qdict, "bar", "hello world");
  38. qdict_put_null(qdict, "baz");
  39. qlist_append_int(list, 43);
  40. qlist_append_int(list, 44);
  41. qlist_append_bool(list, true);
  42. qdict_put(qdict, "bee", list);
  43. return QOBJECT(qdict);
  44. }
  45. static void qlit_equal_qobject_test(void)
  46. {
  47. QObject *qobj = make_qobject();
  48. g_assert(qlit_equal_qobject(&qlit, qobj));
  49. g_assert(!qlit_equal_qobject(&qlit_foo, qobj));
  50. qdict_put(qobject_to(QDict, qobj), "bee", qlist_new());
  51. g_assert(!qlit_equal_qobject(&qlit, qobj));
  52. qobject_unref(qobj);
  53. }
  54. static void qobject_from_qlit_test(void)
  55. {
  56. QObject *obj, *qobj = qobject_from_qlit(&qlit);
  57. QDict *qdict;
  58. QList *bee;
  59. qdict = qobject_to(QDict, qobj);
  60. g_assert_cmpint(qdict_get_int(qdict, "foo"), ==, 42);
  61. g_assert_cmpstr(qdict_get_str(qdict, "bar"), ==, "hello world");
  62. g_assert(qobject_type(qdict_get(qdict, "baz")) == QTYPE_QNULL);
  63. bee = qdict_get_qlist(qdict, "bee");
  64. obj = qlist_pop(bee);
  65. g_assert_cmpint(qnum_get_int(qobject_to(QNum, obj)), ==, 43);
  66. qobject_unref(obj);
  67. obj = qlist_pop(bee);
  68. g_assert_cmpint(qnum_get_int(qobject_to(QNum, obj)), ==, 44);
  69. qobject_unref(obj);
  70. obj = qlist_pop(bee);
  71. g_assert(qbool_get_bool(qobject_to(QBool, obj)));
  72. qobject_unref(obj);
  73. qobject_unref(qobj);
  74. }
  75. int main(int argc, char **argv)
  76. {
  77. g_test_init(&argc, &argv, NULL);
  78. g_test_add_func("/qlit/equal_qobject", qlit_equal_qobject_test);
  79. g_test_add_func("/qlit/qobject_from_qlit", qobject_from_qlit_test);
  80. return g_test_run();
  81. }