zf_test.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #if !defined(_ZF_TEST_STRINGIFY) && !defined(_ZF_TEST__STRINGIFY)
  6. #define _ZF_TEST__STRINGIFY(x) #x
  7. #define _ZF_TEST_STRINGIFY(x) _ZF_TEST__STRINGIFY(x)
  8. #endif
  9. #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
  10. #define _ZF_TEST_INLINE __inline
  11. #else
  12. #define _ZF_TEST_INLINE inline
  13. #endif
  14. /* Workaround for MSVC warning 4127 about constant expression in if condition.
  15. */
  16. static _ZF_TEST_INLINE bool _zf_test_bool(const bool v)
  17. {
  18. return v;
  19. }
  20. #define TEST_RUNNER_CREATE(argc, argv) \
  21. (void)argc; (void)argv;
  22. #define TEST_SUIT_ARGUMENTS
  23. #define TEST_VERIFY_TRUE(a) \
  24. if (!_zf_test_bool(a)) { \
  25. fprintf(stderr, "%s:%u: %s:\n", __FILE__, __LINE__, "not true"); \
  26. fprintf(stderr, " false: %s\n", _ZF_TEST_STRINGIFY(a)); \
  27. exit(1); \
  28. }
  29. #define TEST_VERIFY_TRUE_MSG(a, ...) \
  30. if (!_zf_test_bool(a)) { \
  31. fprintf(stderr, "%s:%u: %s:\n", __FILE__, __LINE__, "not true"); \
  32. fprintf(stderr, " false: %s\n", _ZF_TEST_STRINGIFY(a)); \
  33. fprintf(stderr, " about: "); \
  34. fprintf(stderr, __VA_ARGS__); \
  35. fprintf(stderr, "\n"); \
  36. exit(1); \
  37. }
  38. #define TEST_VERIFY_FALSE(a) \
  39. if (_zf_test_bool(a)) { \
  40. fprintf(stderr, "%s:%u: %s:\n", __FILE__, __LINE__, "not false"); \
  41. fprintf(stderr, " true: %s\n", _ZF_TEST_STRINGIFY(a)); \
  42. exit(1); \
  43. }
  44. #define TEST_VERIFY_EQUAL(a, b) \
  45. if (!_zf_test_bool((a) == (b))) { \
  46. fprintf(stderr, "%s:%u: %s:\n", __FILE__, __LINE__, "not equal"); \
  47. fprintf(stderr, " left: %s\n", _ZF_TEST_STRINGIFY(a)); \
  48. fprintf(stderr, " reight: %s\n", _ZF_TEST_STRINGIFY(b)); \
  49. exit(1); \
  50. }
  51. #define TEST_VERIFY_NOT_EQUAL(a, b) \
  52. if (!_zf_test_bool((a) != (b))) { \
  53. fprintf(stderr, "%s:%u: %s:\n", __FILE__, __LINE__, "equal"); \
  54. fprintf(stderr, " left: %s\n", _ZF_TEST_STRINGIFY(a)); \
  55. fprintf(stderr, " reight: %s\n", _ZF_TEST_STRINGIFY(b)); \
  56. exit(1); \
  57. }
  58. #define TEST_VERIFY_GREATER_OR_EQUAL(a, b) \
  59. if (!_zf_test_bool((a) >= (b))) { \
  60. fprintf(stderr, "%s:%u: %s:\n", __FILE__, __LINE__, "less"); \
  61. fprintf(stderr, " left: %s\n", _ZF_TEST_STRINGIFY(a)); \
  62. fprintf(stderr, " reight: %s\n", _ZF_TEST_STRINGIFY(b)); \
  63. exit(1); \
  64. }
  65. #define TEST_EXECUTE(f) \
  66. f
  67. #define TEST_EXECUTE_SUITE(s) \
  68. s()
  69. #define TEST_RUNNER_EXIT_CODE() \
  70. 0