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