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 491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <ctype.h> 501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <string.h> 511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <iomanip> 521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <limits> 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <set> 541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-string.h" 5641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-filepath.h" 5741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/internal/gtest-type-util.h" 581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Due to C++ preprocessor weirdness, we need double indirection to 601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// concatenate two tokens when one of them is __LINE__. Writing 611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// foo ## __LINE__ 631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// will result in the token foo__LINE__, instead of foo followed by 651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the current line number. For more details, see 661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6 671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar) 681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar 691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Google Test defines the testing::Message class to allow construction of 711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test messages via the << operator. The idea is that anything 721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// streamable to std::ostream can be streamed to a testing::Message. 731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This allows a user to use his own types in Google Test assertions by 741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// overloading the << operator. 751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// util/gtl/stl_logging-inl.h overloads << for STL containers. These 771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// overloads cannot be defined in the std namespace, as that will be 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// undefined behavior. Therefore, they are defined in the global 791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// namespace instead. 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// C++'s symbol lookup rule (i.e. Koenig lookup) says that these 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// overloads are visible in either the std namespace or the global 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// namespace, but not other namespaces, including the testing 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// namespace which Google Test's Message class is in. 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// To allow STL containers (and other types that has a << operator 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// defined in the global namespace) to be used in Google Test assertions, 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// testing::Message must access the custom << operator from the global 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// namespace. Hence this helper function. 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Note: Jeffrey Yasskin suggested an alternative fix by "using 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ::operator<<;" in the definition of Message's operator<<. That fix 931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// doesn't require a helper function, but unfortunately doesn't 941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// compile with MSVC. 951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline void GTestStreamToHelper(std::ostream* os, const T& val) { 971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania *os << val; 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass ProtocolMessage; 10141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotnamespace proto2 { class Message; } 10241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace testing { 1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Forward declarations. 1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass AssertionResult; // Result of an assertion. 1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass Message; // Represents a failure message. 1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass Test; // Represents a test. 1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestInfo; // Information about a test. 11141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass TestPartResult; // Result of a test part. 1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass UnitTest; // A collection of test cases. 11341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 11441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 11541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot::std::string PrintToString(const T& value); 1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace internal { 1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniastruct TraceInfo; // Information about a trace point. 1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ScopedTrace; // Implements scoped trace. 1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestInfoImpl; // Opaque implementation of TestInfo 1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass UnitTestImpl; // Opaque implementation of UnitTest 1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// How many times InitGoogleTest() has been called. 1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaextern int g_init_gtest_count; 1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The text used in failure messages to indicate the start of the 1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// stack trace. 12941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ extern const char kStackTraceMarker[]; 1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A secret type that Google Test users don't know about. It has no 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// definition on purpose. Therefore it's impossible to create a 1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Secret object, which is what we want. 1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass Secret; 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Two overloaded helpers for checking at compile time whether an 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// expression is a null pointer literal (i.e. NULL or any 0-valued 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// compile-time integral constant). Their return values have 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// different sizes, so we can use sizeof() to test which version is 1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// picked by the compiler. These helpers have no implementations, as 1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// we only need their signatures. 1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Given IsNullLiteralHelper(x), the compiler will pick the first 1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// version if x can be implicitly converted to Secret*, and pick the 1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// second version otherwise. Since Secret is a secret and incomplete 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// type, the only expression a user can write that has type Secret* is 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// a null pointer literal. Therefore, we know that x is a null 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// pointer literal if and only if the first version is picked by the 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// compiler. 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniachar IsNullLiteralHelper(Secret* p); 1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniachar (&IsNullLiteralHelper(...))[2]; // NOLINT 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A compile-time bool constant that is true if and only if x is a 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// null pointer literal (i.e. NULL or any 0-valued compile-time 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// integral constant). 15641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef GTEST_ELLIPSIS_NEEDS_POD_ 15741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// We lose support for NULL detection where the compiler doesn't like 15841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// passing non-POD classes through ellipsis (...). 15941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_IS_NULL_LITERAL_(x) false 1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 16141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_IS_NULL_LITERAL_(x) \ 1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1) 16341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_ELLIPSIS_NEEDS_POD_ 1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Appends the user-supplied message to the Google-Test-generated message. 16641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ String AppendUserMessage(const String& gtest_msg, 16741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const Message& user_msg); 1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A helper class for creating scoped traces in user programs. 17041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ ScopedTrace { 1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The c'tor pushes the given source file location and message onto 1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // a trace stack maintained by Google Test. 1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ScopedTrace(const char* file, int line, const Message& message); 1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The d'tor pops the info pushed by the c'tor. 1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Note that the d'tor is not virtual in order to be efficient. 1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Don't inherit from ScopedTrace! 1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~ScopedTrace(); 1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace); 1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} GTEST_ATTRIBUTE_UNUSED_; // A ScopedTrace object does its job in its 1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // c'tor and d'tor. Therefore it doesn't 1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // need to be used otherwise. 1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Converts a streamable value to a String. A NULL pointer is 1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// converted to "(null)". When the input value is a ::string, 1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ::std::string, ::wstring, or ::std::wstring object, each NUL 1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// character in it is replaced with "\\0". 1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Declared here but defined in gtest.h, so that it has access 1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// to the definition of the Message class, required by the ARM 1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// compiler. 1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaString StreamableToString(const T& streamable); 1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The Symbian compiler has a bug that prevents it from selecting the 19941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// correct overload of FormatForComparisonFailureMessage (see below) 20041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// unless we pass the first argument by reference. If we do that, 20141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// however, Visual Age C++ 10.1 generates a compiler error. Therefore 20241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// we only apply the work-around for Symbian. 20341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if defined(__SYMBIAN32__) 20441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_CREF_WORKAROUND_ const& 2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#else 20641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# define GTEST_CREF_WORKAROUND_ 20741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 20941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// When this operand is a const char* or char*, if the other operand 2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// is a ::std::string or ::string, we print this operand as a C string 21141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// rather than a pointer (we do the same for wide strings); otherwise 21241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// we print it as a pointer to be safe. 2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This internal macro is used to avoid duplicated code. 2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_FORMAT_IMPL_(operand2_type, operand1_printer)\ 2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline String FormatForComparisonFailureMessage(\ 21741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot operand2_type::value_type* GTEST_CREF_WORKAROUND_ str, \ 21841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const operand2_type& /*operand2*/) {\ 2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return operand1_printer(str);\ 2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}\ 2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline String FormatForComparisonFailureMessage(\ 22241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const operand2_type::value_type* GTEST_CREF_WORKAROUND_ str, \ 22341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const operand2_type& /*operand2*/) {\ 2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return operand1_printer(str);\ 2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_FORMAT_IMPL_(::std::string, String::ShowCStringQuoted) 2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_STD_WSTRING 2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_FORMAT_IMPL_(::std::wstring, String::ShowWideCStringQuoted) 2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_STD_WSTRING 2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_GLOBAL_STRING 2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_FORMAT_IMPL_(::string, String::ShowCStringQuoted) 2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_GLOBAL_STRING 2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_GLOBAL_WSTRING 2361be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGTEST_FORMAT_IMPL_(::wstring, String::ShowWideCStringQuoted) 2371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_GLOBAL_WSTRING 2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#undef GTEST_FORMAT_IMPL_ 2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The next four overloads handle the case where the operand being 24241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// printed is a char/wchar_t pointer and the other operand is not a 24341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// string/wstring object. In such cases, we just print the operand as 24441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// a pointer to be safe. 24541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_FORMAT_CHAR_PTR_IMPL_(CharType) \ 24641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot template <typename T> \ 24741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot String FormatForComparisonFailureMessage(CharType* GTEST_CREF_WORKAROUND_ p, \ 24841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const T&) { \ 24941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return PrintToString(static_cast<const void*>(p)); \ 25041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 25141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 25241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_FORMAT_CHAR_PTR_IMPL_(char) 25341d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_FORMAT_CHAR_PTR_IMPL_(const char) 25441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_FORMAT_CHAR_PTR_IMPL_(wchar_t) 25541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_FORMAT_CHAR_PTR_IMPL_(const wchar_t) 25641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 25741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#undef GTEST_FORMAT_CHAR_PTR_IMPL_ 25841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 2591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Constructs and returns the message for an equality assertion 2601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure. 2611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The first four parameters are the expressions used in the assertion 2631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// and their values, as strings. For example, for ASSERT_EQ(foo, bar) 2641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// where foo is 5 and bar is 6, we have: 2651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// expected_expression: "foo" 2671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// actual_expression: "bar" 2681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// expected_value: "5" 2691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// actual_value: "6" 2701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The ignoring_case parameter is true iff the assertion is a 2721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// *_STRCASEEQ*. When it's true, the string " (ignoring case)" will 2731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// be inserted into the message. 27441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult EqFailure(const char* expected_expression, 27541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual_expression, 27641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const String& expected_value, 27741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const String& actual_value, 27841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool ignoring_case); 27941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 28041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Constructs a failure message for Boolean assertions such as EXPECT_TRUE. 28141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ String GetBoolAssertionFailureMessage( 28241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const AssertionResult& assertion_result, 28341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* expression_text, 28441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* actual_predicate_value, 28541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* expected_predicate_value); 2861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This template class represents an IEEE floating-point number 2881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (either single-precision or double-precision, depending on the 2891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// template parameters). 2901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The purpose of this class is to do more sophisticated number 2921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// comparison. (Due to round-off error, etc, it's very unlikely that 2931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// two floating-points will be equal exactly. Hence a naive 2941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// comparison by the == operation often doesn't work.) 2951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Format of IEEE floating-point: 2971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 2981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The most-significant bit being the leftmost, an IEEE 2991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// floating-point looks like 3001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// sign_bit exponent_bits fraction_bits 3021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Here, sign_bit is a single bit that designates the sign of the 3041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// number. 3051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For float, there are 8 exponent bits and 23 fraction bits. 3071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For double, there are 11 exponent bits and 52 fraction bits. 3091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// More details can be found at 3111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// http://en.wikipedia.org/wiki/IEEE_floating-point_standard. 3121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Template parameter: 3141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// RawType: the raw floating-point type (either float or double) 3161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename RawType> 3171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass FloatingPoint { 3181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 3191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Defines the unsigned integer type that has the same size as the 3201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // floating point number. 3211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania typedef typename TypeWithSize<sizeof(RawType)>::UInt Bits; 3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constants. 3241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // # of bits in a number. 3261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const size_t kBitCount = 8*sizeof(RawType); 3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // # of fraction bits in a number. 3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const size_t kFractionBitCount = 3301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania std::numeric_limits<RawType>::digits - 1; 3311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // # of exponent bits in a number. 3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount; 3341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The mask for the sign bit. 3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1); 3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The mask for the fraction bits. 3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const Bits kFractionBitMask = 3401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~static_cast<Bits>(0) >> (kExponentBitCount + 1); 3411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The mask for the exponent bits. 3431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask); 3441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // How many ULP's (Units in the Last Place) we want to tolerate when 3461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // comparing two numbers. The larger the value, the more error we 3471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // allow. A 0 value means that two numbers must be exactly the same 3481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // to be considered equal. 3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 3501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The maximum error of a single floating-point operation is 0.5 3511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // units in the last place. On Intel CPU's, all floating-point 3521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // calculations are done with 80-bit precision, while double has 64 3531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // bits. Therefore, 4 should be enough for ordinary use. 3541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 3551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // See the following article for more details on ULP: 3561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm. 3571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const size_t kMaxUlps = 4; 3581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Constructs a FloatingPoint from a raw floating-point number. 3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 3611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // On an Intel CPU, passing a non-normalized NAN (Not a Number) 3621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // around may change its bits, although the new value is guaranteed 3631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // to be also a NAN. Therefore, don't expect this constructor to 3641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // preserve the bits in x when x is a NAN. 36541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot explicit FloatingPoint(const RawType& x) { u_.value_ = x; } 3661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Static methods 3681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Reinterprets a bit pattern as a floating-point number. 3701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 3711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This function is needed to test the AlmostEquals() method. 3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static RawType ReinterpretBits(const Bits bits) { 3731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FloatingPoint fp(0); 37441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fp.u_.bits_ = bits; 37541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return fp.u_.value_; 3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the floating-point number that represent positive infinity. 3791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static RawType Infinity() { 3801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return ReinterpretBits(kExponentBitMask); 3811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Non-static methods 3841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the bits that represents this number. 38641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const Bits &bits() const { return u_.bits_; } 3871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the exponent bits of this number. 38941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Bits exponent_bits() const { return kExponentBitMask & u_.bits_; } 3901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the fraction bits of this number. 39241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Bits fraction_bits() const { return kFractionBitMask & u_.bits_; } 3931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns the sign bit of this number. 39541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Bits sign_bit() const { return kSignBitMask & u_.bits_; } 3961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this is NAN (not a number). 3981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool is_nan() const { 3991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // It's a NAN if the exponent bits are all ones and the fraction 4001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // bits are not entirely zeros. 4011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0); 4021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Returns true iff this number is at most kMaxUlps ULP's away from 4051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // rhs. In particular, this function: 4061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // - returns false if either number is (or both are) NAN. 4081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // - treats really large numbers as almost equal to infinity. 4091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // - thinks +0.0 and -0.0 are 0 DLP's apart. 4101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool AlmostEquals(const FloatingPoint& rhs) const { 4111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The IEEE standard says that any comparison operation involving 4121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // a NAN must return false. 4131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (is_nan() || rhs.is_nan()) return false; 4141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 41541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_) 41641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot <= kMaxUlps; 4171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 42041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The data type used to store the actual floating-point number. 42141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot union FloatingPointUnion { 42241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot RawType value_; // The raw floating-point number. 42341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Bits bits_; // The bits that represent the number. 42441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot }; 42541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 4261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Converts an integer from the sign-and-magnitude representation to 4271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the biased representation. More precisely, let N be 2 to the 4281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // power of (kBitCount - 1), an integer x is represented by the 4291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // unsigned number x + N. 4301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // For instance, 4321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // -N + 1 (the most negative number representable using 4341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // sign-and-magnitude) is represented by 1; 4351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 0 is represented by N; and 4361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // N - 1 (the biggest number representable using 4371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // sign-and-magnitude) is represented by 2N - 1. 4381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 4391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Read http://en.wikipedia.org/wiki/Signed_number_representations 4401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // for more details on signed number representations. 4411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static Bits SignAndMagnitudeToBiased(const Bits &sam) { 4421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (kSignBitMask & sam) { 4431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // sam represents a negative number. 4441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return ~sam + 1; 4451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 4461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // sam represents a positive number. 4471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return kSignBitMask | sam; 4481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Given two numbers in the sign-and-magnitude representation, 4521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // returns the distance between them as an unsigned number. 4531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits &sam1, 4541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Bits &sam2) { 4551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Bits biased1 = SignAndMagnitudeToBiased(sam1); 4561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const Bits biased2 = SignAndMagnitudeToBiased(sam2); 4571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1); 4581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FloatingPointUnion u_; 4611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Typedefs the instances of the FloatingPoint template class that we 4641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// care to use. 4651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef FloatingPoint<float> Float; 4661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef FloatingPoint<double> Double; 4671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// In order to catch the mistake of putting tests that use different 4691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test fixture classes in the same test case, we need to assign 4701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// unique IDs to fixture classes and compare them. The TypeId type is 4711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// used to hold such IDs. The user should treat TypeId as an opaque 4721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// type: the only operation allowed on TypeId values is to compare 4731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// them for equality using the == operator. 4741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef const void* TypeId; 4751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 4771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypeIdHelper { 4781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 4791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // dummy_ must not have a const type. Otherwise an overly eager 4801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // compiler (e.g. MSVC 7.1 & 8.0) may try to merge 4811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TypeIdHelper<T>::dummy_ for different Ts as an "optimization". 4821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool dummy_; 4831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 4861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniabool TypeIdHelper<T>::dummy_ = false; 4871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GetTypeId<T>() returns the ID of type T. Different values will be 4891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returned for different types. Calling the function twice with the 4901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// same type argument is guaranteed to return the same ID. 4911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 4921be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTypeId GetTypeId() { 4931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The compiler is required to allocate a different 4941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // TypeIdHelper<T>::dummy_ variable for each T used to instantiate 4951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the template. Therefore, the address of dummy_ is guaranteed to 4961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // be unique. 4971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return &(TypeIdHelper<T>::dummy_); 4981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the type ID of ::testing::Test. Always call this instead 5011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of GetTypeId< ::testing::Test>() to get the type ID of 5021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ::testing::Test, as the latter may give the wrong result due to a 5031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// suspected linker bug when compiling Google Test as a Mac OS X 5041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// framework. 50541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ TypeId GetTestTypeId(); 5061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Defines the abstract factory interface that creates instances 5081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of a Test object. 5091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestFactoryBase { 5101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 5111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~TestFactoryBase() {} 5121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Creates a test instance to run. The instance is both created and destroyed 5141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // within TestInfoImpl::Run() 5151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual Test* CreateTest() = 0; 5161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 5181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestFactoryBase() {} 5191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 5211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase); 5221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 5231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This class provides implementation of TeastFactoryBase interface. 5251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// It is used in TEST and TEST_F macros. 5261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <class TestClass> 5271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TestFactoryImpl : public TestFactoryBase { 5281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 5291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual Test* CreateTest() { return new TestClass; } 5301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 5311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_OS_WINDOWS 5331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Predicate-formatters for implementing the HRESULT checking macros 5351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED} 5361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We pass a long instead of HRESULT to avoid causing an 5371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// include dependency for the HRESULT type. 53841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsHRESULTSuccess(const char* expr, 53941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot long hr); // NOLINT 54041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ AssertionResult IsHRESULTFailure(const char* expr, 54141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot long hr); // NOLINT 5421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_OS_WINDOWS 5441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Types of SetUpTestCase() and TearDownTestCase() functions. 5461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef void (*SetUpTestCaseFunc)(); 5471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef void (*TearDownTestCaseFunc)(); 5481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Creates a new TestInfo object and registers it with Google Test; 5501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns the created object. 5511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 5521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Arguments: 5531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 5541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test_case_name: name of the test case 5551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// name: name of the test 55641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// type_param the name of the test's type parameter, or NULL if 55741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// this is not a typed or a type-parameterized test. 55841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// value_param text representation of the test's value parameter, 55941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// or NULL if this is not a type-parameterized test. 5601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// fixture_class_id: ID of the test fixture class 5611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// set_up_tc: pointer to the function that sets up the test case 5621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// tear_down_tc: pointer to the function that tears down the test case 5631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// factory: pointer to the factory that creates a test object. 5641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The newly created TestInfo instance will assume 5651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// ownership of the factory object. 56641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ TestInfo* MakeAndRegisterTestInfo( 5671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* test_case_name, const char* name, 56841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* type_param, 56941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* value_param, 5701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TypeId fixture_class_id, 5711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SetUpTestCaseFunc set_up_tc, 5721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TearDownTestCaseFunc tear_down_tc, 5731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestFactoryBase* factory); 5741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 57541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// If *pstr starts with the given prefix, modifies *pstr to be right 57641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// past the prefix and returns true; otherwise leaves *pstr unchanged 57741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// and returns false. None of pstr, *pstr, and prefix can be NULL. 57841d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool SkipPrefix(const char* prefix, const char** pstr); 57941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 5801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P 5811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// State of the definition of a type-parameterized test case. 58341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ TypedTestCasePState { 5841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 5851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TypedTestCasePState() : registered_(false) {} 5861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Adds the given test name to defined_test_names_ and return true 5881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // if the test case hasn't been registered; otherwise aborts the 5891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // program. 5901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool AddTestName(const char* file, int line, const char* case_name, 5911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* test_name) { 5921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (registered_) { 5931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fprintf(stderr, "%s Test %s must be defined before " 5941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "REGISTER_TYPED_TEST_CASE_P(%s, ...).\n", 5951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FormatFileLocation(file, line).c_str(), test_name, case_name); 5961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stderr); 59741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot posix::Abort(); 5981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 5991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania defined_test_names_.insert(test_name); 6001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 6011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Verifies that registered_tests match the test names in 6041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // defined_test_names_; returns registered_tests if successful, or 6051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // aborts the program otherwise. 6061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* VerifyRegisteredTestNames( 6071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* file, int line, const char* registered_tests); 6081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 6101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool registered_; 6111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::std::set<const char*> defined_test_names_; 6121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 6131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Skips to the first non-space char after the first comma in 'str'; 6151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// returns NULL if no comma is found in 'str'. 6161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline const char* SkipComma(const char* str) { 6171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* comma = strchr(str, ','); 6181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (comma == NULL) { 6191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return NULL; 6201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 62141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot while (IsSpace(*(++comma))) {} 6221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return comma; 6231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the prefix of 'str' before the first comma in it; returns 6261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the entire string if it contains no comma. 6271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniainline String GetPrefixUntilComma(const char* str) { 6281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* comma = strchr(str, ','); 6291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return comma == NULL ? String(str) : String(str, comma - str); 6301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TypeParameterizedTest<Fixture, TestSel, Types>::Register() 6331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// registers a list of type-parameterized tests with Google Test. The 6341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// return value is insignificant - we just need to return something 6351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// such that we can call this function in a namespace scope. 6361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 6371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Implementation note: The GTEST_TEMPLATE_ macro declares a template 6381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// template parameter. It's defined in gtest-type-util.h. 6391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <GTEST_TEMPLATE_ Fixture, class TestSel, typename Types> 6401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypeParameterizedTest { 6411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 6421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 'index' is the index of the test in the type list 'Types' 6431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // specified in INSTANTIATE_TYPED_TEST_CASE_P(Prefix, TestCase, 6441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Types). Valid values for 'index' are [0, N - 1] where N is the 6451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // length of Types. 6461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool Register(const char* prefix, const char* case_name, 6471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* test_names, int index) { 6481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania typedef typename Types::Head Type; 6491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania typedef Fixture<Type> FixtureClass; 6501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania typedef typename GTEST_BIND_(TestSel, Type) TestClass; 6511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // First, registers the first type-parameterized test in the type 6531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // list. 6541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania MakeAndRegisterTestInfo( 6551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania String::Format("%s%s%s/%d", prefix, prefix[0] == '\0' ? "" : "/", 6561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania case_name, index).c_str(), 6571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GetPrefixUntilComma(test_names).c_str(), 65841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GetTypeName<Type>().c_str(), 65941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot NULL, // No value parameter. 6601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GetTypeId<FixtureClass>(), 6611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestClass::SetUpTestCase, 6621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestClass::TearDownTestCase, 6631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania new TestFactoryImpl<TestClass>); 6641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Next, recurses (at compile time) with the tail of the type list. 6661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return TypeParameterizedTest<Fixture, TestSel, typename Types::Tail> 6671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::Register(prefix, case_name, test_names, index + 1); 6681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 6701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The base case for the compile time recursion. 6721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <GTEST_TEMPLATE_ Fixture, class TestSel> 6731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypeParameterizedTest<Fixture, TestSel, Types0> { 6741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 6751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool Register(const char* /*prefix*/, const char* /*case_name*/, 6761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* /*test_names*/, int /*index*/) { 6771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 6781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 6801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// TypeParameterizedTestCase<Fixture, Tests, Types>::Register() 6821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// registers *all combinations* of 'Tests' and 'Types' with Google 6831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Test. The return value is insignificant - we just need to return 6841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// something such that we can call this function in a namespace scope. 6851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <GTEST_TEMPLATE_ Fixture, typename Tests, typename Types> 6861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypeParameterizedTestCase { 6871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 6881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static bool Register(const char* prefix, const char* case_name, 6891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania const char* test_names) { 6901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania typedef typename Tests::Head Head; 6911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // First, register the first test in 'Test' for each type in 'Types'. 6931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TypeParameterizedTest<Fixture, Head, Types>::Register( 6941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania prefix, case_name, test_names, 0); 6951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Next, recurses (at compile time) with the tail of the test list. 6971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return TypeParameterizedTestCase<Fixture, typename Tests::Tail, Types> 6981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::Register(prefix, case_name, SkipComma(test_names)); 6991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 7001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 7011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The base case for the compile time recursion. 7031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <GTEST_TEMPLATE_ Fixture, typename Types> 7041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypeParameterizedTestCase<Fixture, Templates0, Types> { 7051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 70641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static bool Register(const char* /*prefix*/, const char* /*case_name*/, 70741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* /*test_names*/) { 7081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return true; 7091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 7101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 7111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P 7131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Returns the current OS stack trace as a String. 7151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 7161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The maximum number of stack frames to be included is specified by 7171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the gtest_stack_trace_depth flag. The skip_count parameter 7181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// specifies the number of top frames to be skipped, which doesn't 7191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// count against the number of frames to be included. 7201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 7211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// For example, if Foo() calls Bar(), which in turn calls 7221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in 7231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't. 72441d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ String GetCurrentOsStackTraceExceptTop(UnitTest* unit_test, 72541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot int skip_count); 72641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 72741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Helpers for suppressing warnings on unreachable code or constant 72841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// condition. 7291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 73041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Always returns true. 73141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotGTEST_API_ bool AlwaysTrue(); 7321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 73341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Always returns false. 73441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline bool AlwaysFalse() { return !AlwaysTrue(); } 73541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 73641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Helper for suppressing false warning from Clang on a const char* 73741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// variable declared in a conditional expression always being NULL in 73841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the else branch. 73941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct GTEST_API_ ConstCharPtr { 74041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ConstCharPtr(const char* str) : value(str) {} 74141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot operator bool() const { return true; } 74241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const char* value; 74341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 74441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 74541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A simple Linear Congruential Generator for generating random 74641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// numbers with a uniform distribution. Unlike rand() and srand(), it 74741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// doesn't use global state (and therefore can't interfere with user 74841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// code). Unlike rand_r(), it's portable. An LCG isn't very random, 74941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// but it's good enough for our purposes. 75041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTEST_API_ Random { 75141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 75241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static const UInt32 kMaxRange = 1u << 31; 75341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 75441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot explicit Random(UInt32 seed) : state_(seed) {} 75541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 75641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void Reseed(UInt32 seed) { state_ = seed; } 75741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 75841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Generates a random number from [0, range). Crashes if 'range' is 75941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 0 or greater than kMaxRange. 76041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot UInt32 Generate(UInt32 range); 76141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 76241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 76341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot UInt32 state_; 76441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_COPY_AND_ASSIGN_(Random); 76541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 76641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 76741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a 76841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// compiler error iff T1 and T2 are different types. 76941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T1, typename T2> 77041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct CompileAssertTypesEqual; 77141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 77241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 77341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct CompileAssertTypesEqual<T, T> { 77441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 77541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 77641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Removes the reference from a type if it is a reference type, 77741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// otherwise leaves it unchanged. This is the same as 77841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// tr1::remove_reference, which is not widely available yet. 77941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 78041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct RemoveReference { typedef T type; }; // NOLINT 78141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 78241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct RemoveReference<T&> { typedef T type; }; // NOLINT 78341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 78441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A handy wrapper around RemoveReference that works when the argument 78541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// T depends on template parameters. 78641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_REMOVE_REFERENCE_(T) \ 78741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename ::testing::internal::RemoveReference<T>::type 78841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 78941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Removes const from a type if it is a const type, otherwise leaves 79041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// it unchanged. This is the same as tr1::remove_const, which is not 79141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// widely available yet. 79241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 79341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct RemoveConst { typedef T type; }; // NOLINT 79441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 79541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct RemoveConst<const T> { typedef T type; }; // NOLINT 79641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 79741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// MSVC 8.0, Sun C++, and IBM XL C++ have a bug which causes the above 79841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// definition to fail to remove the const in 'const int[3]' and 'const 79941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// char[3][4]'. The following specialization works around the bug. 80041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// However, it causes trouble with GCC and thus needs to be 80141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// conditionally compiled. 80241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if defined(_MSC_VER) || defined(__SUNPRO_CC) || defined(__IBMCPP__) 80341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, size_t N> 80441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct RemoveConst<const T[N]> { 80541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typedef typename RemoveConst<T>::type type[N]; 80641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 80741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 80841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 80941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A handy wrapper around RemoveConst that works when the argument 81041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// T depends on template parameters. 81141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_REMOVE_CONST_(T) \ 81241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename ::testing::internal::RemoveConst<T>::type 81341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 81441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Turns const U&, U&, const U, and U all into U. 81541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \ 81641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_REMOVE_CONST_(GTEST_REMOVE_REFERENCE_(T)) 81741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 81841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Adds reference to a type if it is not a reference type, 81941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// otherwise leaves it unchanged. This is the same as 82041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// tr1::add_reference, which is not widely available yet. 82141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 82241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct AddReference { typedef T& type; }; // NOLINT 82341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 82441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct AddReference<T&> { typedef T& type; }; // NOLINT 82541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 82641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A handy wrapper around AddReference that works when the argument T 82741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// depends on template parameters. 82841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_ADD_REFERENCE_(T) \ 82941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename ::testing::internal::AddReference<T>::type 83041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 83141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Adds a reference to const on top of T as necessary. For example, 83241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// it transforms 83341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 83441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// char ==> const char& 83541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// const char ==> const char& 83641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// char& ==> const char& 83741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// const char& ==> const char& 83841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 83941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The argument T must depend on some template parameters. 84041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_REFERENCE_TO_CONST_(T) \ 84141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_ADD_REFERENCE_(const GTEST_REMOVE_REFERENCE_(T)) 84241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 84341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ImplicitlyConvertible<From, To>::value is a compile-time bool 84441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// constant that's true iff type From can be implicitly converted to 84541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// type To. 84641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename From, typename To> 84741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass ImplicitlyConvertible { 84841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 84941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We need the following helper functions only for their types. 85041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // They have no implementations. 85141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 85241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // MakeFrom() is an expression whose type is From. We cannot simply 85341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // use From(), as the type From may not have a public default 85441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // constructor. 85541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static From MakeFrom(); 85641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 85741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // These two functions are overloaded. Given an expression 85841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Helper(x), the compiler will pick the first version if x can be 85941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // implicitly converted to type To; otherwise it will pick the 86041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // second version. 86141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 86241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // The first version returns a value of size 1, and the second 86341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // version returns a value of size 2. Therefore, by checking the 86441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // size of Helper(x), which can be done at compile time, we can tell 86541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // which version of Helper() is used, and hence whether x can be 86641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // implicitly converted to type To. 86741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static char Helper(To); 86841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static char (&Helper(...))[2]; // NOLINT 86941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 87041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // We have to put the 'public' section after the 'private' section, 87141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // or MSVC refuses to compile the code. 87241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 87341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // MSVC warns about implicitly converting from double to int for 87441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // possible loss of data, so we need to temporarily disable the 87541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // warning. 87641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#ifdef _MSC_VER 87741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(push) // Saves the current warning state. 87841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(disable:4244) // Temporarily disables warning 4244. 87941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 88041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static const bool value = 88141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 88241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# pragma warning(pop) // Restores the warning state. 88341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#elif defined(__BORLANDC__) 88441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // C++Builder cannot use member overload resolution during template 88541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // instantiation. The simplest workaround is to use its C++0x type traits 88641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // functions (C++Builder 2009 and above only). 88741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static const bool value = __is_convertible(From, To); 88841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#else 88941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static const bool value = 89041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 89141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // _MSV_VER 89241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 89341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename From, typename To> 89441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotconst bool ImplicitlyConvertible<From, To>::value; 89541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 89641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// IsAProtocolMessage<T>::value is a compile-time bool constant that's 89741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// true iff T is type ProtocolMessage, proto2::Message, or a subclass 89841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// of those. 89941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T> 90041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct IsAProtocolMessage 90141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot : public bool_constant< 90241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ImplicitlyConvertible<const T*, const ::ProtocolMessage*>::value || 90341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ImplicitlyConvertible<const T*, const ::proto2::Message*>::value> { 90441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 90541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 90641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// When the compiler sees expression IsContainerTest<C>(0), if C is an 90741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// STL-style container class, the first overload of IsContainerTest 90841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// will be viable (since both C::iterator* and C::const_iterator* are 90941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// valid types and NULL can be implicitly converted to them). It will 91041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// be picked over the second overload as 'int' is a perfect match for 91141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the type of argument 0. If C::iterator or C::const_iterator is not 91241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// a valid type, the first overload is not viable, and the second 91341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// overload will be picked. Therefore, we can determine whether C is 91441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// a container class by checking the type of IsContainerTest<C>(0). 91541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The value of the expression is insignificant. 91641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 91741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Note that we look for both C::iterator and C::const_iterator. The 91841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// reason is that C++ injects the name of a class as a member of the 91941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// class itself (e.g. you can refer to class iterator as either 92041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 'iterator' or 'iterator::iterator'). If we look for C::iterator 92141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// only, for example, we would mistakenly think that a class named 92241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// iterator is an STL container. 92341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 92441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Also note that the simpler approach of overloading 92541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// IsContainerTest(typename C::const_iterator*) and 92641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// IsContainerTest(...) doesn't work with Visual Age C++ and Sun C++. 92741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottypedef int IsContainer; 92841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <class C> 92941d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotIsContainer IsContainerTest(int /* dummy */, 93041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename C::iterator* /* it */ = NULL, 93141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typename C::const_iterator* /* const_it */ = NULL) { 93241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return 0; 93341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 93441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 93541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottypedef char IsNotContainer; 93641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <class C> 93741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotIsNotContainer IsContainerTest(long /* dummy */) { return '\0'; } 93841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 93941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// EnableIf<condition>::type is void when 'Cond' is true, and 94041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// undefined when 'Cond' is false. To use SFINAE to make a function 94141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// overload only apply when a particular expression is true, add 94241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// "typename EnableIf<expression>::type* = 0" as the last parameter. 94341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate<bool> struct EnableIf; 94441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate<> struct EnableIf<true> { typedef void type; }; // NOLINT 94541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 94641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Utilities for native arrays. 94741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 94841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ArrayEq() compares two k-dimensional native arrays using the 94941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// elements' operator==, where k can be any integer >= 0. When k is 95041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 0, ArrayEq() degenerates into comparing a single pair of values. 95141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 95241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 95341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool ArrayEq(const T* lhs, size_t size, const U* rhs); 95441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 95541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This generic version is used when k is 0. 95641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 95741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline bool ArrayEq(const T& lhs, const U& rhs) { return lhs == rhs; } 95841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 95941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This overload is used when k >= 1. 96041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U, size_t N> 96141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline bool ArrayEq(const T(&lhs)[N], const U(&rhs)[N]) { 96241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return internal::ArrayEq(lhs, N, rhs); 96341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 96441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 96541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This helper reduces code bloat. If we instead put its logic inside 96641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the previous ArrayEq() function, arrays with different sizes would 96741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// lead to different copies of the template code. 96841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 96941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool ArrayEq(const T* lhs, size_t size, const U* rhs) { 97041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i != size; i++) { 97141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (!internal::ArrayEq(lhs[i], rhs[i])) 97241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return false; 97341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 97441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return true; 97541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 97641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 97741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Finds the first element in the iterator range [begin, end) that 97841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// equals elem. Element may be a native array type itself. 97941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename Iter, typename Element> 98041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotIter ArrayAwareFind(Iter begin, Iter end, const Element& elem) { 98141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (Iter it = begin; it != end; ++it) { 98241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (internal::ArrayEq(*it, elem)) 98341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return it; 98441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 98541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return end; 98641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 98741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 98841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// CopyArray() copies a k-dimensional native array using the elements' 98941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// operator=, where k can be any integer >= 0. When k is 0, 99041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// CopyArray() degenerates into copying a single value. 99141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 99241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 99341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid CopyArray(const T* from, size_t size, U* to); 99441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 99541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This generic version is used when k is 0. 99641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 99741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline void CopyArray(const T& from, U* to) { *to = from; } 99841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 99941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This overload is used when k >= 1. 100041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U, size_t N> 100141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotinline void CopyArray(const T(&from)[N], U(*to)[N]) { 100241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::CopyArray(from, N, *to); 100341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 100441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 100541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This helper reduces code bloat. If we instead put its logic inside 100641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// the previous CopyArray() function, arrays with different sizes 100741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// would lead to different copies of the template code. 100841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename T, typename U> 100941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid CopyArray(const T* from, size_t size, U* to) { 101041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot for (size_t i = 0; i != size; i++) { 101141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot internal::CopyArray(from[i], to + i); 101241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 101341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 101441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 101541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The relation between an NativeArray object (see below) and the 101641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// native array it represents. 101741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotenum RelationToSource { 101841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot kReference, // The NativeArray references the native array. 101941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot kCopy // The NativeArray makes a copy of the native array and 102041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // owns the copy. 102141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 102241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 102341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Adapts a native array to a read-only STL-style container. Instead 102441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// of the complete STL container concept, this adaptor only implements 102541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// members useful for Google Mock's container matchers. New members 102641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// should be added as needed. To simplify the implementation, we only 102741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// support Element being a raw type (i.e. having no top-level const or 102841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// reference modifier). It's the client's responsibility to satisfy 102941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// this requirement. Element can be an array type itself (hence 103041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// multi-dimensional arrays are supported). 103141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabottemplate <typename Element> 103241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass NativeArray { 103341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: 103441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // STL-style container typedefs. 103541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typedef Element value_type; 103641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typedef Element* iterator; 103741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot typedef const Element* const_iterator; 103841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 103941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Constructs from a native array. 104041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot NativeArray(const Element* array, size_t count, RelationToSource relation) { 104141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Init(array, count, relation); 104241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 104341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 104441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Copy constructor. 104541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot NativeArray(const NativeArray& rhs) { 104641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Init(rhs.array_, rhs.size_, rhs.relation_to_source_); 104741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 104841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 104941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ~NativeArray() { 105041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Ensures that the user doesn't instantiate NativeArray with a 105141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // const or reference type. 105241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static_cast<void>(StaticAssertTypeEqHelper<Element, 105341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_REMOVE_REFERENCE_AND_CONST_(Element)>()); 105441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (relation_to_source_ == kCopy) 105541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot delete[] array_; 105641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 105741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 105841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // STL-style container methods. 105941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot size_t size() const { return size_; } 106041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const_iterator begin() const { return array_; } 106141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const_iterator end() const { return array_ + size_; } 106241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot bool operator==(const NativeArray& rhs) const { 106341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return size() == rhs.size() && 106441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ArrayEq(begin(), size(), rhs.begin()); 106541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 106641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 106741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot private: 106841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Initializes this object; makes a copy of the input array if 106941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // 'relation' is kCopy. 107041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot void Init(const Element* array, size_t a_size, RelationToSource relation) { 107141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (relation == kReference) { 107241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot array_ = array; 107341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } else { 107441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Element* const copy = new Element[a_size]; 107541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CopyArray(array, a_size, copy); 107641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot array_ = copy; 107741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 107841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot size_ = a_size; 107941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot relation_to_source_ = relation; 108041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 108141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 108241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot const Element* array_; 108341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot size_t size_; 108441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot RelationToSource relation_to_source_; 108541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 108641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_ASSIGN_(NativeArray); 108741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 10881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace internal 10901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace testing 10911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 109241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_MESSAGE_AT_(file, line, message, result_type) \ 109341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::testing::internal::AssertHelper(result_type, file, line, message) \ 10941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania = ::testing::Message() 10951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 109641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_MESSAGE_(message, result_type) \ 109741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type) 109841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 10991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_FATAL_FAILURE_(message) \ 110041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure) 11011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_NONFATAL_FAILURE_(message) \ 110341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure) 11041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_SUCCESS_(message) \ 110641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess) 11071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Suppresses MSVC warnings 4072 (unreachable code) for the code following 11091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// statement if it returns or throws (or doesn't return or throw in some 11101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// situations). 111141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \ 11121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (::testing::internal::AlwaysTrue()) { statement; } 11131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_THROW_(statement, expected_exception, fail) \ 11151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 111641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (::testing::internal::ConstCharPtr gtest_msg = "") { \ 11171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool gtest_caught_expected = false; \ 11181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try { \ 111941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 11201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania catch (expected_exception const&) { \ 11221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania gtest_caught_expected = true; \ 11231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania catch (...) { \ 112541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot gtest_msg.value = \ 112641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Expected: " #statement " throws an exception of type " \ 112741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot #expected_exception ".\n Actual: it throws a different type."; \ 11281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ 11291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!gtest_caught_expected) { \ 113141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot gtest_msg.value = \ 113241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "Expected: " #statement " throws an exception of type " \ 113341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot #expected_exception ".\n Actual: it throws nothing."; \ 11341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ 11351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else \ 11371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \ 113841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fail(gtest_msg.value) 11391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_NO_THROW_(statement, fail) \ 11411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 114241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (::testing::internal::AlwaysTrue()) { \ 11431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try { \ 114441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 11451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania catch (...) { \ 11471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ 11481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else \ 11501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \ 115141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fail("Expected: " #statement " doesn't throw an exception.\n" \ 115241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " Actual: it throws.") 11531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_ANY_THROW_(statement, fail) \ 11551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 115641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (::testing::internal::AlwaysTrue()) { \ 11571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania bool gtest_caught_any = false; \ 11581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try { \ 115941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 11601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania catch (...) { \ 11621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania gtest_caught_any = true; \ 11631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (!gtest_caught_any) { \ 11651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \ 11661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else \ 11681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__): \ 116941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fail("Expected: " #statement " throws an exception.\n" \ 117041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " Actual: it doesn't.") 11711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 117341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Implements Boolean test assertions such as EXPECT_TRUE. expression can be 117441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// either a boolean expression or an AssertionResult. text is a textual 117541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// represenation of expression as it was passed into the EXPECT_TRUE. 117641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \ 11771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 117841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (const ::testing::AssertionResult gtest_ar_ = \ 117941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ::testing::AssertionResult(expression)) \ 11801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ; \ 11811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else \ 118241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fail(::testing::internal::GetBoolAssertionFailureMessage(\ 118341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot gtest_ar_, text, #actual, #expected).c_str()) 11841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \ 11861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 118741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if (::testing::internal::AlwaysTrue()) { \ 11881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \ 118941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 11901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \ 11911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \ 11921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } \ 11931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else \ 11941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__): \ 119541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot fail("Expected: " #statement " doesn't generate new fatal " \ 119641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot "failures in the current thread.\n" \ 119741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot " Actual: it does.") 11981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 11991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Expands to the name of the class that implements the given test. 12001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \ 12011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania test_case_name##_##test_name##_Test 12021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Helper macro for defining tests. 12041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\ 12051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\ 12061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public:\ 12071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\ 12081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private:\ 12091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TestBody();\ 121041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;\ 12111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_DISALLOW_COPY_AND_ASSIGN_(\ 12121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\ 12131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania};\ 12141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania\ 12151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\ 12161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::test_info_ =\ 12171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ::testing::internal::MakeAndRegisterTestInfo(\ 121841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot #test_case_name, #test_name, NULL, NULL, \ 12191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (parent_id), \ 12201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parent_class::SetUpTestCase, \ 12211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania parent_class::TearDownTestCase, \ 12221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania new ::testing::internal::TestFactoryImpl<\ 12231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\ 12241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() 12251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 12261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ 1227