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