common.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifndef _USE_MATH_DEFINES
  10. # define _USE_MATH_DEFINES /* for windows */
  11. #endif
  12. #ifndef _CRT_SECURE_NO_WARNINGS
  13. # define _CRT_SECURE_NO_WARNINGS /* for windows */
  14. #endif
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <cglm/cglm.h>
  19. #include <cglm/struct.h>
  20. #include <cglm/call.h>
  21. typedef struct test_status_t {
  22. const char *msg;
  23. int status;
  24. } test_status_t;
  25. typedef test_status_t (*fntest)(void);
  26. typedef struct test_entry_t {
  27. char *name;
  28. fntest entry;
  29. int ret;
  30. int show_output;
  31. } test_entry_t;
  32. #ifndef GLM_TESTS_NO_COLORFUL_OUTPUT
  33. #define RESET "\033[0m"
  34. #define BLACK "\033[30m" /* Black */
  35. #define RED "\033[31m" /* Red */
  36. #define GREEN "\033[32m" /* Green */
  37. #define YELLOW "\033[33m" /* Yellow */
  38. #define BLUE "\033[34m" /* Blue */
  39. #define MAGENTA "\033[35m" /* Magenta */
  40. #define CYAN "\033[36m" /* Cyan */
  41. #define WHITE "\033[37m" /* White */
  42. #define BOLDBLACK "\033[1m\033[30m" /* Bold Black */
  43. #define BOLDRED "\033[1m\033[31m" /* Bold Red */
  44. #define BOLDGREEN "\033[1m\033[32m" /* Bold Green */
  45. #define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
  46. #define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */
  47. #define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */
  48. #define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */
  49. #define BOLDWHITE "\033[1m\033[37m" /* Bold White */
  50. #else
  51. #define RESET
  52. #define BLACK
  53. #define RED
  54. #define GREEN
  55. #define YELLOW
  56. #define BLUE
  57. #define MAGENTA
  58. #define CYAN
  59. #define WHITE
  60. #define BOLDBLACK
  61. #define BOLDRED
  62. #define BOLDGREEN
  63. #define BOLDYELLOW
  64. #define BOLDBLUE
  65. #define BOLDMAGENTA
  66. #define BOLDCYAN
  67. #define BOLDWHITE
  68. #endif
  69. #define TEST_DECLARE(FUN) test_status_t test_ ## FUN(void);
  70. #define TEST_ENTRY(FUN) { #FUN, test_ ## FUN, 0, 0 },
  71. #define TEST_LIST static test_entry_t tests[] =
  72. /* __VA_ARGS__ workaround for MSVC: https://stackoverflow.com/a/5134656 */
  73. #define EXPAND(x) x
  74. #define TEST_OK 1
  75. #define TEST_SUCCESS return (test_status_t){NULL, TEST_OK};
  76. #define TEST_IMPL_ARG1(FUN) \
  77. test_status_t test_ ## FUN (void); \
  78. test_status_t test_ ## FUN()
  79. #define TEST_IMPL_ARG2(PREFIX, FUN) TEST_IMPL_ARG1(PREFIX ## FUN)
  80. #define TEST_IMPL_ARG3(arg1, arg2, arg3, ...) arg3
  81. #define TEST_IMPL_CHOOSER(...) \
  82. EXPAND(TEST_IMPL_ARG3(__VA_ARGS__, TEST_IMPL_ARG2, TEST_IMPL_ARG1))
  83. #define TEST_IMPL(...) EXPAND(TEST_IMPL_CHOOSER(__VA_ARGS__)(__VA_ARGS__))
  84. #define ASSERT_EXT(expr, msg) \
  85. if (!(expr)) { \
  86. fprintf(stderr, \
  87. RED " assert fail" RESET \
  88. " in " BOLDCYAN "%s " RESET \
  89. "on " BOLDMAGENTA "line %d" RESET \
  90. " : " BOLDWHITE " ASSERT(%s)\n" RESET, \
  91. __FILE__, \
  92. __LINE__, \
  93. #expr); \
  94. return (test_status_t){msg, 0}; \
  95. }
  96. #define ASSERT_ARG1(expr) ASSERT_EXT(expr, NULL)
  97. #define ASSERT_ARG2(expr, msg) ASSERT_EXT(expr, msg)
  98. #define ASSERT_ARG3(arg1, arg2, arg3, ...) arg3
  99. #define ASSERT_CHOOSER(...) ASSERT_ARG3(__VA_ARGS__, ASSERT_ARG2, ASSERT_ARG1)
  100. #define ASSERT(...) do { ASSERT_CHOOSER(__VA_ARGS__)(__VA_ARGS__) } while(0);
  101. #define ASSERTIFY(expr) do { \
  102. test_status_t ts; \
  103. ts = expr; \
  104. if (ts.status != TEST_OK) { \
  105. fprintf(stderr, \
  106. RED " assert fail" RESET \
  107. " in " BOLDCYAN "%s " RESET \
  108. "on " BOLDMAGENTA "line %d" RESET \
  109. " : " BOLDWHITE " ASSERTIFY(%s)\n" RESET, \
  110. __FILE__, \
  111. __LINE__, \
  112. #expr); \
  113. return (test_status_t){ts.msg, 0}; \
  114. } \
  115. } while(0);
  116. #if defined(_WIN32)
  117. # define drand48() ((float)(rand() / (RAND_MAX + 1.0)))
  118. # define OK_TEXT "ok:"
  119. # define FAIL_TEXT "fail:"
  120. # define FINAL_TEXT "^_^"
  121. #else
  122. # define OK_TEXT "✔︎"
  123. # define FAIL_TEXT "𐄂"
  124. # define FINAL_TEXT "🎉"
  125. #endif
  126. #endif /* common_h */