1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
32 // The Google C++ Testing Framework (Google Test)
34 // This header file declares functions and macros used internally by
35 // Google Test. They are subject to change without notice.
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>
47 #endif // GTEST_OS_LINUX
55 #include <gtest/internal/gtest-string.h>
56 #include <gtest/internal/gtest-filepath.h>
57 #include <gtest/internal/gtest-type-util.h>
59 #include "llvm/Support/raw_os_ostream.h"
61 // Due to C++ preprocessor weirdness, we need double indirection to
62 // concatenate two tokens when one of them is __LINE__. Writing
66 // will result in the token foo__LINE__, instead of foo followed by
67 // the current line number. For more details, see
68 // http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6
69 #define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar)
70 #define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar
72 // Google Test defines the testing::Message class to allow construction of
73 // test messages via the << operator. The idea is that anything
74 // streamable to std::ostream can be streamed to a testing::Message.
75 // This allows a user to use his own types in Google Test assertions by
76 // overloading the << operator.
78 // util/gtl/stl_logging-inl.h overloads << for STL containers. These
79 // overloads cannot be defined in the std namespace, as that will be
80 // undefined behavior. Therefore, they are defined in the global
83 // C++'s symbol lookup rule (i.e. Koenig lookup) says that these
84 // overloads are visible in either the std namespace or the global
85 // namespace, but not other namespaces, including the testing
86 // namespace which Google Test's Message class is in.
88 // To allow STL containers (and other types that has a << operator
89 // defined in the global namespace) to be used in Google Test assertions,
90 // testing::Message must access the custom << operator from the global
91 // namespace. Hence this helper function.
93 // Note: Jeffrey Yasskin suggested an alternative fix by "using
94 // ::operator<<;" in the definition of Message's operator<<. That fix
95 // doesn't require a helper function, but unfortunately doesn't
98 // LLVM INTERNAL CHANGE: To allow operator<< to work with both
99 // std::ostreams and LLVM's raw_ostreams, we define a special
100 // std::ostream with an implicit conversion to raw_ostream& and stream
101 // to that. This causes the compiler to prefer std::ostream overloads
102 // but still find raw_ostream& overloads.
104 class convertible_fwd_ostream
: public std::ostream
{
109 convertible_fwd_ostream(std::ostream
& os
)
110 : std::ostream(os
.rdbuf()), os_(os
), ros_(*this) {}
111 operator raw_ostream
&() { return ros_
; }
114 template <typename T
>
115 inline void GTestStreamToHelper(std::ostream
* os
, const T
& val
) {
116 llvm::convertible_fwd_ostream
cos(*os
);
122 // Forward declaration of classes.
124 class Message
; // Represents a failure message.
125 class Test
; // Represents a test.
126 class TestCase
; // A collection of related tests.
127 class TestPartResult
; // Result of a test part.
128 class TestInfo
; // Information about a test.
129 class UnitTest
; // A collection of test cases.
130 class UnitTestEventListenerInterface
; // Listens to Google Test events.
131 class AssertionResult
; // Result of an assertion.
135 struct TraceInfo
; // Information about a trace point.
136 class ScopedTrace
; // Implements scoped trace.
137 class TestInfoImpl
; // Opaque implementation of TestInfo
138 class TestResult
; // Result of a single Test.
139 class UnitTestImpl
; // Opaque implementation of UnitTest
141 template <typename E
> class List
; // A generic list.
142 template <typename E
> class ListNode
; // A node in a generic list.
144 // How many times InitGoogleTest() has been called.
145 extern int g_init_gtest_count
;
147 // The text used in failure messages to indicate the start of the
149 extern const char kStackTraceMarker
[];
151 // A secret type that Google Test users don't know about. It has no
152 // definition on purpose. Therefore it's impossible to create a
153 // Secret object, which is what we want.
156 // Two overloaded helpers for checking at compile time whether an
157 // expression is a null pointer literal (i.e. NULL or any 0-valued
158 // compile-time integral constant). Their return values have
159 // different sizes, so we can use sizeof() to test which version is
160 // picked by the compiler. These helpers have no implementations, as
161 // we only need their signatures.
163 // Given IsNullLiteralHelper(x), the compiler will pick the first
164 // version if x can be implicitly converted to Secret*, and pick the
165 // second version otherwise. Since Secret is a secret and incomplete
166 // type, the only expression a user can write that has type Secret* is
167 // a null pointer literal. Therefore, we know that x is a null
168 // pointer literal if and only if the first version is picked by the
170 char IsNullLiteralHelper(Secret
* p
);
171 char (&IsNullLiteralHelper(...))[2]; // NOLINT
173 // A compile-time bool constant that is true if and only if x is a
174 // null pointer literal (i.e. NULL or any 0-valued compile-time
175 // integral constant).
176 #ifdef GTEST_ELLIPSIS_NEEDS_COPY_
177 // Passing non-POD classes through ellipsis (...) crashes the ARM
178 // compiler. The Nokia Symbian and the IBM XL C/C++ compiler try to
179 // instantiate a copy constructor for objects passed through ellipsis
180 // (...), failing for uncopyable objects. Hence we define this to
181 // false (and lose support for NULL detection).
182 #define GTEST_IS_NULL_LITERAL_(x) false
184 #define GTEST_IS_NULL_LITERAL_(x) \
185 (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1)
186 #endif // GTEST_ELLIPSIS_NEEDS_COPY_
188 // Appends the user-supplied message to the Google-Test-generated message.
189 String
AppendUserMessage(const String
& gtest_msg
,
190 const Message
& user_msg
);
192 // A helper class for creating scoped traces in user programs.
195 // The c'tor pushes the given source file location and message onto
196 // a trace stack maintained by Google Test.
197 ScopedTrace(const char* file
, int line
, const Message
& message
);
199 // The d'tor pops the info pushed by the c'tor.
201 // Note that the d'tor is not virtual in order to be efficient.
202 // Don't inherit from ScopedTrace!
206 GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace
);
207 } GTEST_ATTRIBUTE_UNUSED_
; // A ScopedTrace object does its job in its
208 // c'tor and d'tor. Therefore it doesn't
209 // need to be used otherwise.
211 // Converts a streamable value to a String. A NULL pointer is
212 // converted to "(null)". When the input value is a ::string,
213 // ::std::string, ::wstring, or ::std::wstring object, each NUL
214 // character in it is replaced with "\\0".
215 // Declared here but defined in gtest.h, so that it has access
216 // to the definition of the Message class, required by the ARM
218 template <typename T
>
219 String
StreamableToString(const T
& streamable
);
221 // Formats a value to be used in a failure message.
223 #ifdef GTEST_NEEDS_IS_POINTER_
225 // These are needed as the Nokia Symbian and IBM XL C/C++ compilers
226 // cannot decide between const T& and const T* in a function template.
227 // These compilers _can_ decide between class template specializations
228 // for T and T*, so a tr1::type_traits-like is_pointer works, and we
229 // can overload on that.
231 // This overload makes sure that all pointers (including
232 // those to char or wchar_t) are printed as raw pointers.
233 template <typename T
>
234 inline String
FormatValueForFailureMessage(internal::true_type dummy
,
236 return StreamableToString(static_cast<const void*>(pointer
));
239 template <typename T
>
240 inline String
FormatValueForFailureMessage(internal::false_type dummy
,
242 return StreamableToString(value
);
245 template <typename T
>
246 inline String
FormatForFailureMessage(const T
& value
) {
247 return FormatValueForFailureMessage(
248 typename
internal::is_pointer
<T
>::type(), value
);
253 // These are needed as the above solution using is_pointer has the
254 // limitation that T cannot be a type without external linkage, when
255 // compiled using MSVC.
257 template <typename T
>
258 inline String
FormatForFailureMessage(const T
& value
) {
259 return StreamableToString(value
);
262 // This overload makes sure that all pointers (including
263 // those to char or wchar_t) are printed as raw pointers.
264 template <typename T
>
265 inline String
FormatForFailureMessage(T
* pointer
) {
266 return StreamableToString(static_cast<const void*>(pointer
));
269 #endif // GTEST_NEEDS_IS_POINTER_
271 // These overloaded versions handle narrow and wide characters.
272 String
FormatForFailureMessage(char ch
);
273 String
FormatForFailureMessage(wchar_t wchar
);
275 // When this operand is a const char* or char*, and the other operand
276 // is a ::std::string or ::string, we print this operand as a C string
277 // rather than a pointer. We do the same for wide strings.
279 // This internal macro is used to avoid duplicated code.
280 #define GTEST_FORMAT_IMPL_(operand2_type, operand1_printer)\
281 inline String FormatForComparisonFailureMessage(\
282 operand2_type::value_type* str, const operand2_type& /*operand2*/) {\
283 return operand1_printer(str);\
285 inline String FormatForComparisonFailureMessage(\
286 const operand2_type::value_type* str, const operand2_type& /*operand2*/) {\
287 return operand1_printer(str);\
290 #if GTEST_HAS_STD_STRING
291 GTEST_FORMAT_IMPL_(::std::string
, String::ShowCStringQuoted
)
292 #endif // GTEST_HAS_STD_STRING
293 #if GTEST_HAS_STD_WSTRING
294 GTEST_FORMAT_IMPL_(::std::wstring
, String::ShowWideCStringQuoted
)
295 #endif // GTEST_HAS_STD_WSTRING
297 #if GTEST_HAS_GLOBAL_STRING
298 GTEST_FORMAT_IMPL_(::string
, String::ShowCStringQuoted
)
299 #endif // GTEST_HAS_GLOBAL_STRING
300 #if GTEST_HAS_GLOBAL_WSTRING
301 GTEST_FORMAT_IMPL_(::wstring
, String::ShowWideCStringQuoted
)
302 #endif // GTEST_HAS_GLOBAL_WSTRING
304 #undef GTEST_FORMAT_IMPL_
306 // Constructs and returns the message for an equality assertion
307 // (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure.
309 // The first four parameters are the expressions used in the assertion
310 // and their values, as strings. For example, for ASSERT_EQ(foo, bar)
311 // where foo is 5 and bar is 6, we have:
313 // expected_expression: "foo"
314 // actual_expression: "bar"
315 // expected_value: "5"
318 // The ignoring_case parameter is true iff the assertion is a
319 // *_STRCASEEQ*. When it's true, the string " (ignoring case)" will
320 // be inserted into the message.
321 AssertionResult
EqFailure(const char* expected_expression
,
322 const char* actual_expression
,
323 const String
& expected_value
,
324 const String
& actual_value
,
328 // This template class represents an IEEE floating-point number
329 // (either single-precision or double-precision, depending on the
330 // template parameters).
332 // The purpose of this class is to do more sophisticated number
333 // comparison. (Due to round-off error, etc, it's very unlikely that
334 // two floating-points will be equal exactly. Hence a naive
335 // comparison by the == operation often doesn't work.)
337 // Format of IEEE floating-point:
339 // The most-significant bit being the leftmost, an IEEE
340 // floating-point looks like
342 // sign_bit exponent_bits fraction_bits
344 // Here, sign_bit is a single bit that designates the sign of the
347 // For float, there are 8 exponent bits and 23 fraction bits.
349 // For double, there are 11 exponent bits and 52 fraction bits.
351 // More details can be found at
352 // http://en.wikipedia.org/wiki/IEEE_floating-point_standard.
354 // Template parameter:
356 // RawType: the raw floating-point type (either float or double)
357 template <typename RawType
>
358 class FloatingPoint
{
360 // Defines the unsigned integer type that has the same size as the
361 // floating point number.
362 typedef typename TypeWithSize
<sizeof(RawType
)>::UInt Bits
;
366 // # of bits in a number.
367 static const size_t kBitCount
= 8*sizeof(RawType
);
369 // # of fraction bits in a number.
370 static const size_t kFractionBitCount
=
371 std::numeric_limits
<RawType
>::digits
- 1;
373 // # of exponent bits in a number.
374 static const size_t kExponentBitCount
= kBitCount
- 1 - kFractionBitCount
;
376 // The mask for the sign bit.
377 static const Bits kSignBitMask
= static_cast<Bits
>(1) << (kBitCount
- 1);
379 // The mask for the fraction bits.
380 static const Bits kFractionBitMask
=
381 ~static_cast<Bits
>(0) >> (kExponentBitCount
+ 1);
383 // The mask for the exponent bits.
384 static const Bits kExponentBitMask
= ~(kSignBitMask
| kFractionBitMask
);
386 // How many ULP's (Units in the Last Place) we want to tolerate when
387 // comparing two numbers. The larger the value, the more error we
388 // allow. A 0 value means that two numbers must be exactly the same
389 // to be considered equal.
391 // The maximum error of a single floating-point operation is 0.5
392 // units in the last place. On Intel CPU's, all floating-point
393 // calculations are done with 80-bit precision, while double has 64
394 // bits. Therefore, 4 should be enough for ordinary use.
396 // See the following article for more details on ULP:
397 // http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm.
398 static const size_t kMaxUlps
= 4;
400 // Constructs a FloatingPoint from a raw floating-point number.
402 // On an Intel CPU, passing a non-normalized NAN (Not a Number)
403 // around may change its bits, although the new value is guaranteed
404 // to be also a NAN. Therefore, don't expect this constructor to
405 // preserve the bits in x when x is a NAN.
406 explicit FloatingPoint(const RawType
& x
) : value_(x
) {}
410 // Reinterprets a bit pattern as a floating-point number.
412 // This function is needed to test the AlmostEquals() method.
413 static RawType
ReinterpretBits(const Bits bits
) {
419 // Returns the floating-point number that represent positive infinity.
420 static RawType
Infinity() {
421 return ReinterpretBits(kExponentBitMask
);
424 // Non-static methods
426 // Returns the bits that represents this number.
427 const Bits
&bits() const { return bits_
; }
429 // Returns the exponent bits of this number.
430 Bits
exponent_bits() const { return kExponentBitMask
& bits_
; }
432 // Returns the fraction bits of this number.
433 Bits
fraction_bits() const { return kFractionBitMask
& bits_
; }
435 // Returns the sign bit of this number.
436 Bits
sign_bit() const { return kSignBitMask
& bits_
; }
438 // Returns true iff this is NAN (not a number).
439 bool is_nan() const {
440 // It's a NAN if the exponent bits are all ones and the fraction
441 // bits are not entirely zeros.
442 return (exponent_bits() == kExponentBitMask
) && (fraction_bits() != 0);
445 // Returns true iff this number is at most kMaxUlps ULP's away from
446 // rhs. In particular, this function:
448 // - returns false if either number is (or both are) NAN.
449 // - treats really large numbers as almost equal to infinity.
450 // - thinks +0.0 and -0.0 are 0 DLP's apart.
451 bool AlmostEquals(const FloatingPoint
& rhs
) const {
452 // The IEEE standard says that any comparison operation involving
453 // a NAN must return false.
454 if (is_nan() || rhs
.is_nan()) return false;
456 return DistanceBetweenSignAndMagnitudeNumbers(bits_
, rhs
.bits_
) <= kMaxUlps
;
460 // Converts an integer from the sign-and-magnitude representation to
461 // the biased representation. More precisely, let N be 2 to the
462 // power of (kBitCount - 1), an integer x is represented by the
463 // unsigned number x + N.
467 // -N + 1 (the most negative number representable using
468 // sign-and-magnitude) is represented by 1;
469 // 0 is represented by N; and
470 // N - 1 (the biggest number representable using
471 // sign-and-magnitude) is represented by 2N - 1.
473 // Read http://en.wikipedia.org/wiki/Signed_number_representations
474 // for more details on signed number representations.
475 static Bits
SignAndMagnitudeToBiased(const Bits
&sam
) {
476 if (kSignBitMask
& sam
) {
477 // sam represents a negative number.
480 // sam represents a positive number.
481 return kSignBitMask
| sam
;
485 // Given two numbers in the sign-and-magnitude representation,
486 // returns the distance between them as an unsigned number.
487 static Bits
DistanceBetweenSignAndMagnitudeNumbers(const Bits
&sam1
,
489 const Bits biased1
= SignAndMagnitudeToBiased(sam1
);
490 const Bits biased2
= SignAndMagnitudeToBiased(sam2
);
491 return (biased1
>= biased2
) ? (biased1
- biased2
) : (biased2
- biased1
);
495 RawType value_
; // The raw floating-point number.
496 Bits bits_
; // The bits that represent the number.
500 // Typedefs the instances of the FloatingPoint template class that we
502 typedef FloatingPoint
<float> Float
;
503 typedef FloatingPoint
<double> Double
;
505 // In order to catch the mistake of putting tests that use different
506 // test fixture classes in the same test case, we need to assign
507 // unique IDs to fixture classes and compare them. The TypeId type is
508 // used to hold such IDs. The user should treat TypeId as an opaque
509 // type: the only operation allowed on TypeId values is to compare
510 // them for equality using the == operator.
511 typedef const void* TypeId
;
513 template <typename T
>
516 // dummy_ must not have a const type. Otherwise an overly eager
517 // compiler (e.g. MSVC 7.1 & 8.0) may try to merge
518 // TypeIdHelper<T>::dummy_ for different Ts as an "optimization".
522 template <typename T
>
523 bool TypeIdHelper
<T
>::dummy_
= false;
525 // GetTypeId<T>() returns the ID of type T. Different values will be
526 // returned for different types. Calling the function twice with the
527 // same type argument is guaranteed to return the same ID.
528 template <typename T
>
530 // The compiler is required to allocate a different
531 // TypeIdHelper<T>::dummy_ variable for each T used to instantiate
532 // the template. Therefore, the address of dummy_ is guaranteed to
534 return &(TypeIdHelper
<T
>::dummy_
);
537 // Returns the type ID of ::testing::Test. Always call this instead
538 // of GetTypeId< ::testing::Test>() to get the type ID of
539 // ::testing::Test, as the latter may give the wrong result due to a
540 // suspected linker bug when compiling Google Test as a Mac OS X
542 TypeId
GetTestTypeId();
544 // Defines the abstract factory interface that creates instances
546 class TestFactoryBase
{
548 virtual ~TestFactoryBase() {}
550 // Creates a test instance to run. The instance is both created and destroyed
551 // within TestInfoImpl::Run()
552 virtual Test
* CreateTest() = 0;
558 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase
);
561 // This class provides implementation of TeastFactoryBase interface.
562 // It is used in TEST and TEST_F macros.
563 template <class TestClass
>
564 class TestFactoryImpl
: public TestFactoryBase
{
566 virtual Test
* CreateTest() { return new TestClass
; }
569 #ifdef GTEST_OS_WINDOWS
571 // Predicate-formatters for implementing the HRESULT checking macros
572 // {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}
573 // We pass a long instead of HRESULT to avoid causing an
574 // include dependency for the HRESULT type.
575 AssertionResult
IsHRESULTSuccess(const char* expr
, long hr
); // NOLINT
576 AssertionResult
IsHRESULTFailure(const char* expr
, long hr
); // NOLINT
578 #endif // GTEST_OS_WINDOWS
580 // Formats a source file path and a line number as they would appear
581 // in a compiler error message.
582 inline String
FormatFileLocation(const char* file
, int line
) {
583 const char* const file_name
= file
== NULL
? "unknown file" : file
;
585 return String::Format("%s:", file_name
);
588 return String::Format("%s(%d):", file_name
, line
);
590 return String::Format("%s:%d:", file_name
, line
);
594 // Types of SetUpTestCase() and TearDownTestCase() functions.
595 typedef void (*SetUpTestCaseFunc
)();
596 typedef void (*TearDownTestCaseFunc
)();
598 // Creates a new TestInfo object and registers it with Google Test;
599 // returns the created object.
603 // test_case_name: name of the test case
604 // name: name of the test
605 // test_case_comment: a comment on the test case that will be included in
607 // comment: a comment on the test that will be included in the
609 // fixture_class_id: ID of the test fixture class
610 // set_up_tc: pointer to the function that sets up the test case
611 // tear_down_tc: pointer to the function that tears down the test case
612 // factory: pointer to the factory that creates a test object.
613 // The newly created TestInfo instance will assume
614 // ownership of the factory object.
615 TestInfo
* MakeAndRegisterTestInfo(
616 const char* test_case_name
, const char* name
,
617 const char* test_case_comment
, const char* comment
,
618 TypeId fixture_class_id
,
619 SetUpTestCaseFunc set_up_tc
,
620 TearDownTestCaseFunc tear_down_tc
,
621 TestFactoryBase
* factory
);
623 #if defined(GTEST_HAS_TYPED_TEST) || defined(GTEST_HAS_TYPED_TEST_P)
625 // State of the definition of a type-parameterized test case.
626 class TypedTestCasePState
{
628 TypedTestCasePState() : registered_(false) {}
630 // Adds the given test name to defined_test_names_ and return true
631 // if the test case hasn't been registered; otherwise aborts the
633 bool AddTestName(const char* file
, int line
, const char* case_name
,
634 const char* test_name
) {
636 fprintf(stderr
, "%s Test %s must be defined before "
637 "REGISTER_TYPED_TEST_CASE_P(%s, ...).\n",
638 FormatFileLocation(file
, line
).c_str(), test_name
, case_name
);
641 defined_test_names_
.insert(test_name
);
645 // Verifies that registered_tests match the test names in
646 // defined_test_names_; returns registered_tests if successful, or
647 // aborts the program otherwise.
648 const char* VerifyRegisteredTestNames(
649 const char* file
, int line
, const char* registered_tests
);
653 ::std::set
<const char*> defined_test_names_
;
656 // Skips to the first non-space char after the first comma in 'str';
657 // returns NULL if no comma is found in 'str'.
658 inline const char* SkipComma(const char* str
) {
659 const char* comma
= strchr(str
, ',');
663 while (isspace(*(++comma
))) {}
667 // Returns the prefix of 'str' before the first comma in it; returns
668 // the entire string if it contains no comma.
669 inline String
GetPrefixUntilComma(const char* str
) {
670 const char* comma
= strchr(str
, ',');
671 return comma
== NULL
? String(str
) : String(str
, comma
- str
);
674 // TypeParameterizedTest<Fixture, TestSel, Types>::Register()
675 // registers a list of type-parameterized tests with Google Test. The
676 // return value is insignificant - we just need to return something
677 // such that we can call this function in a namespace scope.
679 // Implementation note: The GTEST_TEMPLATE_ macro declares a template
680 // template parameter. It's defined in gtest-type-util.h.
681 template <GTEST_TEMPLATE_ Fixture
, class TestSel
, typename Types
>
682 class TypeParameterizedTest
{
684 // 'index' is the index of the test in the type list 'Types'
685 // specified in INSTANTIATE_TYPED_TEST_CASE_P(Prefix, TestCase,
686 // Types). Valid values for 'index' are [0, N - 1] where N is the
688 static bool Register(const char* prefix
, const char* case_name
,
689 const char* test_names
, int index
) {
690 typedef typename
Types::Head Type
;
691 typedef Fixture
<Type
> FixtureClass
;
692 typedef typename
GTEST_BIND_(TestSel
, Type
) TestClass
;
694 // First, registers the first type-parameterized test in the type
696 MakeAndRegisterTestInfo(
697 String::Format("%s%s%s/%d", prefix
, prefix
[0] == '\0' ? "" : "/",
698 case_name
, index
).c_str(),
699 GetPrefixUntilComma(test_names
).c_str(),
700 String::Format("TypeParam = %s", GetTypeName
<Type
>().c_str()).c_str(),
702 GetTypeId
<FixtureClass
>(),
703 TestClass::SetUpTestCase
,
704 TestClass::TearDownTestCase
,
705 new TestFactoryImpl
<TestClass
>);
707 // Next, recurses (at compile time) with the tail of the type list.
708 return TypeParameterizedTest
<Fixture
, TestSel
, typename
Types::Tail
>
709 ::Register(prefix
, case_name
, test_names
, index
+ 1);
713 // The base case for the compile time recursion.
714 template <GTEST_TEMPLATE_ Fixture
, class TestSel
>
715 class TypeParameterizedTest
<Fixture
, TestSel
, Types0
> {
717 static bool Register(const char* /*prefix*/, const char* /*case_name*/,
718 const char* /*test_names*/, int /*index*/) {
723 // TypeParameterizedTestCase<Fixture, Tests, Types>::Register()
724 // registers *all combinations* of 'Tests' and 'Types' with Google
725 // Test. The return value is insignificant - we just need to return
726 // something such that we can call this function in a namespace scope.
727 template <GTEST_TEMPLATE_ Fixture
, typename Tests
, typename Types
>
728 class TypeParameterizedTestCase
{
730 static bool Register(const char* prefix
, const char* case_name
,
731 const char* test_names
) {
732 typedef typename
Tests::Head Head
;
734 // First, register the first test in 'Test' for each type in 'Types'.
735 TypeParameterizedTest
<Fixture
, Head
, Types
>::Register(
736 prefix
, case_name
, test_names
, 0);
738 // Next, recurses (at compile time) with the tail of the test list.
739 return TypeParameterizedTestCase
<Fixture
, typename
Tests::Tail
, Types
>
740 ::Register(prefix
, case_name
, SkipComma(test_names
));
744 // The base case for the compile time recursion.
745 template <GTEST_TEMPLATE_ Fixture
, typename Types
>
746 class TypeParameterizedTestCase
<Fixture
, Templates0
, Types
> {
748 static bool Register(const char* prefix
, const char* case_name
,
749 const char* test_names
) {
754 #endif // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
756 // Returns the current OS stack trace as a String.
758 // The maximum number of stack frames to be included is specified by
759 // the gtest_stack_trace_depth flag. The skip_count parameter
760 // specifies the number of top frames to be skipped, which doesn't
761 // count against the number of frames to be included.
763 // For example, if Foo() calls Bar(), which in turn calls
764 // GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in
765 // the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.
766 String
GetCurrentOsStackTraceExceptTop(UnitTest
* unit_test
, int skip_count
);
768 // Returns the number of failed test parts in the given test result object.
769 int GetFailedPartCount(const TestResult
* result
);
771 } // namespace internal
772 } // namespace testing
774 #define GTEST_MESSAGE_(message, result_type) \
775 ::testing::internal::AssertHelper(result_type, __FILE__, __LINE__, message) \
776 = ::testing::Message()
778 #define GTEST_FATAL_FAILURE_(message) \
779 return GTEST_MESSAGE_(message, ::testing::TPRT_FATAL_FAILURE)
781 #define GTEST_NONFATAL_FAILURE_(message) \
782 GTEST_MESSAGE_(message, ::testing::TPRT_NONFATAL_FAILURE)
784 #define GTEST_SUCCESS_(message) \
785 GTEST_MESSAGE_(message, ::testing::TPRT_SUCCESS)
787 #define GTEST_TEST_THROW_(statement, expected_exception, fail) \
788 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
789 if (const char* gtest_msg = "") { \
790 bool gtest_caught_expected = false; \
794 catch (expected_exception const&) { \
795 gtest_caught_expected = true; \
798 gtest_msg = "Expected: " #statement " throws an exception of type " \
799 #expected_exception ".\n Actual: it throws a different " \
801 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
803 if (!gtest_caught_expected) { \
804 gtest_msg = "Expected: " #statement " throws an exception of type " \
805 #expected_exception ".\n Actual: it throws nothing."; \
806 goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
809 GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
812 #define GTEST_TEST_NO_THROW_(statement, fail) \
813 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
814 if (const char* gtest_msg = "") { \
819 gtest_msg = "Expected: " #statement " doesn't throw an exception.\n" \
820 " Actual: it throws."; \
821 goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \
824 GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \
827 #define GTEST_TEST_ANY_THROW_(statement, fail) \
828 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
829 if (const char* gtest_msg = "") { \
830 bool gtest_caught_any = false; \
835 gtest_caught_any = true; \
837 if (!gtest_caught_any) { \
838 gtest_msg = "Expected: " #statement " throws an exception.\n" \
839 " Actual: it doesn't."; \
840 goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \
843 GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__): \
847 #define GTEST_TEST_BOOLEAN_(boolexpr, booltext, actual, expected, fail) \
848 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
852 fail("Value of: " booltext "\n Actual: " #actual "\nExpected: " #expected)
854 #define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \
855 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
856 if (const char* gtest_msg = "") { \
857 ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \
859 if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \
860 gtest_msg = "Expected: " #statement " doesn't generate new fatal " \
861 "failures in the current thread.\n" \
862 " Actual: it does."; \
863 goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \
866 GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__): \
869 // Expands to the name of the class that implements the given test.
870 #define GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \
871 test_case_name##_##test_name##_Test
873 // Helper macro for defining tests.
874 #define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\
875 class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\
877 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\
879 virtual void TestBody();\
880 static ::testing::TestInfo* const test_info_;\
881 GTEST_DISALLOW_COPY_AND_ASSIGN_(\
882 GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\
885 ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\
887 ::testing::internal::MakeAndRegisterTestInfo(\
888 #test_case_name, #test_name, "", "", \
890 parent_class::SetUpTestCase, \
891 parent_class::TearDownTestCase, \
892 new ::testing::internal::TestFactoryImpl<\
893 GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\
894 void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody()
896 #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_