common.h 5.4 KB

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