common.h 5.2 KB

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