1 // Copyright 2005, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // Authors: wan (at) google.com (Zhanyong Wan), eefacm (at) gmail.com (Sean Mcafee) 31 // 32 // The Google C++ Testing Framework (Google Test) 33 // 34 // This header file declares functions and macros used internally by 35 // Google Test. They are subject to change without notice. 36 37 #ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ 38 #define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ 39 40 #include "gtest/internal/gtest-port.h" 41 42 #if GTEST_OS_LINUX 43 # include <stdlib.h> 44 # include <sys/types.h> 45 # include <sys/wait.h> 46 # include <unistd.h> 47 #endif // GTEST_OS_LINUX 48 49 #include <ctype.h> 50 #include <string.h> 51 #include <iomanip> 52 #include <limits> 53 #include <set> 54 55 #include "gtest/internal/gtest-string.h" 56 #include "gtest/internal/gtest-filepath.h" 57 #include "gtest/internal/gtest-type-util.h" 58 59 // Due to C++ preprocessor weirdness, we need double indirection to 60 // concatenate two tokens when one of them is __LINE__. Writing 61 // 62 // foo ## __LINE__ 63 // 64 // will result in the token foo__LINE__, instead of foo followed by 65 // the current line number. For more details, see 66 // http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6 67 #define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar) 68 #define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar 69 70 // Google Test defines the testing::Message class to allow construction of 71 // test messages via the << operator. The idea is that anything 72 // streamable to std::ostream can be streamed to a testing::Message. 73 // This allows a user to use his own types in Google Test assertions by 74 // overloading the << operator. 75 // 76 // util/gtl/stl_logging-inl.h overloads << for STL containers. These 77 // overloads cannot be defined in the std namespace, as that will be 78 // undefined behavior. Therefore, they are defined in the global 79 // namespace instead. 80 // 81 // C++'s symbol lookup rule (i.e. Koenig lookup) says that these 82 // overloads are visible in either the std namespace or the global 83 // namespace, but not other namespaces, including the testing 84 // namespace which Google Test's Message class is in. 85 // 86 // To allow STL containers (and other types that has a << operator 87 // defined in the global namespace) to be used in Google Test assertions, 88 // testing::Message must access the custom << operator from the global 89 // namespace. Hence this helper function. 90 // 91 // Note: Jeffrey Yasskin suggested an alternative fix by "using 92 // ::operator<<;" in the definition of Message's operator<<. That fix 93 // doesn't require a helper function, but unfortunately doesn't 94 // compile with MSVC. 95 template <typename T> 96 inline void GTestStreamToHelper(std::ostream* os, const T& val) { 97 *os << val; 98 } 99 100 class ProtocolMessage; 101 namespace proto2 { class Message; } 102 103 namespace testing { 104 105 // Forward declarations. 106 107 class AssertionResult; // Result of an assertion. 108 class Message; // Represents a failure message. 109 class Test; // Represents a test. 110 class TestInfo; // Information about a test. 111 class TestPartResult; // Result of a test part. 112 class UnitTest; // A collection of test cases. 113 114 template <typename T> 115 ::std::string PrintToString(const T& value); 116 117 namespace internal { 118 119 struct TraceInfo; // Information about a trace point. 120 class ScopedTrace; // Implements scoped trace. 121 class TestInfoImpl; // Opaque implementation of TestInfo 122 class UnitTestImpl; // Opaque implementation of UnitTest 123 124 // How many times InitGoogleTest() has been called. 125 extern int g_init_gtest_count; 126 127 // The text used in failure messages to indicate the start of the 128 // stack trace. 129 GTEST_API_ extern const char kStackTraceMarker[]; 130 131 // A secret type that Google Test users don't know about. It has no 132 // definition on purpose. Therefore it's impossible to create a 133 // Secret object, which is what we want. 134 class Secret; 135 136 // Two overloaded helpers for checking at compile time whether an 137 // expression is a null pointer literal (i.e. NULL or any 0-valued 138 // compile-time integral constant). Their return values have 139 // different sizes, so we can use sizeof() to test which version is 140 // picked by the compiler. These helpers have no implementations, as 141 // we only need their signatures. 142 // 143 // Given IsNullLiteralHelper(x), the compiler will pick the first 144 // version if x can be implicitly converted to Secret*, and pick the 145 // second version otherwise. Since Secret is a secret and incomplete 146 // type, the only expression a user can write that has type Secret* is 147 // a null pointer literal. Therefore, we know that x is a null 148 // pointer literal if and only if the first version is picked by the 149 // compiler. 150 char IsNullLiteralHelper(Secret* p); 151 char (&IsNullLiteralHelper(...))[2]; // NOLINT 152 153 // A compile-time bool constant that is true if and only if x is a 154 // null pointer literal (i.e. NULL or any 0-valued compile-time 155 // integral constant). 156 #ifdef GTEST_ELLIPSIS_NEEDS_POD_ 157 // We lose support for NULL detection where the compiler doesn't like 158 // passing non-POD classes through ellipsis (...). 159 # define GTEST_IS_NULL_LITERAL_(x) false 160 #else 161 # define GTEST_IS_NULL_LITERAL_(x) \ 162 (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1) 163 #endif // GTEST_ELLIPSIS_NEEDS_POD_ 164 165 // Appends the user-supplied message to the Google-Test-generated message. 166 GTEST_API_ String AppendUserMessage(const String& gtest_msg, 167 const Message& user_msg); 168 169 // A helper class for creating scoped traces in user programs. 170 class GTEST_API_ ScopedTrace { 171 public: 172 // The c'tor pushes the given source file location and message onto 173 // a trace stack maintained by Google Test. 174 ScopedTrace(const char* file, int line, const Message& message); 175 176 // The d'tor pops the info pushed by the c'tor. 177 // 178 // Note that the d'tor is not virtual in order to be efficient. 179 // Don't inherit from ScopedTrace! 180 ~ScopedTrace(); 181 182 private: 183 GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace); 184 } GTEST_ATTRIBUTE_UNUSED_; // A ScopedTrace object does its job in its 185 // c'tor and d'tor. Therefore it doesn't 186 // need to be used otherwise. 187 188 // Converts a streamable value to a String. A NULL pointer is 189 // converted to "(null)". When the input value is a ::string, 190 // ::std::string, ::wstring, or ::std::wstring object, each NUL 191 // character in it is replaced with "\\0". 192 // Declared here but defined in gtest.h, so that it has access 193 // to the definition of the Message class, required by the ARM 194 // compiler. 195 template <typename T> 196 String StreamableToString(const T& streamable); 197 198 // When this operand is a const char* or char*, if the other operand 199 // is a ::std::string or ::string, we print this operand as a C string 200 // rather than a pointer (we do the same for wide strings); otherwise 201 // we print it as a pointer to be safe. 202 203 // This internal macro is used to avoid duplicated code. 204 // Making the first operand const reference works around a bug in the 205 // Symbian compiler which is unable to select the correct specialization of 206 // FormatForComparisonFailureMessage. 207 #define GTEST_FORMAT_IMPL_(operand2_type, operand1_printer)\ 208 inline String FormatForComparisonFailureMessage(\ 209 operand2_type::value_type* const& str, const operand2_type& /*operand2*/) {\ 210 return operand1_printer(str);\ 211 }\ 212 inline String FormatForComparisonFailureMessage(\ 213 const operand2_type::value_type* const& str, \ 214 const operand2_type& /*operand2*/) {\ 215 return operand1_printer(str);\ 216 } 217 218 GTEST_FORMAT_IMPL_(::std::string, String::ShowCStringQuoted) 219 #if GTEST_HAS_STD_WSTRING 220 GTEST_FORMAT_IMPL_(::std::wstring, String::ShowWideCStringQuoted) 221 #endif // GTEST_HAS_STD_WSTRING 222 223 #if GTEST_HAS_GLOBAL_STRING 224 GTEST_FORMAT_IMPL_(::string, String::ShowCStringQuoted) 225 #endif // GTEST_HAS_GLOBAL_STRING 226 #if GTEST_HAS_GLOBAL_WSTRING 227 GTEST_FORMAT_IMPL_(::wstring, String::ShowWideCStringQuoted) 228 #endif // GTEST_HAS_GLOBAL_WSTRING 229 230 #undef GTEST_FORMAT_IMPL_ 231 232 // The next four overloads handle the case where the operand being 233 // printed is a char/wchar_t pointer and the other operand is not a 234 // string/wstring object. In such cases, we just print the operand as 235 // a pointer to be safe. 236 // 237 // Making the first operand const reference works around a bug in the 238 // Symbian compiler which is unable to select the correct specialization of 239 // FormatForComparisonFailureMessage. 240 #define GTEST_FORMAT_CHAR_PTR_IMPL_(CharType) \ 241 template <typename T> \ 242 String FormatForComparisonFailureMessage(CharType* const& p, const T&) { \ 243 return PrintToString(static_cast<const void*>(p)); \ 244 } 245 246 GTEST_FORMAT_CHAR_PTR_IMPL_(char) 247 GTEST_FORMAT_CHAR_PTR_IMPL_(const char) 248 GTEST_FORMAT_CHAR_PTR_IMPL_(wchar_t) 249 GTEST_FORMAT_CHAR_PTR_IMPL_(const wchar_t) 250 251 #undef GTEST_FORMAT_CHAR_PTR_IMPL_ 252 253 // Constructs and returns the message for an equality assertion 254 // (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure. 255 // 256 // The first four parameters are the expressions used in the assertion 257 // and their values, as strings. For example, for ASSERT_EQ(foo, bar) 258 // where foo is 5 and bar is 6, we have: 259 // 260 // expected_expression: "foo" 261 // actual_expression: "bar" 262 // expected_value: "5" 263 // actual_value: "6" 264 // 265 // The ignoring_case parameter is true iff the assertion is a 266 // *_STRCASEEQ*. When it's true, the string " (ignoring case)" will 267 // be inserted into the message. 268 GTEST_API_ AssertionResult EqFailure(const char* expected_expression, 269 const char* actual_expression, 270 const String& expected_value, 271 const String& actual_value, 272 bool ignoring_case); 273 274 // Constructs a failure message for Boolean assertions such as EXPECT_TRUE. 275 GTEST_API_ String GetBoolAssertionFailureMessage( 276 const AssertionResult& assertion_result, 277 const char* expression_text, 278 const char* actual_predicate_value, 279 const char* expected_predicate_value); 280 281 // This template class represents an IEEE floating-point number 282 // (either single-precision or double-precision, depending on the 283 // template parameters). 284 // 285 // The purpose of this class is to do more sophisticated number 286 // comparison. (Due to round-off error, etc, it's very unlikely that 287 // two floating-points will be equal exactly. Hence a naive 288 // comparison by the == operation often doesn't work.) 289 // 290 // Format of IEEE floating-point: 291 // 292 // The most-significant bit being the leftmost, an IEEE 293 // floating-point looks like 294 // 295 // sign_bit exponent_bits fraction_bits 296 // 297 // Here, sign_bit is a single bit that designates the sign of the 298 // number. 299 // 300 // For float, there are 8 exponent bits and 23 fraction bits. 301 // 302 // For double, there are 11 exponent bits and 52 fraction bits. 303 // 304 // More details can be found at 305 // http://en.wikipedia.org/wiki/IEEE_floating-point_standard. 306 // 307 // Template parameter: 308 // 309 // RawType: the raw floating-point type (either float or double) 310 template <typename RawType> 311 class FloatingPoint { 312 public: 313 // Defines the unsigned integer type that has the same size as the 314 // floating point number. 315 typedef typename TypeWithSize<sizeof(RawType)>::UInt Bits; 316 317 // Constants. 318 319 // # of bits in a number. 320 static const size_t kBitCount = 8*sizeof(RawType); 321 322 // # of fraction bits in a number. 323 static const size_t kFractionBitCount = 324 std::numeric_limits<RawType>::digits - 1; 325 326 // # of exponent bits in a number. 327 static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount; 328 329 // The mask for the sign bit. 330 static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1); 331 332 // The mask for the fraction bits. 333 static const Bits kFractionBitMask = 334 ~static_cast<Bits>(0) >> (kExponentBitCount + 1); 335 336 // The mask for the exponent bits. 337 static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask); 338 339 // How many ULP's (Units in the Last Place) we want to tolerate when 340 // comparing two numbers. The larger the value, the more error we 341 // allow. A 0 value means that two numbers must be exactly the same 342 // to be considered equal. 343 // 344 // The maximum error of a single floating-point operation is 0.5 345 // units in the last place. On Intel CPU's, all floating-point 346 // calculations are done with 80-bit precision, while double has 64 347 // bits. Therefore, 4 should be enough for ordinary use. 348 // 349 // See the following article for more details on ULP: 350 // http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm. 351 static const size_t kMaxUlps = 4; 352 353 // Constructs a FloatingPoint from a raw floating-point number. 354 // 355 // On an Intel CPU, passing a non-normalized NAN (Not a Number) 356 // around may change its bits, although the new value is guaranteed 357 // to be also a NAN. Therefore, don't expect this constructor to 358 // preserve the bits in x when x is a NAN. 359 explicit FloatingPoint(const RawType& x) { u_.value_ = x; } 360 361 // Static methods 362 363 // Reinterprets a bit pattern as a floating-point number. 364 // 365 // This function is needed to test the AlmostEquals() method. 366 static RawType ReinterpretBits(const Bits bits) { 367 FloatingPoint fp(0); 368 fp.u_.bits_ = bits; 369 return fp.u_.value_; 370 } 371 372 // Returns the floating-point number that represent positive infinity. 373 static RawType Infinity() { 374 return ReinterpretBits(kExponentBitMask); 375 } 376 377 // Non-static methods 378 379 // Returns the bits that represents this number. 380 const Bits &bits() const { return u_.bits_; } 381 382 // Returns the exponent bits of this number. 383 Bits exponent_bits() const { return kExponentBitMask & u_.bits_; } 384 385 // Returns the fraction bits of this number. 386 Bits fraction_bits() const { return kFractionBitMask & u_.bits_; } 387 388 // Returns the sign bit of this number. 389 Bits sign_bit() const { return kSignBitMask & u_.bits_; } 390 391 // Returns true iff this is NAN (not a number). 392 bool is_nan() const { 393 // It's a NAN if the exponent bits are all ones and the fraction 394 // bits are not entirely zeros. 395 return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0); 396 } 397 398 // Returns true iff this number is at most kMaxUlps ULP's away from 399 // rhs. In particular, this function: 400 // 401 // - returns false if either number is (or both are) NAN. 402 // - treats really large numbers as almost equal to infinity. 403 // - thinks +0.0 and -0.0 are 0 DLP's apart. 404 bool AlmostEquals(const FloatingPoint& rhs) const { 405 // The IEEE standard says that any comparison operation involving 406 // a NAN must return false. 407 if (is_nan() || rhs.is_nan()) return false; 408 409 return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_) 410 <= kMaxUlps; 411 } 412 413 private: 414 // The data type used to store the actual floating-point number. 415 union FloatingPointUnion { 416 RawType value_; // The raw floating-point number. 417 Bits bits_; // The bits that represent the number. 418 }; 419 420 // Converts an integer from the sign-and-magnitude representation to 421 // the biased representation. More precisely, let N be 2 to the 422 // power of (kBitCount - 1), an integer x is represented by the 423 // unsigned number x + N. 424 // 425 // For instance, 426 // 427 // -N + 1 (the most negative number representable using 428 // sign-and-magnitude) is represented by 1; 429 // 0 is represented by N; and 430 // N - 1 (the biggest number representable using 431 // sign-and-magnitude) is represented by 2N - 1. 432 // 433 // Read http://en.wikipedia.org/wiki/Signed_number_representations 434 // for more details on signed number representations. 435 static Bits SignAndMagnitudeToBiased(const Bits &sam) { 436 if (kSignBitMask & sam) { 437 // sam represents a negative number. 438 return ~sam + 1; 439 } else { 440 // sam represents a positive number. 441 return kSignBitMask | sam; 442 } 443 } 444 445 // Given two numbers in the sign-and-magnitude representation, 446 // returns the distance between them as an unsigned number. 447 static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits &sam1, 448 const Bits &sam2) { 449 const Bits biased1 = SignAndMagnitudeToBiased(sam1); 450 const Bits biased2 = SignAndMagnitudeToBiased(sam2); 451 return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1); 452 } 453 454 FloatingPointUnion u_; 455 }; 456 457 // Typedefs the instances of the FloatingPoint template class that we 458 // care to use. 459 typedef FloatingPoint<float> Float; 460 typedef FloatingPoint<double> Double; 461 462 // In order to catch the mistake of putting tests that use different 463 // test fixture classes in the same test case, we need to assign 464 // unique IDs to fixture classes and compare them. The TypeId type is 465 // used to hold such IDs. The user should treat TypeId as an opaque 466 // type: the only operation allowed on TypeId values is to compare 467 // them for equality using the == operator. 468 typedef const void* TypeId; 469 470 template <typename T> 471 class TypeIdHelper { 472 public: 473 // dummy_ must not have a const type. Otherwise an overly eager 474 // compiler (e.g. MSVC 7.1 & 8.0) may try to merge 475 // TypeIdHelper<T>::dummy_ for different Ts as an "optimization". 476 static bool dummy_; 477 }; 478 479 template <typename T> 480 bool TypeIdHelper<T>::dummy_ = false; 481 482 // GetTypeId<T>() returns the ID of type T. Different values will be 483 // returned for different types. Calling the function twice with the 484 // same type argument is guaranteed to return the same ID. 485 template <typename T> 486 TypeId GetTypeId() { 487 // The compiler is required to allocate a different 488 // TypeIdHelper<T>::dummy_ variable for each T used to instantiate 489 // the template. Therefore, the address of dummy_ is guaranteed to 490 // be unique. 491 return &(TypeIdHelper<T>::dummy_); 492 } 493 494 // Returns the type ID of ::testing::Test. Always call this instead 495 // of GetTypeId< ::testing::Test>() to get the type ID of 496 // ::testing::Test, as the latter may give the wrong result due to a 497 // suspected linker bug when compiling Google Test as a Mac OS X 498 // framework. 499 GTEST_API_ TypeId GetTestTypeId(); 500 501 // Defines the abstract factory interface that creates instances 502 // of a Test object. 503 class TestFactoryBase { 504 public: 505 virtual ~TestFactoryBase() {} 506 507 // Creates a test instance to run. The instance is both created and destroyed 508 // within TestInfoImpl::Run() 509 virtual Test* CreateTest() = 0; 510 511 protected: 512 TestFactoryBase() {} 513 514 private: 515 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase); 516 }; 517 518 // This class provides implementation of TeastFactoryBase interface. 519 // It is used in TEST and TEST_F macros. 520 template <class TestClass> 521 class TestFactoryImpl : public TestFactoryBase { 522 public: 523 virtual Test* CreateTest() { return new TestClass; } 524 }; 525 526 #if GTEST_OS_WINDOWS 527 528 // Predicate-formatters for implementing the HRESULT checking macros 529 // {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED} 530 // We pass a long instead of HRESULT to avoid causing an 531 // include dependency for the HRESULT type. 532 GTEST_API_ AssertionResult IsHRESULTSuccess(const char* expr, 533 long hr); // NOLINT 534 GTEST_API_ AssertionResult IsHRESULTFailure(const char* expr, 535 long hr); // NOLINT 536 537 #endif // GTEST_OS_WINDOWS 538 539 // Types of SetUpTestCase() and TearDownTestCase() functions. 540 typedef void (*SetUpTestCaseFunc)(); 541 typedef void (*TearDownTestCaseFunc)(); 542 543 // Creates a new TestInfo object and registers it with Google Test; 544 // returns the created object. 545 // 546 // Arguments: 547 // 548 // test_case_name: name of the test case 549 // name: name of the test 550 // type_param the name of the test's type parameter, or NULL if 551 // this is not a typed or a type-parameterized test. 552 // value_param text representation of the test's value parameter, 553 // or NULL if this is not a type-parameterized test. 554 // fixture_class_id: ID of the test fixture class 555 // set_up_tc: pointer to the function that sets up the test case 556 // tear_down_tc: pointer to the function that tears down the test case 557 // factory: pointer to the factory that creates a test object. 558 // The newly created TestInfo instance will assume 559 // ownership of the factory object. 560 GTEST_API_ TestInfo* MakeAndRegisterTestInfo( 561 const char* test_case_name, const char* name, 562 const char* type_param, 563 const char* value_param, 564 TypeId fixture_class_id, 565 SetUpTestCaseFunc set_up_tc, 566 TearDownTestCaseFunc tear_down_tc, 567 TestFactoryBase* factory); 568 569 // If *pstr starts with the given prefix, modifies *pstr to be right 570 // past the prefix and returns true; otherwise leaves *pstr unchanged 571 // and returns false. None of pstr, *pstr, and prefix can be NULL. 572 GTEST_API_ bool SkipPrefix(const char* prefix, const char** pstr); 573 574 #if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P 575 576 // State of the definition of a type-parameterized test case. 577 class GTEST_API_ TypedTestCasePState { 578 public: 579 TypedTestCasePState() : registered_(false) {} 580 581 // Adds the given test name to defined_test_names_ and return true 582 // if the test case hasn't been registered; otherwise aborts the 583 // program. 584 bool AddTestName(const char* file, int line, const char* case_name, 585 const char* test_name) { 586 if (registered_) { 587 fprintf(stderr, "%s Test %s must be defined before " 588 "REGISTER_TYPED_TEST_CASE_P(%s, ...).\n", 589 FormatFileLocation(file, line).c_str(), test_name, case_name); 590 fflush(stderr); 591 posix::Abort(); 592 } 593 defined_test_names_.insert(test_name); 594 return true; 595 } 596 597 // Verifies that registered_tests match the test names in 598 // defined_test_names_; returns registered_tests if successful, or 599 // aborts the program otherwise. 600 const char* VerifyRegisteredTestNames( 601 const char* file, int line, const char* registered_tests); 602 603 private: 604 bool registered_; 605 ::std::set<const char*> defined_test_names_; 606 }; 607 608 // Skips to the first non-space char after the first comma in 'str'; 609 // returns NULL if no comma is found in 'str'. 610 inline const char* SkipComma(const char* str) { 611 const char* comma = strchr(str, ','); 612 if (comma == NULL) { 613 return NULL; 614 } 615 while (IsSpace(*(++comma))) {} 616 return comma; 617 } 618 619 // Returns the prefix of 'str' before the first comma in it; returns 620 // the entire string if it contains no comma. 621 inline String GetPrefixUntilComma(const char* str) { 622 const char* comma = strchr(str, ','); 623 return comma == NULL ? String(str) : String(str, comma - str); 624 } 625 626 // TypeParameterizedTest<Fixture, TestSel, Types>::Register() 627 // registers a list of type-parameterized tests with Google Test. The 628 // return value is insignificant - we just need to return something 629 // such that we can call this function in a namespace scope. 630 // 631 // Implementation note: The GTEST_TEMPLATE_ macro declares a template 632 // template parameter. It's defined in gtest-type-util.h. 633 template <GTEST_TEMPLATE_ Fixture, class TestSel, typename Types> 634 class TypeParameterizedTest { 635 public: 636 // 'index' is the index of the test in the type list 'Types' 637 // specified in INSTANTIATE_TYPED_TEST_CASE_P(Prefix, TestCase, 638 // Types). Valid values for 'index' are [0, N - 1] where N is the 639 // length of Types. 640 static bool Register(const char* prefix, const char* case_name, 641 const char* test_names, int index) { 642 typedef typename Types::Head Type; 643 typedef Fixture<Type> FixtureClass; 644 typedef typename GTEST_BIND_(TestSel, Type) TestClass; 645 646 // First, registers the first type-parameterized test in the type 647 // list. 648 MakeAndRegisterTestInfo( 649 String::Format("%s%s%s/%d", prefix, prefix[0] == '\0' ? "" : "/", 650 case_name, index).c_str(), 651 GetPrefixUntilComma(test_names).c_str(), 652 GetTypeName<Type>().c_str(), 653 NULL, // No value parameter. 654 GetTypeId<FixtureClass>(), 655 TestClass::SetUpTestCase, 656 TestClass::TearDownTestCase, 657 new TestFactoryImpl<TestClass>); 658 659 // Next, recurses (at compile time) with the tail of the type list. 660 return TypeParameterizedTest<Fixture, TestSel, typename Types::Tail> 661 ::Register(prefix, case_name, test_names, index + 1); 662 } 663 }; 664 665 // The base case for the compile time recursion. 666 template <GTEST_TEMPLATE_ Fixture, class TestSel> 667 class TypeParameterizedTest<Fixture, TestSel, Types0> { 668 public: 669 static bool Register(const char* /*prefix*/, const char* /*case_name*/, 670 const char* /*test_names*/, int /*index*/) { 671 return true; 672 } 673 }; 674 675 // TypeParameterizedTestCase<Fixture, Tests, Types>::Register() 676 // registers *all combinations* of 'Tests' and 'Types' with Google 677 // Test. The return value is insignificant - we just need to return 678 // something such that we can call this function in a namespace scope. 679 template <GTEST_TEMPLATE_ Fixture, typename Tests, typename Types> 680 class TypeParameterizedTestCase { 681 public: 682 static bool Register(const char* prefix, const char* case_name, 683 const char* test_names) { 684 typedef typename Tests::Head Head; 685 686 // First, register the first test in 'Test' for each type in 'Types'. 687 TypeParameterizedTest<Fixture, Head, Types>::Register( 688 prefix, case_name, test_names, 0); 689 690 // Next, recurses (at compile time) with the tail of the test list. 691 return TypeParameterizedTestCase<Fixture, typename Tests::Tail, Types> 692 ::Register(prefix, case_name, SkipComma(test_names)); 693 } 694 }; 695 696 // The base case for the compile time recursion. 697 template <GTEST_TEMPLATE_ Fixture, typename Types> 698 class TypeParameterizedTestCase<Fixture, Templates0, Types> { 699 public: 700 static bool Register(const char* /*prefix*/, const char* /*case_name*/, 701 const char* /*test_names*/) { 702 return true; 703 } 704 }; 705 706 #endif // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P 707 708 // Returns the current OS stack trace as a String. 709 // 710 // The maximum number of stack frames to be included is specified by 711 // the gtest_stack_trace_depth flag. The skip_count parameter 712 // specifies the number of top frames to be skipped, which doesn't 713 // count against the number of frames to be included. 714 // 715 // For example, if Foo() calls Bar(), which in turn calls 716 // GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in 717 // the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't. 718 GTEST_API_ String GetCurrentOsStackTraceExceptTop(UnitTest* unit_test, 719 int skip_count); 720 721 // Helpers for suppressing warnings on unreachable code or constant 722 // condition. 723 724 // Always returns true. 725 GTEST_API_ bool AlwaysTrue(); 726 727 // Always returns false. 728 inline bool AlwaysFalse() { return !AlwaysTrue(); } 729 730 // Helper for suppressing false warning from Clang on a const char* 731 // variable declared in a conditional expression always being NULL in 732 // the else branch. 733 struct GTEST_API_ ConstCharPtr { 734 ConstCharPtr(const char* str) : value(str) {} 735 operator bool() const { return true; } 736 const char* value; 737 }; 738 739 // A simple Linear Congruential Generator for generating random 740 // numbers with a uniform distribution. Unlike rand() and srand(), it 741 // doesn't use global state (and therefore can't interfere with user 742 // code). Unlike rand_r(), it's portable. An LCG isn't very random, 743 // but it's good enough for our purposes. 744 class GTEST_API_ Random { 745 public: 746 static const UInt32 kMaxRange = 1u << 31; 747 748 explicit Random(UInt32 seed) : state_(seed) {} 749 750 void Reseed(UInt32 seed) { state_ = seed; } 751 752 // Generates a random number from [0, range). Crashes if 'range' is 753 // 0 or greater than kMaxRange. 754 UInt32 Generate(UInt32 range); 755 756 private: 757 UInt32 state_; 758 GTEST_DISALLOW_COPY_AND_ASSIGN_(Random); 759 }; 760 761 // Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a 762 // compiler error iff T1 and T2 are different types. 763 template <typename T1, typename T2> 764 struct CompileAssertTypesEqual; 765 766 template <typename T> 767 struct CompileAssertTypesEqual<T, T> { 768 }; 769 770 // Removes the reference from a type if it is a reference type, 771 // otherwise leaves it unchanged. This is the same as 772 // tr1::remove_reference, which is not widely available yet. 773 template <typename T> 774 struct RemoveReference { typedef T type; }; // NOLINT 775 template <typename T> 776 struct RemoveReference<T&> { typedef T type; }; // NOLINT 777 778 // A handy wrapper around RemoveReference that works when the argument 779 // T depends on template parameters. 780 #define GTEST_REMOVE_REFERENCE_(T) \ 781 typename ::testing::internal::RemoveReference<T>::type 782 783 // Removes const from a type if it is a const type, otherwise leaves 784 // it unchanged. This is the same as tr1::remove_const, which is not 785 // widely available yet. 786 template <typename T> 787 struct RemoveConst { typedef T type; }; // NOLINT 788 template <typename T> 789 struct RemoveConst<const T> { typedef T type; }; // NOLINT 790 791 // MSVC 8.0 and Sun C++ have a bug which causes the above definition 792 // to fail to remove the const in 'const int[3]'. The following 793 // specialization works around the bug. However, it causes trouble 794 // with GCC and thus needs to be conditionally compiled. 795 #if defined(_MSC_VER) || defined(__SUNPRO_CC) 796 template <typename T, size_t N> 797 struct RemoveConst<T[N]> { 798 typedef typename RemoveConst<T>::type type[N]; 799 }; 800 #endif 801 802 // A handy wrapper around RemoveConst that works when the argument 803 // T depends on template parameters. 804 #define GTEST_REMOVE_CONST_(T) \ 805 typename ::testing::internal::RemoveConst<T>::type 806 807 // Turns const U&, U&, const U, and U all into U. 808 #define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \ 809 GTEST_REMOVE_CONST_(GTEST_REMOVE_REFERENCE_(T)) 810 811 // Adds reference to a type if it is not a reference type, 812 // otherwise leaves it unchanged. This is the same as 813 // tr1::add_reference, which is not widely available yet. 814 template <typename T> 815 struct AddReference { typedef T& type; }; // NOLINT 816 template <typename T> 817 struct AddReference<T&> { typedef T& type; }; // NOLINT 818 819 // A handy wrapper around AddReference that works when the argument T 820 // depends on template parameters. 821 #define GTEST_ADD_REFERENCE_(T) \ 822 typename ::testing::internal::AddReference<T>::type 823 824 // Adds a reference to const on top of T as necessary. For example, 825 // it transforms 826 // 827 // char ==> const char& 828 // const char ==> const char& 829 // char& ==> const char& 830 // const char& ==> const char& 831 // 832 // The argument T must depend on some template parameters. 833 #define GTEST_REFERENCE_TO_CONST_(T) \ 834 GTEST_ADD_REFERENCE_(const GTEST_REMOVE_REFERENCE_(T)) 835 836 // ImplicitlyConvertible<From, To>::value is a compile-time bool 837 // constant that's true iff type From can be implicitly converted to 838 // type To. 839 template <typename From, typename To> 840 class ImplicitlyConvertible { 841 private: 842 // We need the following helper functions only for their types. 843 // They have no implementations. 844 845 // MakeFrom() is an expression whose type is From. We cannot simply 846 // use From(), as the type From may not have a public default 847 // constructor. 848 static From MakeFrom(); 849 850 // These two functions are overloaded. Given an expression 851 // Helper(x), the compiler will pick the first version if x can be 852 // implicitly converted to type To; otherwise it will pick the 853 // second version. 854 // 855 // The first version returns a value of size 1, and the second 856 // version returns a value of size 2. Therefore, by checking the 857 // size of Helper(x), which can be done at compile time, we can tell 858 // which version of Helper() is used, and hence whether x can be 859 // implicitly converted to type To. 860 static char Helper(To); 861 static char (&Helper(...))[2]; // NOLINT 862 863 // We have to put the 'public' section after the 'private' section, 864 // or MSVC refuses to compile the code. 865 public: 866 // MSVC warns about implicitly converting from double to int for 867 // possible loss of data, so we need to temporarily disable the 868 // warning. 869 #ifdef _MSC_VER 870 # pragma warning(push) // Saves the current warning state. 871 # pragma warning(disable:4244) // Temporarily disables warning 4244. 872 873 static const bool value = 874 sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 875 # pragma warning(pop) // Restores the warning state. 876 #elif defined(__BORLANDC__) 877 // C++Builder cannot use member overload resolution during template 878 // instantiation. The simplest workaround is to use its C++0x type traits 879 // functions (C++Builder 2009 and above only). 880 static const bool value = __is_convertible(From, To); 881 #else 882 static const bool value = 883 sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 884 #endif // _MSV_VER 885 }; 886 template <typename From, typename To> 887 const bool ImplicitlyConvertible<From, To>::value; 888 889 // IsAProtocolMessage<T>::value is a compile-time bool constant that's 890 // true iff T is type ProtocolMessage, proto2::Message, or a subclass 891 // of those. 892 template <typename T> 893 struct IsAProtocolMessage 894 : public bool_constant< 895 ImplicitlyConvertible<const T*, const ::ProtocolMessage*>::value || 896 ImplicitlyConvertible<const T*, const ::proto2::Message*>::value> { 897 }; 898 899 // When the compiler sees expression IsContainerTest<C>(0), if C is an 900 // STL-style container class, the first overload of IsContainerTest 901 // will be viable (since both C::iterator* and C::const_iterator* are 902 // valid types and NULL can be implicitly converted to them). It will 903 // be picked over the second overload as 'int' is a perfect match for 904 // the type of argument 0. If C::iterator or C::const_iterator is not 905 // a valid type, the first overload is not viable, and the second 906 // overload will be picked. Therefore, we can determine whether C is 907 // a container class by checking the type of IsContainerTest<C>(0). 908 // The value of the expression is insignificant. 909 // 910 // Note that we look for both C::iterator and C::const_iterator. The 911 // reason is that C++ injects the name of a class as a member of the 912 // class itself (e.g. you can refer to class iterator as either 913 // 'iterator' or 'iterator::iterator'). If we look for C::iterator 914 // only, for example, we would mistakenly think that a class named 915 // iterator is an STL container. 916 // 917 // Also note that the simpler approach of overloading 918 // IsContainerTest(typename C::const_iterator*) and 919 // IsContainerTest(...) doesn't work with Visual Age C++ and Sun C++. 920 typedef int IsContainer; 921 template <class C> 922 IsContainer IsContainerTest(int /* dummy */, 923 typename C::iterator* /* it */ = NULL, 924 typename C::const_iterator* /* const_it */ = NULL) { 925 return 0; 926 } 927 928 typedef char IsNotContainer; 929 template <class C> 930 IsNotContainer IsContainerTest(long /* dummy */) { return '\0'; } 931 932 // EnableIf<condition>::type is void when 'Cond' is true, and 933 // undefined when 'Cond' is false. To use SFINAE to make a function 934 // overload only apply when a particular expression is true, add 935 // "typename EnableIf<expression>::type* = 0" as the last parameter. 936 template<bool> struct EnableIf; 937 template<> struct EnableIf<true> { typedef void type; }; // NOLINT 938 939 // Utilities for native arrays. 940 941 // ArrayEq() compares two k-dimensional native arrays using the 942 // elements' operator==, where k can be any integer >= 0. When k is 943 // 0, ArrayEq() degenerates into comparing a single pair of values. 944 945 template <typename T, typename U> 946 bool ArrayEq(const T* lhs, size_t size, const U* rhs); 947 948 // This generic version is used when k is 0. 949 template <typename T, typename U> 950 inline bool ArrayEq(const T& lhs, const U& rhs) { return lhs == rhs; } 951 952 // This overload is used when k >= 1. 953 template <typename T, typename U, size_t N> 954 inline bool ArrayEq(const T(&lhs)[N], const U(&rhs)[N]) { 955 return internal::ArrayEq(lhs, N, rhs); 956 } 957 958 // This helper reduces code bloat. If we instead put its logic inside 959 // the previous ArrayEq() function, arrays with different sizes would 960 // lead to different copies of the template code. 961 template <typename T, typename U> 962 bool ArrayEq(const T* lhs, size_t size, const U* rhs) { 963 for (size_t i = 0; i != size; i++) { 964 if (!internal::ArrayEq(lhs[i], rhs[i])) 965 return false; 966 } 967 return true; 968 } 969 970 // Finds the first element in the iterator range [begin, end) that 971 // equals elem. Element may be a native array type itself. 972 template <typename Iter, typename Element> 973 Iter ArrayAwareFind(Iter begin, Iter end, const Element& elem) { 974 for (Iter it = begin; it != end; ++it) { 975 if (internal::ArrayEq(*it, elem)) 976 return it; 977 } 978 return end; 979 } 980 981 // CopyArray() copies a k-dimensional native array using the elements' 982 // operator=, where k can be any integer >= 0. When k is 0, 983 // CopyArray() degenerates into copying a single value. 984 985 template <typename T, typename U> 986 void CopyArray(const T* from, size_t size, U* to); 987 988 // This generic version is used when k is 0. 989 template <typename T, typename U> 990 inline void CopyArray(const T& from, U* to) { *to = from; } 991 992 // This overload is used when k >= 1. 993 template <typename T, typename U, size_t N> 994 inline void CopyArray(const T(&from)[N], U(*to)[N]) { 995 internal::CopyArray(from, N, *to); 996 } 997 998 // This helper reduces code bloat. If we instead put its logic inside 999 // the previous CopyArray() function, arrays with different sizes 1000 // would lead to different copies of the template code. 1001 template <typename T, typename U> 1002 void CopyArray(const T* from, size_t size, U* to) { 1003 for (size_t i = 0; i != size; i++) { 1004 internal::CopyArray(from[i], to + i); 1005 } 1006 } 1007 1008 // The relation between an NativeArray object (see below) and the 1009 // native array it represents. 1010 enum RelationToSource { 1011 kReference, // The NativeArray references the native array. 1012 kCopy // The NativeArray makes a copy of the native array and 1013 // owns the copy. 1014 }; 1015 1016 // Adapts a native array to a read-only STL-style container. Instead 1017 // of the complete STL container concept, this adaptor only implements 1018 // members useful for Google Mock's container matchers. New members 1019 // should be added as needed. To simplify the implementation, we only 1020 // support Element being a raw type (i.e. having no top-level const or 1021 // reference modifier). It's the client's responsibility to satisfy 1022 // this requirement. Element can be an array type itself (hence 1023 // multi-dimensional arrays are supported). 1024 template <typename Element> 1025 class NativeArray { 1026 public: 1027 // STL-style container typedefs. 1028 typedef Element value_type; 1029 typedef Element* iterator; 1030 typedef const Element* const_iterator; 1031 1032 // Constructs from a native array. 1033 NativeArray(const Element* array, size_t count, RelationToSource relation) { 1034 Init(array, count, relation); 1035 } 1036 1037 // Copy constructor. 1038 NativeArray(const NativeArray& rhs) { 1039 Init(rhs.array_, rhs.size_, rhs.relation_to_source_); 1040 } 1041 1042 ~NativeArray() { 1043 // Ensures that the user doesn't instantiate NativeArray with a 1044 // const or reference type. 1045 static_cast<void>(StaticAssertTypeEqHelper<Element, 1046 GTEST_REMOVE_REFERENCE_AND_CONST_(Element)>()); 1047 if (relation_to_source_ == kCopy) 1048 delete[] array_; 1049 } 1050 1051 // STL-style container methods. 1052 size_t size() const { return size_; } 1053 const_iterator begin() const { return array_; } 1054 const_iterator end() const { return array_ + size_; } 1055 bool operator==(const NativeArray& rhs) const { 1056 return size() == rhs.size() && 1057 ArrayEq(begin(), size(), rhs.begin()); 1058 } 1059 1060 private: 1061 // Initializes this object; makes a copy of the input array if 1062 // 'relation' is kCopy. 1063 void Init(const Element* array, size_t a_size, RelationToSource relation) { 1064 if (relation == kReference) { 1065 array_ = array; 1066 } else { 1067 Element* const copy = new Element[a_size]; 1068 CopyArray(array, a_size, copy); 1069 array_ = copy; 1070 } 1071 size_ = a_size; 1072 relation_to_source_ = relation; 1073 } 1074 1075 const Element* array_; 1076 size_t size_; 1077 RelationToSource relation_to_source_; 1078 1079 GTEST_DISALLOW_ASSIGN_(NativeArray); 1080 }; 1081 1082 } // namespace internal 1083 } // namespace testing 1084 1085 #define GTEST_MESSAGE_AT_(file, line, message, result_type) \ 1086 ::testing::internal::AssertHelper(result_type, file, line, message) \ 1087 = ::testing::Message() 1088 1089 #define GTEST_MESSAGE_(message, result_type) \ 1090 GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type) 1091 1092 #define GTEST_FATAL_FAILURE_(message) \ 1093 return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure) 1094 1095 #define GTEST_NONFATAL_FAILURE_(message) \ 1096 GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure) 1097 1098 #define GTEST_SUCCESS_(message) \ 1099 GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess) 1100 1101 // Suppresses MSVC warnings 4072 (unreachable code) for the code following 1102 // statement if it returns or throws (or doesn't return or throw in some 1103 // situations). 1104 #define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \ 1105 if (::testing::internal::AlwaysTrue()) { statement; } 1106 1107 #define GTEST_TEST_THROW_(statement, expected_exception, fail) \ 1108 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1109 if (::testing::internal::ConstCharPtr gtest_msg = "") { \ 1110 bool gtest_caught_expected = false; \ 1111 try { \ 1112 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 1113 } \ 1114 catch (expected_exception const&) { \ 1115 gtest_caught_expected = true; \ 1116 } \ 1117 catch (...) { \ 1118 gtest_msg.value = \ 1119 "Expected: " #statement " throws an exception of type " \ 1120 #expected_exception ".\n Actual: it throws a different type."; \ 1121 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ 1122 } \ 1123 if (!gtest_caught_expected) { \ 1124 gtest_msg.value = \ 1125 "Expected: " #statement " throws an exception of type " \ 1126 #expected_exception ".\n Actual: it throws nothing."; \ 1127 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ 1128 } \ 1129 } else \ 1130 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \ 1131 fail(gtest_msg.value) 1132 1133 #define GTEST_TEST_NO_THROW_(statement, fail) \ 1134 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1135 if (::testing::internal::AlwaysTrue()) { \ 1136 try { \ 1137 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 1138 } \ 1139 catch (...) { \ 1140 goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ 1141 } \ 1142 } else \ 1143 GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \ 1144 fail("Expected: " #statement " doesn't throw an exception.\n" \ 1145 " Actual: it throws.") 1146 1147 #define GTEST_TEST_ANY_THROW_(statement, fail) \ 1148 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1149 if (::testing::internal::AlwaysTrue()) { \ 1150 bool gtest_caught_any = false; \ 1151 try { \ 1152 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 1153 } \ 1154 catch (...) { \ 1155 gtest_caught_any = true; \ 1156 } \ 1157 if (!gtest_caught_any) { \ 1158 goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \ 1159 } \ 1160 } else \ 1161 GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__): \ 1162 fail("Expected: " #statement " throws an exception.\n" \ 1163 " Actual: it doesn't.") 1164 1165 1166 // Implements Boolean test assertions such as EXPECT_TRUE. expression can be 1167 // either a boolean expression or an AssertionResult. text is a textual 1168 // represenation of expression as it was passed into the EXPECT_TRUE. 1169 #define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \ 1170 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1171 if (const ::testing::AssertionResult gtest_ar_ = \ 1172 ::testing::AssertionResult(expression)) \ 1173 ; \ 1174 else \ 1175 fail(::testing::internal::GetBoolAssertionFailureMessage(\ 1176 gtest_ar_, text, #actual, #expected).c_str()) 1177 1178 #define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \ 1179 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1180 if (::testing::internal::AlwaysTrue()) { \ 1181 ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \ 1182 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 1183 if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \ 1184 goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \ 1185 } \ 1186 } else \ 1187 GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__): \ 1188 fail("Expected: " #statement " doesn't generate new fatal " \ 1189 "failures in the current thread.\n" \ 1190 " Actual: it does.") 1191 1192 // Expands to the name of the class that implements the given test. 1193 #define GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \ 1194 test_case_name##_##test_name##_Test 1195 1196 // Helper macro for defining tests. 1197 #define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\ 1198 class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\ 1199 public:\ 1200 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\ 1201 private:\ 1202 virtual void TestBody();\ 1203 static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;\ 1204 GTEST_DISALLOW_COPY_AND_ASSIGN_(\ 1205 GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\ 1206 };\ 1207 \ 1208 ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\ 1209 ::test_info_ =\ 1210 ::testing::internal::MakeAndRegisterTestInfo(\ 1211 #test_case_name, #test_name, NULL, NULL, \ 1212 (parent_id), \ 1213 parent_class::SetUpTestCase, \ 1214 parent_class::TearDownTestCase, \ 1215 new ::testing::internal::TestFactoryImpl<\ 1216 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\ 1217 void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() 1218 1219 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ 1220