1b89a7cc2SEnji Cooper // Copyright 2005, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper // * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper // * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper // * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2928f6c2f2SEnji Cooper
30b89a7cc2SEnji Cooper // The Google C++ Testing and Mocking Framework (Google Test)
31b89a7cc2SEnji Cooper //
32b89a7cc2SEnji Cooper // This header file declares functions and macros used internally by
33b89a7cc2SEnji Cooper // Google Test. They are subject to change without notice.
34b89a7cc2SEnji Cooper
3528f6c2f2SEnji Cooper // IWYU pragma: private, include "gtest/gtest.h"
3628f6c2f2SEnji Cooper // IWYU pragma: friend gtest/.*
3728f6c2f2SEnji Cooper // IWYU pragma: friend gmock/.*
38b89a7cc2SEnji Cooper
3928f6c2f2SEnji Cooper #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
4028f6c2f2SEnji Cooper #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
41b89a7cc2SEnji Cooper
42b89a7cc2SEnji Cooper #include "gtest/internal/gtest-port.h"
43b89a7cc2SEnji Cooper
4428f6c2f2SEnji Cooper #ifdef GTEST_OS_LINUX
45b89a7cc2SEnji Cooper #include <stdlib.h>
46b89a7cc2SEnji Cooper #include <sys/types.h>
47b89a7cc2SEnji Cooper #include <sys/wait.h>
48b89a7cc2SEnji Cooper #include <unistd.h>
49b89a7cc2SEnji Cooper #endif // GTEST_OS_LINUX
50b89a7cc2SEnji Cooper
51b89a7cc2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
52b89a7cc2SEnji Cooper #include <stdexcept>
53b89a7cc2SEnji Cooper #endif
54b89a7cc2SEnji Cooper
55b89a7cc2SEnji Cooper #include <ctype.h>
56b89a7cc2SEnji Cooper #include <float.h>
57b89a7cc2SEnji Cooper #include <string.h>
5828f6c2f2SEnji Cooper
5928f6c2f2SEnji Cooper #include <cstdint>
6028f6c2f2SEnji Cooper #include <functional>
61b89a7cc2SEnji Cooper #include <iomanip>
62b89a7cc2SEnji Cooper #include <limits>
63b89a7cc2SEnji Cooper #include <map>
64b89a7cc2SEnji Cooper #include <set>
65b89a7cc2SEnji Cooper #include <string>
6628f6c2f2SEnji Cooper #include <type_traits>
6728f6c2f2SEnji Cooper #include <utility>
68b89a7cc2SEnji Cooper #include <vector>
69b89a7cc2SEnji Cooper
70b89a7cc2SEnji Cooper #include "gtest/gtest-message.h"
71b89a7cc2SEnji Cooper #include "gtest/internal/gtest-filepath.h"
72b89a7cc2SEnji Cooper #include "gtest/internal/gtest-string.h"
73b89a7cc2SEnji Cooper #include "gtest/internal/gtest-type-util.h"
74b89a7cc2SEnji Cooper
75b89a7cc2SEnji Cooper // Due to C++ preprocessor weirdness, we need double indirection to
76b89a7cc2SEnji Cooper // concatenate two tokens when one of them is __LINE__. Writing
77b89a7cc2SEnji Cooper //
78b89a7cc2SEnji Cooper // foo ## __LINE__
79b89a7cc2SEnji Cooper //
80b89a7cc2SEnji Cooper // will result in the token foo__LINE__, instead of foo followed by
81b89a7cc2SEnji Cooper // the current line number. For more details, see
82b89a7cc2SEnji Cooper // http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6
83b89a7cc2SEnji Cooper #define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar)
84b89a7cc2SEnji Cooper #define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo##bar
85b89a7cc2SEnji Cooper
86b89a7cc2SEnji Cooper // Stringifies its argument.
8728f6c2f2SEnji Cooper // Work around a bug in visual studio which doesn't accept code like this:
8828f6c2f2SEnji Cooper //
8928f6c2f2SEnji Cooper // #define GTEST_STRINGIFY_(name) #name
9028f6c2f2SEnji Cooper // #define MACRO(a, b, c) ... GTEST_STRINGIFY_(a) ...
9128f6c2f2SEnji Cooper // MACRO(, x, y)
9228f6c2f2SEnji Cooper //
9328f6c2f2SEnji Cooper // Complaining about the argument to GTEST_STRINGIFY_ being empty.
9428f6c2f2SEnji Cooper // This is allowed by the spec.
9528f6c2f2SEnji Cooper #define GTEST_STRINGIFY_HELPER_(name, ...) #name
9628f6c2f2SEnji Cooper #define GTEST_STRINGIFY_(...) GTEST_STRINGIFY_HELPER_(__VA_ARGS__, )
97b89a7cc2SEnji Cooper
9828f6c2f2SEnji Cooper namespace proto2 {
9928f6c2f2SEnji Cooper class MessageLite;
10028f6c2f2SEnji Cooper }
101b89a7cc2SEnji Cooper
102b89a7cc2SEnji Cooper namespace testing {
103b89a7cc2SEnji Cooper
104b89a7cc2SEnji Cooper // Forward declarations.
105b89a7cc2SEnji Cooper
106b89a7cc2SEnji Cooper class AssertionResult; // Result of an assertion.
107b89a7cc2SEnji Cooper class Message; // Represents a failure message.
108b89a7cc2SEnji Cooper class Test; // Represents a test.
109b89a7cc2SEnji Cooper class TestInfo; // Information about a test.
110b89a7cc2SEnji Cooper class TestPartResult; // Result of a test part.
11128f6c2f2SEnji Cooper class UnitTest; // A collection of test suites.
112b89a7cc2SEnji Cooper
113b89a7cc2SEnji Cooper template <typename T>
114b89a7cc2SEnji Cooper ::std::string PrintToString(const T& value);
115b89a7cc2SEnji Cooper
116b89a7cc2SEnji Cooper namespace internal {
117b89a7cc2SEnji Cooper
118b89a7cc2SEnji Cooper struct TraceInfo; // Information about a trace point.
119b89a7cc2SEnji Cooper class TestInfoImpl; // Opaque implementation of TestInfo
120b89a7cc2SEnji Cooper class UnitTestImpl; // Opaque implementation of UnitTest
121b89a7cc2SEnji Cooper
122b89a7cc2SEnji Cooper // The text used in failure messages to indicate the start of the
123b89a7cc2SEnji Cooper // stack trace.
124b89a7cc2SEnji Cooper GTEST_API_ extern const char kStackTraceMarker[];
125b89a7cc2SEnji Cooper
12628f6c2f2SEnji Cooper // An IgnoredValue object can be implicitly constructed from ANY value.
12728f6c2f2SEnji Cooper class IgnoredValue {
12828f6c2f2SEnji Cooper struct Sink {};
129b89a7cc2SEnji Cooper
13028f6c2f2SEnji Cooper public:
13128f6c2f2SEnji Cooper // This constructor template allows any value to be implicitly
13228f6c2f2SEnji Cooper // converted to IgnoredValue. The object has no data member and
13328f6c2f2SEnji Cooper // doesn't try to remember anything about the argument. We
13428f6c2f2SEnji Cooper // deliberately omit the 'explicit' keyword in order to allow the
13528f6c2f2SEnji Cooper // conversion to be implicit.
13628f6c2f2SEnji Cooper // Disable the conversion if T already has a magical conversion operator.
13728f6c2f2SEnji Cooper // Otherwise we get ambiguity.
13828f6c2f2SEnji Cooper template <typename T,
13928f6c2f2SEnji Cooper typename std::enable_if<!std::is_convertible<T, Sink>::value,
14028f6c2f2SEnji Cooper int>::type = 0>
IgnoredValue(const T &)14128f6c2f2SEnji Cooper IgnoredValue(const T& /* ignored */) {} // NOLINT(runtime/explicit)
14228f6c2f2SEnji Cooper };
143b89a7cc2SEnji Cooper
144b89a7cc2SEnji Cooper // Appends the user-supplied message to the Google-Test-generated message.
14528f6c2f2SEnji Cooper GTEST_API_ std::string AppendUserMessage(const std::string& gtest_msg,
14628f6c2f2SEnji Cooper const Message& user_msg);
147b89a7cc2SEnji Cooper
148b89a7cc2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
149b89a7cc2SEnji Cooper
15028f6c2f2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(
15128f6c2f2SEnji Cooper 4275 /* an exported class was derived from a class that was not exported */)
152b89a7cc2SEnji Cooper
153b89a7cc2SEnji Cooper // This exception is thrown by (and only by) a failed Google Test
154b89a7cc2SEnji Cooper // assertion when GTEST_FLAG(throw_on_failure) is true (if exceptions
155b89a7cc2SEnji Cooper // are enabled). We derive it from std::runtime_error, which is for
156b89a7cc2SEnji Cooper // errors presumably detectable only at run time. Since
157b89a7cc2SEnji Cooper // std::runtime_error inherits from std::exception, many testing
158b89a7cc2SEnji Cooper // frameworks know how to extract and print the message inside it.
159b89a7cc2SEnji Cooper class GTEST_API_ GoogleTestFailureException : public ::std::runtime_error {
160b89a7cc2SEnji Cooper public:
161b89a7cc2SEnji Cooper explicit GoogleTestFailureException(const TestPartResult& failure);
162b89a7cc2SEnji Cooper };
163b89a7cc2SEnji Cooper
GTEST_DISABLE_MSC_WARNINGS_POP_()164b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4275
165b89a7cc2SEnji Cooper
166b89a7cc2SEnji Cooper #endif // GTEST_HAS_EXCEPTIONS
167b89a7cc2SEnji Cooper
168b89a7cc2SEnji Cooper namespace edit_distance {
169b89a7cc2SEnji Cooper // Returns the optimal edits to go from 'left' to 'right'.
170b89a7cc2SEnji Cooper // All edits cost the same, with replace having lower priority than
171b89a7cc2SEnji Cooper // add/remove.
172b89a7cc2SEnji Cooper // Simple implementation of the Wagner-Fischer algorithm.
173b89a7cc2SEnji Cooper // See http://en.wikipedia.org/wiki/Wagner-Fischer_algorithm
174b89a7cc2SEnji Cooper enum EditType { kMatch, kAdd, kRemove, kReplace };
175b89a7cc2SEnji Cooper GTEST_API_ std::vector<EditType> CalculateOptimalEdits(
176b89a7cc2SEnji Cooper const std::vector<size_t>& left, const std::vector<size_t>& right);
177b89a7cc2SEnji Cooper
178b89a7cc2SEnji Cooper // Same as above, but the input is represented as strings.
179b89a7cc2SEnji Cooper GTEST_API_ std::vector<EditType> CalculateOptimalEdits(
180b89a7cc2SEnji Cooper const std::vector<std::string>& left,
181b89a7cc2SEnji Cooper const std::vector<std::string>& right);
182b89a7cc2SEnji Cooper
183b89a7cc2SEnji Cooper // Create a diff of the input strings in Unified diff format.
184b89a7cc2SEnji Cooper GTEST_API_ std::string CreateUnifiedDiff(const std::vector<std::string>& left,
185b89a7cc2SEnji Cooper const std::vector<std::string>& right,
186b89a7cc2SEnji Cooper size_t context = 2);
187b89a7cc2SEnji Cooper
188b89a7cc2SEnji Cooper } // namespace edit_distance
189b89a7cc2SEnji Cooper
190b89a7cc2SEnji Cooper // Constructs and returns the message for an equality assertion
191b89a7cc2SEnji Cooper // (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure.
192b89a7cc2SEnji Cooper //
193b89a7cc2SEnji Cooper // The first four parameters are the expressions used in the assertion
194b89a7cc2SEnji Cooper // and their values, as strings. For example, for ASSERT_EQ(foo, bar)
195b89a7cc2SEnji Cooper // where foo is 5 and bar is 6, we have:
196b89a7cc2SEnji Cooper //
197b89a7cc2SEnji Cooper // expected_expression: "foo"
198b89a7cc2SEnji Cooper // actual_expression: "bar"
199b89a7cc2SEnji Cooper // expected_value: "5"
200b89a7cc2SEnji Cooper // actual_value: "6"
201b89a7cc2SEnji Cooper //
20228f6c2f2SEnji Cooper // The ignoring_case parameter is true if and only if the assertion is a
203b89a7cc2SEnji Cooper // *_STRCASEEQ*. When it's true, the string " (ignoring case)" will
204b89a7cc2SEnji Cooper // be inserted into the message.
205b89a7cc2SEnji Cooper GTEST_API_ AssertionResult EqFailure(const char* expected_expression,
206b89a7cc2SEnji Cooper const char* actual_expression,
207b89a7cc2SEnji Cooper const std::string& expected_value,
208b89a7cc2SEnji Cooper const std::string& actual_value,
209b89a7cc2SEnji Cooper bool ignoring_case);
210b89a7cc2SEnji Cooper
211b89a7cc2SEnji Cooper // Constructs a failure message for Boolean assertions such as EXPECT_TRUE.
212b89a7cc2SEnji Cooper GTEST_API_ std::string GetBoolAssertionFailureMessage(
21328f6c2f2SEnji Cooper const AssertionResult& assertion_result, const char* expression_text,
21428f6c2f2SEnji Cooper const char* actual_predicate_value, const char* expected_predicate_value);
215b89a7cc2SEnji Cooper
216b89a7cc2SEnji Cooper // This template class represents an IEEE floating-point number
217b89a7cc2SEnji Cooper // (either single-precision or double-precision, depending on the
218b89a7cc2SEnji Cooper // template parameters).
219b89a7cc2SEnji Cooper //
220b89a7cc2SEnji Cooper // The purpose of this class is to do more sophisticated number
221b89a7cc2SEnji Cooper // comparison. (Due to round-off error, etc, it's very unlikely that
222b89a7cc2SEnji Cooper // two floating-points will be equal exactly. Hence a naive
223b89a7cc2SEnji Cooper // comparison by the == operation often doesn't work.)
224b89a7cc2SEnji Cooper //
225b89a7cc2SEnji Cooper // Format of IEEE floating-point:
226b89a7cc2SEnji Cooper //
227b89a7cc2SEnji Cooper // The most-significant bit being the leftmost, an IEEE
228b89a7cc2SEnji Cooper // floating-point looks like
229b89a7cc2SEnji Cooper //
230b89a7cc2SEnji Cooper // sign_bit exponent_bits fraction_bits
231b89a7cc2SEnji Cooper //
232b89a7cc2SEnji Cooper // Here, sign_bit is a single bit that designates the sign of the
233b89a7cc2SEnji Cooper // number.
234b89a7cc2SEnji Cooper //
235b89a7cc2SEnji Cooper // For float, there are 8 exponent bits and 23 fraction bits.
236b89a7cc2SEnji Cooper //
237b89a7cc2SEnji Cooper // For double, there are 11 exponent bits and 52 fraction bits.
238b89a7cc2SEnji Cooper //
239b89a7cc2SEnji Cooper // More details can be found at
240b89a7cc2SEnji Cooper // http://en.wikipedia.org/wiki/IEEE_floating-point_standard.
241b89a7cc2SEnji Cooper //
242b89a7cc2SEnji Cooper // Template parameter:
243b89a7cc2SEnji Cooper //
244b89a7cc2SEnji Cooper // RawType: the raw floating-point type (either float or double)
245b89a7cc2SEnji Cooper template <typename RawType>
246b89a7cc2SEnji Cooper class FloatingPoint {
247b89a7cc2SEnji Cooper public:
248b89a7cc2SEnji Cooper // Defines the unsigned integer type that has the same size as the
249b89a7cc2SEnji Cooper // floating point number.
250b89a7cc2SEnji Cooper typedef typename TypeWithSize<sizeof(RawType)>::UInt Bits;
251b89a7cc2SEnji Cooper
252b89a7cc2SEnji Cooper // Constants.
253b89a7cc2SEnji Cooper
254b89a7cc2SEnji Cooper // # of bits in a number.
255b89a7cc2SEnji Cooper static const size_t kBitCount = 8 * sizeof(RawType);
256b89a7cc2SEnji Cooper
257b89a7cc2SEnji Cooper // # of fraction bits in a number.
258b89a7cc2SEnji Cooper static const size_t kFractionBitCount =
259b89a7cc2SEnji Cooper std::numeric_limits<RawType>::digits - 1;
260b89a7cc2SEnji Cooper
261b89a7cc2SEnji Cooper // # of exponent bits in a number.
262b89a7cc2SEnji Cooper static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount;
263b89a7cc2SEnji Cooper
264b89a7cc2SEnji Cooper // The mask for the sign bit.
265b89a7cc2SEnji Cooper static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1);
266b89a7cc2SEnji Cooper
267b89a7cc2SEnji Cooper // The mask for the fraction bits.
26828f6c2f2SEnji Cooper static const Bits kFractionBitMask = ~static_cast<Bits>(0) >>
26928f6c2f2SEnji Cooper (kExponentBitCount + 1);
270b89a7cc2SEnji Cooper
271b89a7cc2SEnji Cooper // The mask for the exponent bits.
272b89a7cc2SEnji Cooper static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask);
273b89a7cc2SEnji Cooper
274b89a7cc2SEnji Cooper // How many ULP's (Units in the Last Place) we want to tolerate when
275b89a7cc2SEnji Cooper // comparing two numbers. The larger the value, the more error we
276b89a7cc2SEnji Cooper // allow. A 0 value means that two numbers must be exactly the same
277b89a7cc2SEnji Cooper // to be considered equal.
278b89a7cc2SEnji Cooper //
279b89a7cc2SEnji Cooper // The maximum error of a single floating-point operation is 0.5
280b89a7cc2SEnji Cooper // units in the last place. On Intel CPU's, all floating-point
281b89a7cc2SEnji Cooper // calculations are done with 80-bit precision, while double has 64
282b89a7cc2SEnji Cooper // bits. Therefore, 4 should be enough for ordinary use.
283b89a7cc2SEnji Cooper //
284b89a7cc2SEnji Cooper // See the following article for more details on ULP:
285b89a7cc2SEnji Cooper // http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
28628f6c2f2SEnji Cooper static const uint32_t kMaxUlps = 4;
287b89a7cc2SEnji Cooper
288b89a7cc2SEnji Cooper // Constructs a FloatingPoint from a raw floating-point number.
289b89a7cc2SEnji Cooper //
290b89a7cc2SEnji Cooper // On an Intel CPU, passing a non-normalized NAN (Not a Number)
291b89a7cc2SEnji Cooper // around may change its bits, although the new value is guaranteed
292b89a7cc2SEnji Cooper // to be also a NAN. Therefore, don't expect this constructor to
293b89a7cc2SEnji Cooper // preserve the bits in x when x is a NAN.
FloatingPoint(const RawType & x)294b89a7cc2SEnji Cooper explicit FloatingPoint(const RawType& x) { u_.value_ = x; }
295b89a7cc2SEnji Cooper
296b89a7cc2SEnji Cooper // Static methods
297b89a7cc2SEnji Cooper
298b89a7cc2SEnji Cooper // Reinterprets a bit pattern as a floating-point number.
299b89a7cc2SEnji Cooper //
300b89a7cc2SEnji Cooper // This function is needed to test the AlmostEquals() method.
ReinterpretBits(const Bits bits)301b89a7cc2SEnji Cooper static RawType ReinterpretBits(const Bits bits) {
302b89a7cc2SEnji Cooper FloatingPoint fp(0);
303b89a7cc2SEnji Cooper fp.u_.bits_ = bits;
304b89a7cc2SEnji Cooper return fp.u_.value_;
305b89a7cc2SEnji Cooper }
306b89a7cc2SEnji Cooper
307b89a7cc2SEnji Cooper // Returns the floating-point number that represent positive infinity.
Infinity()30828f6c2f2SEnji Cooper static RawType Infinity() { return ReinterpretBits(kExponentBitMask); }
309b89a7cc2SEnji Cooper
310b89a7cc2SEnji Cooper // Non-static methods
311b89a7cc2SEnji Cooper
312b89a7cc2SEnji Cooper // Returns the bits that represents this number.
bits()313b89a7cc2SEnji Cooper const Bits& bits() const { return u_.bits_; }
314b89a7cc2SEnji Cooper
315b89a7cc2SEnji Cooper // Returns the exponent bits of this number.
exponent_bits()316b89a7cc2SEnji Cooper Bits exponent_bits() const { return kExponentBitMask & u_.bits_; }
317b89a7cc2SEnji Cooper
318b89a7cc2SEnji Cooper // Returns the fraction bits of this number.
fraction_bits()319b89a7cc2SEnji Cooper Bits fraction_bits() const { return kFractionBitMask & u_.bits_; }
320b89a7cc2SEnji Cooper
321b89a7cc2SEnji Cooper // Returns the sign bit of this number.
sign_bit()322b89a7cc2SEnji Cooper Bits sign_bit() const { return kSignBitMask & u_.bits_; }
323b89a7cc2SEnji Cooper
32428f6c2f2SEnji Cooper // Returns true if and only if this is NAN (not a number).
is_nan()325b89a7cc2SEnji Cooper bool is_nan() const {
326b89a7cc2SEnji Cooper // It's a NAN if the exponent bits are all ones and the fraction
327b89a7cc2SEnji Cooper // bits are not entirely zeros.
328b89a7cc2SEnji Cooper return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0);
329b89a7cc2SEnji Cooper }
330b89a7cc2SEnji Cooper
33128f6c2f2SEnji Cooper // Returns true if and only if this number is at most kMaxUlps ULP's away
33228f6c2f2SEnji Cooper // from rhs. In particular, this function:
333b89a7cc2SEnji Cooper //
334b89a7cc2SEnji Cooper // - returns false if either number is (or both are) NAN.
335b89a7cc2SEnji Cooper // - treats really large numbers as almost equal to infinity.
336b89a7cc2SEnji Cooper // - thinks +0.0 and -0.0 are 0 DLP's apart.
AlmostEquals(const FloatingPoint & rhs)337b89a7cc2SEnji Cooper bool AlmostEquals(const FloatingPoint& rhs) const {
338b89a7cc2SEnji Cooper // The IEEE standard says that any comparison operation involving
339b89a7cc2SEnji Cooper // a NAN must return false.
340b89a7cc2SEnji Cooper if (is_nan() || rhs.is_nan()) return false;
341b89a7cc2SEnji Cooper
34228f6c2f2SEnji Cooper return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_) <=
34328f6c2f2SEnji Cooper kMaxUlps;
344b89a7cc2SEnji Cooper }
345b89a7cc2SEnji Cooper
346b89a7cc2SEnji Cooper private:
347b89a7cc2SEnji Cooper // The data type used to store the actual floating-point number.
348b89a7cc2SEnji Cooper union FloatingPointUnion {
349b89a7cc2SEnji Cooper RawType value_; // The raw floating-point number.
350b89a7cc2SEnji Cooper Bits bits_; // The bits that represent the number.
351b89a7cc2SEnji Cooper };
352b89a7cc2SEnji Cooper
353b89a7cc2SEnji Cooper // Converts an integer from the sign-and-magnitude representation to
354b89a7cc2SEnji Cooper // the biased representation. More precisely, let N be 2 to the
355b89a7cc2SEnji Cooper // power of (kBitCount - 1), an integer x is represented by the
356b89a7cc2SEnji Cooper // unsigned number x + N.
357b89a7cc2SEnji Cooper //
358b89a7cc2SEnji Cooper // For instance,
359b89a7cc2SEnji Cooper //
360b89a7cc2SEnji Cooper // -N + 1 (the most negative number representable using
361b89a7cc2SEnji Cooper // sign-and-magnitude) is represented by 1;
362b89a7cc2SEnji Cooper // 0 is represented by N; and
363b89a7cc2SEnji Cooper // N - 1 (the biggest number representable using
364b89a7cc2SEnji Cooper // sign-and-magnitude) is represented by 2N - 1.
365b89a7cc2SEnji Cooper //
366b89a7cc2SEnji Cooper // Read http://en.wikipedia.org/wiki/Signed_number_representations
367b89a7cc2SEnji Cooper // for more details on signed number representations.
SignAndMagnitudeToBiased(const Bits & sam)368b89a7cc2SEnji Cooper static Bits SignAndMagnitudeToBiased(const Bits& sam) {
369b89a7cc2SEnji Cooper if (kSignBitMask & sam) {
370b89a7cc2SEnji Cooper // sam represents a negative number.
371b89a7cc2SEnji Cooper return ~sam + 1;
372b89a7cc2SEnji Cooper } else {
373b89a7cc2SEnji Cooper // sam represents a positive number.
374b89a7cc2SEnji Cooper return kSignBitMask | sam;
375b89a7cc2SEnji Cooper }
376b89a7cc2SEnji Cooper }
377b89a7cc2SEnji Cooper
378b89a7cc2SEnji Cooper // Given two numbers in the sign-and-magnitude representation,
379b89a7cc2SEnji Cooper // returns the distance between them as an unsigned number.
DistanceBetweenSignAndMagnitudeNumbers(const Bits & sam1,const Bits & sam2)380b89a7cc2SEnji Cooper static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits& sam1,
381b89a7cc2SEnji Cooper const Bits& sam2) {
382b89a7cc2SEnji Cooper const Bits biased1 = SignAndMagnitudeToBiased(sam1);
383b89a7cc2SEnji Cooper const Bits biased2 = SignAndMagnitudeToBiased(sam2);
384b89a7cc2SEnji Cooper return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
385b89a7cc2SEnji Cooper }
386b89a7cc2SEnji Cooper
387b89a7cc2SEnji Cooper FloatingPointUnion u_;
388b89a7cc2SEnji Cooper };
389b89a7cc2SEnji Cooper
390b89a7cc2SEnji Cooper // Typedefs the instances of the FloatingPoint template class that we
391b89a7cc2SEnji Cooper // care to use.
392b89a7cc2SEnji Cooper typedef FloatingPoint<float> Float;
393b89a7cc2SEnji Cooper typedef FloatingPoint<double> Double;
394b89a7cc2SEnji Cooper
395b89a7cc2SEnji Cooper // In order to catch the mistake of putting tests that use different
39628f6c2f2SEnji Cooper // test fixture classes in the same test suite, we need to assign
397b89a7cc2SEnji Cooper // unique IDs to fixture classes and compare them. The TypeId type is
398b89a7cc2SEnji Cooper // used to hold such IDs. The user should treat TypeId as an opaque
399b89a7cc2SEnji Cooper // type: the only operation allowed on TypeId values is to compare
400b89a7cc2SEnji Cooper // them for equality using the == operator.
401b89a7cc2SEnji Cooper typedef const void* TypeId;
402b89a7cc2SEnji Cooper
403b89a7cc2SEnji Cooper template <typename T>
404b89a7cc2SEnji Cooper class TypeIdHelper {
405b89a7cc2SEnji Cooper public:
406b89a7cc2SEnji Cooper // dummy_ must not have a const type. Otherwise an overly eager
407b89a7cc2SEnji Cooper // compiler (e.g. MSVC 7.1 & 8.0) may try to merge
408b89a7cc2SEnji Cooper // TypeIdHelper<T>::dummy_ for different Ts as an "optimization".
409b89a7cc2SEnji Cooper static bool dummy_;
410b89a7cc2SEnji Cooper };
411b89a7cc2SEnji Cooper
412b89a7cc2SEnji Cooper template <typename T>
413b89a7cc2SEnji Cooper bool TypeIdHelper<T>::dummy_ = false;
414b89a7cc2SEnji Cooper
415b89a7cc2SEnji Cooper // GetTypeId<T>() returns the ID of type T. Different values will be
416b89a7cc2SEnji Cooper // returned for different types. Calling the function twice with the
417b89a7cc2SEnji Cooper // same type argument is guaranteed to return the same ID.
418b89a7cc2SEnji Cooper template <typename T>
GetTypeId()419b89a7cc2SEnji Cooper TypeId GetTypeId() {
420b89a7cc2SEnji Cooper // The compiler is required to allocate a different
421b89a7cc2SEnji Cooper // TypeIdHelper<T>::dummy_ variable for each T used to instantiate
422b89a7cc2SEnji Cooper // the template. Therefore, the address of dummy_ is guaranteed to
423b89a7cc2SEnji Cooper // be unique.
424b89a7cc2SEnji Cooper return &(TypeIdHelper<T>::dummy_);
425b89a7cc2SEnji Cooper }
426b89a7cc2SEnji Cooper
427b89a7cc2SEnji Cooper // Returns the type ID of ::testing::Test. Always call this instead
428b89a7cc2SEnji Cooper // of GetTypeId< ::testing::Test>() to get the type ID of
429b89a7cc2SEnji Cooper // ::testing::Test, as the latter may give the wrong result due to a
430b89a7cc2SEnji Cooper // suspected linker bug when compiling Google Test as a Mac OS X
431b89a7cc2SEnji Cooper // framework.
432b89a7cc2SEnji Cooper GTEST_API_ TypeId GetTestTypeId();
433b89a7cc2SEnji Cooper
434b89a7cc2SEnji Cooper // Defines the abstract factory interface that creates instances
435b89a7cc2SEnji Cooper // of a Test object.
436b89a7cc2SEnji Cooper class TestFactoryBase {
437b89a7cc2SEnji Cooper public:
43828f6c2f2SEnji Cooper virtual ~TestFactoryBase() = default;
439b89a7cc2SEnji Cooper
440b89a7cc2SEnji Cooper // Creates a test instance to run. The instance is both created and destroyed
441b89a7cc2SEnji Cooper // within TestInfoImpl::Run()
442b89a7cc2SEnji Cooper virtual Test* CreateTest() = 0;
443b89a7cc2SEnji Cooper
444b89a7cc2SEnji Cooper protected:
TestFactoryBase()445b89a7cc2SEnji Cooper TestFactoryBase() {}
446b89a7cc2SEnji Cooper
447b89a7cc2SEnji Cooper private:
44828f6c2f2SEnji Cooper TestFactoryBase(const TestFactoryBase&) = delete;
44928f6c2f2SEnji Cooper TestFactoryBase& operator=(const TestFactoryBase&) = delete;
450b89a7cc2SEnji Cooper };
451b89a7cc2SEnji Cooper
45228f6c2f2SEnji Cooper // This class provides implementation of TestFactoryBase interface.
453b89a7cc2SEnji Cooper // It is used in TEST and TEST_F macros.
454b89a7cc2SEnji Cooper template <class TestClass>
455b89a7cc2SEnji Cooper class TestFactoryImpl : public TestFactoryBase {
456b89a7cc2SEnji Cooper public:
CreateTest()45728f6c2f2SEnji Cooper Test* CreateTest() override { return new TestClass; }
458b89a7cc2SEnji Cooper };
459b89a7cc2SEnji Cooper
46028f6c2f2SEnji Cooper #ifdef GTEST_OS_WINDOWS
461b89a7cc2SEnji Cooper
462b89a7cc2SEnji Cooper // Predicate-formatters for implementing the HRESULT checking macros
463b89a7cc2SEnji Cooper // {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}
464b89a7cc2SEnji Cooper // We pass a long instead of HRESULT to avoid causing an
465b89a7cc2SEnji Cooper // include dependency for the HRESULT type.
466b89a7cc2SEnji Cooper GTEST_API_ AssertionResult IsHRESULTSuccess(const char* expr,
467b89a7cc2SEnji Cooper long hr); // NOLINT
468b89a7cc2SEnji Cooper GTEST_API_ AssertionResult IsHRESULTFailure(const char* expr,
469b89a7cc2SEnji Cooper long hr); // NOLINT
470b89a7cc2SEnji Cooper
471b89a7cc2SEnji Cooper #endif // GTEST_OS_WINDOWS
472b89a7cc2SEnji Cooper
47328f6c2f2SEnji Cooper // Types of SetUpTestSuite() and TearDownTestSuite() functions.
47428f6c2f2SEnji Cooper using SetUpTestSuiteFunc = void (*)();
47528f6c2f2SEnji Cooper using TearDownTestSuiteFunc = void (*)();
476b89a7cc2SEnji Cooper
477b89a7cc2SEnji Cooper struct CodeLocation {
CodeLocationCodeLocation478b89a7cc2SEnji Cooper CodeLocation(const std::string& a_file, int a_line)
479b89a7cc2SEnji Cooper : file(a_file), line(a_line) {}
480b89a7cc2SEnji Cooper
481b89a7cc2SEnji Cooper std::string file;
482b89a7cc2SEnji Cooper int line;
483b89a7cc2SEnji Cooper };
484b89a7cc2SEnji Cooper
48528f6c2f2SEnji Cooper // Helper to identify which setup function for TestCase / TestSuite to call.
48628f6c2f2SEnji Cooper // Only one function is allowed, either TestCase or TestSute but not both.
48728f6c2f2SEnji Cooper
48828f6c2f2SEnji Cooper // Utility functions to help SuiteApiResolver
48928f6c2f2SEnji Cooper using SetUpTearDownSuiteFuncType = void (*)();
49028f6c2f2SEnji Cooper
GetNotDefaultOrNull(SetUpTearDownSuiteFuncType a,SetUpTearDownSuiteFuncType def)49128f6c2f2SEnji Cooper inline SetUpTearDownSuiteFuncType GetNotDefaultOrNull(
49228f6c2f2SEnji Cooper SetUpTearDownSuiteFuncType a, SetUpTearDownSuiteFuncType def) {
49328f6c2f2SEnji Cooper return a == def ? nullptr : a;
49428f6c2f2SEnji Cooper }
49528f6c2f2SEnji Cooper
49628f6c2f2SEnji Cooper template <typename T>
49728f6c2f2SEnji Cooper // Note that SuiteApiResolver inherits from T because
49828f6c2f2SEnji Cooper // SetUpTestSuite()/TearDownTestSuite() could be protected. This way
49928f6c2f2SEnji Cooper // SuiteApiResolver can access them.
50028f6c2f2SEnji Cooper struct SuiteApiResolver : T {
50128f6c2f2SEnji Cooper // testing::Test is only forward declared at this point. So we make it a
50228f6c2f2SEnji Cooper // dependent class for the compiler to be OK with it.
50328f6c2f2SEnji Cooper using Test =
50428f6c2f2SEnji Cooper typename std::conditional<sizeof(T) != 0, ::testing::Test, void>::type;
50528f6c2f2SEnji Cooper
GetSetUpCaseOrSuiteSuiteApiResolver50628f6c2f2SEnji Cooper static SetUpTearDownSuiteFuncType GetSetUpCaseOrSuite(const char* filename,
50728f6c2f2SEnji Cooper int line_num) {
50828f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
50928f6c2f2SEnji Cooper SetUpTearDownSuiteFuncType test_case_fp =
51028f6c2f2SEnji Cooper GetNotDefaultOrNull(&T::SetUpTestCase, &Test::SetUpTestCase);
51128f6c2f2SEnji Cooper SetUpTearDownSuiteFuncType test_suite_fp =
51228f6c2f2SEnji Cooper GetNotDefaultOrNull(&T::SetUpTestSuite, &Test::SetUpTestSuite);
51328f6c2f2SEnji Cooper
51428f6c2f2SEnji Cooper GTEST_CHECK_(!test_case_fp || !test_suite_fp)
51528f6c2f2SEnji Cooper << "Test can not provide both SetUpTestSuite and SetUpTestCase, please "
51628f6c2f2SEnji Cooper "make sure there is only one present at "
51728f6c2f2SEnji Cooper << filename << ":" << line_num;
51828f6c2f2SEnji Cooper
51928f6c2f2SEnji Cooper return test_case_fp != nullptr ? test_case_fp : test_suite_fp;
52028f6c2f2SEnji Cooper #else
52128f6c2f2SEnji Cooper (void)(filename);
52228f6c2f2SEnji Cooper (void)(line_num);
52328f6c2f2SEnji Cooper return &T::SetUpTestSuite;
52428f6c2f2SEnji Cooper #endif
52528f6c2f2SEnji Cooper }
52628f6c2f2SEnji Cooper
GetTearDownCaseOrSuiteSuiteApiResolver52728f6c2f2SEnji Cooper static SetUpTearDownSuiteFuncType GetTearDownCaseOrSuite(const char* filename,
52828f6c2f2SEnji Cooper int line_num) {
52928f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
53028f6c2f2SEnji Cooper SetUpTearDownSuiteFuncType test_case_fp =
53128f6c2f2SEnji Cooper GetNotDefaultOrNull(&T::TearDownTestCase, &Test::TearDownTestCase);
53228f6c2f2SEnji Cooper SetUpTearDownSuiteFuncType test_suite_fp =
53328f6c2f2SEnji Cooper GetNotDefaultOrNull(&T::TearDownTestSuite, &Test::TearDownTestSuite);
53428f6c2f2SEnji Cooper
53528f6c2f2SEnji Cooper GTEST_CHECK_(!test_case_fp || !test_suite_fp)
53628f6c2f2SEnji Cooper << "Test can not provide both TearDownTestSuite and TearDownTestCase,"
53728f6c2f2SEnji Cooper " please make sure there is only one present at"
53828f6c2f2SEnji Cooper << filename << ":" << line_num;
53928f6c2f2SEnji Cooper
54028f6c2f2SEnji Cooper return test_case_fp != nullptr ? test_case_fp : test_suite_fp;
54128f6c2f2SEnji Cooper #else
54228f6c2f2SEnji Cooper (void)(filename);
54328f6c2f2SEnji Cooper (void)(line_num);
54428f6c2f2SEnji Cooper return &T::TearDownTestSuite;
54528f6c2f2SEnji Cooper #endif
54628f6c2f2SEnji Cooper }
54728f6c2f2SEnji Cooper };
54828f6c2f2SEnji Cooper
549b89a7cc2SEnji Cooper // Creates a new TestInfo object and registers it with Google Test;
550b89a7cc2SEnji Cooper // returns the created object.
551b89a7cc2SEnji Cooper //
552b89a7cc2SEnji Cooper // Arguments:
553b89a7cc2SEnji Cooper //
55428f6c2f2SEnji Cooper // test_suite_name: name of the test suite
555b89a7cc2SEnji Cooper // name: name of the test
55628f6c2f2SEnji Cooper // type_param: the name of the test's type parameter, or NULL if
557b89a7cc2SEnji Cooper // this is not a typed or a type-parameterized test.
55828f6c2f2SEnji Cooper // value_param: text representation of the test's value parameter,
559b89a7cc2SEnji Cooper // or NULL if this is not a type-parameterized test.
560b89a7cc2SEnji Cooper // code_location: code location where the test is defined
561b89a7cc2SEnji Cooper // fixture_class_id: ID of the test fixture class
56228f6c2f2SEnji Cooper // set_up_tc: pointer to the function that sets up the test suite
56328f6c2f2SEnji Cooper // tear_down_tc: pointer to the function that tears down the test suite
564b89a7cc2SEnji Cooper // factory: pointer to the factory that creates a test object.
565b89a7cc2SEnji Cooper // The newly created TestInfo instance will assume
566b89a7cc2SEnji Cooper // ownership of the factory object.
567b89a7cc2SEnji Cooper GTEST_API_ TestInfo* MakeAndRegisterTestInfo(
56828f6c2f2SEnji Cooper const char* test_suite_name, const char* name, const char* type_param,
56928f6c2f2SEnji Cooper const char* value_param, CodeLocation code_location,
57028f6c2f2SEnji Cooper TypeId fixture_class_id, SetUpTestSuiteFunc set_up_tc,
57128f6c2f2SEnji Cooper TearDownTestSuiteFunc tear_down_tc, TestFactoryBase* factory);
572b89a7cc2SEnji Cooper
573b89a7cc2SEnji Cooper // If *pstr starts with the given prefix, modifies *pstr to be right
574b89a7cc2SEnji Cooper // past the prefix and returns true; otherwise leaves *pstr unchanged
575b89a7cc2SEnji Cooper // and returns false. None of pstr, *pstr, and prefix can be NULL.
576b89a7cc2SEnji Cooper GTEST_API_ bool SkipPrefix(const char* prefix, const char** pstr);
577b89a7cc2SEnji Cooper
578b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
579b89a7cc2SEnji Cooper /* class A needs to have dll-interface to be used by clients of class B */)
580b89a7cc2SEnji Cooper
58128f6c2f2SEnji Cooper // State of the definition of a type-parameterized test suite.
58228f6c2f2SEnji Cooper class GTEST_API_ TypedTestSuitePState {
583b89a7cc2SEnji Cooper public:
TypedTestSuitePState()58428f6c2f2SEnji Cooper TypedTestSuitePState() : registered_(false) {}
585b89a7cc2SEnji Cooper
586b89a7cc2SEnji Cooper // Adds the given test name to defined_test_names_ and return true
58728f6c2f2SEnji Cooper // if the test suite hasn't been registered; otherwise aborts the
588b89a7cc2SEnji Cooper // program.
AddTestName(const char * file,int line,const char * case_name,const char * test_name)589b89a7cc2SEnji Cooper bool AddTestName(const char* file, int line, const char* case_name,
590b89a7cc2SEnji Cooper const char* test_name) {
591b89a7cc2SEnji Cooper if (registered_) {
59228f6c2f2SEnji Cooper fprintf(stderr,
59328f6c2f2SEnji Cooper "%s Test %s must be defined before "
59428f6c2f2SEnji Cooper "REGISTER_TYPED_TEST_SUITE_P(%s, ...).\n",
595b89a7cc2SEnji Cooper FormatFileLocation(file, line).c_str(), test_name, case_name);
596b89a7cc2SEnji Cooper fflush(stderr);
597b89a7cc2SEnji Cooper posix::Abort();
598b89a7cc2SEnji Cooper }
599b89a7cc2SEnji Cooper registered_tests_.insert(
600b89a7cc2SEnji Cooper ::std::make_pair(test_name, CodeLocation(file, line)));
601b89a7cc2SEnji Cooper return true;
602b89a7cc2SEnji Cooper }
603b89a7cc2SEnji Cooper
TestExists(const std::string & test_name)604b89a7cc2SEnji Cooper bool TestExists(const std::string& test_name) const {
605b89a7cc2SEnji Cooper return registered_tests_.count(test_name) > 0;
606b89a7cc2SEnji Cooper }
607b89a7cc2SEnji Cooper
GetCodeLocation(const std::string & test_name)608b89a7cc2SEnji Cooper const CodeLocation& GetCodeLocation(const std::string& test_name) const {
609b89a7cc2SEnji Cooper RegisteredTestsMap::const_iterator it = registered_tests_.find(test_name);
610b89a7cc2SEnji Cooper GTEST_CHECK_(it != registered_tests_.end());
611b89a7cc2SEnji Cooper return it->second;
612b89a7cc2SEnji Cooper }
613b89a7cc2SEnji Cooper
614b89a7cc2SEnji Cooper // Verifies that registered_tests match the test names in
615b89a7cc2SEnji Cooper // defined_test_names_; returns registered_tests if successful, or
616b89a7cc2SEnji Cooper // aborts the program otherwise.
61728f6c2f2SEnji Cooper const char* VerifyRegisteredTestNames(const char* test_suite_name,
61828f6c2f2SEnji Cooper const char* file, int line,
61928f6c2f2SEnji Cooper const char* registered_tests);
620b89a7cc2SEnji Cooper
621b89a7cc2SEnji Cooper private:
62228f6c2f2SEnji Cooper typedef ::std::map<std::string, CodeLocation, std::less<>> RegisteredTestsMap;
623b89a7cc2SEnji Cooper
624b89a7cc2SEnji Cooper bool registered_;
625b89a7cc2SEnji Cooper RegisteredTestsMap registered_tests_;
626b89a7cc2SEnji Cooper };
627b89a7cc2SEnji Cooper
62828f6c2f2SEnji Cooper // Legacy API is deprecated but still available
62928f6c2f2SEnji Cooper #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
63028f6c2f2SEnji Cooper using TypedTestCasePState = TypedTestSuitePState;
63128f6c2f2SEnji Cooper #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
63228f6c2f2SEnji Cooper
GTEST_DISABLE_MSC_WARNINGS_POP_()633b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
634b89a7cc2SEnji Cooper
635b89a7cc2SEnji Cooper // Skips to the first non-space char after the first comma in 'str';
636b89a7cc2SEnji Cooper // returns NULL if no comma is found in 'str'.
637b89a7cc2SEnji Cooper inline const char* SkipComma(const char* str) {
638b89a7cc2SEnji Cooper const char* comma = strchr(str, ',');
63928f6c2f2SEnji Cooper if (comma == nullptr) {
64028f6c2f2SEnji Cooper return nullptr;
641b89a7cc2SEnji Cooper }
64228f6c2f2SEnji Cooper while (IsSpace(*(++comma))) {
64328f6c2f2SEnji Cooper }
644b89a7cc2SEnji Cooper return comma;
645b89a7cc2SEnji Cooper }
646b89a7cc2SEnji Cooper
647b89a7cc2SEnji Cooper // Returns the prefix of 'str' before the first comma in it; returns
648b89a7cc2SEnji Cooper // the entire string if it contains no comma.
GetPrefixUntilComma(const char * str)649b89a7cc2SEnji Cooper inline std::string GetPrefixUntilComma(const char* str) {
650b89a7cc2SEnji Cooper const char* comma = strchr(str, ',');
65128f6c2f2SEnji Cooper return comma == nullptr ? str : std::string(str, comma);
652b89a7cc2SEnji Cooper }
653b89a7cc2SEnji Cooper
654b89a7cc2SEnji Cooper // Splits a given string on a given delimiter, populating a given
655b89a7cc2SEnji Cooper // vector with the fields.
656b89a7cc2SEnji Cooper void SplitString(const ::std::string& str, char delimiter,
657b89a7cc2SEnji Cooper ::std::vector<::std::string>* dest);
658b89a7cc2SEnji Cooper
659b89a7cc2SEnji Cooper // The default argument to the template below for the case when the user does
660b89a7cc2SEnji Cooper // not provide a name generator.
661b89a7cc2SEnji Cooper struct DefaultNameGenerator {
662b89a7cc2SEnji Cooper template <typename T>
GetNameDefaultNameGenerator663b89a7cc2SEnji Cooper static std::string GetName(int i) {
664b89a7cc2SEnji Cooper return StreamableToString(i);
665b89a7cc2SEnji Cooper }
666b89a7cc2SEnji Cooper };
667b89a7cc2SEnji Cooper
668b89a7cc2SEnji Cooper template <typename Provided = DefaultNameGenerator>
669b89a7cc2SEnji Cooper struct NameGeneratorSelector {
670b89a7cc2SEnji Cooper typedef Provided type;
671b89a7cc2SEnji Cooper };
672b89a7cc2SEnji Cooper
673b89a7cc2SEnji Cooper template <typename NameGenerator>
GenerateNamesRecursively(internal::None,std::vector<std::string> *,int)67428f6c2f2SEnji Cooper void GenerateNamesRecursively(internal::None, std::vector<std::string>*, int) {}
675b89a7cc2SEnji Cooper
676b89a7cc2SEnji Cooper template <typename NameGenerator, typename Types>
GenerateNamesRecursively(Types,std::vector<std::string> * result,int i)677b89a7cc2SEnji Cooper void GenerateNamesRecursively(Types, std::vector<std::string>* result, int i) {
678b89a7cc2SEnji Cooper result->push_back(NameGenerator::template GetName<typename Types::Head>(i));
679b89a7cc2SEnji Cooper GenerateNamesRecursively<NameGenerator>(typename Types::Tail(), result,
680b89a7cc2SEnji Cooper i + 1);
681b89a7cc2SEnji Cooper }
682b89a7cc2SEnji Cooper
683b89a7cc2SEnji Cooper template <typename NameGenerator, typename Types>
GenerateNames()684b89a7cc2SEnji Cooper std::vector<std::string> GenerateNames() {
685b89a7cc2SEnji Cooper std::vector<std::string> result;
686b89a7cc2SEnji Cooper GenerateNamesRecursively<NameGenerator>(Types(), &result, 0);
687b89a7cc2SEnji Cooper return result;
688b89a7cc2SEnji Cooper }
689b89a7cc2SEnji Cooper
690b89a7cc2SEnji Cooper // TypeParameterizedTest<Fixture, TestSel, Types>::Register()
691b89a7cc2SEnji Cooper // registers a list of type-parameterized tests with Google Test. The
692b89a7cc2SEnji Cooper // return value is insignificant - we just need to return something
693b89a7cc2SEnji Cooper // such that we can call this function in a namespace scope.
694b89a7cc2SEnji Cooper //
695b89a7cc2SEnji Cooper // Implementation note: The GTEST_TEMPLATE_ macro declares a template
696b89a7cc2SEnji Cooper // template parameter. It's defined in gtest-type-util.h.
697b89a7cc2SEnji Cooper template <GTEST_TEMPLATE_ Fixture, class TestSel, typename Types>
698b89a7cc2SEnji Cooper class TypeParameterizedTest {
699b89a7cc2SEnji Cooper public:
700b89a7cc2SEnji Cooper // 'index' is the index of the test in the type list 'Types'
70128f6c2f2SEnji Cooper // specified in INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, TestSuite,
702b89a7cc2SEnji Cooper // Types). Valid values for 'index' are [0, N - 1] where N is the
703b89a7cc2SEnji Cooper // length of Types.
704b89a7cc2SEnji Cooper static bool Register(const char* prefix, const CodeLocation& code_location,
705b89a7cc2SEnji Cooper const char* case_name, const char* test_names, int index,
706b89a7cc2SEnji Cooper const std::vector<std::string>& type_names =
707b89a7cc2SEnji Cooper GenerateNames<DefaultNameGenerator, Types>()) {
708b89a7cc2SEnji Cooper typedef typename Types::Head Type;
709b89a7cc2SEnji Cooper typedef Fixture<Type> FixtureClass;
710b89a7cc2SEnji Cooper typedef typename GTEST_BIND_(TestSel, Type) TestClass;
711b89a7cc2SEnji Cooper
712b89a7cc2SEnji Cooper // First, registers the first type-parameterized test in the type
713b89a7cc2SEnji Cooper // list.
714b89a7cc2SEnji Cooper MakeAndRegisterTestInfo(
715b89a7cc2SEnji Cooper (std::string(prefix) + (prefix[0] == '\0' ? "" : "/") + case_name +
71628f6c2f2SEnji Cooper "/" + type_names[static_cast<size_t>(index)])
717b89a7cc2SEnji Cooper .c_str(),
718b89a7cc2SEnji Cooper StripTrailingSpaces(GetPrefixUntilComma(test_names)).c_str(),
719b89a7cc2SEnji Cooper GetTypeName<Type>().c_str(),
72028f6c2f2SEnji Cooper nullptr, // No value parameter.
72128f6c2f2SEnji Cooper code_location, GetTypeId<FixtureClass>(),
72228f6c2f2SEnji Cooper SuiteApiResolver<TestClass>::GetSetUpCaseOrSuite(
72328f6c2f2SEnji Cooper code_location.file.c_str(), code_location.line),
72428f6c2f2SEnji Cooper SuiteApiResolver<TestClass>::GetTearDownCaseOrSuite(
72528f6c2f2SEnji Cooper code_location.file.c_str(), code_location.line),
72628f6c2f2SEnji Cooper new TestFactoryImpl<TestClass>);
727b89a7cc2SEnji Cooper
728b89a7cc2SEnji Cooper // Next, recurses (at compile time) with the tail of the type list.
729b89a7cc2SEnji Cooper return TypeParameterizedTest<Fixture, TestSel,
730b89a7cc2SEnji Cooper typename Types::Tail>::Register(prefix,
731b89a7cc2SEnji Cooper code_location,
732b89a7cc2SEnji Cooper case_name,
733b89a7cc2SEnji Cooper test_names,
734b89a7cc2SEnji Cooper index + 1,
735b89a7cc2SEnji Cooper type_names);
736b89a7cc2SEnji Cooper }
737b89a7cc2SEnji Cooper };
738b89a7cc2SEnji Cooper
739b89a7cc2SEnji Cooper // The base case for the compile time recursion.
740b89a7cc2SEnji Cooper template <GTEST_TEMPLATE_ Fixture, class TestSel>
74128f6c2f2SEnji Cooper class TypeParameterizedTest<Fixture, TestSel, internal::None> {
742b89a7cc2SEnji Cooper public:
743b89a7cc2SEnji Cooper static bool Register(const char* /*prefix*/, const CodeLocation&,
744b89a7cc2SEnji Cooper const char* /*case_name*/, const char* /*test_names*/,
745b89a7cc2SEnji Cooper int /*index*/,
746b89a7cc2SEnji Cooper const std::vector<std::string>& =
747b89a7cc2SEnji Cooper std::vector<std::string>() /*type_names*/) {
748b89a7cc2SEnji Cooper return true;
749b89a7cc2SEnji Cooper }
750b89a7cc2SEnji Cooper };
751b89a7cc2SEnji Cooper
75228f6c2f2SEnji Cooper GTEST_API_ void RegisterTypeParameterizedTestSuite(const char* test_suite_name,
75328f6c2f2SEnji Cooper CodeLocation code_location);
75428f6c2f2SEnji Cooper GTEST_API_ void RegisterTypeParameterizedTestSuiteInstantiation(
75528f6c2f2SEnji Cooper const char* case_name);
75628f6c2f2SEnji Cooper
75728f6c2f2SEnji Cooper // TypeParameterizedTestSuite<Fixture, Tests, Types>::Register()
758b89a7cc2SEnji Cooper // registers *all combinations* of 'Tests' and 'Types' with Google
759b89a7cc2SEnji Cooper // Test. The return value is insignificant - we just need to return
760b89a7cc2SEnji Cooper // something such that we can call this function in a namespace scope.
761b89a7cc2SEnji Cooper template <GTEST_TEMPLATE_ Fixture, typename Tests, typename Types>
76228f6c2f2SEnji Cooper class TypeParameterizedTestSuite {
763b89a7cc2SEnji Cooper public:
764b89a7cc2SEnji Cooper static bool Register(const char* prefix, CodeLocation code_location,
76528f6c2f2SEnji Cooper const TypedTestSuitePState* state, const char* case_name,
766b89a7cc2SEnji Cooper const char* test_names,
767b89a7cc2SEnji Cooper const std::vector<std::string>& type_names =
768b89a7cc2SEnji Cooper GenerateNames<DefaultNameGenerator, Types>()) {
76928f6c2f2SEnji Cooper RegisterTypeParameterizedTestSuiteInstantiation(case_name);
77028f6c2f2SEnji Cooper std::string test_name =
77128f6c2f2SEnji Cooper StripTrailingSpaces(GetPrefixUntilComma(test_names));
772b89a7cc2SEnji Cooper if (!state->TestExists(test_name)) {
773b89a7cc2SEnji Cooper fprintf(stderr, "Failed to get code location for test %s.%s at %s.",
774b89a7cc2SEnji Cooper case_name, test_name.c_str(),
77528f6c2f2SEnji Cooper FormatFileLocation(code_location.file.c_str(), code_location.line)
77628f6c2f2SEnji Cooper .c_str());
777b89a7cc2SEnji Cooper fflush(stderr);
778b89a7cc2SEnji Cooper posix::Abort();
779b89a7cc2SEnji Cooper }
780b89a7cc2SEnji Cooper const CodeLocation& test_location = state->GetCodeLocation(test_name);
781b89a7cc2SEnji Cooper
782b89a7cc2SEnji Cooper typedef typename Tests::Head Head;
783b89a7cc2SEnji Cooper
784b89a7cc2SEnji Cooper // First, register the first test in 'Test' for each type in 'Types'.
785b89a7cc2SEnji Cooper TypeParameterizedTest<Fixture, Head, Types>::Register(
786b89a7cc2SEnji Cooper prefix, test_location, case_name, test_names, 0, type_names);
787b89a7cc2SEnji Cooper
788b89a7cc2SEnji Cooper // Next, recurses (at compile time) with the tail of the test list.
78928f6c2f2SEnji Cooper return TypeParameterizedTestSuite<Fixture, typename Tests::Tail,
790b89a7cc2SEnji Cooper Types>::Register(prefix, code_location,
791b89a7cc2SEnji Cooper state, case_name,
792b89a7cc2SEnji Cooper SkipComma(test_names),
793b89a7cc2SEnji Cooper type_names);
794b89a7cc2SEnji Cooper }
795b89a7cc2SEnji Cooper };
796b89a7cc2SEnji Cooper
797b89a7cc2SEnji Cooper // The base case for the compile time recursion.
798b89a7cc2SEnji Cooper template <GTEST_TEMPLATE_ Fixture, typename Types>
79928f6c2f2SEnji Cooper class TypeParameterizedTestSuite<Fixture, internal::None, Types> {
800b89a7cc2SEnji Cooper public:
801b89a7cc2SEnji Cooper static bool Register(const char* /*prefix*/, const CodeLocation&,
80228f6c2f2SEnji Cooper const TypedTestSuitePState* /*state*/,
803b89a7cc2SEnji Cooper const char* /*case_name*/, const char* /*test_names*/,
804b89a7cc2SEnji Cooper const std::vector<std::string>& =
805b89a7cc2SEnji Cooper std::vector<std::string>() /*type_names*/) {
806b89a7cc2SEnji Cooper return true;
807b89a7cc2SEnji Cooper }
808b89a7cc2SEnji Cooper };
809b89a7cc2SEnji Cooper
810b89a7cc2SEnji Cooper // Returns the current OS stack trace as an std::string.
811b89a7cc2SEnji Cooper //
812b89a7cc2SEnji Cooper // The maximum number of stack frames to be included is specified by
813b89a7cc2SEnji Cooper // the gtest_stack_trace_depth flag. The skip_count parameter
814b89a7cc2SEnji Cooper // specifies the number of top frames to be skipped, which doesn't
815b89a7cc2SEnji Cooper // count against the number of frames to be included.
816b89a7cc2SEnji Cooper //
817b89a7cc2SEnji Cooper // For example, if Foo() calls Bar(), which in turn calls
818b89a7cc2SEnji Cooper // GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in
819b89a7cc2SEnji Cooper // the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.
82028f6c2f2SEnji Cooper GTEST_API_ std::string GetCurrentOsStackTraceExceptTop(int skip_count);
821b89a7cc2SEnji Cooper
822b89a7cc2SEnji Cooper // Helpers for suppressing warnings on unreachable code or constant
823b89a7cc2SEnji Cooper // condition.
824b89a7cc2SEnji Cooper
825b89a7cc2SEnji Cooper // Always returns true.
826b89a7cc2SEnji Cooper GTEST_API_ bool AlwaysTrue();
827b89a7cc2SEnji Cooper
828b89a7cc2SEnji Cooper // Always returns false.
AlwaysFalse()829b89a7cc2SEnji Cooper inline bool AlwaysFalse() { return !AlwaysTrue(); }
830b89a7cc2SEnji Cooper
831b89a7cc2SEnji Cooper // Helper for suppressing false warning from Clang on a const char*
832b89a7cc2SEnji Cooper // variable declared in a conditional expression always being NULL in
833b89a7cc2SEnji Cooper // the else branch.
834b89a7cc2SEnji Cooper struct GTEST_API_ ConstCharPtr {
ConstCharPtrConstCharPtr835b89a7cc2SEnji Cooper ConstCharPtr(const char* str) : value(str) {}
836b89a7cc2SEnji Cooper operator bool() const { return true; }
837b89a7cc2SEnji Cooper const char* value;
838b89a7cc2SEnji Cooper };
839b89a7cc2SEnji Cooper
84028f6c2f2SEnji Cooper // Helper for declaring std::string within 'if' statement
84128f6c2f2SEnji Cooper // in pre C++17 build environment.
84228f6c2f2SEnji Cooper struct TrueWithString {
84328f6c2f2SEnji Cooper TrueWithString() = default;
TrueWithStringTrueWithString84428f6c2f2SEnji Cooper explicit TrueWithString(const char* str) : value(str) {}
TrueWithStringTrueWithString84528f6c2f2SEnji Cooper explicit TrueWithString(const std::string& str) : value(str) {}
84628f6c2f2SEnji Cooper explicit operator bool() const { return true; }
84728f6c2f2SEnji Cooper std::string value;
84828f6c2f2SEnji Cooper };
84928f6c2f2SEnji Cooper
850b89a7cc2SEnji Cooper // A simple Linear Congruential Generator for generating random
851b89a7cc2SEnji Cooper // numbers with a uniform distribution. Unlike rand() and srand(), it
852b89a7cc2SEnji Cooper // doesn't use global state (and therefore can't interfere with user
853b89a7cc2SEnji Cooper // code). Unlike rand_r(), it's portable. An LCG isn't very random,
854b89a7cc2SEnji Cooper // but it's good enough for our purposes.
855b89a7cc2SEnji Cooper class GTEST_API_ Random {
856b89a7cc2SEnji Cooper public:
85728f6c2f2SEnji Cooper static const uint32_t kMaxRange = 1u << 31;
858b89a7cc2SEnji Cooper
Random(uint32_t seed)85928f6c2f2SEnji Cooper explicit Random(uint32_t seed) : state_(seed) {}
860b89a7cc2SEnji Cooper
Reseed(uint32_t seed)86128f6c2f2SEnji Cooper void Reseed(uint32_t seed) { state_ = seed; }
862b89a7cc2SEnji Cooper
863b89a7cc2SEnji Cooper // Generates a random number from [0, range). Crashes if 'range' is
864b89a7cc2SEnji Cooper // 0 or greater than kMaxRange.
86528f6c2f2SEnji Cooper uint32_t Generate(uint32_t range);
866b89a7cc2SEnji Cooper
867b89a7cc2SEnji Cooper private:
86828f6c2f2SEnji Cooper uint32_t state_;
86928f6c2f2SEnji Cooper Random(const Random&) = delete;
87028f6c2f2SEnji Cooper Random& operator=(const Random&) = delete;
871b89a7cc2SEnji Cooper };
872b89a7cc2SEnji Cooper
873b89a7cc2SEnji Cooper // Turns const U&, U&, const U, and U all into U.
874b89a7cc2SEnji Cooper #define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \
87528f6c2f2SEnji Cooper typename std::remove_const<typename std::remove_reference<T>::type>::type
876b89a7cc2SEnji Cooper
87728f6c2f2SEnji Cooper // HasDebugStringAndShortDebugString<T>::value is a compile-time bool constant
87828f6c2f2SEnji Cooper // that's true if and only if T has methods DebugString() and ShortDebugString()
87928f6c2f2SEnji Cooper // that return std::string.
880b89a7cc2SEnji Cooper template <typename T>
88128f6c2f2SEnji Cooper class HasDebugStringAndShortDebugString {
88228f6c2f2SEnji Cooper private:
88328f6c2f2SEnji Cooper template <typename C>
88428f6c2f2SEnji Cooper static auto CheckDebugString(C*) -> typename std::is_same<
88528f6c2f2SEnji Cooper std::string, decltype(std::declval<const C>().DebugString())>::type;
88628f6c2f2SEnji Cooper template <typename>
88728f6c2f2SEnji Cooper static std::false_type CheckDebugString(...);
88828f6c2f2SEnji Cooper
88928f6c2f2SEnji Cooper template <typename C>
89028f6c2f2SEnji Cooper static auto CheckShortDebugString(C*) -> typename std::is_same<
89128f6c2f2SEnji Cooper std::string, decltype(std::declval<const C>().ShortDebugString())>::type;
89228f6c2f2SEnji Cooper template <typename>
89328f6c2f2SEnji Cooper static std::false_type CheckShortDebugString(...);
89428f6c2f2SEnji Cooper
89528f6c2f2SEnji Cooper using HasDebugStringType = decltype(CheckDebugString<T>(nullptr));
89628f6c2f2SEnji Cooper using HasShortDebugStringType = decltype(CheckShortDebugString<T>(nullptr));
89728f6c2f2SEnji Cooper
89828f6c2f2SEnji Cooper public:
89928f6c2f2SEnji Cooper static constexpr bool value =
90028f6c2f2SEnji Cooper HasDebugStringType::value && HasShortDebugStringType::value;
901b89a7cc2SEnji Cooper };
902b89a7cc2SEnji Cooper
90328f6c2f2SEnji Cooper #ifdef GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
90428f6c2f2SEnji Cooper template <typename T>
90528f6c2f2SEnji Cooper constexpr bool HasDebugStringAndShortDebugString<T>::value;
90628f6c2f2SEnji Cooper #endif
90728f6c2f2SEnji Cooper
908b89a7cc2SEnji Cooper // When the compiler sees expression IsContainerTest<C>(0), if C is an
909b89a7cc2SEnji Cooper // STL-style container class, the first overload of IsContainerTest
910b89a7cc2SEnji Cooper // will be viable (since both C::iterator* and C::const_iterator* are
911b89a7cc2SEnji Cooper // valid types and NULL can be implicitly converted to them). It will
912b89a7cc2SEnji Cooper // be picked over the second overload as 'int' is a perfect match for
913b89a7cc2SEnji Cooper // the type of argument 0. If C::iterator or C::const_iterator is not
914b89a7cc2SEnji Cooper // a valid type, the first overload is not viable, and the second
915b89a7cc2SEnji Cooper // overload will be picked. Therefore, we can determine whether C is
916b89a7cc2SEnji Cooper // a container class by checking the type of IsContainerTest<C>(0).
917b89a7cc2SEnji Cooper // The value of the expression is insignificant.
918b89a7cc2SEnji Cooper //
919b89a7cc2SEnji Cooper // In C++11 mode we check the existence of a const_iterator and that an
920b89a7cc2SEnji Cooper // iterator is properly implemented for the container.
921b89a7cc2SEnji Cooper //
922b89a7cc2SEnji Cooper // For pre-C++11 that we look for both C::iterator and C::const_iterator.
923b89a7cc2SEnji Cooper // The reason is that C++ injects the name of a class as a member of the
924b89a7cc2SEnji Cooper // class itself (e.g. you can refer to class iterator as either
925b89a7cc2SEnji Cooper // 'iterator' or 'iterator::iterator'). If we look for C::iterator
926b89a7cc2SEnji Cooper // only, for example, we would mistakenly think that a class named
927b89a7cc2SEnji Cooper // iterator is an STL container.
928b89a7cc2SEnji Cooper //
929b89a7cc2SEnji Cooper // Also note that the simpler approach of overloading
930b89a7cc2SEnji Cooper // IsContainerTest(typename C::const_iterator*) and
931b89a7cc2SEnji Cooper // IsContainerTest(...) doesn't work with Visual Age C++ and Sun C++.
932b89a7cc2SEnji Cooper typedef int IsContainer;
933b89a7cc2SEnji Cooper template <class C,
934b89a7cc2SEnji Cooper class Iterator = decltype(::std::declval<const C&>().begin()),
935b89a7cc2SEnji Cooper class = decltype(::std::declval<const C&>().end()),
936b89a7cc2SEnji Cooper class = decltype(++::std::declval<Iterator&>()),
937b89a7cc2SEnji Cooper class = decltype(*::std::declval<Iterator>()),
938b89a7cc2SEnji Cooper class = typename C::const_iterator>
IsContainerTest(int)939b89a7cc2SEnji Cooper IsContainer IsContainerTest(int /* dummy */) {
940b89a7cc2SEnji Cooper return 0;
941b89a7cc2SEnji Cooper }
942b89a7cc2SEnji Cooper
943b89a7cc2SEnji Cooper typedef char IsNotContainer;
944b89a7cc2SEnji Cooper template <class C>
IsContainerTest(long)94528f6c2f2SEnji Cooper IsNotContainer IsContainerTest(long /* dummy */) {
94628f6c2f2SEnji Cooper return '\0';
94728f6c2f2SEnji Cooper }
948b89a7cc2SEnji Cooper
949b89a7cc2SEnji Cooper // Trait to detect whether a type T is a hash table.
950b89a7cc2SEnji Cooper // The heuristic used is that the type contains an inner type `hasher` and does
951b89a7cc2SEnji Cooper // not contain an inner type `reverse_iterator`.
952b89a7cc2SEnji Cooper // If the container is iterable in reverse, then order might actually matter.
953b89a7cc2SEnji Cooper template <typename T>
954b89a7cc2SEnji Cooper struct IsHashTable {
955b89a7cc2SEnji Cooper private:
956b89a7cc2SEnji Cooper template <typename U>
957b89a7cc2SEnji Cooper static char test(typename U::hasher*, typename U::reverse_iterator*);
958b89a7cc2SEnji Cooper template <typename U>
959b89a7cc2SEnji Cooper static int test(typename U::hasher*, ...);
960b89a7cc2SEnji Cooper template <typename U>
961b89a7cc2SEnji Cooper static char test(...);
962b89a7cc2SEnji Cooper
963b89a7cc2SEnji Cooper public:
96428f6c2f2SEnji Cooper static const bool value = sizeof(test<T>(nullptr, nullptr)) == sizeof(int);
965b89a7cc2SEnji Cooper };
966b89a7cc2SEnji Cooper
967b89a7cc2SEnji Cooper template <typename T>
968b89a7cc2SEnji Cooper const bool IsHashTable<T>::value;
969b89a7cc2SEnji Cooper
970b89a7cc2SEnji Cooper template <typename C,
97128f6c2f2SEnji Cooper bool = sizeof(IsContainerTest<C>(0)) == sizeof(IsContainer)>
972b89a7cc2SEnji Cooper struct IsRecursiveContainerImpl;
973b89a7cc2SEnji Cooper
97428f6c2f2SEnji Cooper template <typename C>
97528f6c2f2SEnji Cooper struct IsRecursiveContainerImpl<C, false> : public std::false_type {};
976b89a7cc2SEnji Cooper
977b89a7cc2SEnji Cooper // Since the IsRecursiveContainerImpl depends on the IsContainerTest we need to
978b89a7cc2SEnji Cooper // obey the same inconsistencies as the IsContainerTest, namely check if
979b89a7cc2SEnji Cooper // something is a container is relying on only const_iterator in C++11 and
980b89a7cc2SEnji Cooper // is relying on both const_iterator and iterator otherwise
981b89a7cc2SEnji Cooper template <typename C>
98228f6c2f2SEnji Cooper struct IsRecursiveContainerImpl<C, true> {
98328f6c2f2SEnji Cooper using value_type = decltype(*std::declval<typename C::const_iterator>());
98428f6c2f2SEnji Cooper using type =
98528f6c2f2SEnji Cooper std::is_same<typename std::remove_const<
98628f6c2f2SEnji Cooper typename std::remove_reference<value_type>::type>::type,
98728f6c2f2SEnji Cooper C>;
988b89a7cc2SEnji Cooper };
989b89a7cc2SEnji Cooper
990b89a7cc2SEnji Cooper // IsRecursiveContainer<Type> is a unary compile-time predicate that
991b89a7cc2SEnji Cooper // evaluates whether C is a recursive container type. A recursive container
992b89a7cc2SEnji Cooper // type is a container type whose value_type is equal to the container type
993b89a7cc2SEnji Cooper // itself. An example for a recursive container type is
994b89a7cc2SEnji Cooper // boost::filesystem::path, whose iterator has a value_type that is equal to
995b89a7cc2SEnji Cooper // boost::filesystem::path.
996b89a7cc2SEnji Cooper template <typename C>
997b89a7cc2SEnji Cooper struct IsRecursiveContainer : public IsRecursiveContainerImpl<C>::type {};
998b89a7cc2SEnji Cooper
999b89a7cc2SEnji Cooper // Utilities for native arrays.
1000b89a7cc2SEnji Cooper
1001b89a7cc2SEnji Cooper // ArrayEq() compares two k-dimensional native arrays using the
1002b89a7cc2SEnji Cooper // elements' operator==, where k can be any integer >= 0. When k is
1003b89a7cc2SEnji Cooper // 0, ArrayEq() degenerates into comparing a single pair of values.
1004b89a7cc2SEnji Cooper
1005b89a7cc2SEnji Cooper template <typename T, typename U>
1006b89a7cc2SEnji Cooper bool ArrayEq(const T* lhs, size_t size, const U* rhs);
1007b89a7cc2SEnji Cooper
1008b89a7cc2SEnji Cooper // This generic version is used when k is 0.
1009b89a7cc2SEnji Cooper template <typename T, typename U>
101028f6c2f2SEnji Cooper inline bool ArrayEq(const T& lhs, const U& rhs) {
101128f6c2f2SEnji Cooper return lhs == rhs;
101228f6c2f2SEnji Cooper }
1013b89a7cc2SEnji Cooper
1014b89a7cc2SEnji Cooper // This overload is used when k >= 1.
1015b89a7cc2SEnji Cooper template <typename T, typename U, size_t N>
1016b89a7cc2SEnji Cooper inline bool ArrayEq(const T (&lhs)[N], const U (&rhs)[N]) {
1017b89a7cc2SEnji Cooper return internal::ArrayEq(lhs, N, rhs);
1018b89a7cc2SEnji Cooper }
1019b89a7cc2SEnji Cooper
1020b89a7cc2SEnji Cooper // This helper reduces code bloat. If we instead put its logic inside
1021b89a7cc2SEnji Cooper // the previous ArrayEq() function, arrays with different sizes would
1022b89a7cc2SEnji Cooper // lead to different copies of the template code.
1023b89a7cc2SEnji Cooper template <typename T, typename U>
1024b89a7cc2SEnji Cooper bool ArrayEq(const T* lhs, size_t size, const U* rhs) {
1025b89a7cc2SEnji Cooper for (size_t i = 0; i != size; i++) {
102628f6c2f2SEnji Cooper if (!internal::ArrayEq(lhs[i], rhs[i])) return false;
1027b89a7cc2SEnji Cooper }
1028b89a7cc2SEnji Cooper return true;
1029b89a7cc2SEnji Cooper }
1030b89a7cc2SEnji Cooper
1031b89a7cc2SEnji Cooper // Finds the first element in the iterator range [begin, end) that
1032b89a7cc2SEnji Cooper // equals elem. Element may be a native array type itself.
1033b89a7cc2SEnji Cooper template <typename Iter, typename Element>
1034b89a7cc2SEnji Cooper Iter ArrayAwareFind(Iter begin, Iter end, const Element& elem) {
1035b89a7cc2SEnji Cooper for (Iter it = begin; it != end; ++it) {
103628f6c2f2SEnji Cooper if (internal::ArrayEq(*it, elem)) return it;
1037b89a7cc2SEnji Cooper }
1038b89a7cc2SEnji Cooper return end;
1039b89a7cc2SEnji Cooper }
1040b89a7cc2SEnji Cooper
1041b89a7cc2SEnji Cooper // CopyArray() copies a k-dimensional native array using the elements'
1042b89a7cc2SEnji Cooper // operator=, where k can be any integer >= 0. When k is 0,
1043b89a7cc2SEnji Cooper // CopyArray() degenerates into copying a single value.
1044b89a7cc2SEnji Cooper
1045b89a7cc2SEnji Cooper template <typename T, typename U>
1046b89a7cc2SEnji Cooper void CopyArray(const T* from, size_t size, U* to);
1047b89a7cc2SEnji Cooper
1048b89a7cc2SEnji Cooper // This generic version is used when k is 0.
1049b89a7cc2SEnji Cooper template <typename T, typename U>
105028f6c2f2SEnji Cooper inline void CopyArray(const T& from, U* to) {
105128f6c2f2SEnji Cooper *to = from;
105228f6c2f2SEnji Cooper }
1053b89a7cc2SEnji Cooper
1054b89a7cc2SEnji Cooper // This overload is used when k >= 1.
1055b89a7cc2SEnji Cooper template <typename T, typename U, size_t N>
1056b89a7cc2SEnji Cooper inline void CopyArray(const T (&from)[N], U (*to)[N]) {
1057b89a7cc2SEnji Cooper internal::CopyArray(from, N, *to);
1058b89a7cc2SEnji Cooper }
1059b89a7cc2SEnji Cooper
1060b89a7cc2SEnji Cooper // This helper reduces code bloat. If we instead put its logic inside
1061b89a7cc2SEnji Cooper // the previous CopyArray() function, arrays with different sizes
1062b89a7cc2SEnji Cooper // would lead to different copies of the template code.
1063b89a7cc2SEnji Cooper template <typename T, typename U>
1064b89a7cc2SEnji Cooper void CopyArray(const T* from, size_t size, U* to) {
1065b89a7cc2SEnji Cooper for (size_t i = 0; i != size; i++) {
1066b89a7cc2SEnji Cooper internal::CopyArray(from[i], to + i);
1067b89a7cc2SEnji Cooper }
1068b89a7cc2SEnji Cooper }
1069b89a7cc2SEnji Cooper
1070b89a7cc2SEnji Cooper // The relation between an NativeArray object (see below) and the
1071b89a7cc2SEnji Cooper // native array it represents.
1072b89a7cc2SEnji Cooper // We use 2 different structs to allow non-copyable types to be used, as long
1073b89a7cc2SEnji Cooper // as RelationToSourceReference() is passed.
1074b89a7cc2SEnji Cooper struct RelationToSourceReference {};
1075b89a7cc2SEnji Cooper struct RelationToSourceCopy {};
1076b89a7cc2SEnji Cooper
1077b89a7cc2SEnji Cooper // Adapts a native array to a read-only STL-style container. Instead
1078b89a7cc2SEnji Cooper // of the complete STL container concept, this adaptor only implements
1079b89a7cc2SEnji Cooper // members useful for Google Mock's container matchers. New members
1080b89a7cc2SEnji Cooper // should be added as needed. To simplify the implementation, we only
1081b89a7cc2SEnji Cooper // support Element being a raw type (i.e. having no top-level const or
1082b89a7cc2SEnji Cooper // reference modifier). It's the client's responsibility to satisfy
1083b89a7cc2SEnji Cooper // this requirement. Element can be an array type itself (hence
1084b89a7cc2SEnji Cooper // multi-dimensional arrays are supported).
1085b89a7cc2SEnji Cooper template <typename Element>
1086b89a7cc2SEnji Cooper class NativeArray {
1087b89a7cc2SEnji Cooper public:
1088b89a7cc2SEnji Cooper // STL-style container typedefs.
1089b89a7cc2SEnji Cooper typedef Element value_type;
1090b89a7cc2SEnji Cooper typedef Element* iterator;
1091b89a7cc2SEnji Cooper typedef const Element* const_iterator;
1092b89a7cc2SEnji Cooper
1093b89a7cc2SEnji Cooper // Constructs from a native array. References the source.
1094b89a7cc2SEnji Cooper NativeArray(const Element* array, size_t count, RelationToSourceReference) {
1095b89a7cc2SEnji Cooper InitRef(array, count);
1096b89a7cc2SEnji Cooper }
1097b89a7cc2SEnji Cooper
1098b89a7cc2SEnji Cooper // Constructs from a native array. Copies the source.
1099b89a7cc2SEnji Cooper NativeArray(const Element* array, size_t count, RelationToSourceCopy) {
1100b89a7cc2SEnji Cooper InitCopy(array, count);
1101b89a7cc2SEnji Cooper }
1102b89a7cc2SEnji Cooper
1103b89a7cc2SEnji Cooper // Copy constructor.
1104b89a7cc2SEnji Cooper NativeArray(const NativeArray& rhs) {
1105b89a7cc2SEnji Cooper (this->*rhs.clone_)(rhs.array_, rhs.size_);
1106b89a7cc2SEnji Cooper }
1107b89a7cc2SEnji Cooper
1108b89a7cc2SEnji Cooper ~NativeArray() {
110928f6c2f2SEnji Cooper if (clone_ != &NativeArray::InitRef) delete[] array_;
1110b89a7cc2SEnji Cooper }
1111b89a7cc2SEnji Cooper
1112b89a7cc2SEnji Cooper // STL-style container methods.
1113b89a7cc2SEnji Cooper size_t size() const { return size_; }
1114b89a7cc2SEnji Cooper const_iterator begin() const { return array_; }
1115b89a7cc2SEnji Cooper const_iterator end() const { return array_ + size_; }
1116b89a7cc2SEnji Cooper bool operator==(const NativeArray& rhs) const {
111728f6c2f2SEnji Cooper return size() == rhs.size() && ArrayEq(begin(), size(), rhs.begin());
1118b89a7cc2SEnji Cooper }
1119b89a7cc2SEnji Cooper
1120b89a7cc2SEnji Cooper private:
112128f6c2f2SEnji Cooper static_assert(!std::is_const<Element>::value, "Type must not be const");
112228f6c2f2SEnji Cooper static_assert(!std::is_reference<Element>::value,
112328f6c2f2SEnji Cooper "Type must not be a reference");
1124b89a7cc2SEnji Cooper
1125b89a7cc2SEnji Cooper // Initializes this object with a copy of the input.
1126b89a7cc2SEnji Cooper void InitCopy(const Element* array, size_t a_size) {
1127b89a7cc2SEnji Cooper Element* const copy = new Element[a_size];
1128b89a7cc2SEnji Cooper CopyArray(array, a_size, copy);
1129b89a7cc2SEnji Cooper array_ = copy;
1130b89a7cc2SEnji Cooper size_ = a_size;
1131b89a7cc2SEnji Cooper clone_ = &NativeArray::InitCopy;
1132b89a7cc2SEnji Cooper }
1133b89a7cc2SEnji Cooper
1134b89a7cc2SEnji Cooper // Initializes this object with a reference of the input.
1135b89a7cc2SEnji Cooper void InitRef(const Element* array, size_t a_size) {
1136b89a7cc2SEnji Cooper array_ = array;
1137b89a7cc2SEnji Cooper size_ = a_size;
1138b89a7cc2SEnji Cooper clone_ = &NativeArray::InitRef;
1139b89a7cc2SEnji Cooper }
1140b89a7cc2SEnji Cooper
1141b89a7cc2SEnji Cooper const Element* array_;
1142b89a7cc2SEnji Cooper size_t size_;
1143b89a7cc2SEnji Cooper void (NativeArray::*clone_)(const Element*, size_t);
1144b89a7cc2SEnji Cooper };
1145b89a7cc2SEnji Cooper
114628f6c2f2SEnji Cooper // Backport of std::index_sequence.
114728f6c2f2SEnji Cooper template <size_t... Is>
114828f6c2f2SEnji Cooper struct IndexSequence {
114928f6c2f2SEnji Cooper using type = IndexSequence;
115028f6c2f2SEnji Cooper };
115128f6c2f2SEnji Cooper
115228f6c2f2SEnji Cooper // Double the IndexSequence, and one if plus_one is true.
115328f6c2f2SEnji Cooper template <bool plus_one, typename T, size_t sizeofT>
115428f6c2f2SEnji Cooper struct DoubleSequence;
115528f6c2f2SEnji Cooper template <size_t... I, size_t sizeofT>
115628f6c2f2SEnji Cooper struct DoubleSequence<true, IndexSequence<I...>, sizeofT> {
115728f6c2f2SEnji Cooper using type = IndexSequence<I..., (sizeofT + I)..., 2 * sizeofT>;
115828f6c2f2SEnji Cooper };
115928f6c2f2SEnji Cooper template <size_t... I, size_t sizeofT>
116028f6c2f2SEnji Cooper struct DoubleSequence<false, IndexSequence<I...>, sizeofT> {
116128f6c2f2SEnji Cooper using type = IndexSequence<I..., (sizeofT + I)...>;
116228f6c2f2SEnji Cooper };
116328f6c2f2SEnji Cooper
116428f6c2f2SEnji Cooper // Backport of std::make_index_sequence.
116528f6c2f2SEnji Cooper // It uses O(ln(N)) instantiation depth.
116628f6c2f2SEnji Cooper template <size_t N>
116728f6c2f2SEnji Cooper struct MakeIndexSequenceImpl
116828f6c2f2SEnji Cooper : DoubleSequence<N % 2 == 1, typename MakeIndexSequenceImpl<N / 2>::type,
116928f6c2f2SEnji Cooper N / 2>::type {};
117028f6c2f2SEnji Cooper
117128f6c2f2SEnji Cooper template <>
117228f6c2f2SEnji Cooper struct MakeIndexSequenceImpl<0> : IndexSequence<> {};
117328f6c2f2SEnji Cooper
117428f6c2f2SEnji Cooper template <size_t N>
117528f6c2f2SEnji Cooper using MakeIndexSequence = typename MakeIndexSequenceImpl<N>::type;
117628f6c2f2SEnji Cooper
117728f6c2f2SEnji Cooper template <typename... T>
117828f6c2f2SEnji Cooper using IndexSequenceFor = typename MakeIndexSequence<sizeof...(T)>::type;
117928f6c2f2SEnji Cooper
118028f6c2f2SEnji Cooper template <size_t>
118128f6c2f2SEnji Cooper struct Ignore {
118228f6c2f2SEnji Cooper Ignore(...); // NOLINT
118328f6c2f2SEnji Cooper };
118428f6c2f2SEnji Cooper
118528f6c2f2SEnji Cooper template <typename>
118628f6c2f2SEnji Cooper struct ElemFromListImpl;
118728f6c2f2SEnji Cooper template <size_t... I>
118828f6c2f2SEnji Cooper struct ElemFromListImpl<IndexSequence<I...>> {
118928f6c2f2SEnji Cooper // We make Ignore a template to solve a problem with MSVC.
119028f6c2f2SEnji Cooper // A non-template Ignore would work fine with `decltype(Ignore(I))...`, but
119128f6c2f2SEnji Cooper // MSVC doesn't understand how to deal with that pack expansion.
119228f6c2f2SEnji Cooper // Use `0 * I` to have a single instantiation of Ignore.
119328f6c2f2SEnji Cooper template <typename R>
119428f6c2f2SEnji Cooper static R Apply(Ignore<0 * I>..., R (*)(), ...);
119528f6c2f2SEnji Cooper };
119628f6c2f2SEnji Cooper
119728f6c2f2SEnji Cooper template <size_t N, typename... T>
119828f6c2f2SEnji Cooper struct ElemFromList {
119928f6c2f2SEnji Cooper using type =
120028f6c2f2SEnji Cooper decltype(ElemFromListImpl<typename MakeIndexSequence<N>::type>::Apply(
120128f6c2f2SEnji Cooper static_cast<T (*)()>(nullptr)...));
120228f6c2f2SEnji Cooper };
120328f6c2f2SEnji Cooper
120428f6c2f2SEnji Cooper struct FlatTupleConstructTag {};
120528f6c2f2SEnji Cooper
120628f6c2f2SEnji Cooper template <typename... T>
120728f6c2f2SEnji Cooper class FlatTuple;
120828f6c2f2SEnji Cooper
120928f6c2f2SEnji Cooper template <typename Derived, size_t I>
121028f6c2f2SEnji Cooper struct FlatTupleElemBase;
121128f6c2f2SEnji Cooper
121228f6c2f2SEnji Cooper template <typename... T, size_t I>
121328f6c2f2SEnji Cooper struct FlatTupleElemBase<FlatTuple<T...>, I> {
121428f6c2f2SEnji Cooper using value_type = typename ElemFromList<I, T...>::type;
121528f6c2f2SEnji Cooper FlatTupleElemBase() = default;
121628f6c2f2SEnji Cooper template <typename Arg>
121728f6c2f2SEnji Cooper explicit FlatTupleElemBase(FlatTupleConstructTag, Arg&& t)
121828f6c2f2SEnji Cooper : value(std::forward<Arg>(t)) {}
121928f6c2f2SEnji Cooper value_type value;
122028f6c2f2SEnji Cooper };
122128f6c2f2SEnji Cooper
122228f6c2f2SEnji Cooper template <typename Derived, typename Idx>
122328f6c2f2SEnji Cooper struct FlatTupleBase;
122428f6c2f2SEnji Cooper
122528f6c2f2SEnji Cooper template <size_t... Idx, typename... T>
122628f6c2f2SEnji Cooper struct FlatTupleBase<FlatTuple<T...>, IndexSequence<Idx...>>
122728f6c2f2SEnji Cooper : FlatTupleElemBase<FlatTuple<T...>, Idx>... {
122828f6c2f2SEnji Cooper using Indices = IndexSequence<Idx...>;
122928f6c2f2SEnji Cooper FlatTupleBase() = default;
123028f6c2f2SEnji Cooper template <typename... Args>
123128f6c2f2SEnji Cooper explicit FlatTupleBase(FlatTupleConstructTag, Args&&... args)
123228f6c2f2SEnji Cooper : FlatTupleElemBase<FlatTuple<T...>, Idx>(FlatTupleConstructTag{},
123328f6c2f2SEnji Cooper std::forward<Args>(args))... {}
123428f6c2f2SEnji Cooper
123528f6c2f2SEnji Cooper template <size_t I>
123628f6c2f2SEnji Cooper const typename ElemFromList<I, T...>::type& Get() const {
123728f6c2f2SEnji Cooper return FlatTupleElemBase<FlatTuple<T...>, I>::value;
123828f6c2f2SEnji Cooper }
123928f6c2f2SEnji Cooper
124028f6c2f2SEnji Cooper template <size_t I>
124128f6c2f2SEnji Cooper typename ElemFromList<I, T...>::type& Get() {
124228f6c2f2SEnji Cooper return FlatTupleElemBase<FlatTuple<T...>, I>::value;
124328f6c2f2SEnji Cooper }
124428f6c2f2SEnji Cooper
124528f6c2f2SEnji Cooper template <typename F>
124628f6c2f2SEnji Cooper auto Apply(F&& f) -> decltype(std::forward<F>(f)(this->Get<Idx>()...)) {
124728f6c2f2SEnji Cooper return std::forward<F>(f)(Get<Idx>()...);
124828f6c2f2SEnji Cooper }
124928f6c2f2SEnji Cooper
125028f6c2f2SEnji Cooper template <typename F>
125128f6c2f2SEnji Cooper auto Apply(F&& f) const -> decltype(std::forward<F>(f)(this->Get<Idx>()...)) {
125228f6c2f2SEnji Cooper return std::forward<F>(f)(Get<Idx>()...);
125328f6c2f2SEnji Cooper }
125428f6c2f2SEnji Cooper };
125528f6c2f2SEnji Cooper
125628f6c2f2SEnji Cooper // Analog to std::tuple but with different tradeoffs.
125728f6c2f2SEnji Cooper // This class minimizes the template instantiation depth, thus allowing more
125828f6c2f2SEnji Cooper // elements than std::tuple would. std::tuple has been seen to require an
125928f6c2f2SEnji Cooper // instantiation depth of more than 10x the number of elements in some
126028f6c2f2SEnji Cooper // implementations.
126128f6c2f2SEnji Cooper // FlatTuple and ElemFromList are not recursive and have a fixed depth
126228f6c2f2SEnji Cooper // regardless of T...
126328f6c2f2SEnji Cooper // MakeIndexSequence, on the other hand, it is recursive but with an
126428f6c2f2SEnji Cooper // instantiation depth of O(ln(N)).
126528f6c2f2SEnji Cooper template <typename... T>
126628f6c2f2SEnji Cooper class FlatTuple
126728f6c2f2SEnji Cooper : private FlatTupleBase<FlatTuple<T...>,
126828f6c2f2SEnji Cooper typename MakeIndexSequence<sizeof...(T)>::type> {
126928f6c2f2SEnji Cooper using Indices = typename FlatTupleBase<
127028f6c2f2SEnji Cooper FlatTuple<T...>, typename MakeIndexSequence<sizeof...(T)>::type>::Indices;
127128f6c2f2SEnji Cooper
127228f6c2f2SEnji Cooper public:
127328f6c2f2SEnji Cooper FlatTuple() = default;
127428f6c2f2SEnji Cooper template <typename... Args>
127528f6c2f2SEnji Cooper explicit FlatTuple(FlatTupleConstructTag tag, Args&&... args)
127628f6c2f2SEnji Cooper : FlatTuple::FlatTupleBase(tag, std::forward<Args>(args)...) {}
127728f6c2f2SEnji Cooper
127828f6c2f2SEnji Cooper using FlatTuple::FlatTupleBase::Apply;
127928f6c2f2SEnji Cooper using FlatTuple::FlatTupleBase::Get;
128028f6c2f2SEnji Cooper };
128128f6c2f2SEnji Cooper
128228f6c2f2SEnji Cooper // Utility functions to be called with static_assert to induce deprecation
128328f6c2f2SEnji Cooper // warnings.
128428f6c2f2SEnji Cooper GTEST_INTERNAL_DEPRECATED(
128528f6c2f2SEnji Cooper "INSTANTIATE_TEST_CASE_P is deprecated, please use "
128628f6c2f2SEnji Cooper "INSTANTIATE_TEST_SUITE_P")
128728f6c2f2SEnji Cooper constexpr bool InstantiateTestCase_P_IsDeprecated() { return true; }
128828f6c2f2SEnji Cooper
128928f6c2f2SEnji Cooper GTEST_INTERNAL_DEPRECATED(
129028f6c2f2SEnji Cooper "TYPED_TEST_CASE_P is deprecated, please use "
129128f6c2f2SEnji Cooper "TYPED_TEST_SUITE_P")
129228f6c2f2SEnji Cooper constexpr bool TypedTestCase_P_IsDeprecated() { return true; }
129328f6c2f2SEnji Cooper
129428f6c2f2SEnji Cooper GTEST_INTERNAL_DEPRECATED(
129528f6c2f2SEnji Cooper "TYPED_TEST_CASE is deprecated, please use "
129628f6c2f2SEnji Cooper "TYPED_TEST_SUITE")
129728f6c2f2SEnji Cooper constexpr bool TypedTestCaseIsDeprecated() { return true; }
129828f6c2f2SEnji Cooper
129928f6c2f2SEnji Cooper GTEST_INTERNAL_DEPRECATED(
130028f6c2f2SEnji Cooper "REGISTER_TYPED_TEST_CASE_P is deprecated, please use "
130128f6c2f2SEnji Cooper "REGISTER_TYPED_TEST_SUITE_P")
130228f6c2f2SEnji Cooper constexpr bool RegisterTypedTestCase_P_IsDeprecated() { return true; }
130328f6c2f2SEnji Cooper
130428f6c2f2SEnji Cooper GTEST_INTERNAL_DEPRECATED(
130528f6c2f2SEnji Cooper "INSTANTIATE_TYPED_TEST_CASE_P is deprecated, please use "
130628f6c2f2SEnji Cooper "INSTANTIATE_TYPED_TEST_SUITE_P")
130728f6c2f2SEnji Cooper constexpr bool InstantiateTypedTestCase_P_IsDeprecated() { return true; }
130828f6c2f2SEnji Cooper
1309b89a7cc2SEnji Cooper } // namespace internal
1310b89a7cc2SEnji Cooper } // namespace testing
1311b89a7cc2SEnji Cooper
131228f6c2f2SEnji Cooper namespace std {
131328f6c2f2SEnji Cooper // Some standard library implementations use `struct tuple_size` and some use
131428f6c2f2SEnji Cooper // `class tuple_size`. Clang warns about the mismatch.
131528f6c2f2SEnji Cooper // https://reviews.llvm.org/D55466
131628f6c2f2SEnji Cooper #ifdef __clang__
131728f6c2f2SEnji Cooper #pragma clang diagnostic push
131828f6c2f2SEnji Cooper #pragma clang diagnostic ignored "-Wmismatched-tags"
131928f6c2f2SEnji Cooper #endif
132028f6c2f2SEnji Cooper template <typename... Ts>
132128f6c2f2SEnji Cooper struct tuple_size<testing::internal::FlatTuple<Ts...>>
132228f6c2f2SEnji Cooper : std::integral_constant<size_t, sizeof...(Ts)> {};
132328f6c2f2SEnji Cooper #ifdef __clang__
132428f6c2f2SEnji Cooper #pragma clang diagnostic pop
132528f6c2f2SEnji Cooper #endif
132628f6c2f2SEnji Cooper } // namespace std
132728f6c2f2SEnji Cooper
1328b89a7cc2SEnji Cooper #define GTEST_MESSAGE_AT_(file, line, message, result_type) \
132928f6c2f2SEnji Cooper ::testing::internal::AssertHelper(result_type, file, line, message) = \
133028f6c2f2SEnji Cooper ::testing::Message()
1331b89a7cc2SEnji Cooper
1332b89a7cc2SEnji Cooper #define GTEST_MESSAGE_(message, result_type) \
1333b89a7cc2SEnji Cooper GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type)
1334b89a7cc2SEnji Cooper
1335b89a7cc2SEnji Cooper #define GTEST_FATAL_FAILURE_(message) \
1336b89a7cc2SEnji Cooper return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure)
1337b89a7cc2SEnji Cooper
1338b89a7cc2SEnji Cooper #define GTEST_NONFATAL_FAILURE_(message) \
1339b89a7cc2SEnji Cooper GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure)
1340b89a7cc2SEnji Cooper
1341b89a7cc2SEnji Cooper #define GTEST_SUCCESS_(message) \
1342b89a7cc2SEnji Cooper GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess)
1343b89a7cc2SEnji Cooper
1344da1a9eb0SAlan Somers #define GTEST_SKIP_(message) \
1345da1a9eb0SAlan Somers return GTEST_MESSAGE_(message, ::testing::TestPartResult::kSkip)
1346da1a9eb0SAlan Somers
1347da1a9eb0SAlan Somers // Suppress MSVC warning 4072 (unreachable code) for the code following
1348b89a7cc2SEnji Cooper // statement if it returns or throws (or doesn't return or throw in some
1349b89a7cc2SEnji Cooper // situations).
135028f6c2f2SEnji Cooper // NOTE: The "else" is important to keep this expansion to prevent a top-level
135128f6c2f2SEnji Cooper // "else" from attaching to our "if".
1352b89a7cc2SEnji Cooper #define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \
135328f6c2f2SEnji Cooper if (::testing::internal::AlwaysTrue()) { \
135428f6c2f2SEnji Cooper statement; \
135528f6c2f2SEnji Cooper } else /* NOLINT */ \
135628f6c2f2SEnji Cooper static_assert(true, "") // User must have a semicolon after expansion.
135728f6c2f2SEnji Cooper
135828f6c2f2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
135928f6c2f2SEnji Cooper
136028f6c2f2SEnji Cooper namespace testing {
136128f6c2f2SEnji Cooper namespace internal {
136228f6c2f2SEnji Cooper
136328f6c2f2SEnji Cooper class NeverThrown {
136428f6c2f2SEnji Cooper public:
136528f6c2f2SEnji Cooper const char* what() const noexcept {
136628f6c2f2SEnji Cooper return "this exception should never be thrown";
136728f6c2f2SEnji Cooper }
136828f6c2f2SEnji Cooper };
136928f6c2f2SEnji Cooper
137028f6c2f2SEnji Cooper } // namespace internal
137128f6c2f2SEnji Cooper } // namespace testing
137228f6c2f2SEnji Cooper
137328f6c2f2SEnji Cooper #if GTEST_HAS_RTTI
137428f6c2f2SEnji Cooper
137528f6c2f2SEnji Cooper #define GTEST_EXCEPTION_TYPE_(e) ::testing::internal::GetTypeName(typeid(e))
137628f6c2f2SEnji Cooper
137728f6c2f2SEnji Cooper #else // GTEST_HAS_RTTI
137828f6c2f2SEnji Cooper
137928f6c2f2SEnji Cooper #define GTEST_EXCEPTION_TYPE_(e) \
138028f6c2f2SEnji Cooper std::string { "an std::exception-derived error" }
138128f6c2f2SEnji Cooper
138228f6c2f2SEnji Cooper #endif // GTEST_HAS_RTTI
138328f6c2f2SEnji Cooper
138428f6c2f2SEnji Cooper #define GTEST_TEST_THROW_CATCH_STD_EXCEPTION_(statement, expected_exception) \
138528f6c2f2SEnji Cooper catch (typename std::conditional< \
138628f6c2f2SEnji Cooper std::is_same<typename std::remove_cv<typename std::remove_reference< \
138728f6c2f2SEnji Cooper expected_exception>::type>::type, \
138828f6c2f2SEnji Cooper std::exception>::value, \
138928f6c2f2SEnji Cooper const ::testing::internal::NeverThrown&, const std::exception&>::type \
139028f6c2f2SEnji Cooper e) { \
139128f6c2f2SEnji Cooper gtest_msg.value = "Expected: " #statement \
139228f6c2f2SEnji Cooper " throws an exception of type " #expected_exception \
139328f6c2f2SEnji Cooper ".\n Actual: it throws "; \
139428f6c2f2SEnji Cooper gtest_msg.value += GTEST_EXCEPTION_TYPE_(e); \
139528f6c2f2SEnji Cooper gtest_msg.value += " with description \""; \
139628f6c2f2SEnji Cooper gtest_msg.value += e.what(); \
139728f6c2f2SEnji Cooper gtest_msg.value += "\"."; \
139828f6c2f2SEnji Cooper goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
139928f6c2f2SEnji Cooper }
140028f6c2f2SEnji Cooper
140128f6c2f2SEnji Cooper #else // GTEST_HAS_EXCEPTIONS
140228f6c2f2SEnji Cooper
140328f6c2f2SEnji Cooper #define GTEST_TEST_THROW_CATCH_STD_EXCEPTION_(statement, expected_exception)
140428f6c2f2SEnji Cooper
140528f6c2f2SEnji Cooper #endif // GTEST_HAS_EXCEPTIONS
1406b89a7cc2SEnji Cooper
1407b89a7cc2SEnji Cooper #define GTEST_TEST_THROW_(statement, expected_exception, fail) \
1408b89a7cc2SEnji Cooper GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
140928f6c2f2SEnji Cooper if (::testing::internal::TrueWithString gtest_msg{}) { \
1410b89a7cc2SEnji Cooper bool gtest_caught_expected = false; \
1411b89a7cc2SEnji Cooper try { \
1412b89a7cc2SEnji Cooper GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
141328f6c2f2SEnji Cooper } catch (expected_exception const&) { \
1414b89a7cc2SEnji Cooper gtest_caught_expected = true; \
1415b89a7cc2SEnji Cooper } \
141628f6c2f2SEnji Cooper GTEST_TEST_THROW_CATCH_STD_EXCEPTION_(statement, expected_exception) \
1417b89a7cc2SEnji Cooper catch (...) { \
141828f6c2f2SEnji Cooper gtest_msg.value = "Expected: " #statement \
141928f6c2f2SEnji Cooper " throws an exception of type " #expected_exception \
142028f6c2f2SEnji Cooper ".\n Actual: it throws a different type."; \
1421b89a7cc2SEnji Cooper goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
1422b89a7cc2SEnji Cooper } \
1423b89a7cc2SEnji Cooper if (!gtest_caught_expected) { \
142428f6c2f2SEnji Cooper gtest_msg.value = "Expected: " #statement \
142528f6c2f2SEnji Cooper " throws an exception of type " #expected_exception \
142628f6c2f2SEnji Cooper ".\n Actual: it throws nothing."; \
1427b89a7cc2SEnji Cooper goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
1428b89a7cc2SEnji Cooper } \
142928f6c2f2SEnji Cooper } else /*NOLINT*/ \
143028f6c2f2SEnji Cooper GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__) \
143128f6c2f2SEnji Cooper : fail(gtest_msg.value.c_str())
143228f6c2f2SEnji Cooper
143328f6c2f2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
143428f6c2f2SEnji Cooper
143528f6c2f2SEnji Cooper #define GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_() \
143628f6c2f2SEnji Cooper catch (std::exception const& e) { \
143728f6c2f2SEnji Cooper gtest_msg.value = "it throws "; \
143828f6c2f2SEnji Cooper gtest_msg.value += GTEST_EXCEPTION_TYPE_(e); \
143928f6c2f2SEnji Cooper gtest_msg.value += " with description \""; \
144028f6c2f2SEnji Cooper gtest_msg.value += e.what(); \
144128f6c2f2SEnji Cooper gtest_msg.value += "\"."; \
144228f6c2f2SEnji Cooper goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \
144328f6c2f2SEnji Cooper }
144428f6c2f2SEnji Cooper
144528f6c2f2SEnji Cooper #else // GTEST_HAS_EXCEPTIONS
144628f6c2f2SEnji Cooper
144728f6c2f2SEnji Cooper #define GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_()
144828f6c2f2SEnji Cooper
144928f6c2f2SEnji Cooper #endif // GTEST_HAS_EXCEPTIONS
1450b89a7cc2SEnji Cooper
1451b89a7cc2SEnji Cooper #define GTEST_TEST_NO_THROW_(statement, fail) \
1452b89a7cc2SEnji Cooper GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
145328f6c2f2SEnji Cooper if (::testing::internal::TrueWithString gtest_msg{}) { \
1454b89a7cc2SEnji Cooper try { \
1455b89a7cc2SEnji Cooper GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
1456b89a7cc2SEnji Cooper } \
145728f6c2f2SEnji Cooper GTEST_TEST_NO_THROW_CATCH_STD_EXCEPTION_() \
1458b89a7cc2SEnji Cooper catch (...) { \
145928f6c2f2SEnji Cooper gtest_msg.value = "it throws."; \
1460b89a7cc2SEnji Cooper goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \
1461b89a7cc2SEnji Cooper } \
1462b89a7cc2SEnji Cooper } else \
146328f6c2f2SEnji Cooper GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__) \
146428f6c2f2SEnji Cooper : fail(("Expected: " #statement " doesn't throw an exception.\n" \
146528f6c2f2SEnji Cooper " Actual: " + \
146628f6c2f2SEnji Cooper gtest_msg.value) \
146728f6c2f2SEnji Cooper .c_str())
1468b89a7cc2SEnji Cooper
1469b89a7cc2SEnji Cooper #define GTEST_TEST_ANY_THROW_(statement, fail) \
1470b89a7cc2SEnji Cooper GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
1471b89a7cc2SEnji Cooper if (::testing::internal::AlwaysTrue()) { \
1472b89a7cc2SEnji Cooper bool gtest_caught_any = false; \
1473b89a7cc2SEnji Cooper try { \
1474b89a7cc2SEnji Cooper GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
147528f6c2f2SEnji Cooper } catch (...) { \
1476b89a7cc2SEnji Cooper gtest_caught_any = true; \
1477b89a7cc2SEnji Cooper } \
1478b89a7cc2SEnji Cooper if (!gtest_caught_any) { \
1479b89a7cc2SEnji Cooper goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \
1480b89a7cc2SEnji Cooper } \
1481b89a7cc2SEnji Cooper } else \
148228f6c2f2SEnji Cooper GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__) \
148328f6c2f2SEnji Cooper : fail("Expected: " #statement \
148428f6c2f2SEnji Cooper " throws an exception.\n" \
1485b89a7cc2SEnji Cooper " Actual: it doesn't.")
1486b89a7cc2SEnji Cooper
1487b89a7cc2SEnji Cooper // Implements Boolean test assertions such as EXPECT_TRUE. expression can be
1488b89a7cc2SEnji Cooper // either a boolean expression or an AssertionResult. text is a textual
148928f6c2f2SEnji Cooper // representation of expression as it was passed into the EXPECT_TRUE.
1490b89a7cc2SEnji Cooper #define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \
1491b89a7cc2SEnji Cooper GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
1492b89a7cc2SEnji Cooper if (const ::testing::AssertionResult gtest_ar_ = \
1493b89a7cc2SEnji Cooper ::testing::AssertionResult(expression)) \
1494b89a7cc2SEnji Cooper ; \
1495b89a7cc2SEnji Cooper else \
1496b89a7cc2SEnji Cooper fail(::testing::internal::GetBoolAssertionFailureMessage( \
149728f6c2f2SEnji Cooper gtest_ar_, text, #actual, #expected) \
149828f6c2f2SEnji Cooper .c_str())
1499b89a7cc2SEnji Cooper
1500b89a7cc2SEnji Cooper #define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \
1501b89a7cc2SEnji Cooper GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
1502b89a7cc2SEnji Cooper if (::testing::internal::AlwaysTrue()) { \
150328f6c2f2SEnji Cooper const ::testing::internal::HasNewFatalFailureHelper \
150428f6c2f2SEnji Cooper gtest_fatal_failure_checker; \
1505b89a7cc2SEnji Cooper GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
1506b89a7cc2SEnji Cooper if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \
1507b89a7cc2SEnji Cooper goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \
1508b89a7cc2SEnji Cooper } \
150928f6c2f2SEnji Cooper } else /* NOLINT */ \
151028f6c2f2SEnji Cooper GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__) \
151128f6c2f2SEnji Cooper : fail("Expected: " #statement \
151228f6c2f2SEnji Cooper " doesn't generate new fatal " \
1513b89a7cc2SEnji Cooper "failures in the current thread.\n" \
1514b89a7cc2SEnji Cooper " Actual: it does.")
1515b89a7cc2SEnji Cooper
1516b89a7cc2SEnji Cooper // Expands to the name of the class that implements the given test.
151728f6c2f2SEnji Cooper #define GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
151828f6c2f2SEnji Cooper test_suite_name##_##test_name##_Test
1519b89a7cc2SEnji Cooper
1520b89a7cc2SEnji Cooper // Helper macro for defining tests.
152128f6c2f2SEnji Cooper #define GTEST_TEST_(test_suite_name, test_name, parent_class, parent_id) \
152228f6c2f2SEnji Cooper static_assert(sizeof(GTEST_STRINGIFY_(test_suite_name)) > 1, \
152328f6c2f2SEnji Cooper "test_suite_name must not be empty"); \
152428f6c2f2SEnji Cooper static_assert(sizeof(GTEST_STRINGIFY_(test_name)) > 1, \
152528f6c2f2SEnji Cooper "test_name must not be empty"); \
152628f6c2f2SEnji Cooper class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
152728f6c2f2SEnji Cooper : public parent_class { \
1528b89a7cc2SEnji Cooper public: \
152928f6c2f2SEnji Cooper GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() = default; \
153028f6c2f2SEnji Cooper ~GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() override = default; \
153128f6c2f2SEnji Cooper GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
153228f6c2f2SEnji Cooper (const GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) &) = delete; \
153328f6c2f2SEnji Cooper GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) & operator=( \
153428f6c2f2SEnji Cooper const GTEST_TEST_CLASS_NAME_(test_suite_name, \
153528f6c2f2SEnji Cooper test_name) &) = delete; /* NOLINT */ \
153628f6c2f2SEnji Cooper GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \
153728f6c2f2SEnji Cooper (GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) &&) noexcept = delete; \
153828f6c2f2SEnji Cooper GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) & operator=( \
153928f6c2f2SEnji Cooper GTEST_TEST_CLASS_NAME_(test_suite_name, \
154028f6c2f2SEnji Cooper test_name) &&) noexcept = delete; /* NOLINT */ \
154128f6c2f2SEnji Cooper \
1542b89a7cc2SEnji Cooper private: \
154328f6c2f2SEnji Cooper void TestBody() override; \
1544b89a7cc2SEnji Cooper static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_; \
1545b89a7cc2SEnji Cooper }; \
1546b89a7cc2SEnji Cooper \
154728f6c2f2SEnji Cooper ::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_suite_name, \
154828f6c2f2SEnji Cooper test_name)::test_info_ = \
1549b89a7cc2SEnji Cooper ::testing::internal::MakeAndRegisterTestInfo( \
155028f6c2f2SEnji Cooper #test_suite_name, #test_name, nullptr, nullptr, \
155128f6c2f2SEnji Cooper ::testing::internal::CodeLocation(__FILE__, __LINE__), (parent_id), \
155228f6c2f2SEnji Cooper ::testing::internal::SuiteApiResolver< \
155328f6c2f2SEnji Cooper parent_class>::GetSetUpCaseOrSuite(__FILE__, __LINE__), \
155428f6c2f2SEnji Cooper ::testing::internal::SuiteApiResolver< \
155528f6c2f2SEnji Cooper parent_class>::GetTearDownCaseOrSuite(__FILE__, __LINE__), \
155628f6c2f2SEnji Cooper new ::testing::internal::TestFactoryImpl<GTEST_TEST_CLASS_NAME_( \
155728f6c2f2SEnji Cooper test_suite_name, test_name)>); \
155828f6c2f2SEnji Cooper void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody()
1559b89a7cc2SEnji Cooper
156028f6c2f2SEnji Cooper #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
1561