37 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ 38 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ 40 #include "gtest/internal/gtest-port.h" 44 # include <sys/types.h> 45 # include <sys/wait.h> 47 #endif // GTEST_OS_LINUX 49 #if GTEST_HAS_EXCEPTIONS 60 #include "gtest/gtest-message.h" 61 #include "gtest/internal/gtest-string.h" 62 #include "gtest/internal/gtest-filepath.h" 63 #include "gtest/internal/gtest-type-util.h" 73 #define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar) 74 #define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar 76 class ProtocolMessage;
77 namespace proto2 {
class Message; }
83 class AssertionResult;
91 ::std::string PrintToString(
const T& value);
101 GTEST_API_
extern int g_init_gtest_count;
105 GTEST_API_
extern const char kStackTraceMarker[];
121 char IsNullLiteralHelper(Secret* p);
122 char (&IsNullLiteralHelper(...))[2];
127 #ifdef GTEST_ELLIPSIS_NEEDS_POD_ 130 # define GTEST_IS_NULL_LITERAL_(x) false 132 # define GTEST_IS_NULL_LITERAL_(x) \ 133 (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1) 134 #endif // GTEST_ELLIPSIS_NEEDS_POD_ 137 GTEST_API_ std::string AppendUserMessage(
138 const std::string& gtest_msg,
const Message& user_msg);
140 #if GTEST_HAS_EXCEPTIONS 148 class GTEST_API_ GoogleTestFailureException :
public ::std::runtime_error {
150 explicit GoogleTestFailureException(
const TestPartResult& failure);
153 #endif // GTEST_HAS_EXCEPTIONS 156 class GTEST_API_ ScopedTrace {
160 ScopedTrace(
const char* file,
int line,
const Message& message);
169 GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace);
170 } GTEST_ATTRIBUTE_UNUSED_;
189 GTEST_API_ AssertionResult EqFailure(
const char* expected_expression,
190 const char* actual_expression,
191 const std::string& expected_value,
192 const std::string& actual_value,
196 GTEST_API_ std::string GetBoolAssertionFailureMessage(
197 const AssertionResult& assertion_result,
198 const char* expression_text,
199 const char* actual_predicate_value,
200 const char* expected_predicate_value);
231 template <
typename RawType>
232 class FloatingPoint {
236 typedef typename TypeWithSize<sizeof(RawType)>::UInt Bits;
241 static const size_t kBitCount = 8*
sizeof(RawType);
244 static const size_t kFractionBitCount =
245 std::numeric_limits<RawType>::digits - 1;
248 static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount;
251 static const Bits kSignBitMask =
static_cast<Bits
>(1) << (kBitCount - 1);
254 static const Bits kFractionBitMask =
255 ~static_cast<Bits>(0) >> (kExponentBitCount + 1);
258 static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask);
272 static const size_t kMaxUlps = 4;
280 explicit FloatingPoint(
const RawType& x) { u_.value_ = x; }
287 static RawType ReinterpretBits(
const Bits bits) {
295 return ReinterpretBits(kExponentBitMask);
299 static RawType Max();
304 const Bits &bits()
const {
return u_.bits_; }
307 Bits exponent_bits()
const {
return kExponentBitMask & u_.bits_; }
310 Bits fraction_bits()
const {
return kFractionBitMask & u_.bits_; }
313 Bits sign_bit()
const {
return kSignBitMask & u_.bits_; }
316 bool is_nan()
const {
319 return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0);
328 bool AlmostEquals(
const FloatingPoint& rhs)
const {
331 if (is_nan() || rhs.is_nan())
return false;
333 return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_)
339 union FloatingPointUnion {
359 static Bits SignAndMagnitudeToBiased(
const Bits &sam) {
360 if (kSignBitMask & sam) {
365 return kSignBitMask | sam;
371 static Bits DistanceBetweenSignAndMagnitudeNumbers(
const Bits &sam1,
373 const Bits biased1 = SignAndMagnitudeToBiased(sam1);
374 const Bits biased2 = SignAndMagnitudeToBiased(sam2);
375 return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
378 FloatingPointUnion u_;
384 inline float FloatingPoint<float>::Max() {
return FLT_MAX; }
386 inline double FloatingPoint<double>::Max() {
return DBL_MAX; }
390 typedef FloatingPoint<float> Float;
391 typedef FloatingPoint<double> Double;
399 typedef const void* TypeId;
401 template <
typename T>
410 template <
typename T>
411 bool TypeIdHelper<T>::dummy_ =
false;
416 template <
typename T>
422 return &(TypeIdHelper<T>::dummy_);
430 GTEST_API_ TypeId GetTestTypeId();
434 class TestFactoryBase {
436 virtual ~TestFactoryBase() {}
440 virtual Test* CreateTest() = 0;
446 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase);
451 template <
class TestClass>
452 class TestFactoryImpl :
public TestFactoryBase {
454 virtual Test* CreateTest() {
return new TestClass; }
463 GTEST_API_ AssertionResult IsHRESULTSuccess(
const char* expr,
465 GTEST_API_ AssertionResult IsHRESULTFailure(
const char* expr,
468 #endif // GTEST_OS_WINDOWS 471 typedef void (*SetUpTestCaseFunc)();
472 typedef void (*TearDownTestCaseFunc)();
491 GTEST_API_ TestInfo* MakeAndRegisterTestInfo(
492 const char* test_case_name,
494 const char* type_param,
495 const char* value_param,
496 TypeId fixture_class_id,
497 SetUpTestCaseFunc set_up_tc,
498 TearDownTestCaseFunc tear_down_tc,
499 TestFactoryBase* factory);
504 GTEST_API_
bool SkipPrefix(
const char* prefix,
const char** pstr);
506 #if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P 509 class GTEST_API_ TypedTestCasePState {
511 TypedTestCasePState() : registered_(false) {}
516 bool AddTestName(
const char* file,
int line,
const char* case_name,
517 const char* test_name) {
519 fprintf(stderr,
"%s Test %s must be defined before " 520 "REGISTER_TYPED_TEST_CASE_P(%s, ...).\n",
521 FormatFileLocation(file, line).c_str(), test_name, case_name);
525 defined_test_names_.insert(test_name);
532 const char* VerifyRegisteredTestNames(
533 const char* file,
int line,
const char* registered_tests);
537 ::std::set<const char*> defined_test_names_;
542 inline const char* SkipComma(
const char* str) {
543 const char* comma = strchr(str,
',');
547 while (IsSpace(*(++comma))) {}
553 inline std::string GetPrefixUntilComma(
const char* str) {
554 const char* comma = strchr(str,
',');
555 return comma == NULL ? str : std::string(str, comma);
565 template <GTEST_TEMPLATE_ Fixture,
class TestSel,
typename Types>
566 class TypeParameterizedTest {
572 static bool Register(
const char* prefix,
const char* case_name,
573 const char* test_names,
int index) {
574 typedef typename Types::Head Type;
575 typedef Fixture<Type> FixtureClass;
576 typedef typename GTEST_BIND_(TestSel, Type) TestClass;
580 MakeAndRegisterTestInfo(
581 (
std::
string(prefix) + (prefix[0] == '\0' ? "" : "/") + case_name + "/"
582 + StreamableToString(index)).c_str(),
583 GetPrefixUntilComma(test_names).c_str(),
584 GetTypeName<Type>().c_str(),
586 GetTypeId<FixtureClass>(),
587 TestClass::SetUpTestCase,
588 TestClass::TearDownTestCase,
589 new TestFactoryImpl<TestClass>);
592 return TypeParameterizedTest<Fixture, TestSel, typename Types::Tail>
593 ::Register(prefix, case_name, test_names, index + 1);
598 template <GTEST_TEMPLATE_ Fixture, class TestSel>
599 class TypeParameterizedTest<Fixture, TestSel, Types0> {
601 static bool Register(
const char* ,
const char* ,
602 const char* ,
int ) {
611 template <GTEST_TEMPLATE_ Fixture,
typename Tests,
typename Types>
612 class TypeParameterizedTestCase {
614 static bool Register(
const char* prefix,
const char* case_name,
615 const char* test_names) {
616 typedef typename Tests::Head Head;
619 TypeParameterizedTest<Fixture, Head, Types>::Register(
620 prefix, case_name, test_names, 0);
623 return TypeParameterizedTestCase<Fixture, typename Tests::Tail, Types>
624 ::Register(prefix, case_name, SkipComma(test_names));
629 template <GTEST_TEMPLATE_ Fixture,
typename Types>
630 class TypeParameterizedTestCase<Fixture, Templates0, Types> {
632 static bool Register(
const char* ,
const char* ,
638 #endif // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P 650 GTEST_API_ std::string GetCurrentOsStackTraceExceptTop(
651 UnitTest* unit_test,
int skip_count);
657 GTEST_API_
bool AlwaysTrue();
660 inline bool AlwaysFalse() {
return !AlwaysTrue(); }
665 struct GTEST_API_ ConstCharPtr {
666 ConstCharPtr(
const char* str) : value(str) {}
667 operator bool()
const {
return true; }
676 class GTEST_API_ Random {
678 static const UInt32 kMaxRange = 1u << 31;
680 explicit Random(UInt32 seed) : state_(seed) {}
682 void Reseed(UInt32 seed) { state_ = seed; }
686 UInt32 Generate(UInt32 range);
690 GTEST_DISALLOW_COPY_AND_ASSIGN_(Random);
695 template <
typename T1,
typename T2>
696 struct CompileAssertTypesEqual;
698 template <
typename T>
699 struct CompileAssertTypesEqual<
T,
T> {
705 template <
typename T>
706 struct RemoveReference {
typedef T type; };
707 template <
typename T>
708 struct RemoveReference<T&> {
typedef T type; };
712 #define GTEST_REMOVE_REFERENCE_(T) \ 713 typename ::testing::internal::RemoveReference<T>::type 718 template <
typename T>
719 struct RemoveConst {
typedef T type; };
720 template <
typename T>
721 struct RemoveConst<const T> {
typedef T type; };
726 template <
typename T,
size_t N>
727 struct RemoveConst<const T[N]> {
728 typedef typename RemoveConst<T>::type type[N];
731 #if defined(_MSC_VER) && _MSC_VER < 1400 735 template <
typename T,
size_t N>
736 struct RemoveConst<T[N]> {
737 typedef typename RemoveConst<T>::type type[N];
743 #define GTEST_REMOVE_CONST_(T) \ 744 typename ::testing::internal::RemoveConst<T>::type 747 #define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \ 748 GTEST_REMOVE_CONST_(GTEST_REMOVE_REFERENCE_(T)) 753 template <
typename T>
754 struct AddReference {
typedef T& type; };
755 template <
typename T>
756 struct AddReference<T&> {
typedef T& type; };
760 #define GTEST_ADD_REFERENCE_(T) \ 761 typename ::testing::internal::AddReference<T>::type 772 #define GTEST_REFERENCE_TO_CONST_(T) \ 773 GTEST_ADD_REFERENCE_(const GTEST_REMOVE_REFERENCE_(T)) 778 template <
typename From,
typename To>
779 class ImplicitlyConvertible {
787 static From MakeFrom();
799 static char Helper(To);
800 static char (&Helper(...))[2];
809 # pragma warning(push) // Saves the current warning state. 810 # pragma warning(disable:4244) // Temporarily disables warning 4244. 812 static const bool value =
813 sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1;
814 # pragma warning(pop) // Restores the warning state. 815 #elif defined(__BORLANDC__) 819 static const bool value = __is_convertible(From, To);
821 static const bool value =
822 sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1;
825 template <
typename From,
typename To>
826 const bool ImplicitlyConvertible<From, To>::value;
831 template <
typename T>
832 struct IsAProtocolMessage
833 :
public bool_constant<
834 ImplicitlyConvertible<const T*, const ::ProtocolMessage*>::value ||
835 ImplicitlyConvertible<const T*, const ::proto2::Message*>::value> {
859 typedef int IsContainer;
861 IsContainer IsContainerTest(
int ,
862 typename C::iterator* = NULL,
863 typename C::const_iterator* = NULL) {
867 typedef char IsNotContainer;
869 IsNotContainer IsContainerTest(
long ) {
return '\0'; }
875 template<
bool>
struct EnableIf;
876 template<>
struct EnableIf<true> {
typedef void type; };
884 template <
typename T,
typename U>
885 bool ArrayEq(
const T* lhs,
size_t size,
const U* rhs);
888 template <
typename T,
typename U>
889 inline bool ArrayEq(
const T& lhs,
const U& rhs) {
return lhs == rhs; }
892 template <
typename T,
typename U,
size_t N>
893 inline bool ArrayEq(
const T(&lhs)[N],
const U(&rhs)[N]) {
894 return internal::ArrayEq(lhs, N, rhs);
900 template <
typename T,
typename U>
901 bool ArrayEq(
const T* lhs,
size_t size,
const U* rhs) {
902 for (
size_t i = 0; i !=
size; i++) {
903 if (!internal::ArrayEq(lhs[i], rhs[i]))
911 template <
typename Iter,
typename Element>
912 Iter ArrayAwareFind(Iter begin, Iter end,
const Element& elem) {
913 for (Iter it = begin; it != end; ++it) {
914 if (internal::ArrayEq(*it, elem))
924 template <
typename T,
typename U>
925 void CopyArray(
const T* from,
size_t size, U* to);
928 template <
typename T,
typename U>
929 inline void CopyArray(
const T& from, U* to) { *to = from; }
932 template <
typename T,
typename U,
size_t N>
933 inline void CopyArray(
const T(&from)[N], U(*to)[N]) {
934 internal::CopyArray(from, N, *to);
940 template <
typename T,
typename U>
941 void CopyArray(
const T* from,
size_t size, U* to) {
942 for (
size_t i = 0; i !=
size; i++) {
943 internal::CopyArray(from[i], to + i);
949 enum RelationToSource {
963 template <
typename Element>
967 typedef Element value_type;
968 typedef Element* iterator;
969 typedef const Element* const_iterator;
972 NativeArray(
const Element* array,
size_t count, RelationToSource relation) {
973 Init(array, count, relation);
977 NativeArray(
const NativeArray& rhs) {
978 Init(rhs.array_, rhs.size_, rhs.relation_to_source_);
984 static_cast<void>(StaticAssertTypeEqHelper<Element,
985 GTEST_REMOVE_REFERENCE_AND_CONST_(Element)>());
986 if (relation_to_source_ == kCopy)
991 size_t size()
const {
return size_; }
992 const_iterator begin()
const {
return array_; }
993 const_iterator end()
const {
return array_ + size_; }
994 bool operator==(
const NativeArray& rhs)
const {
995 return size() == rhs.size() &&
996 ArrayEq(begin(),
size(), rhs.begin());
1002 void Init(
const Element* array,
size_t a_size, RelationToSource relation) {
1003 if (relation == kReference) {
1006 Element*
const copy =
new Element[a_size];
1007 CopyArray(array, a_size, copy);
1011 relation_to_source_ = relation;
1014 const Element* array_;
1016 RelationToSource relation_to_source_;
1018 GTEST_DISALLOW_ASSIGN_(NativeArray);
1024 #define GTEST_MESSAGE_AT_(file, line, message, result_type) \ 1025 ::testing::internal::AssertHelper(result_type, file, line, message) \ 1026 = ::testing::Message() 1028 #define GTEST_MESSAGE_(message, result_type) \ 1029 GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type) 1031 #define GTEST_FATAL_FAILURE_(message) \ 1032 return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure) 1034 #define GTEST_NONFATAL_FAILURE_(message) \ 1035 GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure) 1037 #define GTEST_SUCCESS_(message) \ 1038 GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess) 1043 #define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \ 1044 if (::testing::internal::AlwaysTrue()) { statement; } 1046 #define GTEST_TEST_THROW_(statement, expected_exception, fail) \ 1047 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1048 if (::testing::internal::ConstCharPtr gtest_msg = "") { \ 1049 bool gtest_caught_expected = false; \ 1051 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 1053 catch (expected_exception const&) { \ 1054 gtest_caught_expected = true; \ 1058 "Expected: " #statement " throws an exception of type " \ 1059 #expected_exception ".\n Actual: it throws a different type."; \ 1060 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ 1062 if (!gtest_caught_expected) { \ 1064 "Expected: " #statement " throws an exception of type " \ 1065 #expected_exception ".\n Actual: it throws nothing."; \ 1066 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ 1069 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \ 1070 fail(gtest_msg.value) 1072 #define GTEST_TEST_NO_THROW_(statement, fail) \ 1073 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1074 if (::testing::internal::AlwaysTrue()) { \ 1076 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 1079 goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ 1082 GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \ 1083 fail("Expected: " #statement " doesn't throw an exception.\n" \ 1084 " Actual: it throws.") 1086 #define GTEST_TEST_ANY_THROW_(statement, fail) \ 1087 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1088 if (::testing::internal::AlwaysTrue()) { \ 1089 bool gtest_caught_any = false; \ 1091 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 1094 gtest_caught_any = true; \ 1096 if (!gtest_caught_any) { \ 1097 goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \ 1100 GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__): \ 1101 fail("Expected: " #statement " throws an exception.\n" \ 1102 " Actual: it doesn't.") 1108 #define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \ 1109 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1110 if (const ::testing::AssertionResult gtest_ar_ = \ 1111 ::testing::AssertionResult(expression)) \ 1114 fail(::testing::internal::GetBoolAssertionFailureMessage(\ 1115 gtest_ar_, text, #actual, #expected).c_str()) 1117 #define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \ 1118 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1119 if (::testing::internal::AlwaysTrue()) { \ 1120 ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \ 1121 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 1122 if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \ 1123 goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \ 1126 GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__): \ 1127 fail("Expected: " #statement " doesn't generate new fatal " \ 1128 "failures in the current thread.\n" \ 1129 " Actual: it does.") 1132 #define GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \ 1133 test_case_name##_##test_name##_Test 1136 #define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\ 1137 class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\ 1139 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\ 1141 virtual void TestBody();\ 1142 static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;\ 1143 GTEST_DISALLOW_COPY_AND_ASSIGN_(\ 1144 GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\ 1147 ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\ 1149 ::testing::internal::MakeAndRegisterTestInfo(\ 1150 #test_case_name, #test_name, NULL, NULL, \ 1152 parent_class::SetUpTestCase, \ 1153 parent_class::TearDownTestCase, \ 1154 new ::testing::internal::TestFactoryImpl<\ 1155 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\ 1156 void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() 1158 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ Definition: gtest-all.cc:113
Definition: TypeSafeIdHash.h:44
detail::size< coerce_list< Ts... >> size
Get the size of a list (number of elements.)
Definition: Size.h:56
A small structure to hold a non zero as a triplet (i,j,value).
Definition: SparseUtil.h:148
Definition: BandTriangularSolver.h:13
const int Infinity
This value means +Infinity; it is currently used only as the p parameter to MatrixBase::lpNorm<int>()...
Definition: Constants.h:31