minicheck.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #define START_TEST(testname) static void testname(void) { \
  25. _check_set_test_info(__func__, __FILE__, __LINE__); \
  26. {
  27. #define END_TEST } }
  28. #define fail(msg) _fail_unless(0, __FILE__, __LINE__, msg)
  29. typedef void (*tcase_setup_function)(void);
  30. typedef void (*tcase_teardown_function)(void);
  31. typedef void (*tcase_test_function)(void);
  32. typedef struct SRunner SRunner;
  33. typedef struct Suite Suite;
  34. typedef struct TCase TCase;
  35. struct SRunner {
  36. Suite *suite;
  37. int nchecks;
  38. int nfailures;
  39. };
  40. struct Suite {
  41. char *name;
  42. TCase *tests;
  43. };
  44. struct TCase {
  45. char *name;
  46. tcase_setup_function setup;
  47. tcase_teardown_function teardown;
  48. tcase_test_function *tests;
  49. int ntests;
  50. int allocated;
  51. TCase *next_tcase;
  52. };
  53. /* Internal helper. */
  54. void _check_set_test_info(char const *function,
  55. char const *filename, int lineno);
  56. /*
  57. * Prototypes for the actual implementation.
  58. */
  59. void _fail_unless(int condition, const char *file, int line, char *msg);
  60. Suite *suite_create(char *name);
  61. TCase *tcase_create(char *name);
  62. void suite_add_tcase(Suite *suite, TCase *tc);
  63. void tcase_add_checked_fixture(TCase *,
  64. tcase_setup_function,
  65. tcase_teardown_function);
  66. void tcase_add_test(TCase *tc, tcase_test_function test);
  67. SRunner *srunner_create(Suite *suite);
  68. void srunner_run_all(SRunner *runner, int verbosity);
  69. int srunner_ntests_failed(SRunner *runner);
  70. void srunner_free(SRunner *runner);
  71. #ifdef __cplusplus
  72. }
  73. #endif