1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2005, Google Inc.
2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// All rights reserved.
3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without
5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are
6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met:
7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions of source code must retain the above copyright
9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer.
10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions in binary form must reproduce the above
11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer
12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the
13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution.
14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Neither the name of Google Inc. nor the names of its
15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from
16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission.
17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The Google C++ Testing Framework (Google Test)
33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This header file declares functions and macros used internally by
35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Google Test.  They are subject to change without notice.
36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/internal/gtest-port.h>
41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if GTEST_OS_LINUX
43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <stdlib.h>
44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <sys/types.h>
45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <sys/wait.h>
46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <unistd.h>
47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_OS_LINUX
48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <ctype.h>
50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <string.h>
51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <iomanip>
52fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <limits>
53fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <set>
54fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
55fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/internal/gtest-string.h>
56fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/internal/gtest-filepath.h>
57fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/internal/gtest-type-util.h>
58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Due to C++ preprocessor weirdness, we need double indirection to
60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// concatenate two tokens when one of them is __LINE__.  Writing
61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   foo ## __LINE__
63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// will result in the token foo__LINE__, instead of foo followed by
65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// the current line number.  For more details, see
66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6
67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar)
68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar
69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Google Test defines the testing::Message class to allow construction of
71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// test messages via the << operator.  The idea is that anything
72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// streamable to std::ostream can be streamed to a testing::Message.
73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This allows a user to use his own types in Google Test assertions by
74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// overloading the << operator.
75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// util/gtl/stl_logging-inl.h overloads << for STL containers.  These
77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// overloads cannot be defined in the std namespace, as that will be
78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// undefined behavior.  Therefore, they are defined in the global
79fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// namespace instead.
80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
81fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// C++'s symbol lookup rule (i.e. Koenig lookup) says that these
82fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// overloads are visible in either the std namespace or the global
83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// namespace, but not other namespaces, including the testing
84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// namespace which Google Test's Message class is in.
85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// To allow STL containers (and other types that has a << operator
87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// defined in the global namespace) to be used in Google Test assertions,
88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// testing::Message must access the custom << operator from the global
89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// namespace.  Hence this helper function.
90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Note: Jeffrey Yasskin suggested an alternative fix by "using
92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// ::operator<<;" in the definition of Message's operator<<.  That fix
93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// doesn't require a helper function, but unfortunately doesn't
94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// compile with MSVC.
95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline void GTestStreamToHelper(std::ostream* os, const T& val) {
97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  *os << val;
98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace testing {
101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Forward declaration of classes.
103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass AssertionResult;                 // Result of an assertion.
105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass Message;                         // Represents a failure message.
106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass Test;                            // Represents a test.
107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TestInfo;                        // Information about a test.
108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TestPartResult;                  // Result of a test part.
109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass UnitTest;                        // A collection of test cases.
110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace internal {
112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
113fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillestruct TraceInfo;                      // Information about a trace point.
114fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass ScopedTrace;                     // Implements scoped trace.
115fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TestInfoImpl;                    // Opaque implementation of TestInfo
116fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass UnitTestImpl;                    // Opaque implementation of UnitTest
117fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename E> class Vector;    // A generic vector.
118fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
119fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// How many times InitGoogleTest() has been called.
120fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleextern int g_init_gtest_count;
121fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
122fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The text used in failure messages to indicate the start of the
123fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// stack trace.
124fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleextern const char kStackTraceMarker[];
125fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
126fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A secret type that Google Test users don't know about.  It has no
127fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// definition on purpose.  Therefore it's impossible to create a
128fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Secret object, which is what we want.
129fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass Secret;
130fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
131fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Two overloaded helpers for checking at compile time whether an
132fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// expression is a null pointer literal (i.e. NULL or any 0-valued
133fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// compile-time integral constant).  Their return values have
134fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// different sizes, so we can use sizeof() to test which version is
135fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// picked by the compiler.  These helpers have no implementations, as
136fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// we only need their signatures.
137fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
138fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Given IsNullLiteralHelper(x), the compiler will pick the first
139fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// version if x can be implicitly converted to Secret*, and pick the
140fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// second version otherwise.  Since Secret is a secret and incomplete
141fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// type, the only expression a user can write that has type Secret* is
142fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// a null pointer literal.  Therefore, we know that x is a null
143fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// pointer literal if and only if the first version is picked by the
144fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// compiler.
145fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillechar IsNullLiteralHelper(Secret* p);
146fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillechar (&IsNullLiteralHelper(...))[2];  // NOLINT
147fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
148fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A compile-time bool constant that is true if and only if x is a
149fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// null pointer literal (i.e. NULL or any 0-valued compile-time
150fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// integral constant).
151fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifdef GTEST_ELLIPSIS_NEEDS_COPY_
152fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Passing non-POD classes through ellipsis (...) crashes the ARM
153fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// compiler.  The Nokia Symbian and the IBM XL C/C++ compiler try to
154fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// instantiate a copy constructor for objects passed through ellipsis
155fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (...), failing for uncopyable objects.  Hence we define this to
156fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// false (and lose support for NULL detection).
157fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_IS_NULL_LITERAL_(x) false
158fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#else
159fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_IS_NULL_LITERAL_(x) \
160fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1)
161fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_ELLIPSIS_NEEDS_COPY_
162fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
163fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Appends the user-supplied message to the Google-Test-generated message.
164fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleString AppendUserMessage(const String& gtest_msg,
165fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                         const Message& user_msg);
166fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
167fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A helper class for creating scoped traces in user programs.
168fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass ScopedTrace {
169fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
170fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The c'tor pushes the given source file location and message onto
171fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // a trace stack maintained by Google Test.
172fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  ScopedTrace(const char* file, int line, const Message& message);
173fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
174fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The d'tor pops the info pushed by the c'tor.
175fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
176fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Note that the d'tor is not virtual in order to be efficient.
177fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Don't inherit from ScopedTrace!
178fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  ~ScopedTrace();
179fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
180fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
181fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace);
182fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} GTEST_ATTRIBUTE_UNUSED_;  // A ScopedTrace object does its job in its
183fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                            // c'tor and d'tor.  Therefore it doesn't
184fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                            // need to be used otherwise.
185fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
186fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Converts a streamable value to a String.  A NULL pointer is
187fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// converted to "(null)".  When the input value is a ::string,
188fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// ::std::string, ::wstring, or ::std::wstring object, each NUL
189fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// character in it is replaced with "\\0".
190fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Declared here but defined in gtest.h, so that it has access
191fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// to the definition of the Message class, required by the ARM
192fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// compiler.
193fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
194fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleString StreamableToString(const T& streamable);
195fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
196fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Formats a value to be used in a failure message.
197fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
198fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifdef GTEST_NEEDS_IS_POINTER_
199fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
200fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// These are needed as the Nokia Symbian and IBM XL C/C++ compilers
201fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// cannot decide between const T& and const T* in a function template.
202fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// These compilers _can_ decide between class template specializations
203fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// for T and T*, so a tr1::type_traits-like is_pointer works, and we
204fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// can overload on that.
205fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
206fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This overload makes sure that all pointers (including
207fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// those to char or wchar_t) are printed as raw pointers.
208fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
209fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline String FormatValueForFailureMessage(internal::true_type /*dummy*/,
210fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                                           T* pointer) {
211fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return StreamableToString(static_cast<const void*>(pointer));
212fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
213fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
214fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
215fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline String FormatValueForFailureMessage(internal::false_type /*dummy*/,
216fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                                           const T& value) {
217fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return StreamableToString(value);
218fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
219fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
220fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
221fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline String FormatForFailureMessage(const T& value) {
222fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return FormatValueForFailureMessage(
223fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      typename internal::is_pointer<T>::type(), value);
224fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
225fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
226fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#else
227fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
228fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// These are needed as the above solution using is_pointer has the
229fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// limitation that T cannot be a type without external linkage, when
230fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// compiled using MSVC.
231fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
232fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
233fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline String FormatForFailureMessage(const T& value) {
234fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return StreamableToString(value);
235fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
236fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
237fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This overload makes sure that all pointers (including
238fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// those to char or wchar_t) are printed as raw pointers.
239fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
240fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline String FormatForFailureMessage(T* pointer) {
241fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return StreamableToString(static_cast<const void*>(pointer));
242fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
243fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
244fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_NEEDS_IS_POINTER_
245fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
246fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// These overloaded versions handle narrow and wide characters.
247fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleString FormatForFailureMessage(char ch);
248fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleString FormatForFailureMessage(wchar_t wchar);
249fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
250fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// When this operand is a const char* or char*, and the other operand
251fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// is a ::std::string or ::string, we print this operand as a C string
252fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// rather than a pointer.  We do the same for wide strings.
253fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
254fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This internal macro is used to avoid duplicated code.
255fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_FORMAT_IMPL_(operand2_type, operand1_printer)\
256fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline String FormatForComparisonFailureMessage(\
257fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    operand2_type::value_type* str, const operand2_type& /*operand2*/) {\
258fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return operand1_printer(str);\
259fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}\
260fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline String FormatForComparisonFailureMessage(\
261fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    const operand2_type::value_type* str, const operand2_type& /*operand2*/) {\
262fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return operand1_printer(str);\
263fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
264fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
265fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if GTEST_HAS_STD_STRING
266fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleGTEST_FORMAT_IMPL_(::std::string, String::ShowCStringQuoted)
267fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_HAS_STD_STRING
268fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if GTEST_HAS_STD_WSTRING
269fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleGTEST_FORMAT_IMPL_(::std::wstring, String::ShowWideCStringQuoted)
270fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_HAS_STD_WSTRING
271fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
272fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if GTEST_HAS_GLOBAL_STRING
273fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleGTEST_FORMAT_IMPL_(::string, String::ShowCStringQuoted)
274fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_HAS_GLOBAL_STRING
275fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if GTEST_HAS_GLOBAL_WSTRING
276fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleGTEST_FORMAT_IMPL_(::wstring, String::ShowWideCStringQuoted)
277fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_HAS_GLOBAL_WSTRING
278fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
279fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#undef GTEST_FORMAT_IMPL_
280fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
281fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Constructs and returns the message for an equality assertion
282fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure.
283fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
284fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The first four parameters are the expressions used in the assertion
285fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// and their values, as strings.  For example, for ASSERT_EQ(foo, bar)
286fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// where foo is 5 and bar is 6, we have:
287fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
288fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   expected_expression: "foo"
289fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   actual_expression:   "bar"
290fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   expected_value:      "5"
291fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   actual_value:        "6"
292fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
293fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The ignoring_case parameter is true iff the assertion is a
294fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// *_STRCASEEQ*.  When it's true, the string " (ignoring case)" will
295fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// be inserted into the message.
296fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleAssertionResult EqFailure(const char* expected_expression,
297fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                          const char* actual_expression,
298fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                          const String& expected_value,
299fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                          const String& actual_value,
300fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                          bool ignoring_case);
301fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
302fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
303fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This template class represents an IEEE floating-point number
304fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (either single-precision or double-precision, depending on the
305fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// template parameters).
306fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
307fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The purpose of this class is to do more sophisticated number
308fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// comparison.  (Due to round-off error, etc, it's very unlikely that
309fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// two floating-points will be equal exactly.  Hence a naive
310fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// comparison by the == operation often doesn't work.)
311fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
312fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Format of IEEE floating-point:
313fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
314fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   The most-significant bit being the leftmost, an IEEE
315fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   floating-point looks like
316fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
317fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     sign_bit exponent_bits fraction_bits
318fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
319fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   Here, sign_bit is a single bit that designates the sign of the
320fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   number.
321fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
322fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   For float, there are 8 exponent bits and 23 fraction bits.
323fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
324fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   For double, there are 11 exponent bits and 52 fraction bits.
325fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
326fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   More details can be found at
327fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   http://en.wikipedia.org/wiki/IEEE_floating-point_standard.
328fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
329fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Template parameter:
330fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
331fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   RawType: the raw floating-point type (either float or double)
332fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename RawType>
333fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass FloatingPoint {
334fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
335fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Defines the unsigned integer type that has the same size as the
336fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // floating point number.
337fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  typedef typename TypeWithSize<sizeof(RawType)>::UInt Bits;
338fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
339fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Constants.
340fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
341fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // # of bits in a number.
342fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static const size_t kBitCount = 8*sizeof(RawType);
343fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
344fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // # of fraction bits in a number.
345fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static const size_t kFractionBitCount =
346fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    std::numeric_limits<RawType>::digits - 1;
347fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
348fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // # of exponent bits in a number.
349fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount;
350fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
351fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The mask for the sign bit.
352fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1);
353fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
354fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The mask for the fraction bits.
355fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static const Bits kFractionBitMask =
356fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    ~static_cast<Bits>(0) >> (kExponentBitCount + 1);
357fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
358fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The mask for the exponent bits.
359fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask);
360fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
361fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // How many ULP's (Units in the Last Place) we want to tolerate when
362fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // comparing two numbers.  The larger the value, the more error we
363fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // allow.  A 0 value means that two numbers must be exactly the same
364fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // to be considered equal.
365fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
366fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The maximum error of a single floating-point operation is 0.5
367fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // units in the last place.  On Intel CPU's, all floating-point
368fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // calculations are done with 80-bit precision, while double has 64
369fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // bits.  Therefore, 4 should be enough for ordinary use.
370fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
371fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // See the following article for more details on ULP:
372fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm.
373fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static const size_t kMaxUlps = 4;
374fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
375fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Constructs a FloatingPoint from a raw floating-point number.
376fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
377fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // On an Intel CPU, passing a non-normalized NAN (Not a Number)
378fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // around may change its bits, although the new value is guaranteed
379fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // to be also a NAN.  Therefore, don't expect this constructor to
380fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // preserve the bits in x when x is a NAN.
381fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  explicit FloatingPoint(const RawType& x) { u_.value_ = x; }
382fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
383fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Static methods
384fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
385fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Reinterprets a bit pattern as a floating-point number.
386fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
387fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // This function is needed to test the AlmostEquals() method.
388fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static RawType ReinterpretBits(const Bits bits) {
389fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    FloatingPoint fp(0);
390fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    fp.u_.bits_ = bits;
391fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return fp.u_.value_;
392fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
393fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
394fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the floating-point number that represent positive infinity.
395fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static RawType Infinity() {
396fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return ReinterpretBits(kExponentBitMask);
397fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
398fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
399fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Non-static methods
400fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
401fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the bits that represents this number.
402fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const Bits &bits() const { return u_.bits_; }
403fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
404fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the exponent bits of this number.
405fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  Bits exponent_bits() const { return kExponentBitMask & u_.bits_; }
406fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
407fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the fraction bits of this number.
408fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  Bits fraction_bits() const { return kFractionBitMask & u_.bits_; }
409fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
410fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the sign bit of this number.
411fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  Bits sign_bit() const { return kSignBitMask & u_.bits_; }
412fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
413fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true iff this is NAN (not a number).
414fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool is_nan() const {
415fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // It's a NAN if the exponent bits are all ones and the fraction
416fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // bits are not entirely zeros.
417fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0);
418fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
419fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
420fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true iff this number is at most kMaxUlps ULP's away from
421fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // rhs.  In particular, this function:
422fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
423fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   - returns false if either number is (or both are) NAN.
424fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   - treats really large numbers as almost equal to infinity.
425fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   - thinks +0.0 and -0.0 are 0 DLP's apart.
426fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool AlmostEquals(const FloatingPoint& rhs) const {
427fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // The IEEE standard says that any comparison operation involving
428fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // a NAN must return false.
429fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (is_nan() || rhs.is_nan()) return false;
430fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
431fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_)
432fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        <= kMaxUlps;
433fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
434fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
435fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
436fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The data type used to store the actual floating-point number.
437fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  union FloatingPointUnion {
438fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    RawType value_;  // The raw floating-point number.
439fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    Bits bits_;      // The bits that represent the number.
440fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  };
441fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
442fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Converts an integer from the sign-and-magnitude representation to
443fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // the biased representation.  More precisely, let N be 2 to the
444fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // power of (kBitCount - 1), an integer x is represented by the
445fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // unsigned number x + N.
446fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
447fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // For instance,
448fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
449fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   -N + 1 (the most negative number representable using
450fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //          sign-and-magnitude) is represented by 1;
451fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   0      is represented by N; and
452fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   N - 1  (the biggest number representable using
453fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //          sign-and-magnitude) is represented by 2N - 1.
454fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
455fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Read http://en.wikipedia.org/wiki/Signed_number_representations
456fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // for more details on signed number representations.
457fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static Bits SignAndMagnitudeToBiased(const Bits &sam) {
458fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (kSignBitMask & sam) {
459fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      // sam represents a negative number.
460fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      return ~sam + 1;
461fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } else {
462fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      // sam represents a positive number.
463fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      return kSignBitMask | sam;
464fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
465fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
466fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
467fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Given two numbers in the sign-and-magnitude representation,
468fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // returns the distance between them as an unsigned number.
469fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits &sam1,
470fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                                                     const Bits &sam2) {
471fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    const Bits biased1 = SignAndMagnitudeToBiased(sam1);
472fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    const Bits biased2 = SignAndMagnitudeToBiased(sam2);
473fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
474fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
475fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
476fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  FloatingPointUnion u_;
477fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
478fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
479fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Typedefs the instances of the FloatingPoint template class that we
480fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// care to use.
481fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletypedef FloatingPoint<float> Float;
482fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletypedef FloatingPoint<double> Double;
483fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
484fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// In order to catch the mistake of putting tests that use different
485fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// test fixture classes in the same test case, we need to assign
486fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// unique IDs to fixture classes and compare them.  The TypeId type is
487fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// used to hold such IDs.  The user should treat TypeId as an opaque
488fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// type: the only operation allowed on TypeId values is to compare
489fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// them for equality using the == operator.
490fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletypedef const void* TypeId;
491fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
492fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
493fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TypeIdHelper {
494fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
495fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // dummy_ must not have a const type.  Otherwise an overly eager
496fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // compiler (e.g. MSVC 7.1 & 8.0) may try to merge
497fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // TypeIdHelper<T>::dummy_ for different Ts as an "optimization".
498fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static bool dummy_;
499fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
500fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
501fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
502fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillebool TypeIdHelper<T>::dummy_ = false;
503fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
504fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// GetTypeId<T>() returns the ID of type T.  Different values will be
505fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// returned for different types.  Calling the function twice with the
506fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// same type argument is guaranteed to return the same ID.
507fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
508fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTypeId GetTypeId() {
509fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The compiler is required to allocate a different
510fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // TypeIdHelper<T>::dummy_ variable for each T used to instantiate
511fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // the template.  Therefore, the address of dummy_ is guaranteed to
512fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // be unique.
513fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return &(TypeIdHelper<T>::dummy_);
514fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
515fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
516fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns the type ID of ::testing::Test.  Always call this instead
517fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// of GetTypeId< ::testing::Test>() to get the type ID of
518fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// ::testing::Test, as the latter may give the wrong result due to a
519fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// suspected linker bug when compiling Google Test as a Mac OS X
520fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// framework.
521fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTypeId GetTestTypeId();
522fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
523fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Defines the abstract factory interface that creates instances
524fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// of a Test object.
525fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TestFactoryBase {
526fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
527fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual ~TestFactoryBase() {}
528fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
529fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Creates a test instance to run. The instance is both created and destroyed
530fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // within TestInfoImpl::Run()
531fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual Test* CreateTest() = 0;
532fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
533fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville protected:
534fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TestFactoryBase() {}
535fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
536fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
537fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase);
538fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
539fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
540fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This class provides implementation of TeastFactoryBase interface.
541fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// It is used in TEST and TEST_F macros.
542fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <class TestClass>
543fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TestFactoryImpl : public TestFactoryBase {
544fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
545fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual Test* CreateTest() { return new TestClass; }
546fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
547fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
548fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if GTEST_OS_WINDOWS
549fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
550fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Predicate-formatters for implementing the HRESULT checking macros
551fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}
552fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// We pass a long instead of HRESULT to avoid causing an
553fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// include dependency for the HRESULT type.
554fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleAssertionResult IsHRESULTSuccess(const char* expr, long hr);  // NOLINT
555fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleAssertionResult IsHRESULTFailure(const char* expr, long hr);  // NOLINT
556fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
557fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_OS_WINDOWS
558fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
559fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Formats a source file path and a line number as they would appear
560fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in a compiler error message.
561fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline String FormatFileLocation(const char* file, int line) {
562fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const char* const file_name = file == NULL ? "unknown file" : file;
563fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if (line < 0) {
564fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return String::Format("%s:", file_name);
565fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
566fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifdef _MSC_VER
567fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return String::Format("%s(%d):", file_name, line);
568fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#else
569fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return String::Format("%s:%d:", file_name, line);
570fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // _MSC_VER
571fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
572fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
573fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Types of SetUpTestCase() and TearDownTestCase() functions.
574fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletypedef void (*SetUpTestCaseFunc)();
575fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletypedef void (*TearDownTestCaseFunc)();
576fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
577fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Creates a new TestInfo object and registers it with Google Test;
578fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// returns the created object.
579fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
580fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Arguments:
581fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
582fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   test_case_name:   name of the test case
583fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   name:             name of the test
584fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   test_case_comment: a comment on the test case that will be included in
585fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//                      the test output
586fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   comment:          a comment on the test that will be included in the
587fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//                     test output
588fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   fixture_class_id: ID of the test fixture class
589fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   set_up_tc:        pointer to the function that sets up the test case
590fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   tear_down_tc:     pointer to the function that tears down the test case
591fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   factory:          pointer to the factory that creates a test object.
592fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//                     The newly created TestInfo instance will assume
593fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//                     ownership of the factory object.
594fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTestInfo* MakeAndRegisterTestInfo(
595fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    const char* test_case_name, const char* name,
596fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    const char* test_case_comment, const char* comment,
597fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    TypeId fixture_class_id,
598fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    SetUpTestCaseFunc set_up_tc,
599fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    TearDownTestCaseFunc tear_down_tc,
600fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    TestFactoryBase* factory);
601fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
602fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
603fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
604fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// State of the definition of a type-parameterized test case.
605fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TypedTestCasePState {
606fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
607fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TypedTestCasePState() : registered_(false) {}
608fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
609fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Adds the given test name to defined_test_names_ and return true
610fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // if the test case hasn't been registered; otherwise aborts the
611fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // program.
612fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool AddTestName(const char* file, int line, const char* case_name,
613fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                   const char* test_name) {
614fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (registered_) {
615fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      fprintf(stderr, "%s Test %s must be defined before "
616fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville              "REGISTER_TYPED_TEST_CASE_P(%s, ...).\n",
617fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville              FormatFileLocation(file, line).c_str(), test_name, case_name);
618fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      fflush(stderr);
619fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      posix::Abort();
620fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
621fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    defined_test_names_.insert(test_name);
622fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return true;
623fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
624fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
625fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Verifies that registered_tests match the test names in
626fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // defined_test_names_; returns registered_tests if successful, or
627fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // aborts the program otherwise.
628fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const char* VerifyRegisteredTestNames(
629fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      const char* file, int line, const char* registered_tests);
630fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
631fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
632fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool registered_;
633fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  ::std::set<const char*> defined_test_names_;
634fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
635fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
636fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Skips to the first non-space char after the first comma in 'str';
637fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// returns NULL if no comma is found in 'str'.
638fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline const char* SkipComma(const char* str) {
639fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const char* comma = strchr(str, ',');
640fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if (comma == NULL) {
641fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return NULL;
642fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
643fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  while (isspace(*(++comma))) {}
644fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return comma;
645fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
646fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
647fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns the prefix of 'str' before the first comma in it; returns
648fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// the entire string if it contains no comma.
649fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline String GetPrefixUntilComma(const char* str) {
650fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const char* comma = strchr(str, ',');
651fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return comma == NULL ? String(str) : String(str, comma - str);
652fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
653fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
654fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// TypeParameterizedTest<Fixture, TestSel, Types>::Register()
655fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// registers a list of type-parameterized tests with Google Test.  The
656fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// return value is insignificant - we just need to return something
657fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// such that we can call this function in a namespace scope.
658fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
659fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Implementation note: The GTEST_TEMPLATE_ macro declares a template
660fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// template parameter.  It's defined in gtest-type-util.h.
661fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <GTEST_TEMPLATE_ Fixture, class TestSel, typename Types>
662fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TypeParameterizedTest {
663fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
664fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // 'index' is the index of the test in the type list 'Types'
665fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // specified in INSTANTIATE_TYPED_TEST_CASE_P(Prefix, TestCase,
666fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Types).  Valid values for 'index' are [0, N - 1] where N is the
667fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // length of Types.
668fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static bool Register(const char* prefix, const char* case_name,
669fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                       const char* test_names, int index) {
670fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    typedef typename Types::Head Type;
671fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    typedef Fixture<Type> FixtureClass;
672fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    typedef typename GTEST_BIND_(TestSel, Type) TestClass;
673fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
674fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // First, registers the first type-parameterized test in the type
675fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // list.
676fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    MakeAndRegisterTestInfo(
677fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        String::Format("%s%s%s/%d", prefix, prefix[0] == '\0' ? "" : "/",
678fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                       case_name, index).c_str(),
679fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        GetPrefixUntilComma(test_names).c_str(),
680fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        String::Format("TypeParam = %s", GetTypeName<Type>().c_str()).c_str(),
681fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        "",
682fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        GetTypeId<FixtureClass>(),
683fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        TestClass::SetUpTestCase,
684fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        TestClass::TearDownTestCase,
685fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        new TestFactoryImpl<TestClass>);
686fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
687fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // Next, recurses (at compile time) with the tail of the type list.
688fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return TypeParameterizedTest<Fixture, TestSel, typename Types::Tail>
689fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        ::Register(prefix, case_name, test_names, index + 1);
690fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
691fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
692fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
693fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The base case for the compile time recursion.
694fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <GTEST_TEMPLATE_ Fixture, class TestSel>
695fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TypeParameterizedTest<Fixture, TestSel, Types0> {
696fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
697fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static bool Register(const char* /*prefix*/, const char* /*case_name*/,
698fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                       const char* /*test_names*/, int /*index*/) {
699fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return true;
700fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
701fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
702fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
703fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// TypeParameterizedTestCase<Fixture, Tests, Types>::Register()
704fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// registers *all combinations* of 'Tests' and 'Types' with Google
705fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Test.  The return value is insignificant - we just need to return
706fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// something such that we can call this function in a namespace scope.
707fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <GTEST_TEMPLATE_ Fixture, typename Tests, typename Types>
708fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TypeParameterizedTestCase {
709fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
710fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static bool Register(const char* prefix, const char* case_name,
711fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                       const char* test_names) {
712fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    typedef typename Tests::Head Head;
713fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
714fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // First, register the first test in 'Test' for each type in 'Types'.
715fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    TypeParameterizedTest<Fixture, Head, Types>::Register(
716fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        prefix, case_name, test_names, 0);
717fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
718fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // Next, recurses (at compile time) with the tail of the test list.
719fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return TypeParameterizedTestCase<Fixture, typename Tests::Tail, Types>
720fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        ::Register(prefix, case_name, SkipComma(test_names));
721fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
722fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
723fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
724fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The base case for the compile time recursion.
725fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <GTEST_TEMPLATE_ Fixture, typename Types>
726fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TypeParameterizedTestCase<Fixture, Templates0, Types> {
727fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
728d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  static bool Register(const char* /*prefix*/, const char* /*case_name*/,
729d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville                       const char* /*test_names*/) {
730fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return true;
731fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
732fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
733fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
734fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
735fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
736fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns the current OS stack trace as a String.
737fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
738fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The maximum number of stack frames to be included is specified by
739fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// the gtest_stack_trace_depth flag.  The skip_count parameter
740fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// specifies the number of top frames to be skipped, which doesn't
741fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// count against the number of frames to be included.
742fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
743fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// For example, if Foo() calls Bar(), which in turn calls
744fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in
745fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.
746fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleString GetCurrentOsStackTraceExceptTop(UnitTest* unit_test, int skip_count);
747fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
748d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Helpers for suppressing warnings on unreachable code or constant
749d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// condition.
750d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
751d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Always returns true.
752fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillebool AlwaysTrue();
753fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
754d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// Always returns false.
755d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilleinline bool AlwaysFalse() { return !AlwaysTrue(); }
756d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
757d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// A simple Linear Congruential Generator for generating random
758d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// numbers with a uniform distribution.  Unlike rand() and srand(), it
759d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// doesn't use global state (and therefore can't interfere with user
760d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// code).  Unlike rand_r(), it's portable.  An LCG isn't very random,
761d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville// but it's good enough for our purposes.
762d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Savilleclass Random {
763d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville public:
764d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  static const UInt32 kMaxRange = 1u << 31;
765d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
766d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  explicit Random(UInt32 seed) : state_(seed) {}
767d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
768d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  void Reseed(UInt32 seed) { state_ = seed; }
769d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
770d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Generates a random number from [0, range).  Crashes if 'range' is
771d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // 0 or greater than kMaxRange.
772d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  UInt32 Generate(UInt32 range);
773d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
774d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville private:
775d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  UInt32 state_;
776d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  GTEST_DISALLOW_COPY_AND_ASSIGN_(Random);
777d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville};
778d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
779fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}  // namespace internal
780fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}  // namespace testing
781fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
782fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_MESSAGE_(message, result_type) \
783fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  ::testing::internal::AssertHelper(result_type, __FILE__, __LINE__, message) \
784fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    = ::testing::Message()
785fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
786fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_FATAL_FAILURE_(message) \
787d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure)
788fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
789fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_NONFATAL_FAILURE_(message) \
790d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure)
791fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
792fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_SUCCESS_(message) \
793d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess)
794fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
795fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Suppresses MSVC warnings 4072 (unreachable code) for the code following
796fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// statement if it returns or throws (or doesn't return or throw in some
797fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// situations).
798d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \
799fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if (::testing::internal::AlwaysTrue()) { statement; }
800fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
801fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_TEST_THROW_(statement, expected_exception, fail) \
802fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
803fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if (const char* gtest_msg = "") { \
804fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    bool gtest_caught_expected = false; \
805fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    try { \
806d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
807fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } \
808fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    catch (expected_exception const&) { \
809fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      gtest_caught_expected = true; \
810fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } \
811fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    catch (...) { \
812fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      gtest_msg = "Expected: " #statement " throws an exception of type " \
813fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                  #expected_exception ".\n  Actual: it throws a different " \
814fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                  "type."; \
815fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
816fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } \
817fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (!gtest_caught_expected) { \
818fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      gtest_msg = "Expected: " #statement " throws an exception of type " \
819fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                  #expected_exception ".\n  Actual: it throws nothing."; \
820fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \
821fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } \
822fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  } else \
823fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \
824fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      fail(gtest_msg)
825fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
826fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_TEST_NO_THROW_(statement, fail) \
827fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
828fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if (const char* gtest_msg = "") { \
829fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    try { \
830d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
831fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } \
832fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    catch (...) { \
833fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      gtest_msg = "Expected: " #statement " doesn't throw an exception.\n" \
834fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                  "  Actual: it throws."; \
835fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \
836fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } \
837fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  } else \
838fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \
839fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      fail(gtest_msg)
840fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
841fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_TEST_ANY_THROW_(statement, fail) \
842fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
843fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if (const char* gtest_msg = "") { \
844fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    bool gtest_caught_any = false; \
845fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    try { \
846d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
847fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } \
848fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    catch (...) { \
849fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      gtest_caught_any = true; \
850fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } \
851fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (!gtest_caught_any) { \
852fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      gtest_msg = "Expected: " #statement " throws an exception.\n" \
853fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                  "  Actual: it doesn't."; \
854fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \
855fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } \
856fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  } else \
857fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__): \
858fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      fail(gtest_msg)
859fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
860fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
861fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_TEST_BOOLEAN_(boolexpr, booltext, actual, expected, fail) \
862fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
863d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  if (::testing::internal::IsTrue(boolexpr)) \
864fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    ; \
865fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  else \
866fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    fail("Value of: " booltext "\n  Actual: " #actual "\nExpected: " #expected)
867fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
868fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \
869fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
870fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  if (const char* gtest_msg = "") { \
871fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \
872d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \
873fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \
874fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      gtest_msg = "Expected: " #statement " doesn't generate new fatal " \
875fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                  "failures in the current thread.\n" \
876fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                  "  Actual: it does."; \
877fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \
878fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    } \
879