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 68441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Generates a random number from [0, range). Crashes if 'range' is 68541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 0 or greater than kMaxRange. 68641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot UInt32 Generate(UInt32 range); 68741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 68841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 68941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot UInt32 state_; 69041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_COPY_AND_ASSIGN_(Random); 69141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 69241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 69341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a 69441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// compiler error iff T1 and T2 are different types. 69541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T1, typename T2> 69641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct CompileAssertTypesEqual; 69741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 69841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 69941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct CompileAssertTypesEqual<T, T> { 70041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 70141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 70241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Removes the reference from a type if it is a reference type, 70341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// otherwise leaves it unchanged. This is the same as 70441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// tr1::remove_reference, which is not widely available yet. 70541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 70641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct RemoveReference { typedef T type; }; // NOLINT 70741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 70841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct RemoveReference<T&> { typedef T type; }; // NOLINT 70941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 71041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A handy wrapper around RemoveReference that works when the argument 71141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// T depends on template parameters. 71241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_REMOVE_REFERENCE_(T) \ 71341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename ::testing::internal::RemoveReference<T>::type 71441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 71541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Removes const from a type if it is a const type, otherwise leaves 71641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// it unchanged. This is the same as tr1::remove_const, which is not 71741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// widely available yet. 71841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 71941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct RemoveConst { typedef T type; }; // NOLINT 72041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 72141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct RemoveConst<const T> { typedef T type; }; // NOLINT 72241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 72341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// MSVC 8.0, Sun C++, and IBM XL C++ have a bug which causes the above 72441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// definition to fail to remove the const in 'const int[3]' and 'const 72541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// char[3][4]'. The following specialization works around the bug. 72641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, size_t N> 72741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct RemoveConst<const T[N]> { 72841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typedef typename RemoveConst<T>::type type[N]; 72941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 730fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 731fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes#if defined(_MSC_VER) && _MSC_VER < 1400 732fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// This is the only specialization that allows VC++ 7.1 to remove const in 733fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// 'const int[3] and 'const int[3][4]'. However, it causes trouble with GCC 734fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// and thus needs to be conditionally compiled. 735fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughestemplate <typename T, size_t N> 736fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstruct RemoveConst<T[N]> { 737fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes typedef typename RemoveConst<T>::type type[N]; 738fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes}; 73941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 74041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 74141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A handy wrapper around RemoveConst that works when the argument 74241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// T depends on template parameters. 74341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_REMOVE_CONST_(T) \ 74441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename ::testing::internal::RemoveConst<T>::type 74541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 74641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Turns const U&, U&, const U, and U all into U. 74741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \ 74841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_REMOVE_CONST_(GTEST_REMOVE_REFERENCE_(T)) 74941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 75041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Adds reference to a type if it is not a reference type, 75141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// otherwise leaves it unchanged. This is the same as 75241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// tr1::add_reference, which is not widely available yet. 75341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 75441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct AddReference { typedef T& type; }; // NOLINT 75541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 75641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct AddReference<T&> { typedef T& type; }; // NOLINT 75741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 75841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A handy wrapper around AddReference that works when the argument T 75941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// depends on template parameters. 76041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_ADD_REFERENCE_(T) \ 76141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename ::testing::internal::AddReference<T>::type 76241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 76341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Adds a reference to const on top of T as necessary. For example, 76441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// it transforms 76541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 76641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// char ==> const char& 76741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// const char ==> const char& 76841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// char& ==> const char& 76941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// const char& ==> const char& 77041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 77141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The argument T must depend on some template parameters. 77241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_REFERENCE_TO_CONST_(T) \ 77341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_ADD_REFERENCE_(const GTEST_REMOVE_REFERENCE_(T)) 77441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 77541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ImplicitlyConvertible<From, To>::value is a compile-time bool 77641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// constant that's true iff type From can be implicitly converted to 77741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// type To. 77841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename From, typename To> 77941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass ImplicitlyConvertible { 78041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 78141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We need the following helper functions only for their types. 78241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // They have no implementations. 78341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 78441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // MakeFrom() is an expression whose type is From. We cannot simply 78541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // use From(), as the type From may not have a public default 78641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // constructor. 78741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static From MakeFrom(); 78841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 78941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // These two functions are overloaded. Given an expression 79041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Helper(x), the compiler will pick the first version if x can be 79141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // implicitly converted to type To; otherwise it will pick the 79241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // second version. 79341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 79441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The first version returns a value of size 1, and the second 79541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // version returns a value of size 2. Therefore, by checking the 79641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // size of Helper(x), which can be done at compile time, we can tell 79741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // which version of Helper() is used, and hence whether x can be 79841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // implicitly converted to type To. 79941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static char Helper(To); 80041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static char (&Helper(...))[2]; // NOLINT 80141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 80241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We have to put the 'public' section after the 'private' section, 80341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // or MSVC refuses to compile the code. 80441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 80541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // MSVC warns about implicitly converting from double to int for 80641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // possible loss of data, so we need to temporarily disable the 80741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // warning. 80841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef _MSC_VER 80941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(push) // Saves the current warning state. 81041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(disable:4244) // Temporarily disables warning 4244. 81141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 81241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static const bool value = 81341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 81441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(pop) // Restores the warning state. 81541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#elif defined(__BORLANDC__) 81641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // C++Builder cannot use member overload resolution during template 81741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // instantiation. The simplest workaround is to use its C++0x type traits 81841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // functions (C++Builder 2009 and above only). 81941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static const bool value = __is_convertible(From, To); 82041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else 82141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static const bool value = 82241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 82341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // _MSV_VER 82441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 82541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename From, typename To> 82641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst bool ImplicitlyConvertible<From, To>::value; 82741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 82841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// IsAProtocolMessage<T>::value is a compile-time bool constant that's 82941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// true iff T is type ProtocolMessage, proto2::Message, or a subclass 83041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// of those. 83141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 83241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct IsAProtocolMessage 83341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : public bool_constant< 83441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ImplicitlyConvertible<const T*, const ::ProtocolMessage*>::value || 83541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ImplicitlyConvertible<const T*, const ::proto2::Message*>::value> { 83641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 83741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 83841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// When the compiler sees expression IsContainerTest<C>(0), if C is an 83941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// STL-style container class, the first overload of IsContainerTest 84041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// will be viable (since both C::iterator* and C::const_iterator* are 84141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// valid types and NULL can be implicitly converted to them). It will 84241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// be picked over the second overload as 'int' is a perfect match for 84341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the type of argument 0. If C::iterator or C::const_iterator is not 84441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// a valid type, the first overload is not viable, and the second 84541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// overload will be picked. Therefore, we can determine whether C is 84641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// a container class by checking the type of IsContainerTest<C>(0). 84741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The value of the expression is insignificant. 84841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 84941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Note that we look for both C::iterator and C::const_iterator. The 85041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// reason is that C++ injects the name of a class as a member of the 85141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// class itself (e.g. you can refer to class iterator as either 85241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 'iterator' or 'iterator::iterator'). If we look for C::iterator 85341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// only, for example, we would mistakenly think that a class named 85441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// iterator is an STL container. 85541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 85641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Also note that the simpler approach of overloading 85741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// IsContainerTest(typename C::const_iterator*) and 85841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// IsContainerTest(...) doesn't work with Visual Age C++ and Sun C++. 85941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottypedef int IsContainer; 86041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <class C> 86141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotIsContainer IsContainerTest(int /* dummy */, 86241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename C::iterator* /* it */ = NULL, 86341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename C::const_iterator* /* const_it */ = NULL) { 86441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return 0; 86541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 86641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 86741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottypedef char IsNotContainer; 86841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <class C> 86941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotIsNotContainer IsContainerTest(long /* dummy */) { return '\0'; } 87041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 87141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// EnableIf<condition>::type is void when 'Cond' is true, and 87241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// undefined when 'Cond' is false. To use SFINAE to make a function 87341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// overload only apply when a particular expression is true, add 87441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// "typename EnableIf<expression>::type* = 0" as the last parameter. 87541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate<bool> struct EnableIf; 87641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate<> struct EnableIf<true> { typedef void type; }; // NOLINT 87741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 87841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Utilities for native arrays. 87941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 88041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ArrayEq() compares two k-dimensional native arrays using the 88141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// elements' operator==, where k can be any integer >= 0. When k is 88241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 0, ArrayEq() degenerates into comparing a single pair of values. 88341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 88441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 88541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool ArrayEq(const T* lhs, size_t size, const U* rhs); 88641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 88741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This generic version is used when k is 0. 88841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 88941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline bool ArrayEq(const T& lhs, const U& rhs) { return lhs == rhs; } 89041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 89141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This overload is used when k >= 1. 89241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U, size_t N> 89341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline bool ArrayEq(const T(&lhs)[N], const U(&rhs)[N]) { 89441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return internal::ArrayEq(lhs, N, rhs); 89541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 89641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 89741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This helper reduces code bloat. If we instead put its logic inside 89841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the previous ArrayEq() function, arrays with different sizes would 89941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// lead to different copies of the template code. 90041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 90141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool ArrayEq(const T* lhs, size_t size, const U* rhs) { 90241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i != size; i++) { 90341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!internal::ArrayEq(lhs[i], rhs[i])) 90441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return false; 90541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 90641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return true; 90741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 90841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 90941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Finds the first element in the iterator range [begin, end) that 91041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// equals elem. Element may be a native array type itself. 91141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename Iter, typename Element> 91241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotIter ArrayAwareFind(Iter begin, Iter end, const Element& elem) { 91341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (Iter it = begin; it != end; ++it) { 91441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (internal::ArrayEq(*it, elem)) 91541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return it; 91641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 91741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return end; 91841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 91941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 92041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// CopyArray() copies a k-dimensional native array using the elements' 92141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// operator=, where k can be any integer >= 0. When k is 0, 92241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// CopyArray() degenerates into copying a single value. 92341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 92441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 92541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid CopyArray(const T* from, size_t size, U* to); 92641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 92741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This generic version is used when k is 0. 92841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 92941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline void CopyArray(const T& from, U* to) { *to = from; } 93041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 93141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This overload is used when k >= 1. 93241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U, size_t N> 93341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline void CopyArray(const T(&from)[N], U(*to)[N]) { 93441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::CopyArray(from, N, *to); 93541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 93641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 93741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This helper reduces code bloat. If we instead put its logic inside 93841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the previous CopyArray() function, arrays with different sizes 93941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// would lead to different copies of the template code. 94041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 94141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid CopyArray(const T* from, size_t size, U* to) { 94241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i != size; i++) { 94341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::CopyArray(from[i], to + i); 94441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 94541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 94641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 94741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The relation between an NativeArray object (see below) and the 94841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// native array it represents. 94941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotenum RelationToSource { 95041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot kReference, // The NativeArray references the native array. 95141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot kCopy // The NativeArray makes a copy of the native array and 95241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // owns the copy. 95341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 95441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 95541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Adapts a native array to a read-only STL-style container. Instead 95641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// of the complete STL container concept, this adaptor only implements 95741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// members useful for Google Mock's container matchers. New members 95841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// should be added as needed. To simplify the implementation, we only 95941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// support Element being a raw type (i.e. having no top-level const or 96041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// reference modifier). It's the client's responsibility to satisfy 96141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// this requirement. Element can be an array type itself (hence 96241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// multi-dimensional arrays are supported). 96341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename Element> 96441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass NativeArray { 96541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 96641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // STL-style container typedefs. 96741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typedef Element value_type; 96841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typedef Element* iterator; 96941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typedef const Element* const_iterator; 97041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 97141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Constructs from a native array. 97241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot NativeArray(const Element* array, size_t count, RelationToSource relation) { 97341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Init(array, count, relation); 97441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 97541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 97641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Copy constructor. 97741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot NativeArray(const NativeArray& rhs) { 97841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Init(rhs.array_, rhs.size_, rhs.relation_to_source_); 97941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 98041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 98141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ~NativeArray() { 98241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Ensures that the user doesn't instantiate NativeArray with a 98341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // const or reference type. 98441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast<void>(StaticAssertTypeEqHelper<Element, 98541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_REMOVE_REFERENCE_AND_CONST_(Element)>()); 98641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (relation_to_source_ == kCopy) 98741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot delete[] array_; 98841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 98941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 99041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // STL-style container methods. 99141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot size_t size() const { return size_; } 99241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const_iterator begin() const { return array_; } 99341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const_iterator end() const { return array_ + size_; } 99441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool operator==(const NativeArray& rhs) const { 99541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return size() == rhs.size() && 99641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ArrayEq(begin(), size(), rhs.begin()); 99741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 99841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 99941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 100041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Initializes this object; makes a copy of the input array if 100141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 'relation' is kCopy. 100241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void Init(const Element* array, size_t a_size, RelationToSource relation) { 100341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (relation == kReference) { 100441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot array_ = array; 100541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 100641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Element* const copy = new Element[a_size]; 100741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CopyArray(array, a_size, copy); 100841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot array_ = copy; 100941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 101041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot size_ = a_size; 101141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot relation_to_source_ = relation; 101241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 101341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 101441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const Element* array_; 101541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot size_t size_; 101641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot RelationToSource relation_to_source_; 101741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 101841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_ASSIGN_(NativeArray); 101941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 10201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 10221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace testing 10231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 102441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_MESSAGE_AT_(file, line, message, result_type) \ 102541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::testing::internal::AssertHelper(result_type, file, line, message) \ 10261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania = ::testing::Message() 10271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 102841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_MESSAGE_(message, result_type) \ 102941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type) 103041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 10311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_FATAL_FAILURE_(message) \ 103241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure) 10331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_NONFATAL_FAILURE_(message) \ 103541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure) 10361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_SUCCESS_(message) \ 103841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess) 10391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Suppresses MSVC warnings 4072 (unreachable code) for the code following 10411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// statement if it returns or throws (or doesn't return or throw in some 10421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// situations). 104341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \ 10441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (::testing::internal::AlwaysTrue()) { statement; } 10451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_THROW_(statement, expected_exception, fail) \ 10471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 104841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (::testing::internal::ConstCharPtr gtest_msg = "") { \ 10491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool gtest_caught_expected = false; \ 10501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try { \ 105141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 10521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 10531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania catch (expected_exception const&) { \ 10541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania gtest_caught_expected = true; \ 10551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 10561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania catch (...) { \ 105741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot gtest_msg.value = \ 105841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Expected: " #statement " throws an exception of type " \ 105941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot #expected_exception ".\n Actual: it throws a different type."; \ 10601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ 10611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 10621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!gtest_caught_expected) { \ 106341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot gtest_msg.value = \ 106441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Expected: " #statement " throws an exception of type " \ 106541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot #expected_exception ".\n Actual: it throws nothing."; \ 10661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ 10671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 10681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else \ 10691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \ 107041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fail(gtest_msg.value) 10711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_NO_THROW_(statement, fail) \ 10731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 107441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (::testing::internal::AlwaysTrue()) { \ 10751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try { \ 107641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 10771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 10781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania catch (...) { \ 10791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ 10801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 10811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else \ 10821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \ 108341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fail("Expected: " #statement " doesn't throw an exception.\n" \ 108441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " Actual: it throws.") 10851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_ANY_THROW_(statement, fail) \ 10871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 108841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (::testing::internal::AlwaysTrue()) { \ 10891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool gtest_caught_any = false; \ 10901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try { \ 109141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 10921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 10931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania catch (...) { \ 10941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania gtest_caught_any = true; \ 10951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 10961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!gtest_caught_any) { \ 10971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \ 10981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 10991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else \ 11001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__): \ 110141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fail("Expected: " #statement " throws an exception.\n" \ 110241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " Actual: it doesn't.") 11031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 110541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Implements Boolean test assertions such as EXPECT_TRUE. expression can be 110641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// either a boolean expression or an AssertionResult. text is a textual 110741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// represenation of expression as it was passed into the EXPECT_TRUE. 110841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \ 11091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 111041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (const ::testing::AssertionResult gtest_ar_ = \ 111141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::testing::AssertionResult(expression)) \ 11121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ; \ 11131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else \ 111441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fail(::testing::internal::GetBoolAssertionFailureMessage(\ 111541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot gtest_ar_, text, #actual, #expected).c_str()) 11161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \ 11181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 111941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (::testing::internal::AlwaysTrue()) { \ 11201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \ 112141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 11221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \ 11231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \ 11241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else \ 11261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__): \ 112741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fail("Expected: " #statement " doesn't generate new fatal " \ 112841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "failures in the current thread.\n" \ 112941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " Actual: it does.") 11301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Expands to the name of the class that implements the given test. 11321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \ 11331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test_case_name##_##test_name##_Test 11341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper macro for defining tests. 11361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\ 11371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\ 11381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:\ 11391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\ 11401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:\ 11411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TestBody();\ 114241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;\ 11431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(\ 11441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\ 11451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};\ 11461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania\ 11471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\ 11481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::test_info_ =\ 11491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::testing::internal::MakeAndRegisterTestInfo(\ 115041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot #test_case_name, #test_name, NULL, NULL, \ 11511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (parent_id), \ 11521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parent_class::SetUpTestCase, \ 11531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parent_class::TearDownTestCase, \ 11541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania new ::testing::internal::TestFactoryImpl<\ 11551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\ 11561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() 11571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ 1159