17ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Copyright 2005, Google Inc. 27ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// All rights reserved. 37ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 47ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Redistribution and use in source and binary forms, with or without 57ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// modification, are permitted provided that the following conditions are 67ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// met: 77ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 87ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// * Redistributions of source code must retain the above copyright 97ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// notice, this list of conditions and the following disclaimer. 107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// * Redistributions in binary form must reproduce the above 117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// copyright notice, this list of conditions and the following disclaimer 127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// in the documentation and/or other materials provided with the 137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// distribution. 147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// * Neither the name of Google Inc. nor the names of its 157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// contributors may be used to endorse or promote products derived from 167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// this software without specific prior written permission. 177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee) 317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The Google C++ Testing Framework (Google Test) 337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This header file declares functions and macros used internally by 357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Google Test. They are subject to change without notice. 367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ 387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ 397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 40b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-port.h" 417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 42e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_OS_LINUX 43b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <stdlib.h> 44b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <sys/types.h> 45b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <sys/wait.h> 46b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <unistd.h> 477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif // GTEST_OS_LINUX 487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <ctype.h> 507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <string.h> 517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <iomanip> 527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <limits> 537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <set> 547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 55b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-string.h" 56b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-filepath.h" 57b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-type-util.h" 587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5957f7dafcb769852a21870148e91535421c45d037Alexey Samsonov#if !GTEST_NO_LLVM_RAW_OSTREAM 609c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin#include "llvm/Support/raw_os_ostream.h" 6157f7dafcb769852a21870148e91535421c45d037Alexey Samsonov#endif 629c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin 637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Due to C++ preprocessor weirdness, we need double indirection to 647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// concatenate two tokens when one of them is __LINE__. Writing 657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// foo ## __LINE__ 677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// will result in the token foo__LINE__, instead of foo followed by 697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the current line number. For more details, see 707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6 717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar) 727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar 737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Google Test defines the testing::Message class to allow construction of 757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// test messages via the << operator. The idea is that anything 767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// streamable to std::ostream can be streamed to a testing::Message. 777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This allows a user to use his own types in Google Test assertions by 787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// overloading the << operator. 797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// util/gtl/stl_logging-inl.h overloads << for STL containers. These 817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// overloads cannot be defined in the std namespace, as that will be 827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// undefined behavior. Therefore, they are defined in the global 837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// namespace instead. 847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// C++'s symbol lookup rule (i.e. Koenig lookup) says that these 867ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// overloads are visible in either the std namespace or the global 877ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// namespace, but not other namespaces, including the testing 887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// namespace which Google Test's Message class is in. 897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// To allow STL containers (and other types that has a << operator 917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// defined in the global namespace) to be used in Google Test assertions, 927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// testing::Message must access the custom << operator from the global 937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// namespace. Hence this helper function. 947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Note: Jeffrey Yasskin suggested an alternative fix by "using 967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// ::operator<<;" in the definition of Message's operator<<. That fix 977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// doesn't require a helper function, but unfortunately doesn't 987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// compile with MSVC. 999c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin 1009c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin// LLVM INTERNAL CHANGE: To allow operator<< to work with both 1019c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin// std::ostreams and LLVM's raw_ostreams, we define a special 1029c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin// std::ostream with an implicit conversion to raw_ostream& and stream 1039c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin// to that. This causes the compiler to prefer std::ostream overloads 1049c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin// but still find raw_ostream& overloads. 10557f7dafcb769852a21870148e91535421c45d037Alexey Samsonov#if !GTEST_NO_LLVM_RAW_OSTREAM 1069c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskinnamespace llvm { 1079c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskinclass convertible_fwd_ostream : public std::ostream { 1089c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin raw_os_ostream ros_; 1099c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin 1109c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskinpublic: 1119c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin convertible_fwd_ostream(std::ostream& os) 112955f9387516da03b51337b126bebb43567141173Benjamin Kramer : std::ostream(os.rdbuf()), ros_(*this) {} 1139c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin operator raw_ostream&() { return ros_; } 1149c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin}; 1159c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin} 1167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename T> 1177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmaninline void GTestStreamToHelper(std::ostream* os, const T& val) { 1189c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin llvm::convertible_fwd_ostream cos(*os); 1199c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin cos << val; 1207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman} 12157f7dafcb769852a21870148e91535421c45d037Alexey Samsonov#else 12257f7dafcb769852a21870148e91535421c45d037Alexey Samsonovtemplate <typename T> 12357f7dafcb769852a21870148e91535421c45d037Alexey Samsonovinline void GTestStreamToHelper(std::ostream* os, const T& val) { 12457f7dafcb769852a21870148e91535421c45d037Alexey Samsonov *os << val; 12557f7dafcb769852a21870148e91535421c45d037Alexey Samsonov} 12657f7dafcb769852a21870148e91535421c45d037Alexey Samsonov#endif 1277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 128b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadclass ProtocolMessage; 129b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadnamespace proto2 { class Message; } 130b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmannamespace testing { 1327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 133b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Forward declarations. 1347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 135190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramerclass AssertionResult; // Result of an assertion. 1367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass Message; // Represents a failure message. 1377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass Test; // Represents a test. 1387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TestInfo; // Information about a test. 139190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramerclass TestPartResult; // Result of a test part. 1407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass UnitTest; // A collection of test cases. 1417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 142b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T> 143b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad::std::string PrintToString(const T& value); 144b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmannamespace internal { 1467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanstruct TraceInfo; // Information about a trace point. 1487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass ScopedTrace; // Implements scoped trace. 1497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TestInfoImpl; // Opaque implementation of TestInfo 1507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass UnitTestImpl; // Opaque implementation of UnitTest 1517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// How many times InitGoogleTest() has been called. 1537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanextern int g_init_gtest_count; 1547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The text used in failure messages to indicate the start of the 1567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// stack trace. 15757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ extern const char kStackTraceMarker[]; 1587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// A secret type that Google Test users don't know about. It has no 1607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// definition on purpose. Therefore it's impossible to create a 1617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Secret object, which is what we want. 1627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass Secret; 1637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Two overloaded helpers for checking at compile time whether an 1657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// expression is a null pointer literal (i.e. NULL or any 0-valued 1667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// compile-time integral constant). Their return values have 1677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// different sizes, so we can use sizeof() to test which version is 1687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// picked by the compiler. These helpers have no implementations, as 1697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// we only need their signatures. 1707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 1717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Given IsNullLiteralHelper(x), the compiler will pick the first 1727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// version if x can be implicitly converted to Secret*, and pick the 1737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// second version otherwise. Since Secret is a secret and incomplete 1747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// type, the only expression a user can write that has type Secret* is 1757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// a null pointer literal. Therefore, we know that x is a null 1767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// pointer literal if and only if the first version is picked by the 1777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// compiler. 1787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanchar IsNullLiteralHelper(Secret* p); 1797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanchar (&IsNullLiteralHelper(...))[2]; // NOLINT 1807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// A compile-time bool constant that is true if and only if x is a 1827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// null pointer literal (i.e. NULL or any 0-valued compile-time 1837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// integral constant). 18457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#ifdef GTEST_ELLIPSIS_NEEDS_POD_ 18557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// We lose support for NULL detection where the compiler doesn't like 18657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// passing non-POD classes through ellipsis (...). 187b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_IS_NULL_LITERAL_(x) false 1887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else 189b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_IS_NULL_LITERAL_(x) \ 1907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1) 19157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#endif // GTEST_ELLIPSIS_NEEDS_POD_ 1927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Appends the user-supplied message to the Google-Test-generated message. 19457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ String AppendUserMessage(const String& gtest_msg, 19557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer const Message& user_msg); 1967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// A helper class for creating scoped traces in user programs. 19857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramerclass GTEST_API_ ScopedTrace { 1997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public: 2007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // The c'tor pushes the given source file location and message onto 2017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // a trace stack maintained by Google Test. 2027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman ScopedTrace(const char* file, int line, const Message& message); 2037ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 2047ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // The d'tor pops the info pushed by the c'tor. 2057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 2067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Note that the d'tor is not virtual in order to be efficient. 2077ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Don't inherit from ScopedTrace! 2087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman ~ScopedTrace(); 2097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 2107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman private: 2117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace); 2127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman} GTEST_ATTRIBUTE_UNUSED_; // A ScopedTrace object does its job in its 2137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // c'tor and d'tor. Therefore it doesn't 2147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // need to be used otherwise. 2157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 2167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Converts a streamable value to a String. A NULL pointer is 2177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// converted to "(null)". When the input value is a ::string, 2187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// ::std::string, ::wstring, or ::std::wstring object, each NUL 2197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// character in it is replaced with "\\0". 2207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Declared here but defined in gtest.h, so that it has access 2217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// to the definition of the Message class, required by the ARM 2227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// compiler. 2237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename T> 2247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanString StreamableToString(const T& streamable); 2257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 226b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// The Symbian compiler has a bug that prevents it from selecting the 227b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// correct overload of FormatForComparisonFailureMessage (see below) 228b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// unless we pass the first argument by reference. If we do that, 229b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// however, Visual Age C++ 10.1 generates a compiler error. Therefore 230b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// we only apply the work-around for Symbian. 231b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#if defined(__SYMBIAN32__) 232b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_CREF_WORKAROUND_ const& 2337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else 234b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_CREF_WORKAROUND_ 235b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#endif 2367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 237b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// When this operand is a const char* or char*, if the other operand 2387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// is a ::std::string or ::string, we print this operand as a C string 239b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// rather than a pointer (we do the same for wide strings); otherwise 240b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// we print it as a pointer to be safe. 2417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 2427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This internal macro is used to avoid duplicated code. 2437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_FORMAT_IMPL_(operand2_type, operand1_printer)\ 2447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmaninline String FormatForComparisonFailureMessage(\ 245b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad operand2_type::value_type* GTEST_CREF_WORKAROUND_ str, \ 246b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad const operand2_type& /*operand2*/) {\ 2477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return operand1_printer(str);\ 2487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}\ 2497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmaninline String FormatForComparisonFailureMessage(\ 250b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad const operand2_type::value_type* GTEST_CREF_WORKAROUND_ str, \ 251b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad const operand2_type& /*operand2*/) {\ 2527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return operand1_printer(str);\ 2537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman} 2547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 2557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanGTEST_FORMAT_IMPL_(::std::string, String::ShowCStringQuoted) 2567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#if GTEST_HAS_STD_WSTRING 2577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanGTEST_FORMAT_IMPL_(::std::wstring, String::ShowWideCStringQuoted) 2587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif // GTEST_HAS_STD_WSTRING 2597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 2607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#if GTEST_HAS_GLOBAL_STRING 2617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanGTEST_FORMAT_IMPL_(::string, String::ShowCStringQuoted) 2627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif // GTEST_HAS_GLOBAL_STRING 2637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#if GTEST_HAS_GLOBAL_WSTRING 2647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanGTEST_FORMAT_IMPL_(::wstring, String::ShowWideCStringQuoted) 2657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif // GTEST_HAS_GLOBAL_WSTRING 2667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 2677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#undef GTEST_FORMAT_IMPL_ 2687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 269b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// The next four overloads handle the case where the operand being 270b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// printed is a char/wchar_t pointer and the other operand is not a 271b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// string/wstring object. In such cases, we just print the operand as 272b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// a pointer to be safe. 273b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_FORMAT_CHAR_PTR_IMPL_(CharType) \ 274b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad template <typename T> \ 275b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad String FormatForComparisonFailureMessage(CharType* GTEST_CREF_WORKAROUND_ p, \ 276b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad const T&) { \ 277b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad return PrintToString(static_cast<const void*>(p)); \ 278b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } 279b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 280b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadGTEST_FORMAT_CHAR_PTR_IMPL_(char) 281b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadGTEST_FORMAT_CHAR_PTR_IMPL_(const char) 282b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadGTEST_FORMAT_CHAR_PTR_IMPL_(wchar_t) 283b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadGTEST_FORMAT_CHAR_PTR_IMPL_(const wchar_t) 284b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 285b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#undef GTEST_FORMAT_CHAR_PTR_IMPL_ 286b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 2877ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Constructs and returns the message for an equality assertion 2887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure. 2897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 2907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The first four parameters are the expressions used in the assertion 2917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// and their values, as strings. For example, for ASSERT_EQ(foo, bar) 2927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// where foo is 5 and bar is 6, we have: 2937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 2947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// expected_expression: "foo" 2957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// actual_expression: "bar" 2967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// expected_value: "5" 2977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// actual_value: "6" 2987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 29994c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru// The ignoring_case parameter is true iff the assertion is a 3007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// *_STRCASEEQ*. When it's true, the string " (ignoring case)" will 3017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// be inserted into the message. 30257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ AssertionResult EqFailure(const char* expected_expression, 30357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer const char* actual_expression, 30457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer const String& expected_value, 30557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer const String& actual_value, 30657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer bool ignoring_case); 30757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer 30857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// Constructs a failure message for Boolean assertions such as EXPECT_TRUE. 30957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ String GetBoolAssertionFailureMessage( 31057240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer const AssertionResult& assertion_result, 31157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer const char* expression_text, 31257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer const char* actual_predicate_value, 31357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer const char* expected_predicate_value); 3147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This template class represents an IEEE floating-point number 3167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// (either single-precision or double-precision, depending on the 3177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// template parameters). 3187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 3197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The purpose of this class is to do more sophisticated number 3207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// comparison. (Due to round-off error, etc, it's very unlikely that 3217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// two floating-points will be equal exactly. Hence a naive 3227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// comparison by the == operation often doesn't work.) 3237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 3247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Format of IEEE floating-point: 3257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 3267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The most-significant bit being the leftmost, an IEEE 3277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// floating-point looks like 3287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 3297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// sign_bit exponent_bits fraction_bits 3307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 3317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Here, sign_bit is a single bit that designates the sign of the 3327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// number. 3337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 3347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// For float, there are 8 exponent bits and 23 fraction bits. 3357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 3367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// For double, there are 11 exponent bits and 52 fraction bits. 3377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 3387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// More details can be found at 3397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// http://en.wikipedia.org/wiki/IEEE_floating-point_standard. 3407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 3417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Template parameter: 3427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 3437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// RawType: the raw floating-point type (either float or double) 3447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename RawType> 3457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass FloatingPoint { 3467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public: 3477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Defines the unsigned integer type that has the same size as the 3487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // floating point number. 3497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman typedef typename TypeWithSize<sizeof(RawType)>::UInt Bits; 3507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Constants. 3527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // # of bits in a number. 3547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static const size_t kBitCount = 8*sizeof(RawType); 3557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // # of fraction bits in a number. 3577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static const size_t kFractionBitCount = 3587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman std::numeric_limits<RawType>::digits - 1; 3597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // # of exponent bits in a number. 3617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount; 3627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // The mask for the sign bit. 3647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1); 3657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // The mask for the fraction bits. 3677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static const Bits kFractionBitMask = 3687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman ~static_cast<Bits>(0) >> (kExponentBitCount + 1); 3697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // The mask for the exponent bits. 3717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask); 3727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // How many ULP's (Units in the Last Place) we want to tolerate when 3747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // comparing two numbers. The larger the value, the more error we 3757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // allow. A 0 value means that two numbers must be exactly the same 3767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // to be considered equal. 3777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 3787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // The maximum error of a single floating-point operation is 0.5 3797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // units in the last place. On Intel CPU's, all floating-point 3807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // calculations are done with 80-bit precision, while double has 64 3817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // bits. Therefore, 4 should be enough for ordinary use. 3827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 3837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // See the following article for more details on ULP: 3847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm. 3857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static const size_t kMaxUlps = 4; 3867ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3877ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Constructs a FloatingPoint from a raw floating-point number. 3887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 3897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // On an Intel CPU, passing a non-normalized NAN (Not a Number) 3907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // around may change its bits, although the new value is guaranteed 3917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // to be also a NAN. Therefore, don't expect this constructor to 3927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // preserve the bits in x when x is a NAN. 393190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer explicit FloatingPoint(const RawType& x) { u_.value_ = x; } 3947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Static methods 3967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 3977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Reinterprets a bit pattern as a floating-point number. 3987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 3997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // This function is needed to test the AlmostEquals() method. 4007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static RawType ReinterpretBits(const Bits bits) { 4017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman FloatingPoint fp(0); 402190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer fp.u_.bits_ = bits; 403190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer return fp.u_.value_; 4047ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 4057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 4067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Returns the floating-point number that represent positive infinity. 4077ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static RawType Infinity() { 4087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return ReinterpretBits(kExponentBitMask); 4097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 4107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 4117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Non-static methods 4127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 4137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Returns the bits that represents this number. 414190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer const Bits &bits() const { return u_.bits_; } 4157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 4167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Returns the exponent bits of this number. 417190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer Bits exponent_bits() const { return kExponentBitMask & u_.bits_; } 4187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 4197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Returns the fraction bits of this number. 420190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer Bits fraction_bits() const { return kFractionBitMask & u_.bits_; } 4217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 4227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Returns the sign bit of this number. 423190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer Bits sign_bit() const { return kSignBitMask & u_.bits_; } 4247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 42594c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru // Returns true iff this is NAN (not a number). 4267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman bool is_nan() const { 4277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // It's a NAN if the exponent bits are all ones and the fraction 4287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // bits are not entirely zeros. 4297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0); 4307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 4317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 43294c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru // Returns true iff this number is at most kMaxUlps ULP's away from 4337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // rhs. In particular, this function: 4347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 4357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // - returns false if either number is (or both are) NAN. 4367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // - treats really large numbers as almost equal to infinity. 4377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // - thinks +0.0 and -0.0 are 0 DLP's apart. 4387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman bool AlmostEquals(const FloatingPoint& rhs) const { 4397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // The IEEE standard says that any comparison operation involving 4407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // a NAN must return false. 4417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman if (is_nan() || rhs.is_nan()) return false; 4427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 443190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_) 444190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer <= kMaxUlps; 4457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 4467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 4477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman private: 448190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer // The data type used to store the actual floating-point number. 449190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer union FloatingPointUnion { 450190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer RawType value_; // The raw floating-point number. 451190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer Bits bits_; // The bits that represent the number. 452190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer }; 453190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer 4547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Converts an integer from the sign-and-magnitude representation to 4557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // the biased representation. More precisely, let N be 2 to the 4567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // power of (kBitCount - 1), an integer x is represented by the 4577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // unsigned number x + N. 4587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 4597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // For instance, 4607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 4617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // -N + 1 (the most negative number representable using 4627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // sign-and-magnitude) is represented by 1; 4637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 0 is represented by N; and 4647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // N - 1 (the biggest number representable using 4657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // sign-and-magnitude) is represented by 2N - 1. 4667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 4677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Read http://en.wikipedia.org/wiki/Signed_number_representations 4687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // for more details on signed number representations. 4697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static Bits SignAndMagnitudeToBiased(const Bits &sam) { 4707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman if (kSignBitMask & sam) { 4717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // sam represents a negative number. 4727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return ~sam + 1; 4737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } else { 4747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // sam represents a positive number. 4757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return kSignBitMask | sam; 4767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 4777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 4787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 4797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Given two numbers in the sign-and-magnitude representation, 4807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // returns the distance between them as an unsigned number. 4817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits &sam1, 4827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const Bits &sam2) { 4837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const Bits biased1 = SignAndMagnitudeToBiased(sam1); 4847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const Bits biased2 = SignAndMagnitudeToBiased(sam2); 4857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1); 4867ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 4877ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 488190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer FloatingPointUnion u_; 4897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}; 4907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 4917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Typedefs the instances of the FloatingPoint template class that we 4927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// care to use. 4937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantypedef FloatingPoint<float> Float; 4947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantypedef FloatingPoint<double> Double; 4957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 4967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// In order to catch the mistake of putting tests that use different 4977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// test fixture classes in the same test case, we need to assign 4987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// unique IDs to fixture classes and compare them. The TypeId type is 4997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// used to hold such IDs. The user should treat TypeId as an opaque 5007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// type: the only operation allowed on TypeId values is to compare 5017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// them for equality using the == operator. 5027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantypedef const void* TypeId; 5037ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5047ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename T> 5057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TypeIdHelper { 5067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public: 5077ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // dummy_ must not have a const type. Otherwise an overly eager 5087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // compiler (e.g. MSVC 7.1 & 8.0) may try to merge 5097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // TypeIdHelper<T>::dummy_ for different Ts as an "optimization". 5107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static bool dummy_; 5117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}; 5127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename T> 5147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanbool TypeIdHelper<T>::dummy_ = false; 5157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// GetTypeId<T>() returns the ID of type T. Different values will be 5177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// returned for different types. Calling the function twice with the 5187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// same type argument is guaranteed to return the same ID. 5197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename T> 5207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanTypeId GetTypeId() { 5217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // The compiler is required to allocate a different 5227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // TypeIdHelper<T>::dummy_ variable for each T used to instantiate 5237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // the template. Therefore, the address of dummy_ is guaranteed to 5247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // be unique. 5257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return &(TypeIdHelper<T>::dummy_); 5267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman} 5277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns the type ID of ::testing::Test. Always call this instead 5297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// of GetTypeId< ::testing::Test>() to get the type ID of 5307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// ::testing::Test, as the latter may give the wrong result due to a 5317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// suspected linker bug when compiling Google Test as a Mac OS X 5327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// framework. 53357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ TypeId GetTestTypeId(); 5347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Defines the abstract factory interface that creates instances 5367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// of a Test object. 5377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TestFactoryBase { 5387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public: 5397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman virtual ~TestFactoryBase() {} 5407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Creates a test instance to run. The instance is both created and destroyed 5427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // within TestInfoImpl::Run() 5437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman virtual Test* CreateTest() = 0; 5447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman protected: 5467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman TestFactoryBase() {} 5477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman private: 5497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase); 5507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}; 5517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This class provides implementation of TeastFactoryBase interface. 5537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// It is used in TEST and TEST_F macros. 5547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <class TestClass> 5557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TestFactoryImpl : public TestFactoryBase { 5567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public: 5577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman virtual Test* CreateTest() { return new TestClass; } 5587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}; 5597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 560e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_OS_WINDOWS 5617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Predicate-formatters for implementing the HRESULT checking macros 5637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED} 5647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// We pass a long instead of HRESULT to avoid causing an 5657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// include dependency for the HRESULT type. 56657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ AssertionResult IsHRESULTSuccess(const char* expr, 56757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer long hr); // NOLINT 56857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ AssertionResult IsHRESULTFailure(const char* expr, 56957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer long hr); // NOLINT 5707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif // GTEST_OS_WINDOWS 5727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Types of SetUpTestCase() and TearDownTestCase() functions. 5747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantypedef void (*SetUpTestCaseFunc)(); 5757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantypedef void (*TearDownTestCaseFunc)(); 5767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 5777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Creates a new TestInfo object and registers it with Google Test; 5787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// returns the created object. 5797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 5807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Arguments: 5817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 5827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// test_case_name: name of the test case 5837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// name: name of the test 584b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// type_param the name of the test's type parameter, or NULL if 585b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// this is not a typed or a type-parameterized test. 586b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// value_param text representation of the test's value parameter, 587b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// or NULL if this is not a type-parameterized test. 5887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// fixture_class_id: ID of the test fixture class 5897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// set_up_tc: pointer to the function that sets up the test case 5907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// tear_down_tc: pointer to the function that tears down the test case 5917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// factory: pointer to the factory that creates a test object. 5927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The newly created TestInfo instance will assume 5937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// ownership of the factory object. 59457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ TestInfo* MakeAndRegisterTestInfo( 5957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const char* test_case_name, const char* name, 596b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad const char* type_param, 597b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad const char* value_param, 5987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman TypeId fixture_class_id, 5997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman SetUpTestCaseFunc set_up_tc, 6007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman TearDownTestCaseFunc tear_down_tc, 6017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman TestFactoryBase* factory); 6027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 60357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// If *pstr starts with the given prefix, modifies *pstr to be right 60457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// past the prefix and returns true; otherwise leaves *pstr unchanged 60557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// and returns false. None of pstr, *pstr, and prefix can be NULL. 606b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadGTEST_API_ bool SkipPrefix(const char* prefix, const char** pstr); 60757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer 608e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P 6097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 6107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// State of the definition of a type-parameterized test case. 61157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramerclass GTEST_API_ TypedTestCasePState { 6127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public: 6137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman TypedTestCasePState() : registered_(false) {} 6147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 6157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Adds the given test name to defined_test_names_ and return true 6167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // if the test case hasn't been registered; otherwise aborts the 6177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // program. 6187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman bool AddTestName(const char* file, int line, const char* case_name, 6197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const char* test_name) { 6207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman if (registered_) { 6217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman fprintf(stderr, "%s Test %s must be defined before " 6227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman "REGISTER_TYPED_TEST_CASE_P(%s, ...).\n", 6237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman FormatFileLocation(file, line).c_str(), test_name, case_name); 624e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer fflush(stderr); 625190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer posix::Abort(); 6267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 6277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman defined_test_names_.insert(test_name); 6287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return true; 6297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 6307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 6317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Verifies that registered_tests match the test names in 6327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // defined_test_names_; returns registered_tests if successful, or 6337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // aborts the program otherwise. 6347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const char* VerifyRegisteredTestNames( 6357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const char* file, int line, const char* registered_tests); 6367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 6377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman private: 6387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman bool registered_; 6397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman ::std::set<const char*> defined_test_names_; 6407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}; 6417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 6427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Skips to the first non-space char after the first comma in 'str'; 6437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// returns NULL if no comma is found in 'str'. 6447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmaninline const char* SkipComma(const char* str) { 6457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const char* comma = strchr(str, ','); 6467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman if (comma == NULL) { 6477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return NULL; 6487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 649b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad while (IsSpace(*(++comma))) {} 6507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return comma; 6517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman} 6527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 6537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns the prefix of 'str' before the first comma in it; returns 6547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the entire string if it contains no comma. 6557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmaninline String GetPrefixUntilComma(const char* str) { 6567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const char* comma = strchr(str, ','); 6577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return comma == NULL ? String(str) : String(str, comma - str); 6587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman} 6597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 6607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// TypeParameterizedTest<Fixture, TestSel, Types>::Register() 6617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// registers a list of type-parameterized tests with Google Test. The 6627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// return value is insignificant - we just need to return something 6637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// such that we can call this function in a namespace scope. 6647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 6657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Implementation note: The GTEST_TEMPLATE_ macro declares a template 6667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// template parameter. It's defined in gtest-type-util.h. 6677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <GTEST_TEMPLATE_ Fixture, class TestSel, typename Types> 6687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TypeParameterizedTest { 6697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public: 6707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // 'index' is the index of the test in the type list 'Types' 6717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // specified in INSTANTIATE_TYPED_TEST_CASE_P(Prefix, TestCase, 6727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Types). Valid values for 'index' are [0, N - 1] where N is the 6737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // length of Types. 6747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static bool Register(const char* prefix, const char* case_name, 6757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const char* test_names, int index) { 6767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman typedef typename Types::Head Type; 6777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman typedef Fixture<Type> FixtureClass; 6787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman typedef typename GTEST_BIND_(TestSel, Type) TestClass; 6797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 6807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // First, registers the first type-parameterized test in the type 6817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // list. 6827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman MakeAndRegisterTestInfo( 6837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman String::Format("%s%s%s/%d", prefix, prefix[0] == '\0' ? "" : "/", 6847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman case_name, index).c_str(), 6857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GetPrefixUntilComma(test_names).c_str(), 686b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad GetTypeName<Type>().c_str(), 687b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad NULL, // No value parameter. 6887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GetTypeId<FixtureClass>(), 6897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman TestClass::SetUpTestCase, 6907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman TestClass::TearDownTestCase, 6917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman new TestFactoryImpl<TestClass>); 6927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 6937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Next, recurses (at compile time) with the tail of the type list. 6947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return TypeParameterizedTest<Fixture, TestSel, typename Types::Tail> 6957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman ::Register(prefix, case_name, test_names, index + 1); 6967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 6977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}; 6987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 6997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The base case for the compile time recursion. 7007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <GTEST_TEMPLATE_ Fixture, class TestSel> 7017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TypeParameterizedTest<Fixture, TestSel, Types0> { 7027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public: 7037ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static bool Register(const char* /*prefix*/, const char* /*case_name*/, 7047ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const char* /*test_names*/, int /*index*/) { 7057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return true; 7067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 7077ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}; 7087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 7097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// TypeParameterizedTestCase<Fixture, Tests, Types>::Register() 7107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// registers *all combinations* of 'Tests' and 'Types' with Google 7117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Test. The return value is insignificant - we just need to return 7127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// something such that we can call this function in a namespace scope. 7137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <GTEST_TEMPLATE_ Fixture, typename Tests, typename Types> 7147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TypeParameterizedTestCase { 7157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public: 7167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman static bool Register(const char* prefix, const char* case_name, 7177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman const char* test_names) { 7187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman typedef typename Tests::Head Head; 7197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 7207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // First, register the first test in 'Test' for each type in 'Types'. 7217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman TypeParameterizedTest<Fixture, Head, Types>::Register( 7227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman prefix, case_name, test_names, 0); 7237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 7247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman // Next, recurses (at compile time) with the tail of the test list. 7257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return TypeParameterizedTestCase<Fixture, typename Tests::Tail, Types> 7267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman ::Register(prefix, case_name, SkipComma(test_names)); 7277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 7287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}; 7297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 7307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The base case for the compile time recursion. 7317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <GTEST_TEMPLATE_ Fixture, typename Types> 7327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TypeParameterizedTestCase<Fixture, Templates0, Types> { 7337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public: 734190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer static bool Register(const char* /*prefix*/, const char* /*case_name*/, 735190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer const char* /*test_names*/) { 7367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman return true; 7377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } 7387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}; 7397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 7407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P 7417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 7427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns the current OS stack trace as a String. 7437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 7447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The maximum number of stack frames to be included is specified by 7457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the gtest_stack_trace_depth flag. The skip_count parameter 7467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// specifies the number of top frames to be skipped, which doesn't 7477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// count against the number of frames to be included. 7487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// 7497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// For example, if Foo() calls Bar(), which in turn calls 7507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in 7517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't. 75257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ String GetCurrentOsStackTraceExceptTop(UnitTest* unit_test, 75357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer int skip_count); 7547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 755190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// Helpers for suppressing warnings on unreachable code or constant 756190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// condition. 7577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 758190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// Always returns true. 75957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ bool AlwaysTrue(); 760e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer 761190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// Always returns false. 762190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramerinline bool AlwaysFalse() { return !AlwaysTrue(); } 763190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer 764b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Helper for suppressing false warning from Clang on a const char* 765b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// variable declared in a conditional expression always being NULL in 766b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// the else branch. 767b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct GTEST_API_ ConstCharPtr { 768b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad ConstCharPtr(const char* str) : value(str) {} 769b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad operator bool() const { return true; } 770b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad const char* value; 771b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad}; 772b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 773190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// A simple Linear Congruential Generator for generating random 774190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// numbers with a uniform distribution. Unlike rand() and srand(), it 775190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// doesn't use global state (and therefore can't interfere with user 776190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// code). Unlike rand_r(), it's portable. An LCG isn't very random, 777190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// but it's good enough for our purposes. 77857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramerclass GTEST_API_ Random { 779190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer public: 780190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer static const UInt32 kMaxRange = 1u << 31; 781190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer 782190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer explicit Random(UInt32 seed) : state_(seed) {} 783190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer 784190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer void Reseed(UInt32 seed) { state_ = seed; } 785190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer 786190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer // Generates a random number from [0, range). Crashes if 'range' is 787190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer // 0 or greater than kMaxRange. 788190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer UInt32 Generate(UInt32 range); 789190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer 790190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer private: 791190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer UInt32 state_; 792190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer GTEST_DISALLOW_COPY_AND_ASSIGN_(Random); 793190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer}; 794190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer 795b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a 79694c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru// compiler error iff T1 and T2 are different types. 797b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T1, typename T2> 798b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct CompileAssertTypesEqual; 799b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 800b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T> 801b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct CompileAssertTypesEqual<T, T> { 802b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad}; 803b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 804b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Removes the reference from a type if it is a reference type, 805b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// otherwise leaves it unchanged. This is the same as 806b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// tr1::remove_reference, which is not widely available yet. 807b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T> 808b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct RemoveReference { typedef T type; }; // NOLINT 809b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T> 810b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct RemoveReference<T&> { typedef T type; }; // NOLINT 811b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 812b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// A handy wrapper around RemoveReference that works when the argument 813b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// T depends on template parameters. 814b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_REMOVE_REFERENCE_(T) \ 815b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad typename ::testing::internal::RemoveReference<T>::type 816b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 817b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Removes const from a type if it is a const type, otherwise leaves 818b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// it unchanged. This is the same as tr1::remove_const, which is not 819b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// widely available yet. 820b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T> 821b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct RemoveConst { typedef T type; }; // NOLINT 822b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T> 823b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct RemoveConst<const T> { typedef T type; }; // NOLINT 824b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 825b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// MSVC 8.0, Sun C++, and IBM XL C++ have a bug which causes the above 826b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// definition to fail to remove the const in 'const int[3]' and 'const 827b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// char[3][4]'. The following specialization works around the bug. 828b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// However, it causes trouble with GCC and thus needs to be 829b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// conditionally compiled. 830b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#if defined(_MSC_VER) || defined(__SUNPRO_CC) || defined(__IBMCPP__) 831b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T, size_t N> 832b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct RemoveConst<const T[N]> { 833b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad typedef typename RemoveConst<T>::type type[N]; 834b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad}; 835b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#endif 836b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 837b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// A handy wrapper around RemoveConst that works when the argument 838b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// T depends on template parameters. 839b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_REMOVE_CONST_(T) \ 840b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad typename ::testing::internal::RemoveConst<T>::type 841b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 842b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Turns const U&, U&, const U, and U all into U. 843b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \ 844b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad GTEST_REMOVE_CONST_(GTEST_REMOVE_REFERENCE_(T)) 845b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 846b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Adds reference to a type if it is not a reference type, 847b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// otherwise leaves it unchanged. This is the same as 848b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// tr1::add_reference, which is not widely available yet. 849b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T> 850b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct AddReference { typedef T& type; }; // NOLINT 851b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T> 852b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct AddReference<T&> { typedef T& type; }; // NOLINT 853b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 854b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// A handy wrapper around AddReference that works when the argument T 855b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// depends on template parameters. 856b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_ADD_REFERENCE_(T) \ 857b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad typename ::testing::internal::AddReference<T>::type 858b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 859b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Adds a reference to const on top of T as necessary. For example, 860b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// it transforms 861b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// 862b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// char ==> const char& 863b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// const char ==> const char& 864b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// char& ==> const char& 865b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// const char& ==> const char& 866b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// 867b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// The argument T must depend on some template parameters. 868b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_REFERENCE_TO_CONST_(T) \ 869b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad GTEST_ADD_REFERENCE_(const GTEST_REMOVE_REFERENCE_(T)) 870b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 871b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// ImplicitlyConvertible<From, To>::value is a compile-time bool 87294c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru// constant that's true iff type From can be implicitly converted to 873b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// type To. 874b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename From, typename To> 875b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadclass ImplicitlyConvertible { 876b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad private: 877b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // We need the following helper functions only for their types. 878b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // They have no implementations. 879b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 880b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // MakeFrom() is an expression whose type is From. We cannot simply 881b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // use From(), as the type From may not have a public default 882b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // constructor. 883b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad static From MakeFrom(); 884b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 885b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // These two functions are overloaded. Given an expression 886b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // Helper(x), the compiler will pick the first version if x can be 887b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // implicitly converted to type To; otherwise it will pick the 888b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // second version. 889b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // 890b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // The first version returns a value of size 1, and the second 891b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // version returns a value of size 2. Therefore, by checking the 892b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // size of Helper(x), which can be done at compile time, we can tell 893b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // which version of Helper() is used, and hence whether x can be 894b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // implicitly converted to type To. 895b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad static char Helper(To); 896b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad static char (&Helper(...))[2]; // NOLINT 897b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 898b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // We have to put the 'public' section after the 'private' section, 899b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // or MSVC refuses to compile the code. 900b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad public: 901b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // MSVC warns about implicitly converting from double to int for 902b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // possible loss of data, so we need to temporarily disable the 903b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // warning. 904b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#ifdef _MSC_VER 905b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# pragma warning(push) // Saves the current warning state. 906b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# pragma warning(disable:4244) // Temporarily disables warning 4244. 907b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 908b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad static const bool value = 909b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 910b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# pragma warning(pop) // Restores the warning state. 911b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#elif defined(__BORLANDC__) 912b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // C++Builder cannot use member overload resolution during template 913b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // instantiation. The simplest workaround is to use its C++0x type traits 914b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // functions (C++Builder 2009 and above only). 915b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad static const bool value = __is_convertible(From, To); 916b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#else 917b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad static const bool value = 918b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1; 919b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#endif // _MSV_VER 920b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad}; 921b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename From, typename To> 922b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadconst bool ImplicitlyConvertible<From, To>::value; 923b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 924b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// IsAProtocolMessage<T>::value is a compile-time bool constant that's 92594c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru// true iff T is type ProtocolMessage, proto2::Message, or a subclass 926b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// of those. 927b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T> 928b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct IsAProtocolMessage 929b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad : public bool_constant< 930b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad ImplicitlyConvertible<const T*, const ::ProtocolMessage*>::value || 931b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad ImplicitlyConvertible<const T*, const ::proto2::Message*>::value> { 932b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad}; 933b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 934b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// When the compiler sees expression IsContainerTest<C>(0), if C is an 935b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// STL-style container class, the first overload of IsContainerTest 936b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// will be viable (since both C::iterator* and C::const_iterator* are 937b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// valid types and NULL can be implicitly converted to them). It will 938b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// be picked over the second overload as 'int' is a perfect match for 939b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// the type of argument 0. If C::iterator or C::const_iterator is not 940b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// a valid type, the first overload is not viable, and the second 941b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// overload will be picked. Therefore, we can determine whether C is 942b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// a container class by checking the type of IsContainerTest<C>(0). 943b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// The value of the expression is insignificant. 944b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// 945b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Note that we look for both C::iterator and C::const_iterator. The 946b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// reason is that C++ injects the name of a class as a member of the 947b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// class itself (e.g. you can refer to class iterator as either 948b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// 'iterator' or 'iterator::iterator'). If we look for C::iterator 949b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// only, for example, we would mistakenly think that a class named 950b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// iterator is an STL container. 951b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// 952b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Also note that the simpler approach of overloading 953b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// IsContainerTest(typename C::const_iterator*) and 954b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// IsContainerTest(...) doesn't work with Visual Age C++ and Sun C++. 955b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtypedef int IsContainer; 956b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <class C> 957b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadIsContainer IsContainerTest(int /* dummy */, 958b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad typename C::iterator* /* it */ = NULL, 959b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad typename C::const_iterator* /* const_it */ = NULL) { 960b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad return 0; 961b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad} 962b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 963b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtypedef char IsNotContainer; 964b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <class C> 965b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadIsNotContainer IsContainerTest(long /* dummy */) { return '\0'; } 966b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 967b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// EnableIf<condition>::type is void when 'Cond' is true, and 968b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// undefined when 'Cond' is false. To use SFINAE to make a function 969b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// overload only apply when a particular expression is true, add 970b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// "typename EnableIf<expression>::type* = 0" as the last parameter. 971b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate<bool> struct EnableIf; 972b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate<> struct EnableIf<true> { typedef void type; }; // NOLINT 973b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 974b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Utilities for native arrays. 975b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 976b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// ArrayEq() compares two k-dimensional native arrays using the 977b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// elements' operator==, where k can be any integer >= 0. When k is 978b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// 0, ArrayEq() degenerates into comparing a single pair of values. 979b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 980b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T, typename U> 981b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadbool ArrayEq(const T* lhs, size_t size, const U* rhs); 982b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 983b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// This generic version is used when k is 0. 984b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T, typename U> 985b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadinline bool ArrayEq(const T& lhs, const U& rhs) { return lhs == rhs; } 986b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 987b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// This overload is used when k >= 1. 988b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T, typename U, size_t N> 989b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadinline bool ArrayEq(const T(&lhs)[N], const U(&rhs)[N]) { 990b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad return internal::ArrayEq(lhs, N, rhs); 991b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad} 992b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 993b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// This helper reduces code bloat. If we instead put its logic inside 994b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// the previous ArrayEq() function, arrays with different sizes would 995b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// lead to different copies of the template code. 996b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T, typename U> 997b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadbool ArrayEq(const T* lhs, size_t size, const U* rhs) { 998b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad for (size_t i = 0; i != size; i++) { 999b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad if (!internal::ArrayEq(lhs[i], rhs[i])) 1000b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad return false; 1001b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } 1002b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad return true; 1003b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad} 1004b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1005b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Finds the first element in the iterator range [begin, end) that 1006b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// equals elem. Element may be a native array type itself. 1007b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename Iter, typename Element> 1008b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadIter ArrayAwareFind(Iter begin, Iter end, const Element& elem) { 1009b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad for (Iter it = begin; it != end; ++it) { 1010b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad if (internal::ArrayEq(*it, elem)) 1011b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad return it; 1012b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } 1013b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad return end; 1014b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad} 1015b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1016b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// CopyArray() copies a k-dimensional native array using the elements' 1017b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// operator=, where k can be any integer >= 0. When k is 0, 1018b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// CopyArray() degenerates into copying a single value. 1019b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1020b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T, typename U> 1021b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadvoid CopyArray(const T* from, size_t size, U* to); 1022b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1023b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// This generic version is used when k is 0. 1024b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T, typename U> 1025b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadinline void CopyArray(const T& from, U* to) { *to = from; } 1026b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1027b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// This overload is used when k >= 1. 1028b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T, typename U, size_t N> 1029b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadinline void CopyArray(const T(&from)[N], U(*to)[N]) { 1030b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad internal::CopyArray(from, N, *to); 1031b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad} 1032b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1033b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// This helper reduces code bloat. If we instead put its logic inside 1034b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// the previous CopyArray() function, arrays with different sizes 1035b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// would lead to different copies of the template code. 1036b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T, typename U> 1037b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadvoid CopyArray(const T* from, size_t size, U* to) { 1038b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad for (size_t i = 0; i != size; i++) { 1039b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad internal::CopyArray(from[i], to + i); 1040b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } 1041b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad} 1042b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1043b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// The relation between an NativeArray object (see below) and the 1044b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// native array it represents. 1045b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadenum RelationToSource { 1046b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad kReference, // The NativeArray references the native array. 1047b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad kCopy // The NativeArray makes a copy of the native array and 1048b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // owns the copy. 1049b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad}; 1050b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1051b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Adapts a native array to a read-only STL-style container. Instead 1052b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// of the complete STL container concept, this adaptor only implements 1053b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// members useful for Google Mock's container matchers. New members 1054b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// should be added as needed. To simplify the implementation, we only 1055b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// support Element being a raw type (i.e. having no top-level const or 1056b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// reference modifier). It's the client's responsibility to satisfy 1057b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// this requirement. Element can be an array type itself (hence 1058b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// multi-dimensional arrays are supported). 1059b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename Element> 1060b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadclass NativeArray { 1061b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad public: 1062b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // STL-style container typedefs. 1063b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad typedef Element value_type; 1064b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad typedef Element* iterator; 1065b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad typedef const Element* const_iterator; 1066b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1067b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // Constructs from a native array. 1068b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad NativeArray(const Element* array, size_t count, RelationToSource relation) { 1069b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad Init(array, count, relation); 1070b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } 1071b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1072b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // Copy constructor. 1073b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad NativeArray(const NativeArray& rhs) { 1074b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad Init(rhs.array_, rhs.size_, rhs.relation_to_source_); 1075b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } 1076b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1077b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad ~NativeArray() { 1078b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // Ensures that the user doesn't instantiate NativeArray with a 1079b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // const or reference type. 1080b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad static_cast<void>(StaticAssertTypeEqHelper<Element, 1081b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad GTEST_REMOVE_REFERENCE_AND_CONST_(Element)>()); 1082b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad if (relation_to_source_ == kCopy) 1083b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad delete[] array_; 1084b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } 1085b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1086b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // STL-style container methods. 1087b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad size_t size() const { return size_; } 1088b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad const_iterator begin() const { return array_; } 1089b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad const_iterator end() const { return array_ + size_; } 1090b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad bool operator==(const NativeArray& rhs) const { 1091b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad return size() == rhs.size() && 1092b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad ArrayEq(begin(), size(), rhs.begin()); 1093b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } 1094b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1095b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad private: 1096b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // Initializes this object; makes a copy of the input array if 1097b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad // 'relation' is kCopy. 1098b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad void Init(const Element* array, size_t a_size, RelationToSource relation) { 1099b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad if (relation == kReference) { 1100b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad array_ = array; 1101b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } else { 1102b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad Element* const copy = new Element[a_size]; 1103b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad CopyArray(array, a_size, copy); 1104b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad array_ = copy; 1105b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } 1106b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad size_ = a_size; 1107b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad relation_to_source_ = relation; 1108b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad } 1109b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1110b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad const Element* array_; 1111b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad size_t size_; 1112b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad RelationToSource relation_to_source_; 1113b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 1114b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad GTEST_DISALLOW_ASSIGN_(NativeArray); 1115b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad}; 1116b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 11177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman} // namespace internal 11187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman} // namespace testing 11197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1120b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_MESSAGE_AT_(file, line, message, result_type) \ 1121b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad ::testing::internal::AssertHelper(result_type, file, line, message) \ 11227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman = ::testing::Message() 11237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1124b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_MESSAGE_(message, result_type) \ 1125b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type) 1126b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad 11277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_FATAL_FAILURE_(message) \ 1128190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure) 11297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 11307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_NONFATAL_FAILURE_(message) \ 1131190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure) 11327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 11337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_SUCCESS_(message) \ 1134190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess) 11357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 1136e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer// Suppresses MSVC warnings 4072 (unreachable code) for the code following 1137e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer// statement if it returns or throws (or doesn't return or throw in some 1138e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer// situations). 1139190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \ 1140e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer if (::testing::internal::AlwaysTrue()) { statement; } 1141e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer 11427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_TEST_THROW_(statement, expected_exception, fail) \ 11437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1144b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad if (::testing::internal::ConstCharPtr gtest_msg = "") { \ 11457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman bool gtest_caught_expected = false; \ 11467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman try { \ 1147190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 11487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } \ 11497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman catch (expected_exception const&) { \ 11507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman gtest_caught_expected = true; \ 11517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } \ 11527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman catch (...) { \ 1153b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad gtest_msg.value = \ 1154b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad "Expected: " #statement " throws an exception of type " \ 1155b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad #expected_exception ".\n Actual: it throws a different type."; \ 11567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ 11577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } \ 11587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman if (!gtest_caught_expected) { \ 1159b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad gtest_msg.value = \ 1160b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad "Expected: " #statement " throws an exception of type " \ 1161b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad #expected_exception ".\n Actual: it throws nothing."; \ 11627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \ 11637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } \ 11647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } else \ 11657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \ 1166b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad fail(gtest_msg.value) 11677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 11687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_TEST_NO_THROW_(statement, fail) \ 11697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1170b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad if (::testing::internal::AlwaysTrue()) { \ 11717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman try { \ 1172190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 11737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } \ 11747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman catch (...) { \ 11757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \ 11767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } \ 11777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } else \ 11787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \ 1179b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad fail("Expected: " #statement " doesn't throw an exception.\n" \ 1180b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad " Actual: it throws.") 11817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 11827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_TEST_ANY_THROW_(statement, fail) \ 11837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1184b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad if (::testing::internal::AlwaysTrue()) { \ 11857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman bool gtest_caught_any = false; \ 11867ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman try { \ 1187190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 11887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } \ 11897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman catch (...) { \ 11907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman gtest_caught_any = true; \ 11917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } \ 11927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman if (!gtest_caught_any) { \ 11937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \ 11947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } \ 11957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } else \ 11967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__): \ 1197b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad fail("Expected: " #statement " throws an exception.\n" \ 1198b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad " Actual: it doesn't.") 11997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 12007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 120157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// Implements Boolean test assertions such as EXPECT_TRUE. expression can be 120257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// either a boolean expression or an AssertionResult. text is a textual 120357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// represenation of expression as it was passed into the EXPECT_TRUE. 120457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \ 12057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 120657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer if (const ::testing::AssertionResult gtest_ar_ = \ 120757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer ::testing::AssertionResult(expression)) \ 12087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman ; \ 12097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman else \ 121057240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer fail(::testing::internal::GetBoolAssertionFailureMessage(\ 121157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer gtest_ar_, text, #actual, #expected).c_str()) 12127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 12137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \ 12147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ 1215b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad if (::testing::internal::AlwaysTrue()) { \ 12167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \ 1217190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \ 12187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \ 12197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \ 12207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } \ 12217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman } else \ 12227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__): \ 1223b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad fail("Expected: " #statement " doesn't generate new fatal " \ 1224b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad "failures in the current thread.\n" \ 1225b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad " Actual: it does.") 12267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 12277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Expands to the name of the class that implements the given test. 12287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \ 12297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman test_case_name##_##test_name##_Test 12307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 12317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Helper macro for defining tests. 12327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\ 12337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\ 12347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:\ 12357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\ 12367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman private:\ 12377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman virtual void TestBody();\ 1238b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;\ 12397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_DISALLOW_COPY_AND_ASSIGN_(\ 12407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\ 12417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman};\ 12427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman\ 12437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\ 12447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman ::test_info_ =\ 12457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman ::testing::internal::MakeAndRegisterTestInfo(\ 1246b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad #test_case_name, #test_name, NULL, NULL, \ 12477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman (parent_id), \ 12487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman parent_class::SetUpTestCase, \ 12497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman parent_class::TearDownTestCase, \ 12507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman new ::testing::internal::TestFactoryImpl<\ 12517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\ 12527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanvoid GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() 12537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman 12547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_ 1255