minicheck.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Miniature re-implementation of the "check" library.
  2. *
  3. * This is intended to support just enough of check to run the Expat
  4. * tests. This interface is based entirely on the portion of the
  5. * check library being used.
  6. *
  7. * This is *source* compatible, but not necessary *link* compatible.
  8. */
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #define CK_NOFORK 0
  13. #define CK_FORK 1
  14. #define CK_SILENT 0
  15. #define CK_NORMAL 1
  16. #define CK_VERBOSE 2
  17. /* Workaround for Microsoft's compiler and Tru64 Unix systems where the
  18. C compiler has a working __func__, but the C++ compiler only has a
  19. working __FUNCTION__. This could be fixed in configure.in, but it's
  20. not worth it right now. */
  21. #if defined (_MSC_VER) || (defined(__osf__) && defined(__cplusplus))
  22. #define __func__ __FUNCTION__
  23. #endif
  24. /* ISO C90 does not support '__func__' predefined identifier */
  25. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ < 199901)) || \
  26. (defined(__GNUC__) && !defined(__STDC_VERSION__))
  27. # define __func__ "(unknown)"
  28. #endif
  29. #define START_TEST(testname) static void testname(void) { \
  30. _check_set_test_info(__func__, __FILE__, __LINE__); \
  31. {
  32. #define END_TEST } }
  33. #define fail(msg) _fail_unless(0, __FILE__, __LINE__, msg)
  34. typedef void (*tcase_setup_function)(void);
  35. typedef void (*tcase_teardown_function)(void);
  36. typedef void (*tcase_test_function)(void);
  37. typedef struct SRunner SRunner;
  38. typedef struct Suite Suite;
  39. typedef struct TCase TCase;
  40. struct SRunner {
  41. Suite *suite;
  42. int nchecks;
  43. int nfailures;
  44. };
  45. struct Suite {
  46. const char *name;
  47. TCase *tests;
  48. };
  49. struct TCase {
  50. const char *name;
  51. tcase_setup_function setup;
  52. tcase_teardown_function teardown;
  53. tcase_test_function *tests;
  54. int ntests;
  55. int allocated;
  56. TCase *next_tcase;
  57. };
  58. /* Internal helper. */
  59. void _check_set_test_info(char const *function,
  60. char const *filename, int lineno);
  61. /*
  62. * Prototypes for the actual implementation.
  63. */
  64. void _fail_unless(int condition, const char *file, int line, const char *msg);
  65. Suite *suite_create(const char *name);
  66. TCase *tcase_create(const char *name);
  67. void suite_add_tcase(Suite *suite, TCase *tc);
  68. void tcase_add_checked_fixture(TCase *,
  69. tcase_setup_function,
  70. tcase_teardown_function);
  71. void tcase_add_test(TCase *tc, tcase_test_function test);
  72. SRunner *srunner_create(Suite *suite);
  73. void srunner_run_all(SRunner *runner, int verbosity);
  74. int srunner_ntests_failed(SRunner *runner);
  75. void srunner_free(SRunner *runner);
  76. #ifdef __cplusplus
  77. }
  78. #endif