common.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. #ifndef tests_common_h
  8. #define tests_common_h
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <cglm/cglm.h>
  13. #include <cglm/struct.h>
  14. #include <cglm/call.h>
  15. typedef struct test_status_t {
  16. const char *msg;
  17. int status;
  18. } test_status_t;
  19. typedef test_status_t (*fntest)(void);
  20. typedef struct test_entry_t {
  21. char *name;
  22. fntest entry;
  23. int ret;
  24. int show_output;
  25. } test_entry_t;
  26. #define RESET "\033[0m"
  27. #define BLACK "\033[30m" /* Black */
  28. #define RED "\033[31m" /* Red */
  29. #define GREEN "\033[32m" /* Green */
  30. #define YELLOW "\033[33m" /* Yellow */
  31. #define BLUE "\033[34m" /* Blue */
  32. #define MAGENTA "\033[35m" /* Magenta */
  33. #define CYAN "\033[36m" /* Cyan */
  34. #define WHITE "\033[37m" /* White */
  35. #define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
  36. #define BOLDRED "\033[1m\033[31m" /* Bold Red */
  37. #define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
  38. #define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
  39. #define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
  40. #define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
  41. #define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
  42. #define BOLDWHITE "\033[1m\033[37m" /* Bold White */
  43. #define TEST_DECLARE(FUN) test_status_t test_ ## FUN(void);
  44. #define TEST_ENTRY(FUN) { #FUN, test_ ## FUN, 0, 0 },
  45. #define TEST_LIST static test_entry_t tests[] =
  46. /* __VA_ARGS__ workaround for MSVC: https://stackoverflow.com/a/5134656 */
  47. #define EXPAND(x) x
  48. #define TEST_OK 1
  49. #define TEST_SUCCESS return (test_status_t){NULL, TEST_OK};
  50. #define TEST_IMPL_ARG1(FUN) \
  51. test_status_t test_ ## FUN (void); \
  52. test_status_t test_ ## FUN()
  53. #define TEST_IMPL_ARG2(PREFIX, FUN) TEST_IMPL_ARG1(PREFIX ## FUN)
  54. #define TEST_IMPL_ARG3(arg1, arg2, arg3, ...) arg3
  55. #define TEST_IMPL_CHOOSER(...) \
  56. EXPAND(TEST_IMPL_ARG3(__VA_ARGS__, TEST_IMPL_ARG2, TEST_IMPL_ARG1))
  57. #define TEST_IMPL(...) EXPAND(TEST_IMPL_CHOOSER(__VA_ARGS__)(__VA_ARGS__))
  58. #define ASSERT_EXT(expr, msg) \
  59. if (!(expr)) { \
  60. fprintf(stderr, \
  61. RED " assert fail" RESET \
  62. " in " BOLDCYAN "%s " RESET \
  63. "on " BOLDMAGENTA "line %d" RESET \
  64. " : " BOLDWHITE " ASSERT(%s)\n" RESET, \
  65. __FILE__, \
  66. __LINE__, \
  67. #expr); \
  68. return (test_status_t){msg, 0}; \
  69. }
  70. #define ASSERT_ARG1(expr) ASSERT_EXT(expr, NULL)
  71. #define ASSERT_ARG2(expr, msg) ASSERT_EXT(expr, msg)
  72. #define ASSERT_ARG3(arg1, arg2, arg3, ...) arg3
  73. #define ASSERT_CHOOSER(...) ASSERT_ARG3(__VA_ARGS__, ASSERT_ARG2, ASSERT_ARG1)
  74. #define ASSERT(...) do { ASSERT_CHOOSER(__VA_ARGS__)(__VA_ARGS__) } while(0);
  75. #define ASSERTIFY(expr) do { \
  76. test_status_t ts; \
  77. ts = expr; \
  78. if (ts.status != TEST_OK) { \
  79. fprintf(stderr, \
  80. RED " assert fail" RESET \
  81. " in " BOLDCYAN "%s " RESET \
  82. "on " BOLDMAGENTA "line %d" RESET \
  83. " : " BOLDWHITE " ASSERTIFY(%s)\n" RESET, \
  84. __FILE__, \
  85. __LINE__, \
  86. #expr); \
  87. return (test_status_t){ts.msg, 0}; \
  88. } \
  89. } while(0);
  90. #if defined(_WIN32)
  91. # define drand48() ((float)(rand() / (RAND_MAX + 1.0)))
  92. # define OK_TEXT "ok:"
  93. # define FAIL_TEXT "fail:"
  94. # define FINAL_TEXT "^_^"
  95. #else
  96. # define OK_TEXT "✔︎"
  97. # define FAIL_TEXT "𐄂"
  98. # define FINAL_TEXT "🎉"
  99. #endif
  100. #endif /* common_h */