check-qint.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * QInt unit-tests.
  3. *
  4. * Copyright (C) 2009 Red Hat Inc.
  5. *
  6. * Authors:
  7. * Luiz Capitulino <lcapitulino@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. */
  12. #include <check.h>
  13. #include "qint.h"
  14. #include "qemu-common.h"
  15. /*
  16. * Public Interface test-cases
  17. *
  18. * (with some violations to access 'private' data)
  19. */
  20. START_TEST(qint_from_int_test)
  21. {
  22. QInt *qi;
  23. const int value = -42;
  24. qi = qint_from_int(value);
  25. fail_unless(qi != NULL);
  26. fail_unless(qi->value == value);
  27. fail_unless(qi->base.refcnt == 1);
  28. fail_unless(qobject_type(QOBJECT(qi)) == QTYPE_QINT);
  29. // destroy doesn't exit yet
  30. g_free(qi);
  31. }
  32. END_TEST
  33. START_TEST(qint_destroy_test)
  34. {
  35. QInt *qi = qint_from_int(0);
  36. QDECREF(qi);
  37. }
  38. END_TEST
  39. START_TEST(qint_from_int64_test)
  40. {
  41. QInt *qi;
  42. const int64_t value = 0x1234567890abcdefLL;
  43. qi = qint_from_int(value);
  44. fail_unless((int64_t) qi->value == value);
  45. QDECREF(qi);
  46. }
  47. END_TEST
  48. START_TEST(qint_get_int_test)
  49. {
  50. QInt *qi;
  51. const int value = 123456;
  52. qi = qint_from_int(value);
  53. fail_unless(qint_get_int(qi) == value);
  54. QDECREF(qi);
  55. }
  56. END_TEST
  57. START_TEST(qobject_to_qint_test)
  58. {
  59. QInt *qi;
  60. qi = qint_from_int(0);
  61. fail_unless(qobject_to_qint(QOBJECT(qi)) == qi);
  62. QDECREF(qi);
  63. }
  64. END_TEST
  65. static Suite *qint_suite(void)
  66. {
  67. Suite *s;
  68. TCase *qint_public_tcase;
  69. s = suite_create("QInt test-suite");
  70. qint_public_tcase = tcase_create("Public Interface");
  71. suite_add_tcase(s, qint_public_tcase);
  72. tcase_add_test(qint_public_tcase, qint_from_int_test);
  73. tcase_add_test(qint_public_tcase, qint_destroy_test);
  74. tcase_add_test(qint_public_tcase, qint_from_int64_test);
  75. tcase_add_test(qint_public_tcase, qint_get_int_test);
  76. tcase_add_test(qint_public_tcase, qobject_to_qint_test);
  77. return s;
  78. }
  79. int main(void)
  80. {
  81. int nf;
  82. Suite *s;
  83. SRunner *sr;
  84. s = qint_suite();
  85. sr = srunner_create(s);
  86. srunner_run_all(sr, CK_NORMAL);
  87. nf = srunner_ntests_failed(sr);
  88. srunner_free(sr);
  89. return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  90. }