Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(gtest::is_running)
- ASSERT_TRUE(...);
- else
- assert(...);
- #ifdef GTEST_ON
- ASSERT_TRUE(...);
- #else
- assert(...);
- #endif
- #pragma once
- #include "debug.hpp"
- #ifdef UNIT_TESTS
- #include <gtest/gtest.h>
- #endif
- #include <cassert>
- #define ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(exp, precondition)
- if (!(precondition)); else if(!!(exp)); else ::utility::debug_break()
- #ifdef GTEST_FAIL
- #ifdef _MSC_VER
- #if _MSC_VER < 1600 // < MSVC++ 10 (Visual Studio 2010)
- #error lambda is not supported
- #endif
- #else
- #if __cplusplus < 201103L
- #error lambda is not supported
- #endif
- #endif
- // TIPS:
- // * all labdas captured by reference because of the error in the MSVC 2015:
- // `error C3493 : '...' cannot be implicitly captured because no default capture mode has been specified`
- // * if debugger is attached but `::testing::GTEST_FLAG(break_on_failure)` has not been setted,
- // then an assertion does a post break.
- // gtest asserts rebind with the `void` error workaround (C++11 and higher is required)
- #undef ASSERT_TRUE
- #define ASSERT_TRUE(condition) [&]() -> void { GTEST_TEST_BOOLEAN_((condition), #condition, false, true, GTEST_FATAL_FAILURE_);
- ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_true(condition), !::testing::GTEST_FLAG(break_on_failure)); }()
- #undef ASSERT_FALSE
- #define ASSERT_FALSE(condition) [&]() -> void { GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, GTEST_FATAL_FAILURE_);
- ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_false(condition), !::testing::GTEST_FLAG(break_on_failure)); }()
- #if !GTEST_DONT_DEFINE_ASSERT_EQ
- #undef ASSERT_EQ
- # define ASSERT_EQ(val1, val2) [&]() -> void { GTEST_ASSERT_EQ(val1, val2);
- ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_equal(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
- #endif
- #if !GTEST_DONT_DEFINE_ASSERT_NE
- #undef ASSERT_NE
- # define ASSERT_NE(val1, val2) [&]() -> void { GTEST_ASSERT_NE(val1, val2);
- ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_not_equal(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
- #endif
- #if !GTEST_DONT_DEFINE_ASSERT_LE
- #undef ASSERT_LE
- # define ASSERT_LE(val1, val2) [&]() -> void { GTEST_ASSERT_LE(val1, val2);
- ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_less_or_equal(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
- #endif
- #if !GTEST_DONT_DEFINE_ASSERT_LT
- #undef ASSERT_LT
- # define ASSERT_LT(val1, val2) [&]() -> void { GTEST_ASSERT_LT(val1, val2);
- ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_less(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
- #endif
- #if !GTEST_DONT_DEFINE_ASSERT_GE
- #undef ASSERT_GE
- # define ASSERT_GE(val1, val2) [&]() -> void { GTEST_ASSERT_GE(val1, val2);
- ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_greater_or_equal(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
- #endif
- #if !GTEST_DONT_DEFINE_ASSERT_GT
- #undef ASSERT_GT
- # define ASSERT_GT(val1, val2) [&]() -> void { GTEST_ASSERT_GT(val1, val2);
- ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(::utility::is_greater(val1, val2), !::testing::GTEST_FLAG(break_on_failure)); }()
- #endif
- #define ASSERT(x) ASSERT_TRUE(x)
- #else
- #ifndef ASSERT_IMPL
- #define ASSERT_IMPL(exp) assert(exp)
- #endif
- #ifdef _DEBUG
- #define ASSERT_TRUE(exp) ASSERT_IMPL(exp)
- #define ASSERT_FALSE(exp) ASSERT_IMPL(!(exp))
- #define ASSERT_EQ(v1, v2) ASSERT_IMPL((v1) == (v2))
- #define ASSERT_NE(v1, v2) ASSERT_IMPL((v1) != (v2)))
- #define ASSERT_LE(v1, v2) ASSERT_IMPL((v1) <= (v2))
- #define ASSERT_LT(v1, v2) ASSERT_IMPL((v1) < (v2))
- #define ASSERT_GE(v1, v2) ASSERT_IMPL((v1) > (v2))
- #define ASSERT_GT(v1, v2) ASSERT_IMPL((v1) >= (v2))
- #define ASSERT(exp) ASSERT_IMPL(exp)
- #else
- #define ASSERT_TRUE(exp) ::utility::is_true(exp)
- #define ASSERT_FALSE(exp) ::utility::is_false(exp)
- #define ASSERT_EQ(v1, v2) ::utility::is_equal(v1, v2)
- #define ASSERT_NE(v1, v2) ::utility::is_not_equal(v1, v2)
- #define ASSERT_LE(v1, v2) ::utility::is_less_or_equal(v1, v2)
- #define ASSERT_LT(v1, v2) ::utility::is_less(v1, v2)
- #define ASSERT_GE(v1, v2) ::utility::is_greater_or_equal(v1, v2)
- #define ASSERT_GT(v1, v2) ::utility::is_greater(v1, v2)
- #define ASSERT(exp) ::utility::is_true(exp)
- #endif
- #endif
- namespace utility
- {
- // TIPS:
- // * to capture parameters by reference in macro definitions for single evaluation
- // * to suppress `unused variable` warnings like: `warning C4101: '...': unreferenced local variable`
- template<typename T>
- inline bool is_true(const T & v)
- {
- return !!v; // to avoid warnings of truncation to bool
- }
- template<typename T>
- inline bool is_false(const T & v)
- {
- return !v; // to avoid warnings of truncation to bool
- }
- template<typename T1, typename T2>
- inline bool is_equal(const T1 & v1, const T2 & v2)
- {
- return v1 == v2;
- }
- template<typename T1, typename T2>
- inline bool is_not_equal(const T1 & v1, const T2 & v2)
- {
- return v1 != v2;
- }
- template<typename T1, typename T2>
- inline bool is_less_or_equal(const T1 & v1, const T2 & v2)
- {
- return v1 <= v2;
- }
- template<typename T1, typename T2>
- inline bool is_less(const T1 & v1, const T2 & v2)
- {
- return v1 < v2;
- }
- template<typename T1, typename T2>
- inline bool is_greater_or_equal(const T1 & v1, const T2 & v2)
- {
- return v1 >= v2;
- }
- template<typename T1, typename T2>
- inline bool is_greater(const T1 & v1, const T2 & v2)
- {
- return v1 > v2;
- }
- }
- #pragma once
- namespace utility
- {
- void debug_break(bool breakCondition = true);
- bool is_under_debugger();
- }
- #include "debug.hpp"
- #include "platform.hpp"
- #if defined(UTILITY_PLATFORM_WINDOWS)
- #include <windows.h>
- #include <intrin.h>
- #elif defined(UTILITY_PLATFORM_POSIX)
- #include <sys/ptrace.h>
- #include <signal.h>
- static void signal_handler(int) { }
- #else
- #error is_under_debugger is not supported for this platform
- #endif
- namespace utility {
- void debug_break(bool breakCondition)
- {
- // avoid signal if not under debugger
- if (breakCondition && is_under_debugger()) {
- #if defined(UTILITY_COMPILER_CXX_MSC)
- __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
- #elif defined(UTILITY_PLATFORM_POSIX)
- signal(SIGTRAP, signal_handler);
- #else
- #error debug_break is not supported for this platform
- #endif
- }
- }
- bool is_under_debugger()
- {
- #if defined(UTILITY_PLATFORM_WINDOWS)
- return !!::IsDebuggerPresent();
- #elif defined(UTILITY_PLATFORM_POSIX)
- return ptrace(PTRACE_TRACEME, 0, NULL, 0) == -1;
- #endif
- }
- }
- #pragma once
- // linux, also other platforms (Hurd etc) that use GLIBC, should these really have their own config headers though?
- #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
- # define UTILITY_PLATFORM_LINUX
- # define UTILITY_PLATFORM_POSIX
- # if defined(__mcbc__)
- # define UTILITY_PLATFORM_MCBC
- # define UTILITY_PLATFORM_SHORT_NAME "MCBC"
- # elif defined( __astra_linux__ )
- # define UTILITY_PLATFORM_ASTRA_LINUX
- # define UTILITY_PLATFORM_SHORT_NAME "Astra Linux"
- # else
- # define UTILITY_PLATFORM_SHORT_NAME "Linux"
- # endif
- #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) // BSD:
- # define UTILITY_PLATFORM_BSD
- # define UTILITY_PLATFORM_POSIX
- # define UTILITY_PLATFORM_SHORT_NAME "BSD"
- #elif defined(sun) || defined(__sun) // solaris:
- # define UTILITY_PLATFORM_SOLARIS
- # define UTILITY_PLATFORM_POSIX
- # define UTILITY_PLATFORM_SHORT_NAME "Solaris"
- #elif defined(__CYGWIN__) // cygwin is not win32:
- # define UTILITY_PLATFORM_CYGWIN
- # define UTILITY_PLATFORM_POSIX
- # define UTILITY_PLATFORM_SHORT_NAME "Cygwin"
- #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) // win32:
- # define UTILITY_PLATFORM_WINDOWS
- # define UTILITY_PLATFORM_SHORT_NAME "Windows"
- # if defined(__MINGW32__) // Get the information about the MinGW runtime, i.e. __MINGW32_*VERSION.
- # include <_mingw.h>
- # endif
- #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) // MacOS
- # define UTILITY_PLATFORM_APPLE
- # define UTILITY_PLATFORM_POSIX
- # define UTILITY_PLATFORM_SHORT_NAME "MacOS"
- #elif defined(__QNXNTO__) // QNX:
- # define UTILITY_PLATFORM_QNIX
- # define UTILITY_PLATFORM_POSIX
- # define UTILITY_PLATFORM_SHORT_NAME "QNX"
- #elif defined(unix) || defined(__unix) || defined(_XOPEN_SOURCE) || defined(_POSIX_SOURCE)
- # define UTILITY_PLATFORM_UNINX
- # define UTILITY_PLATFORM_POSIX
- # define UTILITY_PLATFORM_SHORT_NAME "Unix"
- #else
- # error Unknown platform
- #endif
- #if defined(__GNUC__)
- # define UTILITY_COMPILER_CXX_GCC
- # define UTILITY_COMPILER_CXX "gcc"
- # define UTILITY_COMPILER_CXX_VERSION __GNUC__
- # if __GNUC__ < 4
- # error "Unsuported gcc version"
- # endif
- #elif defined(_MSC_VER)
- # define UTILITY_COMPILER_CXX_MSC
- # define UTILITY_COMPILER_CXX "MS VisualC"
- # define UTILITY_COMPILER_CXX_VERSION _MSC_VER
- #else
- # error "Unknown compiler"
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement