stest.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef STEST_H
  2. #define STEST_H
  3. #pragma once
  4. #include "config.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /** simple unit test framework */
  9. // int f(st_case_set*);
  10. typedef struct st_case_set st_case_set;
  11. typedef int (*st_case_func)(st_case_set*);
  12. TOOLKIT_API st_case_set* st_init();
  13. TOOLKIT_API void st_term(st_case_set*);
  14. TOOLKIT_API int st_run(st_case_set*);
  15. TOOLKIT_API int st_add(st_case_set*, const char *key, st_case_func func);
  16. TOOLKIT_API int st_remove(st_case_set*, const char *key);
  17. TOOLKIT_API int st_remove_all(st_case_set*);
  18. TOOLKIT_API int st_fail_printf(st_case_set*, const char *, ...);
  19. TOOLKIT_API int st_fail_vprintf(st_case_set*, const char *, va_list arg);
  20. #define ST_BEGIN_MAIN() \
  21. int main() { \
  22. st_case_set *__cset = st_init();
  23. #define ST_ADD(f) \
  24. ST_DECLARE_CASE(f); \
  25. st_add(__cset, #f, &f);
  26. #define ST_REMOVE(f) \
  27. st_remove(__cset, #f);
  28. #define ST_END_MAIN() \
  29. st_run(__cset); \
  30. st_term(__cset); \
  31. getchar(); \
  32. return 0; \
  33. }
  34. #define ST_DECLARE_CASE(f) int f(st_case_set* __cset)
  35. #define ST_ASSERT(b, str, ...) \
  36. do { \
  37. if (!(b)) { \
  38. st_fail_printf(__cset, str, __VA_ARGS__); \
  39. return -1; \
  40. } \
  41. } while(0)
  42. #define ST_OK() \
  43. return 0
  44. #define ST_FAIL(str, ...) \
  45. st_fail_printf(__cset, str, __VA_ARGS__); \
  46. return -1;
  47. #ifdef __cplusplus
  48. } // extern "C" {
  49. #endif
  50. #endif // STEST_H