11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2005, Google Inc.
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved.
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met:
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions of source code must retain the above copyright
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer.
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Redistributions in binary form must reproduce the above
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution.
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     * Neither the name of Google Inc. nor the names of its
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission.
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The Google C++ Testing Framework (Google Test)
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This header file declares functions and macros used internally by
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test.  They are subject to change without notice.
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-port.h"
411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_OS_LINUX
4341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <stdlib.h>
4441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/types.h>
4541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <sys/wait.h>
4641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# include <unistd.h>
471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_OS_LINUX
481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
49fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#if GTEST_HAS_EXCEPTIONS
50fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# include <stdexcept>
51fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#endif
52fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <ctype.h>
54fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#include <float.h>
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <string.h>
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <iomanip>
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <limits>
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <set>
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
60fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#include "gtest/gtest-message.h"
6141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-string.h"
6241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-filepath.h"
6341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-type-util.h"
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Due to C++ preprocessor weirdness, we need double indirection to
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// concatenate two tokens when one of them is __LINE__.  Writing
671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   foo ## __LINE__
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// will result in the token foo__LINE__, instead of foo followed by
711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the current line number.  For more details, see
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar)
741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar
751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
7641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass ProtocolMessage;
7741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotnamespace proto2 { class Message; }
7841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing {
801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Forward declarations.
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
8341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass AssertionResult;                 // Result of an assertion.
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass Message;                         // Represents a failure message.
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass Test;                            // Represents a test.
861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestInfo;                        // Information about a test.
8741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestPartResult;                  // Result of a test part.
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass UnitTest;                        // A collection of test cases.
8941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
9041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T>
9141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot::std::string PrintToString(const T& value);
921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal {
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastruct TraceInfo;                      // Information about a trace point.
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ScopedTrace;                     // Implements scoped trace.
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestInfoImpl;                    // Opaque implementation of TestInfo
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass UnitTestImpl;                    // Opaque implementation of UnitTest
991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// How many times InitGoogleTest() has been called.
101fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_API_ extern int g_init_gtest_count;
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The text used in failure messages to indicate the start of the
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// stack trace.
10541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ extern const char kStackTraceMarker[];
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Two overloaded helpers for checking at compile time whether an
1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// expression is a null pointer literal (i.e. NULL or any 0-valued
1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// compile-time integral constant).  Their return values have
1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// different sizes, so we can use sizeof() to test which version is
1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// picked by the compiler.  These helpers have no implementations, as
1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// we only need their signatures.
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Given IsNullLiteralHelper(x), the compiler will pick the first
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// version if x can be implicitly converted to Secret*, and pick the
1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// second version otherwise.  Since Secret is a secret and incomplete
1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// type, the only expression a user can write that has type Secret* is
1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// a null pointer literal.  Therefore, we know that x is a null
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// pointer literal if and only if the first version is picked by the
1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// compiler.
1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniachar IsNullLiteralHelper(Secret* p);
1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniachar (&IsNullLiteralHelper(...))[2];  // NOLINT
1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A compile-time bool constant that is true if and only if x is a
1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// null pointer literal (i.e. NULL or any 0-valued compile-time
1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// integral constant).
12741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef GTEST_ELLIPSIS_NEEDS_POD_
12841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// We lose support for NULL detection where the compiler doesn't like
12941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// passing non-POD classes through ellipsis (...).
13041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_IS_NULL_LITERAL_(x) false
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else
13241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_IS_NULL_LITERAL_(x) \
1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1)
13441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif  // GTEST_ELLIPSIS_NEEDS_POD_
1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Appends the user-supplied message to the Google-Test-generated message.
137fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_API_ std::string AppendUserMessage(
138fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const std::string& gtest_msg, const Message& user_msg);
139fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
140fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#if GTEST_HAS_EXCEPTIONS
141fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
142fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// This exception is thrown by (and only by) a failed Google Test
143fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// assertion when GTEST_FLAG(throw_on_failure) is true (if exceptions
144fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// are enabled).  We derive it from std::runtime_error, which is for
145fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// errors presumably detectable only at run time.  Since
146fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// std::runtime_error inherits from std::exception, many testing
147fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// frameworks know how to extract and print the message inside it.
148fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesclass GTEST_API_ GoogleTestFailureException : public ::std::runtime_error {
149fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes public:
150fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  explicit GoogleTestFailureException(const TestPartResult& failure);
151fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes};
152fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
153fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#endif  // GTEST_HAS_EXCEPTIONS
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A helper class for creating scoped traces in user programs.
15641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ ScopedTrace {
1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The c'tor pushes the given source file location and message onto
1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // a trace stack maintained by Google Test.
1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ScopedTrace(const char* file, int line, const Message& message);
1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The d'tor pops the info pushed by the c'tor.
1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Note that the d'tor is not virtual in order to be efficient.
1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Don't inherit from ScopedTrace!
1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ~ScopedTrace();
1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace);
1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} GTEST_ATTRIBUTE_UNUSED_;  // A ScopedTrace object does its job in its
1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                            // c'tor and d'tor.  Therefore it doesn't
1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                            // need to be used otherwise.
1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Constructs and returns the message for an equality assertion
1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure.
1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The first four parameters are the expressions used in the assertion
1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and their values, as strings.  For example, for ASSERT_EQ(foo, bar)
1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// where foo is 5 and bar is 6, we have:
1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   expected_expression: "foo"
1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   actual_expression:   "bar"
1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   expected_value:      "5"
1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   actual_value:        "6"
1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The ignoring_case parameter is true iff the assertion is a
1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// *_STRCASEEQ*.  When it's true, the string " (ignoring case)" will
1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// be inserted into the message.
18941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult EqFailure(const char* expected_expression,
19041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                     const char* actual_expression,
191fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                     const std::string& expected_value,
192fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes                                     const std::string& actual_value,
19341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                     bool ignoring_case);
19441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
19541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Constructs a failure message for Boolean assertions such as EXPECT_TRUE.
196fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_API_ std::string GetBoolAssertionFailureMessage(
19741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const AssertionResult& assertion_result,
19841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const char* expression_text,
19941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const char* actual_predicate_value,
20041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const char* expected_predicate_value);
2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This template class represents an IEEE floating-point number
2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (either single-precision or double-precision, depending on the
2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// template parameters).
2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The purpose of this class is to do more sophisticated number
2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// comparison.  (Due to round-off error, etc, it's very unlikely that
2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// two floating-points will be equal exactly.  Hence a naive
2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// comparison by the == operation often doesn't work.)
2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Format of IEEE floating-point:
2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   The most-significant bit being the leftmost, an IEEE
2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   floating-point looks like
2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//     sign_bit exponent_bits fraction_bits
2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   Here, sign_bit is a single bit that designates the sign of the
2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   number.
2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   For float, there are 8 exponent bits and 23 fraction bits.
2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   For double, there are 11 exponent bits and 52 fraction bits.
2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   More details can be found at
2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   http://en.wikipedia.org/wiki/IEEE_floating-point_standard.
2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Template parameter:
2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   RawType: the raw floating-point type (either float or double)
2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename RawType>
2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass FloatingPoint {
2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Defines the unsigned integer type that has the same size as the
2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // floating point number.
2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  typedef typename TypeWithSize<sizeof(RawType)>::UInt Bits;
2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Constants.
2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // # of bits in a number.
2411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const size_t kBitCount = 8*sizeof(RawType);
2421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // # of fraction bits in a number.
2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const size_t kFractionBitCount =
2451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    std::numeric_limits<RawType>::digits - 1;
2461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // # of exponent bits in a number.
2481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount;
2491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The mask for the sign bit.
2511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1);
2521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The mask for the fraction bits.
2541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const Bits kFractionBitMask =
2551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    ~static_cast<Bits>(0) >> (kExponentBitCount + 1);
2561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The mask for the exponent bits.
2581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask);
2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // How many ULP's (Units in the Last Place) we want to tolerate when
2611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // comparing two numbers.  The larger the value, the more error we
2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // allow.  A 0 value means that two numbers must be exactly the same
2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // to be considered equal.
2641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
2651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The maximum error of a single floating-point operation is 0.5
2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // units in the last place.  On Intel CPU's, all floating-point
2671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // calculations are done with 80-bit precision, while double has 64
2681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // bits.  Therefore, 4 should be enough for ordinary use.
2691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
2701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // See the following article for more details on ULP:
271fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
2721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static const size_t kMaxUlps = 4;
2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Constructs a FloatingPoint from a raw floating-point number.
2751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
2761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // On an Intel CPU, passing a non-normalized NAN (Not a Number)
2771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // around may change its bits, although the new value is guaranteed
2781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // to be also a NAN.  Therefore, don't expect this constructor to
2791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // preserve the bits in x when x is a NAN.
28041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  explicit FloatingPoint(const RawType& x) { u_.value_ = x; }
2811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Static methods
2831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Reinterprets a bit pattern as a floating-point number.
2851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
2861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // This function is needed to test the AlmostEquals() method.
2871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static RawType ReinterpretBits(const Bits bits) {
2881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    FloatingPoint fp(0);
28941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    fp.u_.bits_ = bits;
29041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return fp.u_.value_;
2911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
2931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns the floating-point number that represent positive infinity.
2941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static RawType Infinity() {
2951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return ReinterpretBits(kExponentBitMask);
2961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
2971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
298fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  // Returns the maximum representable finite floating-point number.
299fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  static RawType Max();
300fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Non-static methods
3021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns the bits that represents this number.
30441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const Bits &bits() const { return u_.bits_; }
3051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns the exponent bits of this number.
30741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Bits exponent_bits() const { return kExponentBitMask & u_.bits_; }
3081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns the fraction bits of this number.
31041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Bits fraction_bits() const { return kFractionBitMask & u_.bits_; }
3111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns the sign bit of this number.
31341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  Bits sign_bit() const { return kSignBitMask & u_.bits_; }
3141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns true iff this is NAN (not a number).
3161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool is_nan() const {
3171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // It's a NAN if the exponent bits are all ones and the fraction
3181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // bits are not entirely zeros.
3191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0);
3201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Returns true iff this number is at most kMaxUlps ULP's away from
3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // rhs.  In particular, this function:
3241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
3251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //   - returns false if either number is (or both are) NAN.
3261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //   - treats really large numbers as almost equal to infinity.
3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //   - thinks +0.0 and -0.0 are 0 DLP's apart.
3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool AlmostEquals(const FloatingPoint& rhs) const {
3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // The IEEE standard says that any comparison operation involving
3301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // a NAN must return false.
3311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (is_nan() || rhs.is_nan()) return false;
3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
33341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_)
33441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        <= kMaxUlps;
3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
33841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  // The data type used to store the actual floating-point number.
33941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  union FloatingPointUnion {
34041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    RawType value_;  // The raw floating-point number.
34141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    Bits bits_;      // The bits that represent the number.
34241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  };
34341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
3441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Converts an integer from the sign-and-magnitude representation to
3451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // the biased representation.  More precisely, let N be 2 to the
3461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // power of (kBitCount - 1), an integer x is represented by the
3471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // unsigned number x + N.
3481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // For instance,
3501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
3511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //   -N + 1 (the most negative number representable using
3521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //          sign-and-magnitude) is represented by 1;
3531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //   0      is represented by N; and
3541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //   N - 1  (the biggest number representable using
3551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //          sign-and-magnitude) is represented by 2N - 1.
3561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  //
3571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Read http://en.wikipedia.org/wiki/Signed_number_representations
3581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // for more details on signed number representations.
3591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static Bits SignAndMagnitudeToBiased(const Bits &sam) {
3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (kSignBitMask & sam) {
3611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // sam represents a negative number.
3621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return ~sam + 1;
3631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    } else {
3641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      // sam represents a positive number.
3651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      return kSignBitMask | sam;
3661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
3671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Given two numbers in the sign-and-magnitude representation,
3701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // returns the distance between them as an unsigned number.
3711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits &sam1,
3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                                                     const Bits &sam2) {
3731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const Bits biased1 = SignAndMagnitudeToBiased(sam1);
3741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    const Bits biased2 = SignAndMagnitudeToBiased(sam2);
3751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
3771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
37841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  FloatingPointUnion u_;
3791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
3801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
381fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// We cannot use std::numeric_limits<T>::max() as it clashes with the max()
382fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// macro defined by <windows.h>.
383fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughestemplate <>
384fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesinline float FloatingPoint<float>::Max() { return FLT_MAX; }
385fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughestemplate <>
386fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesinline double FloatingPoint<double>::Max() { return DBL_MAX; }
387fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes
3881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Typedefs the instances of the FloatingPoint template class that we
3891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// care to use.
3901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef FloatingPoint<float> Float;
3911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef FloatingPoint<double> Double;
3921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
3931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In order to catch the mistake of putting tests that use different
3941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test fixture classes in the same test case, we need to assign
3951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// unique IDs to fixture classes and compare them.  The TypeId type is
3961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// used to hold such IDs.  The user should treat TypeId as an opaque
3971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// type: the only operation allowed on TypeId values is to compare
3981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// them for equality using the == operator.
3991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef const void* TypeId;
4001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T>
4021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypeIdHelper {
4031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
4041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // dummy_ must not have a const type.  Otherwise an overly eager
4051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // compiler (e.g. MSVC 7.1 & 8.0) may try to merge
4061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // TypeIdHelper<T>::dummy_ for different Ts as an "optimization".
4071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool dummy_;
4081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
4091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T>
4111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool TypeIdHelper<T>::dummy_ = false;
4121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GetTypeId<T>() returns the ID of type T.  Different values will be
4141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returned for different types.  Calling the function twice with the
4151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// same type argument is guaranteed to return the same ID.
4161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T>
4171be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTypeId GetTypeId() {
4181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // The compiler is required to allocate a different
4191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // TypeIdHelper<T>::dummy_ variable for each T used to instantiate
4201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // the template.  Therefore, the address of dummy_ is guaranteed to
4211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // be unique.
4221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return &(TypeIdHelper<T>::dummy_);
4231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
4241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the type ID of ::testing::Test.  Always call this instead
4261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of GetTypeId< ::testing::Test>() to get the type ID of
4271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ::testing::Test, as the latter may give the wrong result due to a
4281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// suspected linker bug when compiling Google Test as a Mac OS X
4291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// framework.
43041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ TypeId GetTestTypeId();
4311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Defines the abstract factory interface that creates instances
4331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of a Test object.
4341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestFactoryBase {
4351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
4361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual ~TestFactoryBase() {}
4371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Creates a test instance to run. The instance is both created and destroyed
4391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // within TestInfoImpl::Run()
4401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual Test* CreateTest() = 0;
4411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected:
4431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  TestFactoryBase() {}
4441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
4461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase);
4471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
4481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class provides implementation of TeastFactoryBase interface.
4501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// It is used in TEST and TEST_F macros.
4511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <class TestClass>
4521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestFactoryImpl : public TestFactoryBase {
4531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
4541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  virtual Test* CreateTest() { return new TestClass; }
4551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
4561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS
4581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Predicate-formatters for implementing the HRESULT checking macros
4601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}
4611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We pass a long instead of HRESULT to avoid causing an
4621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// include dependency for the HRESULT type.
46341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsHRESULTSuccess(const char* expr,
46441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                            long hr);  // NOLINT
46541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsHRESULTFailure(const char* expr,
46641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                                            long hr);  // NOLINT
4671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_OS_WINDOWS
4691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Types of SetUpTestCase() and TearDownTestCase() functions.
4711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef void (*SetUpTestCaseFunc)();
4721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef void (*TearDownTestCaseFunc)();
4731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
4741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a new TestInfo object and registers it with Google Test;
4751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns the created object.
4761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
4771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Arguments:
4781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
4791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   test_case_name:   name of the test case
4801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   name:             name of the test
48141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//   type_param        the name of the test's type parameter, or NULL if
482fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes//                     this is not a typed or a type-parameterized test.
48341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//   value_param       text representation of the test's value parameter,
48441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot//                     or NULL if this is not a type-parameterized test.
4851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   fixture_class_id: ID of the test fixture class
4861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   set_up_tc:        pointer to the function that sets up the test case
4871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   tear_down_tc:     pointer to the function that tears down the test case
4881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//   factory:          pointer to the factory that creates a test object.
4891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//                     The newly created TestInfo instance will assume
4901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//                     ownership of the factory object.
49141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ TestInfo* MakeAndRegisterTestInfo(
492fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const char* test_case_name,
493fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    const char* name,
49441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const char* type_param,
49541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    const char* value_param,
4961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TypeId fixture_class_id,
4971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    SetUpTestCaseFunc set_up_tc,
4981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TearDownTestCaseFunc tear_down_tc,
4991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TestFactoryBase* factory);
5001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
50141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// If *pstr starts with the given prefix, modifies *pstr to be right
50241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// past the prefix and returns true; otherwise leaves *pstr unchanged
50341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// and returns false.  None of pstr, *pstr, and prefix can be NULL.
50441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool SkipPrefix(const char* prefix, const char** pstr);
50541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
5061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
5071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// State of the definition of a type-parameterized test case.
50941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ TypedTestCasePState {
5101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
5111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  TypedTestCasePState() : registered_(false) {}
5121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Adds the given test name to defined_test_names_ and return true
5141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // if the test case hasn't been registered; otherwise aborts the
5151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // program.
5161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool AddTestName(const char* file, int line, const char* case_name,
5171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                   const char* test_name) {
5181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if (registered_) {
5191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      fprintf(stderr, "%s Test %s must be defined before "
5201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania              "REGISTER_TYPED_TEST_CASE_P(%s, ...).\n",
5211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania              FormatFileLocation(file, line).c_str(), test_name, case_name);
5221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      fflush(stderr);
52341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot      posix::Abort();
5241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    }
5251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    defined_test_names_.insert(test_name);
5261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return true;
5271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
5281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Verifies that registered_tests match the test names in
5301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // defined_test_names_; returns registered_tests if successful, or
5311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // aborts the program otherwise.
5321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* VerifyRegisteredTestNames(
5331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      const char* file, int line, const char* registered_tests);
5341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:
5361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  bool registered_;
5371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  ::std::set<const char*> defined_test_names_;
5381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
5391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Skips to the first non-space char after the first comma in 'str';
5411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns NULL if no comma is found in 'str'.
5421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline const char* SkipComma(const char* str) {
5431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* comma = strchr(str, ',');
5441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if (comma == NULL) {
5451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return NULL;
5461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
54741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  while (IsSpace(*(++comma))) {}
5481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  return comma;
5491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the prefix of 'str' before the first comma in it; returns
5521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the entire string if it contains no comma.
553fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesinline std::string GetPrefixUntilComma(const char* str) {
5541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  const char* comma = strchr(str, ',');
555fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes  return comma == NULL ? str : std::string(str, comma);
5561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}
5571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TypeParameterizedTest<Fixture, TestSel, Types>::Register()
5591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// registers a list of type-parameterized tests with Google Test.  The
5601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// return value is insignificant - we just need to return something
5611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// such that we can call this function in a namespace scope.
5621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
5631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implementation note: The GTEST_TEMPLATE_ macro declares a template
5641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// template parameter.  It's defined in gtest-type-util.h.
5651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <GTEST_TEMPLATE_ Fixture, class TestSel, typename Types>
5661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypeParameterizedTest {
5671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
5681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // 'index' is the index of the test in the type list 'Types'
5691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // specified in INSTANTIATE_TYPED_TEST_CASE_P(Prefix, TestCase,
5701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // Types).  Valid values for 'index' are [0, N - 1] where N is the
5711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  // length of Types.
5721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool Register(const char* prefix, const char* case_name,
5731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                       const char* test_names, int index) {
5741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    typedef typename Types::Head Type;
5751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    typedef Fixture<Type> FixtureClass;
5761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    typedef typename GTEST_BIND_(TestSel, Type) TestClass;
5771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // First, registers the first type-parameterized test in the type
5791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // list.
5801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    MakeAndRegisterTestInfo(
581fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes        (std::string(prefix) + (prefix[0] == '\0' ? "" : "/") + case_name + "/"
582fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes         + StreamableToString(index)).c_str(),
5831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        GetPrefixUntilComma(test_names).c_str(),
58441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        GetTypeName<Type>().c_str(),
58541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot        NULL,  // No value parameter.
5861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        GetTypeId<FixtureClass>(),
5871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        TestClass::SetUpTestCase,
5881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        TestClass::TearDownTestCase,
5891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        new TestFactoryImpl<TestClass>);
5901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Next, recurses (at compile time) with the tail of the type list.
5921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return TypeParameterizedTest<Fixture, TestSel, typename Types::Tail>
5931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ::Register(prefix, case_name, test_names, index + 1);
5941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
5951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
5961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
5971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The base case for the compile time recursion.
5981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <GTEST_TEMPLATE_ Fixture, class TestSel>
5991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypeParameterizedTest<Fixture, TestSel, Types0> {
6001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
6011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool Register(const char* /*prefix*/, const char* /*case_name*/,
6021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                       const char* /*test_names*/, int /*index*/) {
6031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return true;
6041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
6051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
6061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TypeParameterizedTestCase<Fixture, Tests, Types>::Register()
6081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// registers *all combinations* of 'Tests' and 'Types' with Google
6091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test.  The return value is insignificant - we just need to return
6101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// something such that we can call this function in a namespace scope.
6111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <GTEST_TEMPLATE_ Fixture, typename Tests, typename Types>
6121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypeParameterizedTestCase {
6131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
6141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  static bool Register(const char* prefix, const char* case_name,
6151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                       const char* test_names) {
6161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    typedef typename Tests::Head Head;
6171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // First, register the first test in 'Test' for each type in 'Types'.
6191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    TypeParameterizedTest<Fixture, Head, Types>::Register(
6201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        prefix, case_name, test_names, 0);
6211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    // Next, recurses (at compile time) with the tail of the test list.
6231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return TypeParameterizedTestCase<Fixture, typename Tests::Tail, Types>
6241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania        ::Register(prefix, case_name, SkipComma(test_names));
6251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
6261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
6271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The base case for the compile time recursion.
6291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <GTEST_TEMPLATE_ Fixture, typename Types>
6301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypeParameterizedTestCase<Fixture, Templates0, Types> {
6311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:
63241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  static bool Register(const char* /*prefix*/, const char* /*case_name*/,
63341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot                       const char* /*test_names*/) {
6341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    return true;
6351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  }
6361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};
6371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
6381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif  // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
6391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
640fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// Returns the current OS stack trace as an std::string.
6411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
6421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum number of stack frames to be included is specified by
6431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the gtest_stack_trace_depth flag.  The skip_count parameter
6441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// specifies the number of top frames to be skipped, which doesn't
6451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// count against the number of frames to be included.
6461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania//
6471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For example, if Foo() calls Bar(), which in turn calls
6481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in
6491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.
650fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesGTEST_API_ std::string GetCurrentOsStackTraceExceptTop(
651fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes    UnitTest* unit_test, int skip_count);
65241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
65341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Helpers for suppressing warnings on unreachable code or constant
65441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// condition.
6551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
65641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Always returns true.
65741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool AlwaysTrue();
6581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
65941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Always returns false.
66041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline bool AlwaysFalse() { return !AlwaysTrue(); }
66141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
66241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Helper for suppressing false warning from Clang on a const char*
66341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// variable declared in a conditional expression always being NULL in
66441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the else branch.
66541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct GTEST_API_ ConstCharPtr {
66641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  ConstCharPtr(const char* str) : value(str) {}
66741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  operator bool() const { return true; }
66841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  const char* value;
66941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot};
67041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
67141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A simple Linear Congruential Generator for generating random
67241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// numbers with a uniform distribution.  Unlike rand() and srand(), it
67341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// doesn't use global state (and therefore can't interfere with user
67441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// code).  Unlike rand_r(), it's portable.  An LCG isn't very random,
67541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// but it's good enough for our purposes.
67641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ Random {
67741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public:
67841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  static const UInt32 kMaxRange = 1u << 31;
67941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
68041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  explicit Random(UInt32 seed) : state_(seed) {}
68141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
68241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  void Reseed(UInt32 seed) { state_ = seed; }
68341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot
684