test.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * test.h - test suite support
  4. *
  5. * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #ifndef LIBDPKG_TEST_H
  21. #define LIBDPKG_TEST_H
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #ifndef TEST_MAIN_PROVIDED
  26. #include <dpkg/ehandle.h>
  27. #endif
  28. /**
  29. * @defgroup dpkg_test Test suite support
  30. * @ingroup dpkg-internal
  31. * @{
  32. */
  33. #define test_bail(reason) \
  34. do { \
  35. printf("Bail out! %s\n", (reason)); \
  36. exit(255); \
  37. } while (0)
  38. #define test_xstringify(str) \
  39. #str
  40. #define test_stringify(str) \
  41. test_xstringify(str)
  42. static inline void *
  43. test_alloc(void *ptr, const char *reason)
  44. {
  45. if (ptr == NULL)
  46. test_bail(reason);
  47. return ptr;
  48. }
  49. #define test_alloc(ptr) \
  50. test_alloc((ptr), "cannot allocate memory for " #ptr " in " __FILE__ ":" test_stringify(__LINE__))
  51. static int test_id = 1;
  52. static int test_skip_code;
  53. static const char *test_skip_prefix;
  54. static const char *test_skip_reason;
  55. #define test_plan(n) \
  56. printf("1..%d\n", n);
  57. #define test_skip_all(reason) \
  58. do { \
  59. printf("1..0 # SKIP %s\n", (reason)); \
  60. exit(0); \
  61. } while (0)
  62. #define test_skip(reason) \
  63. printf("ok %d # SKIP %s\n", test_id++, (reason))
  64. #define test_skip_block(cond) \
  65. for (test_skip_prefix = " # SKIP ", \
  66. test_skip_reason = cond ? #cond : NULL, \
  67. test_skip_code = 1; \
  68. test_skip_prefix; \
  69. test_skip_prefix = test_skip_reason = NULL, test_skip_code = 0)
  70. #define test_todo(a, reason, desc) \
  71. do { \
  72. test_skip_prefix = " # TODO "; \
  73. test_skip_reason = reason; \
  74. test_case(a, "%s", desc); \
  75. test_skip_prefix = test_skip_reason = NULL; \
  76. } while(0)
  77. #define test_todo_block(reason) \
  78. for (test_skip_prefix = " # TODO ", test_skip_reason = reason; \
  79. test_skip_prefix; \
  80. test_skip_prefix = test_skip_reason = NULL)
  81. #define test_case(a, fmt, ...) \
  82. printf("%sok %d - " fmt "%s%s\n", \
  83. test_skip_code || (a) ? "" : "not ", \
  84. test_id++, __VA_ARGS__, \
  85. test_skip_reason ? test_skip_prefix : "", \
  86. test_skip_reason ? test_skip_reason : "")
  87. #define test_pass(a) \
  88. test_case((a), "pass %s", #a)
  89. #define test_fail(a) \
  90. test_case(!(a), "fail %s", #a)
  91. #define test_str(a, op, b) \
  92. test_case(strcmp((a), (b)) op 0, "strcmp '%s' %s '%s'", a, #op, b)
  93. #define test_mem(a, op, b, size) \
  94. test_case(memcmp((a), (b), (size)) op 0, "memcmp %p %s %p", a, #op, b)
  95. /* Specific test macros. */
  96. #define test_warn(e) \
  97. do { \
  98. test_pass((e).type == DPKG_MSG_WARN); \
  99. dpkg_error_destroy(&(e)); \
  100. } while (0)
  101. #define test_error(e) \
  102. do { \
  103. test_pass((e).type == DPKG_MSG_ERROR); \
  104. dpkg_error_destroy(&(e)); \
  105. } while (0)
  106. /** @} */
  107. #ifndef TEST_MAIN_PROVIDED
  108. static void test(void);
  109. int
  110. main(int argc, char **argv)
  111. {
  112. setvbuf(stdout, NULL, _IOLBF, 0);
  113. push_error_context();
  114. test();
  115. pop_error_context(ehflag_normaltidy);
  116. return 0;
  117. }
  118. #endif
  119. #endif