minicheck.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Miniature re-implementation of the "check" library.
  2. This is intended to support just enough of check to run the Expat
  3. tests. This interface is based entirely on the portion of the
  4. check library being used.
  5. This is *source* compatible, but not necessary *link* compatible.
  6. __ __ _
  7. ___\ \/ /_ __ __ _| |_
  8. / _ \\ /| '_ \ / _` | __|
  9. | __// \| |_) | (_| | |_
  10. \___/_/\_\ .__/ \__,_|\__|
  11. |_| XML parser
  12. Copyright (c) 2004-2006 Fred L. Drake, Jr. <[email protected]>
  13. Copyright (c) 2006-2012 Karl Waclawek <[email protected]>
  14. Copyright (c) 2016-2025 Sebastian Pipping <[email protected]>
  15. Copyright (c) 2022 Rhodri James <[email protected]>
  16. Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <[email protected]>
  17. Licensed under the MIT license:
  18. Permission is hereby granted, free of charge, to any person obtaining
  19. a copy of this software and associated documentation files (the
  20. "Software"), to deal in the Software without restriction, including
  21. without limitation the rights to use, copy, modify, merge, publish,
  22. distribute, sublicense, and/or sell copies of the Software, and to permit
  23. persons to whom the Software is furnished to do so, subject to the
  24. following conditions:
  25. The above copyright notice and this permission notice shall be included
  26. in all copies or substantial portions of the Software.
  27. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  30. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  31. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  32. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  33. USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. */
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #ifndef XML_MINICHECK_H
  39. # define XML_MINICHECK_H
  40. # define CK_NOFORK 0
  41. # define CK_FORK 1
  42. # define CK_SILENT 0
  43. # define CK_NORMAL 1
  44. # define CK_VERBOSE 2
  45. /* Workaround for Microsoft's compiler and Tru64 Unix systems where the
  46. C compiler has a working __func__, but the C++ compiler only has a
  47. working __FUNCTION__. This could be fixed in configure.in, but it's
  48. not worth it right now. */
  49. # if defined(_MSC_VER) || (defined(__osf__) && defined(__cplusplus))
  50. # define __func__ __FUNCTION__
  51. # endif
  52. /* PRINTF_LIKE has two effects:
  53. 1. Make clang's -Wformat-nonliteral stop warning about non-literal format
  54. strings in annotated functions' code.
  55. 2. Make both clang and gcc's -Wformat-nonliteral warn about *callers* of
  56. the annotated function that use a non-literal format string.
  57. */
  58. # if defined(__GNUC__)
  59. # define PRINTF_LIKE(fmtpos, argspos) \
  60. __attribute__((format(printf, fmtpos, argspos)))
  61. # else
  62. # define PRINTF_LIKE(fmtpos, argspos)
  63. # endif
  64. # define START_TEST(testname) \
  65. static void testname(void) { \
  66. _check_set_test_info(__func__, __FILE__, __LINE__); \
  67. {
  68. # define END_TEST \
  69. } \
  70. }
  71. void PRINTF_LIKE(1, 2) set_subtest(char const *fmt, ...);
  72. # define fail(msg) _fail(__FILE__, __LINE__, msg)
  73. # define assert_true(cond) \
  74. do { \
  75. if (! (cond)) { \
  76. _fail(__FILE__, __LINE__, "check failed: " #cond); \
  77. } \
  78. } while (0)
  79. typedef void (*tcase_setup_function)(void);
  80. typedef void (*tcase_teardown_function)(void);
  81. typedef void (*tcase_test_function)(void);
  82. typedef struct SRunner SRunner;
  83. typedef struct Suite Suite;
  84. typedef struct TCase TCase;
  85. struct SRunner {
  86. Suite *suite;
  87. int nchecks;
  88. int nfailures;
  89. };
  90. struct Suite {
  91. const char *name;
  92. TCase *tests;
  93. };
  94. struct TCase {
  95. const char *name;
  96. tcase_setup_function setup;
  97. tcase_teardown_function teardown;
  98. tcase_test_function *tests;
  99. int ntests;
  100. int allocated;
  101. TCase *next_tcase;
  102. };
  103. /* Internal helper. */
  104. void _check_set_test_info(char const *function, char const *filename,
  105. int lineno);
  106. /*
  107. * Prototypes for the actual implementation.
  108. */
  109. # if defined(__has_attribute)
  110. # if __has_attribute(noreturn)
  111. __attribute__((noreturn))
  112. # endif
  113. # endif
  114. void
  115. _fail(const char *file, int line, const char *msg);
  116. Suite *suite_create(const char *name);
  117. TCase *tcase_create(const char *name);
  118. void suite_add_tcase(Suite *suite, TCase *tc);
  119. void tcase_add_checked_fixture(TCase *tc, tcase_setup_function setup,
  120. tcase_teardown_function teardown);
  121. void tcase_add_test(TCase *tc, tcase_test_function test);
  122. SRunner *srunner_create(Suite *suite);
  123. void srunner_run_all(SRunner *runner, const char *context, int verbosity);
  124. void srunner_summarize(SRunner *runner, int verbosity);
  125. int srunner_ntests_failed(SRunner *runner);
  126. void srunner_free(SRunner *runner);
  127. #endif /* XML_MINICHECK_H */
  128. #ifdef __cplusplus
  129. }
  130. #endif