17ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Copyright 2005, Google Inc.
27ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// All rights reserved.
37ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
47ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Redistribution and use in source and binary forms, with or without
57ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// modification, are permitted provided that the following conditions are
67ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// met:
77ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
87ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//     * Redistributions of source code must retain the above copyright
97ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// notice, this list of conditions and the following disclaimer.
107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//     * Redistributions in binary form must reproduce the above
117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// copyright notice, this list of conditions and the following disclaimer
127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// in the documentation and/or other materials provided with the
137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// distribution.
147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//     * Neither the name of Google Inc. nor the names of its
157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// contributors may be used to endorse or promote products derived from
167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// this software without specific prior written permission.
177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)
317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The Google C++ Testing Framework (Google Test)
337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This header file declares functions and macros used internally by
357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Google Test.  They are subject to change without notice.
367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_
397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
40b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-port.h"
417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
42e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_OS_LINUX
43b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <stdlib.h>
44b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <sys/types.h>
45b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <sys/wait.h>
46b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# include <unistd.h>
477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif  // GTEST_OS_LINUX
487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <ctype.h>
507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <string.h>
517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <iomanip>
527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <limits>
537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#include <set>
547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
55b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-string.h"
56b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-filepath.h"
57b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#include "gtest/internal/gtest-type-util.h"
587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5957f7dafcb769852a21870148e91535421c45d037Alexey Samsonov#if !GTEST_NO_LLVM_RAW_OSTREAM
609c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin#include "llvm/Support/raw_os_ostream.h"
6157f7dafcb769852a21870148e91535421c45d037Alexey Samsonov#endif
629c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin
637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Due to C++ preprocessor weirdness, we need double indirection to
647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// concatenate two tokens when one of them is __LINE__.  Writing
657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   foo ## __LINE__
677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// will result in the token foo__LINE__, instead of foo followed by
697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the current line number.  For more details, see
707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6
717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar)
727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar
737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Google Test defines the testing::Message class to allow construction of
757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// test messages via the << operator.  The idea is that anything
767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// streamable to std::ostream can be streamed to a testing::Message.
777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This allows a user to use his own types in Google Test assertions by
787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// overloading the << operator.
797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// util/gtl/stl_logging-inl.h overloads << for STL containers.  These
817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// overloads cannot be defined in the std namespace, as that will be
827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// undefined behavior.  Therefore, they are defined in the global
837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// namespace instead.
847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// C++'s symbol lookup rule (i.e. Koenig lookup) says that these
867ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// overloads are visible in either the std namespace or the global
877ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// namespace, but not other namespaces, including the testing
887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// namespace which Google Test's Message class is in.
897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// To allow STL containers (and other types that has a << operator
917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// defined in the global namespace) to be used in Google Test assertions,
927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// testing::Message must access the custom << operator from the global
937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// namespace.  Hence this helper function.
947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Note: Jeffrey Yasskin suggested an alternative fix by "using
967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// ::operator<<;" in the definition of Message's operator<<.  That fix
977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// doesn't require a helper function, but unfortunately doesn't
987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// compile with MSVC.
999c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin
1009c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin// LLVM INTERNAL CHANGE: To allow operator<< to work with both
1019c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin// std::ostreams and LLVM's raw_ostreams, we define a special
1029c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin// std::ostream with an implicit conversion to raw_ostream& and stream
1039c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin// to that.  This causes the compiler to prefer std::ostream overloads
1049c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin// but still find raw_ostream& overloads.
10557f7dafcb769852a21870148e91535421c45d037Alexey Samsonov#if !GTEST_NO_LLVM_RAW_OSTREAM
1069c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskinnamespace llvm {
1079c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskinclass convertible_fwd_ostream : public std::ostream {
108354362524a72b3fa43a6c09380b7ae3b2380cbbaJuergen Ributzka  virtual void anchor();
1099c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin  raw_os_ostream ros_;
1109c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin
1119c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskinpublic:
1129c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin  convertible_fwd_ostream(std::ostream& os)
113955f9387516da03b51337b126bebb43567141173Benjamin Kramer    : std::ostream(os.rdbuf()), ros_(*this) {}
1149c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin  operator raw_ostream&() { return ros_; }
1159c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin};
1169c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin}
1177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename T>
1187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmaninline void GTestStreamToHelper(std::ostream* os, const T& val) {
1199c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin  llvm::convertible_fwd_ostream cos(*os);
1209c0c3bf1ec2feb861f19867d4091350010b23de6Jeffrey Yasskin  cos << val;
1217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
12257f7dafcb769852a21870148e91535421c45d037Alexey Samsonov#else
12357f7dafcb769852a21870148e91535421c45d037Alexey Samsonovtemplate <typename T>
12457f7dafcb769852a21870148e91535421c45d037Alexey Samsonovinline void GTestStreamToHelper(std::ostream* os, const T& val) {
12557f7dafcb769852a21870148e91535421c45d037Alexey Samsonov  *os << val;
12657f7dafcb769852a21870148e91535421c45d037Alexey Samsonov}
12757f7dafcb769852a21870148e91535421c45d037Alexey Samsonov#endif
1287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
129b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadclass ProtocolMessage;
130b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadnamespace proto2 { class Message; }
131b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
1327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmannamespace testing {
1337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
134b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Forward declarations.
1357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
136190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramerclass AssertionResult;                 // Result of an assertion.
1377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass Message;                         // Represents a failure message.
1387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass Test;                            // Represents a test.
1397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TestInfo;                        // Information about a test.
140190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramerclass TestPartResult;                  // Result of a test part.
1417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass UnitTest;                        // A collection of test cases.
1427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
143b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T>
144b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad::std::string PrintToString(const T& value);
145b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
1467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmannamespace internal {
1477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanstruct TraceInfo;                      // Information about a trace point.
1497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass ScopedTrace;                     // Implements scoped trace.
1507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TestInfoImpl;                    // Opaque implementation of TestInfo
1517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass UnitTestImpl;                    // Opaque implementation of UnitTest
1527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// How many times InitGoogleTest() has been called.
1547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanextern int g_init_gtest_count;
1557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The text used in failure messages to indicate the start of the
1577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// stack trace.
15857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ extern const char kStackTraceMarker[];
1597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// A secret type that Google Test users don't know about.  It has no
1617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// definition on purpose.  Therefore it's impossible to create a
1627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Secret object, which is what we want.
1637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass Secret;
1647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Two overloaded helpers for checking at compile time whether an
1667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// expression is a null pointer literal (i.e. NULL or any 0-valued
1677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// compile-time integral constant).  Their return values have
1687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// different sizes, so we can use sizeof() to test which version is
1697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// picked by the compiler.  These helpers have no implementations, as
1707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// we only need their signatures.
1717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
1727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Given IsNullLiteralHelper(x), the compiler will pick the first
1737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// version if x can be implicitly converted to Secret*, and pick the
1747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// second version otherwise.  Since Secret is a secret and incomplete
1757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// type, the only expression a user can write that has type Secret* is
1767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// a null pointer literal.  Therefore, we know that x is a null
1777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// pointer literal if and only if the first version is picked by the
1787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// compiler.
1797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanchar IsNullLiteralHelper(Secret* p);
1807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanchar (&IsNullLiteralHelper(...))[2];  // NOLINT
1817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// A compile-time bool constant that is true if and only if x is a
1837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// null pointer literal (i.e. NULL or any 0-valued compile-time
1847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// integral constant).
18557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#ifdef GTEST_ELLIPSIS_NEEDS_POD_
18657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// We lose support for NULL detection where the compiler doesn't like
18757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// passing non-POD classes through ellipsis (...).
188b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_IS_NULL_LITERAL_(x) false
1897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else
190b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_IS_NULL_LITERAL_(x) \
1917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1)
19257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer#endif  // GTEST_ELLIPSIS_NEEDS_POD_
1937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Appends the user-supplied message to the Google-Test-generated message.
19557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ String AppendUserMessage(const String& gtest_msg,
19657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer                                    const Message& user_msg);
1977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
1987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// A helper class for creating scoped traces in user programs.
19957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramerclass GTEST_API_ ScopedTrace {
2007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:
2017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // The c'tor pushes the given source file location and message onto
2027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // a trace stack maintained by Google Test.
2037ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  ScopedTrace(const char* file, int line, const Message& message);
2047ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // The d'tor pops the info pushed by the c'tor.
2067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //
2077ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Note that the d'tor is not virtual in order to be efficient.
2087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Don't inherit from ScopedTrace!
2097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  ~ScopedTrace();
2107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman private:
2127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace);
2137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman} GTEST_ATTRIBUTE_UNUSED_;  // A ScopedTrace object does its job in its
2147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                            // c'tor and d'tor.  Therefore it doesn't
2157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                            // need to be used otherwise.
2167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Converts a streamable value to a String.  A NULL pointer is
2187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// converted to "(null)".  When the input value is a ::string,
2197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// ::std::string, ::wstring, or ::std::wstring object, each NUL
2207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// character in it is replaced with "\\0".
2217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Declared here but defined in gtest.h, so that it has access
2227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// to the definition of the Message class, required by the ARM
2237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// compiler.
2247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename T>
2257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanString StreamableToString(const T& streamable);
2267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
227b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// The Symbian compiler has a bug that prevents it from selecting the
228b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// correct overload of FormatForComparisonFailureMessage (see below)
229b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// unless we pass the first argument by reference.  If we do that,
230b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// however, Visual Age C++ 10.1 generates a compiler error.  Therefore
231b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// we only apply the work-around for Symbian.
232b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#if defined(__SYMBIAN32__)
233b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_CREF_WORKAROUND_ const&
2347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#else
235b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad# define GTEST_CREF_WORKAROUND_
236b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#endif
2377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
238b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// When this operand is a const char* or char*, if the other operand
2397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// is a ::std::string or ::string, we print this operand as a C string
240b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// rather than a pointer (we do the same for wide strings); otherwise
241b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// we print it as a pointer to be safe.
2427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This internal macro is used to avoid duplicated code.
2447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#define GTEST_FORMAT_IMPL_(operand2_type, operand1_printer)\
2457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmaninline String FormatForComparisonFailureMessage(\
246b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    operand2_type::value_type* GTEST_CREF_WORKAROUND_ str, \
247b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    const operand2_type& /*operand2*/) {\
2487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return operand1_printer(str);\
2497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}\
2507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmaninline String FormatForComparisonFailureMessage(\
251b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    const operand2_type::value_type* GTEST_CREF_WORKAROUND_ str, \
252b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    const operand2_type& /*operand2*/) {\
2537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return operand1_printer(str);\
2547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
2557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanGTEST_FORMAT_IMPL_(::std::string, String::ShowCStringQuoted)
2577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#if GTEST_HAS_STD_WSTRING
2587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanGTEST_FORMAT_IMPL_(::std::wstring, String::ShowWideCStringQuoted)
2597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif  // GTEST_HAS_STD_WSTRING
2607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#if GTEST_HAS_GLOBAL_STRING
2627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanGTEST_FORMAT_IMPL_(::string, String::ShowCStringQuoted)
2637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif  // GTEST_HAS_GLOBAL_STRING
2647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#if GTEST_HAS_GLOBAL_WSTRING
2657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanGTEST_FORMAT_IMPL_(::wstring, String::ShowWideCStringQuoted)
2667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif  // GTEST_HAS_GLOBAL_WSTRING
2677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
2687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#undef GTEST_FORMAT_IMPL_
2697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
270b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// The next four overloads handle the case where the operand being
271b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// printed is a char/wchar_t pointer and the other operand is not a
272b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// string/wstring object.  In such cases, we just print the operand as
273b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// a pointer to be safe.
274b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_FORMAT_CHAR_PTR_IMPL_(CharType)                       \
275b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  template <typename T>                                             \
276b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  String FormatForComparisonFailureMessage(CharType* GTEST_CREF_WORKAROUND_ p, \
277b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad                                           const T&) { \
278b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    return PrintToString(static_cast<const void*>(p));              \
279b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  }
280b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
281b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadGTEST_FORMAT_CHAR_PTR_IMPL_(char)
282b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadGTEST_FORMAT_CHAR_PTR_IMPL_(const char)
283b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadGTEST_FORMAT_CHAR_PTR_IMPL_(wchar_t)
284b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadGTEST_FORMAT_CHAR_PTR_IMPL_(const wchar_t)
285b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
286b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#undef GTEST_FORMAT_CHAR_PTR_IMPL_
287b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
2887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Constructs and returns the message for an equality assertion
2897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure.
2907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
2917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The first four parameters are the expressions used in the assertion
2927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// and their values, as strings.  For example, for ASSERT_EQ(foo, bar)
2937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// where foo is 5 and bar is 6, we have:
2947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
2957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   expected_expression: "foo"
2967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   actual_expression:   "bar"
2977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   expected_value:      "5"
2987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   actual_value:        "6"
2997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
30094c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru// The ignoring_case parameter is true iff the assertion is a
3017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// *_STRCASEEQ*.  When it's true, the string " (ignoring case)" will
3027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// be inserted into the message.
30357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ AssertionResult EqFailure(const char* expected_expression,
30457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer                                     const char* actual_expression,
30557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer                                     const String& expected_value,
30657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer                                     const String& actual_value,
30757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer                                     bool ignoring_case);
30857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer
30957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// Constructs a failure message for Boolean assertions such as EXPECT_TRUE.
31057240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ String GetBoolAssertionFailureMessage(
31157240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer    const AssertionResult& assertion_result,
31257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer    const char* expression_text,
31357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer    const char* actual_predicate_value,
31457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer    const char* expected_predicate_value);
3157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This template class represents an IEEE floating-point number
3177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// (either single-precision or double-precision, depending on the
3187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// template parameters).
3197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
3207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The purpose of this class is to do more sophisticated number
3217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// comparison.  (Due to round-off error, etc, it's very unlikely that
3227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// two floating-points will be equal exactly.  Hence a naive
3237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// comparison by the == operation often doesn't work.)
3247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
3257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Format of IEEE floating-point:
3267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
3277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   The most-significant bit being the leftmost, an IEEE
3287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   floating-point looks like
3297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
3307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//     sign_bit exponent_bits fraction_bits
3317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
3327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   Here, sign_bit is a single bit that designates the sign of the
3337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   number.
3347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
3357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   For float, there are 8 exponent bits and 23 fraction bits.
3367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
3377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   For double, there are 11 exponent bits and 52 fraction bits.
3387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
3397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   More details can be found at
3407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   http://en.wikipedia.org/wiki/IEEE_floating-point_standard.
3417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
3427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Template parameter:
3437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
3447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   RawType: the raw floating-point type (either float or double)
3457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename RawType>
3467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass FloatingPoint {
3477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:
3487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Defines the unsigned integer type that has the same size as the
3497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // floating point number.
3507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  typedef typename TypeWithSize<sizeof(RawType)>::UInt Bits;
3517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Constants.
3537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // # of bits in a number.
3557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static const size_t kBitCount = 8*sizeof(RawType);
3567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // # of fraction bits in a number.
3587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static const size_t kFractionBitCount =
3597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    std::numeric_limits<RawType>::digits - 1;
3607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // # of exponent bits in a number.
3627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount;
3637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // The mask for the sign bit.
3657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1);
3667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // The mask for the fraction bits.
3687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static const Bits kFractionBitMask =
3697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    ~static_cast<Bits>(0) >> (kExponentBitCount + 1);
3707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // The mask for the exponent bits.
3727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask);
3737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // How many ULP's (Units in the Last Place) we want to tolerate when
3757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // comparing two numbers.  The larger the value, the more error we
3767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // allow.  A 0 value means that two numbers must be exactly the same
3777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // to be considered equal.
3787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //
3797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // The maximum error of a single floating-point operation is 0.5
3807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // units in the last place.  On Intel CPU's, all floating-point
3817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // calculations are done with 80-bit precision, while double has 64
3827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // bits.  Therefore, 4 should be enough for ordinary use.
3837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //
3847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // See the following article for more details on ULP:
3857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm.
3867ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static const size_t kMaxUlps = 4;
3877ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Constructs a FloatingPoint from a raw floating-point number.
3897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //
3907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // On an Intel CPU, passing a non-normalized NAN (Not a Number)
3917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // around may change its bits, although the new value is guaranteed
3927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // to be also a NAN.  Therefore, don't expect this constructor to
3937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // preserve the bits in x when x is a NAN.
394190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  explicit FloatingPoint(const RawType& x) { u_.value_ = x; }
3957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Static methods
3977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
3987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Reinterprets a bit pattern as a floating-point number.
3997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //
4007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // This function is needed to test the AlmostEquals() method.
4017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static RawType ReinterpretBits(const Bits bits) {
4027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    FloatingPoint fp(0);
403190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer    fp.u_.bits_ = bits;
404190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer    return fp.u_.value_;
4057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
4067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
4077ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Returns the floating-point number that represent positive infinity.
4087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static RawType Infinity() {
4097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return ReinterpretBits(kExponentBitMask);
4107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
4117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
4127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Non-static methods
4137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
4147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Returns the bits that represents this number.
415190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  const Bits &bits() const { return u_.bits_; }
4167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
4177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Returns the exponent bits of this number.
418190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  Bits exponent_bits() const { return kExponentBitMask & u_.bits_; }
4197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
4207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Returns the fraction bits of this number.
421190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  Bits fraction_bits() const { return kFractionBitMask & u_.bits_; }
4227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
4237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Returns the sign bit of this number.
424190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  Bits sign_bit() const { return kSignBitMask & u_.bits_; }
4257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
42694c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru  // Returns true iff this is NAN (not a number).
4277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  bool is_nan() const {
4287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    // It's a NAN if the exponent bits are all ones and the fraction
4297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    // bits are not entirely zeros.
4307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0);
4317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
4327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
43394c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru  // Returns true iff this number is at most kMaxUlps ULP's away from
4347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // rhs.  In particular, this function:
4357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //
4367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //   - returns false if either number is (or both are) NAN.
4377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //   - treats really large numbers as almost equal to infinity.
4387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //   - thinks +0.0 and -0.0 are 0 DLP's apart.
4397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  bool AlmostEquals(const FloatingPoint& rhs) const {
4407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    // The IEEE standard says that any comparison operation involving
4417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    // a NAN must return false.
4427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    if (is_nan() || rhs.is_nan()) return false;
4437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
444190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer    return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_)
445190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer        <= kMaxUlps;
4467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
4477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
4487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman private:
449190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  // The data type used to store the actual floating-point number.
450190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  union FloatingPointUnion {
451190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer    RawType value_;  // The raw floating-point number.
452190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer    Bits bits_;      // The bits that represent the number.
453190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  };
454190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer
4557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Converts an integer from the sign-and-magnitude representation to
4567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // the biased representation.  More precisely, let N be 2 to the
4577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // power of (kBitCount - 1), an integer x is represented by the
4587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // unsigned number x + N.
4597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //
4607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // For instance,
4617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //
4627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //   -N + 1 (the most negative number representable using
4637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //          sign-and-magnitude) is represented by 1;
4647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //   0      is represented by N; and
4657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //   N - 1  (the biggest number representable using
4667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //          sign-and-magnitude) is represented by 2N - 1.
4677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  //
4687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Read http://en.wikipedia.org/wiki/Signed_number_representations
4697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // for more details on signed number representations.
4707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static Bits SignAndMagnitudeToBiased(const Bits &sam) {
4717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    if (kSignBitMask & sam) {
4727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman      // sam represents a negative number.
4737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman      return ~sam + 1;
4747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    } else {
4757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman      // sam represents a positive number.
4767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman      return kSignBitMask | sam;
4777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    }
4787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
4797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
4807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Given two numbers in the sign-and-magnitude representation,
4817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // returns the distance between them as an unsigned number.
4827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits &sam1,
4837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                                                     const Bits &sam2) {
4847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    const Bits biased1 = SignAndMagnitudeToBiased(sam1);
4857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    const Bits biased2 = SignAndMagnitudeToBiased(sam2);
4867ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);
4877ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
4887ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
489190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  FloatingPointUnion u_;
4907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman};
4917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
4927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Typedefs the instances of the FloatingPoint template class that we
4937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// care to use.
4947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantypedef FloatingPoint<float> Float;
4957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantypedef FloatingPoint<double> Double;
4967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
4977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// In order to catch the mistake of putting tests that use different
4987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// test fixture classes in the same test case, we need to assign
4997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// unique IDs to fixture classes and compare them.  The TypeId type is
5007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// used to hold such IDs.  The user should treat TypeId as an opaque
5017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// type: the only operation allowed on TypeId values is to compare
5027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// them for equality using the == operator.
5037ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantypedef const void* TypeId;
5047ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename T>
5067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TypeIdHelper {
5077ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:
5087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // dummy_ must not have a const type.  Otherwise an overly eager
5097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // compiler (e.g. MSVC 7.1 & 8.0) may try to merge
5107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // TypeIdHelper<T>::dummy_ for different Ts as an "optimization".
5117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static bool dummy_;
5127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman};
5137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename T>
5157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanbool TypeIdHelper<T>::dummy_ = false;
5167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// GetTypeId<T>() returns the ID of type T.  Different values will be
5187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// returned for different types.  Calling the function twice with the
5197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// same type argument is guaranteed to return the same ID.
5207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <typename T>
5217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha BrukmanTypeId GetTypeId() {
5227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // The compiler is required to allocate a different
5237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // TypeIdHelper<T>::dummy_ variable for each T used to instantiate
5247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // the template.  Therefore, the address of dummy_ is guaranteed to
5257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // be unique.
5267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return &(TypeIdHelper<T>::dummy_);
5277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
5287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns the type ID of ::testing::Test.  Always call this instead
5307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// of GetTypeId< ::testing::Test>() to get the type ID of
5317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// ::testing::Test, as the latter may give the wrong result due to a
5327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// suspected linker bug when compiling Google Test as a Mac OS X
5337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// framework.
53457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ TypeId GetTestTypeId();
5357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Defines the abstract factory interface that creates instances
5377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// of a Test object.
5387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TestFactoryBase {
5397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:
540354362524a72b3fa43a6c09380b7ae3b2380cbbaJuergen Ributzka  virtual ~TestFactoryBase();
5417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Creates a test instance to run. The instance is both created and destroyed
5437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // within TestInfoImpl::Run()
5447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  virtual Test* CreateTest() = 0;
5457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman protected:
5477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  TestFactoryBase() {}
5487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman private:
5507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase);
5517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman};
5527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// This class provides implementation of TeastFactoryBase interface.
5547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// It is used in TEST and TEST_F macros.
5557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <class TestClass>
5567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TestFactoryImpl : public TestFactoryBase {
5577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:
5587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  virtual Test* CreateTest() { return new TestClass; }
5597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman};
5607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
561e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_OS_WINDOWS
5627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Predicate-formatters for implementing the HRESULT checking macros
5647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}
5657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// We pass a long instead of HRESULT to avoid causing an
5667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// include dependency for the HRESULT type.
56757240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ AssertionResult IsHRESULTSuccess(const char* expr,
56857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer                                            long hr);  // NOLINT
56957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ AssertionResult IsHRESULTFailure(const char* expr,
57057240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer                                            long hr);  // NOLINT
5717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif  // GTEST_OS_WINDOWS
5737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Types of SetUpTestCase() and TearDownTestCase() functions.
5757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantypedef void (*SetUpTestCaseFunc)();
5767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantypedef void (*TearDownTestCaseFunc)();
5777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
5787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Creates a new TestInfo object and registers it with Google Test;
5797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// returns the created object.
5807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
5817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Arguments:
5827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
5837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   test_case_name:   name of the test case
5847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   name:             name of the test
585b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad//   type_param        the name of the test's type parameter, or NULL if
586b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad//                     this is not  a typed or a type-parameterized test.
587b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad//   value_param       text representation of the test's value parameter,
588b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad//                     or NULL if this is not a type-parameterized test.
5897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   fixture_class_id: ID of the test fixture class
5907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   set_up_tc:        pointer to the function that sets up the test case
5917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   tear_down_tc:     pointer to the function that tears down the test case
5927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//   factory:          pointer to the factory that creates a test object.
5937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//                     The newly created TestInfo instance will assume
5947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//                     ownership of the factory object.
59557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ TestInfo* MakeAndRegisterTestInfo(
5967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    const char* test_case_name, const char* name,
597b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    const char* type_param,
598b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    const char* value_param,
5997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    TypeId fixture_class_id,
6007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    SetUpTestCaseFunc set_up_tc,
6017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    TearDownTestCaseFunc tear_down_tc,
6027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    TestFactoryBase* factory);
6037ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
60457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// If *pstr starts with the given prefix, modifies *pstr to be right
60557240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// past the prefix and returns true; otherwise leaves *pstr unchanged
60657240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer// and returns false.  None of pstr, *pstr, and prefix can be NULL.
607b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay FoadGTEST_API_ bool SkipPrefix(const char* prefix, const char** pstr);
60857240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer
609e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
6107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
6117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// State of the definition of a type-parameterized test case.
61257240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramerclass GTEST_API_ TypedTestCasePState {
6137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:
6147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  TypedTestCasePState() : registered_(false) {}
6157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
6167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Adds the given test name to defined_test_names_ and return true
6177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // if the test case hasn't been registered; otherwise aborts the
6187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // program.
6197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  bool AddTestName(const char* file, int line, const char* case_name,
6207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                   const char* test_name) {
6217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    if (registered_) {
6227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman      fprintf(stderr, "%s Test %s must be defined before "
6237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman              "REGISTER_TYPED_TEST_CASE_P(%s, ...).\n",
6247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman              FormatFileLocation(file, line).c_str(), test_name, case_name);
625e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer      fflush(stderr);
626190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer      posix::Abort();
6277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    }
6287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    defined_test_names_.insert(test_name);
6297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return true;
6307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
6317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
6327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Verifies that registered_tests match the test names in
6337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // defined_test_names_; returns registered_tests if successful, or
6347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // aborts the program otherwise.
6357ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  const char* VerifyRegisteredTestNames(
6367ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman      const char* file, int line, const char* registered_tests);
6377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
6387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman private:
6397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  bool registered_;
6407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  ::std::set<const char*> defined_test_names_;
6417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman};
6427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
6437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Skips to the first non-space char after the first comma in 'str';
6447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// returns NULL if no comma is found in 'str'.
6457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmaninline const char* SkipComma(const char* str) {
6467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  const char* comma = strchr(str, ',');
6477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  if (comma == NULL) {
6487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return NULL;
6497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
650b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  while (IsSpace(*(++comma))) {}
6517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return comma;
6527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
6537ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
6547ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns the prefix of 'str' before the first comma in it; returns
6557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the entire string if it contains no comma.
6567ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmaninline String GetPrefixUntilComma(const char* str) {
6577ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  const char* comma = strchr(str, ',');
6587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  return comma == NULL ? String(str) : String(str, comma - str);
6597ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman}
6607ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
6617ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// TypeParameterizedTest<Fixture, TestSel, Types>::Register()
6627ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// registers a list of type-parameterized tests with Google Test.  The
6637ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// return value is insignificant - we just need to return something
6647ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// such that we can call this function in a namespace scope.
6657ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
6667ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Implementation note: The GTEST_TEMPLATE_ macro declares a template
6677ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// template parameter.  It's defined in gtest-type-util.h.
6687ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <GTEST_TEMPLATE_ Fixture, class TestSel, typename Types>
6697ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TypeParameterizedTest {
6707ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:
6717ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // 'index' is the index of the test in the type list 'Types'
6727ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // specified in INSTANTIATE_TYPED_TEST_CASE_P(Prefix, TestCase,
6737ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // Types).  Valid values for 'index' are [0, N - 1] where N is the
6747ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  // length of Types.
6757ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static bool Register(const char* prefix, const char* case_name,
6767ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                       const char* test_names, int index) {
6777ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    typedef typename Types::Head Type;
6787ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    typedef Fixture<Type> FixtureClass;
6797ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    typedef typename GTEST_BIND_(TestSel, Type) TestClass;
6807ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
6817ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    // First, registers the first type-parameterized test in the type
6827ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    // list.
6837ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    MakeAndRegisterTestInfo(
6847ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman        String::Format("%s%s%s/%d", prefix, prefix[0] == '\0' ? "" : "/",
6857ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                       case_name, index).c_str(),
6867ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman        GetPrefixUntilComma(test_names).c_str(),
687b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad        GetTypeName<Type>().c_str(),
688b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad        NULL,  // No value parameter.
6897ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman        GetTypeId<FixtureClass>(),
6907ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman        TestClass::SetUpTestCase,
6917ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman        TestClass::TearDownTestCase,
6927ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman        new TestFactoryImpl<TestClass>);
6937ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
6947ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    // Next, recurses (at compile time) with the tail of the type list.
6957ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return TypeParameterizedTest<Fixture, TestSel, typename Types::Tail>
6967ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman        ::Register(prefix, case_name, test_names, index + 1);
6977ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
6987ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman};
6997ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
7007ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The base case for the compile time recursion.
7017ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <GTEST_TEMPLATE_ Fixture, class TestSel>
7027ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TypeParameterizedTest<Fixture, TestSel, Types0> {
7037ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:
7047ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static bool Register(const char* /*prefix*/, const char* /*case_name*/,
7057ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                       const char* /*test_names*/, int /*index*/) {
7067ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return true;
7077ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
7087ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman};
7097ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
7107ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// TypeParameterizedTestCase<Fixture, Tests, Types>::Register()
7117ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// registers *all combinations* of 'Tests' and 'Types' with Google
7127ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Test.  The return value is insignificant - we just need to return
7137ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// something such that we can call this function in a namespace scope.
7147ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <GTEST_TEMPLATE_ Fixture, typename Tests, typename Types>
7157ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TypeParameterizedTestCase {
7167ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:
7177ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  static bool Register(const char* prefix, const char* case_name,
7187ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman                       const char* test_names) {
7197ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    typedef typename Tests::Head Head;
7207ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
7217ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    // First, register the first test in 'Test' for each type in 'Types'.
7227ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    TypeParameterizedTest<Fixture, Head, Types>::Register(
7237ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman        prefix, case_name, test_names, 0);
7247ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
7257ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    // Next, recurses (at compile time) with the tail of the test list.
7267ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return TypeParameterizedTestCase<Fixture, typename Tests::Tail, Types>
7277ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman        ::Register(prefix, case_name, SkipComma(test_names));
7287ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
7297ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman};
7307ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
7317ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The base case for the compile time recursion.
7327ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmantemplate <GTEST_TEMPLATE_ Fixture, typename Types>
7337ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukmanclass TypeParameterizedTestCase<Fixture, Templates0, Types> {
7347ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman public:
735190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  static bool Register(const char* /*prefix*/, const char* /*case_name*/,
736190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer                       const char* /*test_names*/) {
7377ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman    return true;
7387ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman  }
7397ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman};
7407ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
7417ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman#endif  // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P
7427ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
7437ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// Returns the current OS stack trace as a String.
7447ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
7457ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// The maximum number of stack frames to be included is specified by
7467ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the gtest_stack_trace_depth flag.  The skip_count parameter
7477ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// specifies the number of top frames to be skipped, which doesn't
7487ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// count against the number of frames to be included.
7497ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman//
7507ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// For example, if Foo() calls Bar(), which in turn calls
7517ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in
7527ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.
75357240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ String GetCurrentOsStackTraceExceptTop(UnitTest* unit_test,
75457240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramer                                                  int skip_count);
7557ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
756190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// Helpers for suppressing warnings on unreachable code or constant
757190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// condition.
7587ae6ff442a26212a0cc4c1929b8b0a105dc988e4Misha Brukman
759190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// Always returns true.
76057240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin KramerGTEST_API_ bool AlwaysTrue();
761e4b9c93fc1b531fe0cfe25a042f6b81c1e7c15c0Benjamin Kramer
762190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// Always returns false.
763190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramerinline bool AlwaysFalse() { return !AlwaysTrue(); }
764190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer
765b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Helper for suppressing false warning from Clang on a const char*
766b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// variable declared in a conditional expression always being NULL in
767b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// the else branch.
768b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct GTEST_API_ ConstCharPtr {
769b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  ConstCharPtr(const char* str) : value(str) {}
770b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  operator bool() const { return true; }
771b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  const char* value;
772b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad};
773b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
774190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// A simple Linear Congruential Generator for generating random
775190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// numbers with a uniform distribution.  Unlike rand() and srand(), it
776190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// doesn't use global state (and therefore can't interfere with user
777190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// code).  Unlike rand_r(), it's portable.  An LCG isn't very random,
778190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer// but it's good enough for our purposes.
77957240ff6e2252f8986f6e47e4010bc52fbae25d1Benjamin Kramerclass GTEST_API_ Random {
780190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer public:
781190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  static const UInt32 kMaxRange = 1u << 31;
782190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer
783190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  explicit Random(UInt32 seed) : state_(seed) {}
784190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer
785190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  void Reseed(UInt32 seed) { state_ = seed; }
786190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer
787190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  // Generates a random number from [0, range).  Crashes if 'range' is
788190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  // 0 or greater than kMaxRange.
789190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  UInt32 Generate(UInt32 range);
790190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer
791190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer private:
792190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  UInt32 state_;
793190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer  GTEST_DISALLOW_COPY_AND_ASSIGN_(Random);
794190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer};
795190f8ee25a6977ac6eb71b816498df42f17ad9a7Benjamin Kramer
796b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a
79794c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru// compiler error iff T1 and T2 are different types.
798b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T1, typename T2>
799b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct CompileAssertTypesEqual;
800b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
801b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T>
802b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct CompileAssertTypesEqual<T, T> {
803b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad};
804b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
805b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Removes the reference from a type if it is a reference type,
806b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// otherwise leaves it unchanged.  This is the same as
807b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// tr1::remove_reference, which is not widely available yet.
808b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T>
809b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct RemoveReference { typedef T type; };  // NOLINT
810b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T>
811b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct RemoveReference<T&> { typedef T type; };  // NOLINT
812b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
813b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// A handy wrapper around RemoveReference that works when the argument
814b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// T depends on template parameters.
815b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_REMOVE_REFERENCE_(T) \
816b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    typename ::testing::internal::RemoveReference<T>::type
817b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
818b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Removes const from a type if it is a const type, otherwise leaves
819b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// it unchanged.  This is the same as tr1::remove_const, which is not
820b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// widely available yet.
821b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T>
822b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct RemoveConst { typedef T type; };  // NOLINT
823b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T>
824b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct RemoveConst<const T> { typedef T type; };  // NOLINT
825b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
826b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// MSVC 8.0, Sun C++, and IBM XL C++ have a bug which causes the above
827b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// definition to fail to remove the const in 'const int[3]' and 'const
828b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// char[3][4]'.  The following specialization works around the bug.
829b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// However, it causes trouble with GCC and thus needs to be
830b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// conditionally compiled.
831b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#if defined(_MSC_VER) || defined(__SUNPRO_CC) || defined(__IBMCPP__)
832b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T, size_t N>
833b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct RemoveConst<const T[N]> {
834b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  typedef typename RemoveConst<T>::type type[N];
835b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad};
836b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#endif
837b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
838b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// A handy wrapper around RemoveConst that works when the argument
839b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// T depends on template parameters.
840b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_REMOVE_CONST_(T) \
841b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    typename ::testing::internal::RemoveConst<T>::type
842b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
843b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Turns const U&, U&, const U, and U all into U.
844b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \
845b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    GTEST_REMOVE_CONST_(GTEST_REMOVE_REFERENCE_(T))
846b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
847b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Adds reference to a type if it is not a reference type,
848b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// otherwise leaves it unchanged.  This is the same as
849b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// tr1::add_reference, which is not widely available yet.
850b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T>
851b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct AddReference { typedef T& type; };  // NOLINT
852b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename T>
853b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadstruct AddReference<T&> { typedef T& type; };  // NOLINT
854b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
855b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// A handy wrapper around AddReference that works when the argument T
856b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// depends on template parameters.
857b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_ADD_REFERENCE_(T) \
858b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    typename ::testing::internal::AddReference<T>::type
859b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
860b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// Adds a reference to const on top of T as necessary.  For example,
861b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// it transforms
862b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad//
863b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad//   char         ==> const char&
864b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad//   const char   ==> const char&
865b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad//   char&        ==> const char&
866b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad//   const char&  ==> const char&
867b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad//
868b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// The argument T must depend on some template parameters.
869b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad#define GTEST_REFERENCE_TO_CONST_(T) \
870b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad    GTEST_ADD_REFERENCE_(const GTEST_REMOVE_REFERENCE_(T))
871b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
872b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// ImplicitlyConvertible<From, To>::value is a compile-time bool
87394c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru// constant that's true iff type From can be implicitly converted to
874b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad// type To.
875b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadtemplate <typename From, typename To>
876b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foadclass ImplicitlyConvertible {
877b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad private:
878b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // We need the following helper functions only for their types.
879b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // They have no implementations.
880b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
881b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // MakeFrom() is an expression whose type is From.  We cannot simply
882b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // use From(), as the type From may not have a public default
883b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // constructor.
884b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  static From MakeFrom();
885b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad
886b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // These two functions are overloaded.  Given an expression
887b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // Helper(x), the compiler will pick the first version if x can be
888b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // implicitly converted to type To; otherwise it will pick the
889b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // second version.
890b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  //
891b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // The first version returns a value of size 1, and the second
892b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // version returns a value of size 2.  Therefore, by checking the
893b33f8e3e55932d0e15a686ef0c598da8dbc37acdJay Foad  // size of Helper(x), which can be done at compile time, we can tell
894