2
0

check-qnull.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * QNull unit-tests.
  3. *
  4. * Copyright (C) 2016 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 "qapi/qmp/qnull.h"
  11. #include "qemu-common.h"
  12. #include "qapi/qobject-input-visitor.h"
  13. #include "qapi/qobject-output-visitor.h"
  14. #include "qapi/error.h"
  15. /*
  16. * Public Interface test-cases
  17. *
  18. * (with some violations to access 'private' data)
  19. */
  20. static void qnull_ref_test(void)
  21. {
  22. QObject *obj;
  23. g_assert(qnull_.base.refcnt == 1);
  24. obj = QOBJECT(qnull());
  25. g_assert(obj);
  26. g_assert(obj == QOBJECT(&qnull_));
  27. g_assert(qnull_.base.refcnt == 2);
  28. g_assert(qobject_type(obj) == QTYPE_QNULL);
  29. qobject_unref(obj);
  30. g_assert(qnull_.base.refcnt == 1);
  31. }
  32. static void qnull_visit_test(void)
  33. {
  34. QObject *obj;
  35. Visitor *v;
  36. QNull *null;
  37. /*
  38. * Most tests of interactions between QObject and visitors are in
  39. * test-qmp-*-visitor; but these tests live here because they
  40. * depend on layering violations to check qnull_ refcnt.
  41. */
  42. g_assert(qnull_.base.refcnt == 1);
  43. obj = QOBJECT(qnull());
  44. v = qobject_input_visitor_new(obj);
  45. qobject_unref(obj);
  46. visit_type_null(v, NULL, &null, &error_abort);
  47. g_assert(obj == QOBJECT(&qnull_));
  48. qobject_unref(null);
  49. visit_free(v);
  50. null = NULL;
  51. v = qobject_output_visitor_new(&obj);
  52. visit_type_null(v, NULL, &null, &error_abort);
  53. visit_complete(v, &obj);
  54. g_assert(obj == QOBJECT(&qnull_));
  55. qobject_unref(null);
  56. qobject_unref(obj);
  57. visit_free(v);
  58. g_assert(qnull_.base.refcnt == 1);
  59. }
  60. int main(int argc, char **argv)
  61. {
  62. g_test_init(&argc, &argv, NULL);
  63. g_test_add_func("/public/qnull_ref", qnull_ref_test);
  64. g_test_add_func("/public/qnull_visit", qnull_visit_test);
  65. return g_test_run();
  66. }