Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | None | 0 0
  1. if(gtest::is_running)
  2. ASSERT_TRUE(...);
  3. else
  4. assert(...);
  5.  
  6. #ifdef GTEST_ON
  7. ASSERT_TRUE(...);
  8. #else
  9. assert(...);
  10. #endif
  11.  
  12. #pragma once
  13.  
  14. #include "debug.hpp"
  15.  
  16. #ifdef UNIT_TESTS
  17. #include <gtest/gtest.h>
  18. #endif
  19.  
  20. #include <cassert>
  21.  
  22. #define ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(exp, precondition)
  23. if (!(precondition)); else if(!!(exp)); else ::utility::debug_break()
  24.  
  25. #ifdef GTEST_FAIL
  26.  
  27. #ifdef _MSC_VER
  28. #if _MSC_VER < 1600 // < MSVC++ 10 (Visual Studio 2010)
  29. #error lambda is not supported
  30. #endif
  31. #else
  32. #if __cplusplus < 201103L
  33. #error lambda is not supported
  34. #endif
  35. #endif
  36.  
  37. // TIPS:
  38. // * all labdas captured by reference because of the error in the MSVC 2015:
  39. // `error C3493 : '...' cannot be implicitly captured because no default capture mode has been specified`
  40. // * if debugger is attached but `::testing::GTEST_FLAG(break_on_failure)` has not been setted,
  41. // then an assertion does a post break.
  42.  
  43. // gtest asserts rebind with the `void` error workaround (C++11 and higher is required)
  44. #undef ASSERT_TRUE
  45. #define ASSERT_TRUE(condition) [&]() -> void { GTEST_TEST_BOOLEAN_((condition), #condition, false, true, GTEST_FATAL_FAILURE_);
  46. ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_true(condition), !::testing::GTEST_FLAG(break_on_failure)); }()
  47. #undef ASSERT_FALSE
  48. #define ASSERT_FALSE(condition) [&]() -> void { GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, GTEST_FATAL_FAILURE_);
  49. ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_false(condition), !::testing::GTEST_FLAG(break_on_failure)); }()
  50.  
  51. #if !GTEST_DONT_DEFINE_ASSERT_EQ
  52. #undef ASSERT_EQ
  53. # define ASSERT_EQ(val1, val2) [&]() -> void { GTEST_ASSERT_EQ(val1, val2);
  54. ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_equal(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
  55. #endif
  56.  
  57. #if !GTEST_DONT_DEFINE_ASSERT_NE
  58. #undef ASSERT_NE
  59. # define ASSERT_NE(val1, val2) [&]() -> void { GTEST_ASSERT_NE(val1, val2);
  60. ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_not_equal(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
  61. #endif
  62.  
  63. #if !GTEST_DONT_DEFINE_ASSERT_LE
  64. #undef ASSERT_LE
  65. # define ASSERT_LE(val1, val2) [&]() -> void { GTEST_ASSERT_LE(val1, val2);
  66. ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_less_or_equal(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
  67. #endif
  68.  
  69. #if !GTEST_DONT_DEFINE_ASSERT_LT
  70. #undef ASSERT_LT
  71. # define ASSERT_LT(val1, val2) [&]() -> void { GTEST_ASSERT_LT(val1, val2);
  72. ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_less(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
  73. #endif
  74.  
  75. #if !GTEST_DONT_DEFINE_ASSERT_GE
  76. #undef ASSERT_GE
  77. # define ASSERT_GE(val1, val2) [&]() -> void { GTEST_ASSERT_GE(val1, val2);
  78. ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_greater_or_equal(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
  79. #endif
  80.  
  81. #if !GTEST_DONT_DEFINE_ASSERT_GT
  82. #undef ASSERT_GT
  83. # define ASSERT_GT(val1, val2) [&]() -> void { GTEST_ASSERT_GT(val1, val2);
  84. ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_greater(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
  85. #endif
  86.  
  87. #define ASSERT(x) ASSERT_TRUE(x)
  88.  
  89. #else
  90.  
  91. #ifndef ASSERT_IMPL
  92. #define ASSERT_IMPL(exp) assert(exp)
  93. #endif
  94.  
  95. #ifdef _DEBUG
  96.  
  97. #define ASSERT_TRUE(exp) ASSERT_IMPL(exp)
  98. #define ASSERT_FALSE(exp) ASSERT_IMPL(!(exp))
  99.  
  100. #define ASSERT_EQ(v1, v2) ASSERT_IMPL((v1) == (v2))
  101. #define ASSERT_NE(v1, v2) ASSERT_IMPL((v1) != (v2)))
  102. #define ASSERT_LE(v1, v2) ASSERT_IMPL((v1) <= (v2))
  103. #define ASSERT_LT(v1, v2) ASSERT_IMPL((v1) < (v2))
  104. #define ASSERT_GE(v1, v2) ASSERT_IMPL((v1) > (v2))
  105. #define ASSERT_GT(v1, v2) ASSERT_IMPL((v1) >= (v2))
  106.  
  107. #define ASSERT(exp) ASSERT_IMPL(exp)
  108.  
  109. #else
  110.  
  111. #define ASSERT_TRUE(exp) ::utility::is_true(exp)
  112. #define ASSERT_FALSE(exp) ::utility::is_false(exp)
  113.  
  114. #define ASSERT_EQ(v1, v2) ::utility::is_equal(v1, v2)
  115. #define ASSERT_NE(v1, v2) ::utility::is_not_equal(v1, v2)
  116. #define ASSERT_LE(v1, v2) ::utility::is_less_or_equal(v1, v2)
  117. #define ASSERT_LT(v1, v2) ::utility::is_less(v1, v2)
  118. #define ASSERT_GE(v1, v2) ::utility::is_greater_or_equal(v1, v2)
  119. #define ASSERT_GT(v1, v2) ::utility::is_greater(v1, v2)
  120.  
  121. #define ASSERT(exp) ::utility::is_true(exp)
  122.  
  123. #endif
  124.  
  125. #endif
  126.  
  127. namespace utility
  128. {
  129. // TIPS:
  130. // * to capture parameters by reference in macro definitions for single evaluation
  131. // * to suppress `unused variable` warnings like: `warning C4101: '...': unreferenced local variable`
  132. template<typename T>
  133. inline bool is_true(const T & v)
  134. {
  135. return !!v; // to avoid warnings of truncation to bool
  136. }
  137.  
  138. template<typename T>
  139. inline bool is_false(const T & v)
  140. {
  141. return !v; // to avoid warnings of truncation to bool
  142. }
  143.  
  144. template<typename T1, typename T2>
  145. inline bool is_equal(const T1 & v1, const T2 & v2)
  146. {
  147. return v1 == v2;
  148. }
  149.  
  150. template<typename T1, typename T2>
  151. inline bool is_not_equal(const T1 & v1, const T2 & v2)
  152. {
  153. return v1 != v2;
  154. }
  155.  
  156. template<typename T1, typename T2>
  157. inline bool is_less_or_equal(const T1 & v1, const T2 & v2)
  158. {
  159. return v1 <= v2;
  160. }
  161.  
  162. template<typename T1, typename T2>
  163. inline bool is_less(const T1 & v1, const T2 & v2)
  164. {
  165. return v1 < v2;
  166. }
  167.  
  168. template<typename T1, typename T2>
  169. inline bool is_greater_or_equal(const T1 & v1, const T2 & v2)
  170. {
  171. return v1 >= v2;
  172. }
  173.  
  174. template<typename T1, typename T2>
  175. inline bool is_greater(const T1 & v1, const T2 & v2)
  176. {
  177. return v1 > v2;
  178. }
  179. }
  180.  
  181. #pragma once
  182.  
  183. namespace utility
  184. {
  185. void debug_break(bool breakCondition = true);
  186. bool is_under_debugger();
  187. }
  188.  
  189. #include "debug.hpp"
  190. #include "platform.hpp"
  191.  
  192. #if defined(UTILITY_PLATFORM_WINDOWS)
  193. #include <windows.h>
  194. #include <intrin.h>
  195. #elif defined(UTILITY_PLATFORM_POSIX)
  196. #include <sys/ptrace.h>
  197. #include <signal.h>
  198. static void signal_handler(int) { }
  199. #else
  200. #error is_under_debugger is not supported for this platform
  201. #endif
  202.  
  203.  
  204. namespace utility {
  205.  
  206. void debug_break(bool breakCondition)
  207. {
  208. // avoid signal if not under debugger
  209. if (breakCondition && is_under_debugger()) {
  210. #if defined(UTILITY_COMPILER_CXX_MSC)
  211. __debugbreak(); // won't require debug symbols to show the call stack, when the DebugBreak() will require system debug symbols to show the call stack correctly
  212. #elif defined(UTILITY_PLATFORM_POSIX)
  213. signal(SIGTRAP, signal_handler);
  214. #else
  215. #error debug_break is not supported for this platform
  216. #endif
  217. }
  218. }
  219.  
  220. bool is_under_debugger()
  221. {
  222. #if defined(UTILITY_PLATFORM_WINDOWS)
  223. return !!::IsDebuggerPresent();
  224. #elif defined(UTILITY_PLATFORM_POSIX)
  225. return ptrace(PTRACE_TRACEME, 0, NULL, 0) == -1;
  226. #endif
  227. }
  228.  
  229. }
  230.  
  231. #pragma once
  232.  
  233. // linux, also other platforms (Hurd etc) that use GLIBC, should these really have their own config headers though?
  234. #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
  235. # define UTILITY_PLATFORM_LINUX
  236. # define UTILITY_PLATFORM_POSIX
  237. # if defined(__mcbc__)
  238. # define UTILITY_PLATFORM_MCBC
  239. # define UTILITY_PLATFORM_SHORT_NAME "MCBC"
  240. # elif defined( __astra_linux__ )
  241. # define UTILITY_PLATFORM_ASTRA_LINUX
  242. # define UTILITY_PLATFORM_SHORT_NAME "Astra Linux"
  243. # else
  244. # define UTILITY_PLATFORM_SHORT_NAME "Linux"
  245. # endif
  246. #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) // BSD:
  247. # define UTILITY_PLATFORM_BSD
  248. # define UTILITY_PLATFORM_POSIX
  249. # define UTILITY_PLATFORM_SHORT_NAME "BSD"
  250. #elif defined(sun) || defined(__sun) // solaris:
  251. # define UTILITY_PLATFORM_SOLARIS
  252. # define UTILITY_PLATFORM_POSIX
  253. # define UTILITY_PLATFORM_SHORT_NAME "Solaris"
  254. #elif defined(__CYGWIN__) // cygwin is not win32:
  255. # define UTILITY_PLATFORM_CYGWIN
  256. # define UTILITY_PLATFORM_POSIX
  257. # define UTILITY_PLATFORM_SHORT_NAME "Cygwin"
  258. #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) // win32:
  259. # define UTILITY_PLATFORM_WINDOWS
  260. # define UTILITY_PLATFORM_SHORT_NAME "Windows"
  261. # if defined(__MINGW32__) // Get the information about the MinGW runtime, i.e. __MINGW32_*VERSION.
  262. # include <_mingw.h>
  263. # endif
  264. #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) // MacOS
  265. # define UTILITY_PLATFORM_APPLE
  266. # define UTILITY_PLATFORM_POSIX
  267. # define UTILITY_PLATFORM_SHORT_NAME "MacOS"
  268. #elif defined(__QNXNTO__) // QNX:
  269. # define UTILITY_PLATFORM_QNIX
  270. # define UTILITY_PLATFORM_POSIX
  271. # define UTILITY_PLATFORM_SHORT_NAME "QNX"
  272. #elif defined(unix) || defined(__unix) || defined(_XOPEN_SOURCE) || defined(_POSIX_SOURCE)
  273. # define UTILITY_PLATFORM_UNINX
  274. # define UTILITY_PLATFORM_POSIX
  275. # define UTILITY_PLATFORM_SHORT_NAME "Unix"
  276. #else
  277. # error Unknown platform
  278. #endif
  279.  
  280. #if defined(__GNUC__)
  281. # define UTILITY_COMPILER_CXX_GCC
  282. # define UTILITY_COMPILER_CXX "gcc"
  283. # define UTILITY_COMPILER_CXX_VERSION __GNUC__
  284. # if __GNUC__ < 4
  285. # error "Unsuported gcc version"
  286. # endif
  287. #elif defined(_MSC_VER)
  288. # define UTILITY_COMPILER_CXX_MSC
  289. # define UTILITY_COMPILER_CXX "MS VisualC"
  290. # define UTILITY_COMPILER_CXX_VERSION _MSC_VER
  291. #else
  292. # error "Unknown compiler"
  293. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement