18b52a31eSHanoh Haim// Copyright 2008, Google Inc.
28b52a31eSHanoh Haim// All rights reserved.
38b52a31eSHanoh Haim//
48b52a31eSHanoh Haim// Redistribution and use in source and binary forms, with or without
58b52a31eSHanoh Haim// modification, are permitted provided that the following conditions are
68b52a31eSHanoh Haim// met:
78b52a31eSHanoh Haim//
88b52a31eSHanoh Haim//     * Redistributions of source code must retain the above copyright
98b52a31eSHanoh Haim// notice, this list of conditions and the following disclaimer.
108b52a31eSHanoh Haim//     * Redistributions in binary form must reproduce the above
118b52a31eSHanoh Haim// copyright notice, this list of conditions and the following disclaimer
128b52a31eSHanoh Haim// in the documentation and/or other materials provided with the
138b52a31eSHanoh Haim// distribution.
148b52a31eSHanoh Haim//     * Neither the name of Google Inc. nor the names of its
158b52a31eSHanoh Haim// contributors may be used to endorse or promote products derived from
168b52a31eSHanoh Haim// this software without specific prior written permission.
178b52a31eSHanoh Haim//
188b52a31eSHanoh Haim// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
198b52a31eSHanoh Haim// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
208b52a31eSHanoh Haim// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
218b52a31eSHanoh Haim// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
228b52a31eSHanoh Haim// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
238b52a31eSHanoh Haim// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
248b52a31eSHanoh Haim// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
258b52a31eSHanoh Haim// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
268b52a31eSHanoh Haim// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278b52a31eSHanoh Haim// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
288b52a31eSHanoh Haim// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298b52a31eSHanoh Haim//
308b52a31eSHanoh Haim// Author: mheule@google.com (Markus Heule)
318b52a31eSHanoh Haim//
328b52a31eSHanoh Haim// Google C++ Testing Framework (Google Test)
338b52a31eSHanoh Haim//
348b52a31eSHanoh Haim// Sometimes it's desirable to build Google Test by compiling a single file.
358b52a31eSHanoh Haim// This file serves this purpose.
368b52a31eSHanoh Haim
378b52a31eSHanoh Haim// This line ensures that gtest.h can be compiled on its own, even
388b52a31eSHanoh Haim// when it's fused.
398b52a31eSHanoh Haim//
408b52a31eSHanoh Haim//
418b52a31eSHanoh Haim/*****
428b52a31eSHanoh Haim * NAME
438b52a31eSHanoh Haim *
448b52a31eSHanoh Haim *
458b52a31eSHanoh Haim * AUTHOR
468b52a31eSHanoh Haim *   google
478b52a31eSHanoh Haim *
488b52a31eSHanoh Haim * COPYRIGHT
498b52a31eSHanoh Haim *   Copyright (c) 2004-2011 by cisco Systems, Inc.
508b52a31eSHanoh Haim *   All rights reserved.
518b52a31eSHanoh Haim *
528b52a31eSHanoh Haim * DESCRIPTION
538b52a31eSHanoh Haim *
548b52a31eSHanoh Haim ****/
558b52a31eSHanoh Haim
568b52a31eSHanoh Haim#include <common/gtest.h>
578b52a31eSHanoh Haim
588b52a31eSHanoh Haim// The following lines pull in the real gtest *.cc files.
598b52a31eSHanoh Haim// Copyright 2005, Google Inc.
608b52a31eSHanoh Haim// All rights reserved.
618b52a31eSHanoh Haim//
628b52a31eSHanoh Haim// Redistribution and use in source and binary forms, with or without
638b52a31eSHanoh Haim// modification, are permitted provided that the following conditions are
648b52a31eSHanoh Haim// met:
658b52a31eSHanoh Haim//
668b52a31eSHanoh Haim//     * Redistributions of source code must retain the above copyright
678b52a31eSHanoh Haim// notice, this list of conditions and the following disclaimer.
688b52a31eSHanoh Haim//     * Redistributions in binary form must reproduce the above
698b52a31eSHanoh Haim// copyright notice, this list of conditions and the following disclaimer
708b52a31eSHanoh Haim// in the documentation and/or other materials provided with the
718b52a31eSHanoh Haim// distribution.
728b52a31eSHanoh Haim//     * Neither the name of Google Inc. nor the names of its
738b52a31eSHanoh Haim// contributors may be used to endorse or promote products derived from
748b52a31eSHanoh Haim// this software without specific prior written permission.
758b52a31eSHanoh Haim//
768b52a31eSHanoh Haim// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
778b52a31eSHanoh Haim// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
788b52a31eSHanoh Haim// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
798b52a31eSHanoh Haim// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
808b52a31eSHanoh Haim// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
818b52a31eSHanoh Haim// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
828b52a31eSHanoh Haim// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
838b52a31eSHanoh Haim// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
848b52a31eSHanoh Haim// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
858b52a31eSHanoh Haim// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
868b52a31eSHanoh Haim// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
878b52a31eSHanoh Haim//
888b52a31eSHanoh Haim// Author: wan@google.com (Zhanyong Wan)
898b52a31eSHanoh Haim//
908b52a31eSHanoh Haim// The Google C++ Testing Framework (Google Test)
918b52a31eSHanoh Haim
928b52a31eSHanoh Haim// Copyright 2007, Google Inc.
938b52a31eSHanoh Haim// All rights reserved.
948b52a31eSHanoh Haim//
958b52a31eSHanoh Haim// Redistribution and use in source and binary forms, with or without
968b52a31eSHanoh Haim// modification, are permitted provided that the following conditions are
978b52a31eSHanoh Haim// met:
988b52a31eSHanoh Haim//
998b52a31eSHanoh Haim//     * Redistributions of source code must retain the above copyright
1008b52a31eSHanoh Haim// notice, this list of conditions and the following disclaimer.
1018b52a31eSHanoh Haim//     * Redistributions in binary form must reproduce the above
1028b52a31eSHanoh Haim// copyright notice, this list of conditions and the following disclaimer
1038b52a31eSHanoh Haim// in the documentation and/or other materials provided with the
1048b52a31eSHanoh Haim// distribution.
1058b52a31eSHanoh Haim//     * Neither the name of Google Inc. nor the names of its
1068b52a31eSHanoh Haim// contributors may be used to endorse or promote products derived from
1078b52a31eSHanoh Haim// this software without specific prior written permission.
1088b52a31eSHanoh Haim//
1098b52a31eSHanoh Haim// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1108b52a31eSHanoh Haim// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1118b52a31eSHanoh Haim// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1128b52a31eSHanoh Haim// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1138b52a31eSHanoh Haim// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1148b52a31eSHanoh Haim// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1158b52a31eSHanoh Haim// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1168b52a31eSHanoh Haim// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1178b52a31eSHanoh Haim// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1188b52a31eSHanoh Haim// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1198b52a31eSHanoh Haim// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1208b52a31eSHanoh Haim//
1218b52a31eSHanoh Haim// Author: wan@google.com (Zhanyong Wan)
1228b52a31eSHanoh Haim//
1238b52a31eSHanoh Haim// Utilities for testing Google Test itself and code that uses Google Test
1248b52a31eSHanoh Haim// (e.g. frameworks built on top of Google Test).
1258b52a31eSHanoh Haim
1268b52a31eSHanoh Haim#ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_
1278b52a31eSHanoh Haim#define GTEST_INCLUDE_GTEST_GTEST_SPI_H_
1288b52a31eSHanoh Haim
1298b52a31eSHanoh Haim
1308b52a31eSHanoh Haimnamespace testing {
1318b52a31eSHanoh Haim
1328b52a31eSHanoh Haim// This helper class can be used to mock out Google Test failure reporting
1338b52a31eSHanoh Haim// so that we can test Google Test or code that builds on Google Test.
1348b52a31eSHanoh Haim//
1358b52a31eSHanoh Haim// An object of this class appends a TestPartResult object to the
1368b52a31eSHanoh Haim// TestPartResultArray object given in the constructor whenever a Google Test
1378b52a31eSHanoh Haim// failure is reported. It can either intercept only failures that are
1388b52a31eSHanoh Haim// generated in the same thread that created this object or it can intercept
1398b52a31eSHanoh Haim// all generated failures. The scope of this mock object can be controlled with
1408b52a31eSHanoh Haim// the second argument to the two arguments constructor.
1418b52a31eSHanoh Haimclass GTEST_API_ ScopedFakeTestPartResultReporter
1428b52a31eSHanoh Haim    : public TestPartResultReporterInterface {
1438b52a31eSHanoh Haim public:
1448b52a31eSHanoh Haim  // The two possible mocking modes of this object.
1458b52a31eSHanoh Haim  enum InterceptMode {
1468b52a31eSHanoh Haim    INTERCEPT_ONLY_CURRENT_THREAD,  // Intercepts only thread local failures.
1478b52a31eSHanoh Haim    INTERCEPT_ALL_THREADS           // Intercepts all failures.
1488b52a31eSHanoh Haim  };
1498b52a31eSHanoh Haim
1508b52a31eSHanoh Haim  // The c'tor sets this object as the test part result reporter used
1518b52a31eSHanoh Haim  // by Google Test.  The 'result' parameter specifies where to report the
1528b52a31eSHanoh Haim  // results. This reporter will only catch failures generated in the current
1538b52a31eSHanoh Haim  // thread. DEPRECATED
1548b52a31eSHanoh Haim  explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
1558b52a31eSHanoh Haim
1568b52a31eSHanoh Haim  // Same as above, but you can choose the interception scope of this object.
1578b52a31eSHanoh Haim  ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
1588b52a31eSHanoh Haim                                   TestPartResultArray* result);
1598b52a31eSHanoh Haim
1608b52a31eSHanoh Haim  // The d'tor restores the previous test part result reporter.
1618b52a31eSHanoh Haim  virtual ~ScopedFakeTestPartResultReporter();
1628b52a31eSHanoh Haim
1638b52a31eSHanoh Haim  // Appends the TestPartResult object to the TestPartResultArray
1648b52a31eSHanoh Haim  // received in the constructor.
1658b52a31eSHanoh Haim  //
1668b52a31eSHanoh Haim  // This method is from the TestPartResultReporterInterface
1678b52a31eSHanoh Haim  // interface.
1688b52a31eSHanoh Haim  virtual void ReportTestPartResult(const TestPartResult& result);
1698b52a31eSHanoh Haim private:
1708b52a31eSHanoh Haim  void Init();
1718b52a31eSHanoh Haim
1728b52a31eSHanoh Haim  const InterceptMode intercept_mode_;
1738b52a31eSHanoh Haim  TestPartResultReporterInterface* old_reporter_;
1748b52a31eSHanoh Haim  TestPartResultArray* const result_;
1758b52a31eSHanoh Haim
1768b52a31eSHanoh Haim  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);
1778b52a31eSHanoh Haim};
1788b52a31eSHanoh Haim
1798b52a31eSHanoh Haimnamespace internal {
1808b52a31eSHanoh Haim
1818b52a31eSHanoh Haim// A helper class for implementing EXPECT_FATAL_FAILURE() and
1828b52a31eSHanoh Haim// EXPECT_NONFATAL_FAILURE().  Its destructor verifies that the given
1838b52a31eSHanoh Haim// TestPartResultArray contains exactly one failure that has the given
1848b52a31eSHanoh Haim// type and contains the given substring.  If that's not the case, a
1858b52a31eSHanoh Haim// non-fatal failure will be generated.
1868b52a31eSHanoh Haimclass GTEST_API_ SingleFailureChecker {
1878b52a31eSHanoh Haim public:
1888b52a31eSHanoh Haim  // The constructor remembers the arguments.
1898b52a31eSHanoh Haim  SingleFailureChecker(const TestPartResultArray* results,
1908b52a31eSHanoh Haim                       TestPartResult::Type type,
1918b52a31eSHanoh Haim                       const char* substr);
1928b52a31eSHanoh Haim  ~SingleFailureChecker();
1938b52a31eSHanoh Haim private:
1948b52a31eSHanoh Haim  const TestPartResultArray* const results_;
1958b52a31eSHanoh Haim  const TestPartResult::Type type_;
1968b52a31eSHanoh Haim  const String substr_;
1978b52a31eSHanoh Haim
1988b52a31eSHanoh Haim  GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);
1998b52a31eSHanoh Haim};
2008b52a31eSHanoh Haim
2018b52a31eSHanoh Haim}  // namespace internal
2028b52a31eSHanoh Haim
2038b52a31eSHanoh Haim}  // namespace testing
2048b52a31eSHanoh Haim
2058b52a31eSHanoh Haim// A set of macros for testing Google Test assertions or code that's expected
2068b52a31eSHanoh Haim// to generate Google Test fatal failures.  It verifies that the given
2078b52a31eSHanoh Haim// statement will cause exactly one fatal Google Test failure with 'substr'
2088b52a31eSHanoh Haim// being part of the failure message.
2098b52a31eSHanoh Haim//
2108b52a31eSHanoh Haim// There are two different versions of this macro. EXPECT_FATAL_FAILURE only
2118b52a31eSHanoh Haim// affects and considers failures generated in the current thread and
2128b52a31eSHanoh Haim// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
2138b52a31eSHanoh Haim//
2148b52a31eSHanoh Haim// The verification of the assertion is done correctly even when the statement
2158b52a31eSHanoh Haim// throws an exception or aborts the current function.
2168b52a31eSHanoh Haim//
2178b52a31eSHanoh Haim// Known restrictions:
2188b52a31eSHanoh Haim//   - 'statement' cannot reference local non-static variables or
2198b52a31eSHanoh Haim//     non-static members of the current object.
2208b52a31eSHanoh Haim//   - 'statement' cannot return a value.
2218b52a31eSHanoh Haim//   - You cannot stream a failure message to this macro.
2228b52a31eSHanoh Haim//
2238b52a31eSHanoh Haim// Note that even though the implementations of the following two
2248b52a31eSHanoh Haim// macros are much alike, we cannot refactor them to use a common
2258b52a31eSHanoh Haim// helper macro, due to some peculiarity in how the preprocessor
2268b52a31eSHanoh Haim// works.  The AcceptsMacroThatExpandsToUnprotectedComma test in
2278b52a31eSHanoh Haim// gtest_unittest.cc will fail to compile if we do that.
2288b52a31eSHanoh Haim#define EXPECT_FATAL_FAILURE(statement, substr) \
2298b52a31eSHanoh Haim  do { \
2308b52a31eSHanoh Haim    class GTestExpectFatalFailureHelper {\
2318b52a31eSHanoh Haim     public:\
2328b52a31eSHanoh Haim      static void Execute() { statement; }\
2338b52a31eSHanoh Haim    };\
2348b52a31eSHanoh Haim    ::testing::TestPartResultArray gtest_failures;\
2358b52a31eSHanoh Haim    ::testing::internal::SingleFailureChecker gtest_checker(\
2368b52a31eSHanoh Haim        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
2378b52a31eSHanoh Haim    {\
2388b52a31eSHanoh Haim      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
2398b52a31eSHanoh Haim          ::testing::ScopedFakeTestPartResultReporter:: \
2408b52a31eSHanoh Haim          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
2418b52a31eSHanoh Haim      GTestExpectFatalFailureHelper::Execute();\
2428b52a31eSHanoh Haim    }\
2438b52a31eSHanoh Haim  } while (::testing::internal::AlwaysFalse())
2448b52a31eSHanoh Haim
2458b52a31eSHanoh Haim#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
2468b52a31eSHanoh Haim  do { \
2478b52a31eSHanoh Haim    class GTestExpectFatalFailureHelper {\
2488b52a31eSHanoh Haim     public:\
2498b52a31eSHanoh Haim      static void Execute() { statement; }\
2508b52a31eSHanoh Haim    };\
2518b52a31eSHanoh Haim    ::testing::TestPartResultArray gtest_failures;\
2528b52a31eSHanoh Haim    ::testing::internal::SingleFailureChecker gtest_checker(\
2538b52a31eSHanoh Haim        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
2548b52a31eSHanoh Haim    {\
2558b52a31eSHanoh Haim      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
2568b52a31eSHanoh Haim          ::testing::ScopedFakeTestPartResultReporter:: \
2578b52a31eSHanoh Haim          INTERCEPT_ALL_THREADS, &gtest_failures);\
2588b52a31eSHanoh Haim      GTestExpectFatalFailureHelper::Execute();\
2598b52a31eSHanoh Haim    }\
2608b52a31eSHanoh Haim  } while (::testing::internal::AlwaysFalse())
2618b52a31eSHanoh Haim
2628b52a31eSHanoh Haim// A macro for testing Google Test assertions or code that's expected to
2638b52a31eSHanoh Haim// generate Google Test non-fatal failures.  It asserts that the given
2648b52a31eSHanoh Haim// statement will cause exactly one non-fatal Google Test failure with 'substr'
2658b52a31eSHanoh Haim// being part of the failure message.
2668b52a31eSHanoh Haim//
2678b52a31eSHanoh Haim// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
2688b52a31eSHanoh Haim// affects and considers failures generated in the current thread and
2698b52a31eSHanoh Haim// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
2708b52a31eSHanoh Haim//
2718b52a31eSHanoh Haim// 'statement' is allowed to reference local variables and members of
2728b52a31eSHanoh Haim// the current object.
2738b52a31eSHanoh Haim//
2748b52a31eSHanoh Haim// The verification of the assertion is done correctly even when the statement
2758b52a31eSHanoh Haim// throws an exception or aborts the current function.
2768b52a31eSHanoh Haim//
2778b52a31eSHanoh Haim// Known restrictions:
2788b52a31eSHanoh Haim//   - You cannot stream a failure message to this macro.
2798b52a31eSHanoh Haim//
2808b52a31eSHanoh Haim// Note that even though the implementations of the following two
2818b52a31eSHanoh Haim// macros are much alike, we cannot refactor them to use a common
2828b52a31eSHanoh Haim// helper macro, due to some peculiarity in how the preprocessor
2838b52a31eSHanoh Haim// works.  If we do that, the code won't compile when the user gives
2848b52a31eSHanoh Haim// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
2858b52a31eSHanoh Haim// expands to code containing an unprotected comma.  The
2868b52a31eSHanoh Haim// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
2878b52a31eSHanoh Haim// catches that.
2888b52a31eSHanoh Haim//
2898b52a31eSHanoh Haim// For the same reason, we have to write
2908b52a31eSHanoh Haim//   if (::testing::internal::AlwaysTrue()) { statement; }
2918b52a31eSHanoh Haim// instead of
2928b52a31eSHanoh Haim//   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
2938b52a31eSHanoh Haim// to avoid an MSVC warning on unreachable code.
2948b52a31eSHanoh Haim#define EXPECT_NONFATAL_FAILURE(statement, substr) \
2958b52a31eSHanoh Haim  do {\
2968b52a31eSHanoh Haim    ::testing::TestPartResultArray gtest_failures;\
2978b52a31eSHanoh Haim    ::testing::internal::SingleFailureChecker gtest_checker(\
2988b52a31eSHanoh Haim        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
2998b52a31eSHanoh Haim        (substr));\
3008b52a31eSHanoh Haim    {\
3018b52a31eSHanoh Haim      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
3028b52a31eSHanoh Haim          ::testing::ScopedFakeTestPartResultReporter:: \
3038b52a31eSHanoh Haim          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
3048b52a31eSHanoh Haim      if (::testing::internal::AlwaysTrue()) { statement; }\
3058b52a31eSHanoh Haim    }\
3068b52a31eSHanoh Haim  } while (::testing::internal::AlwaysFalse())
3078b52a31eSHanoh Haim
3088b52a31eSHanoh Haim#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
3098b52a31eSHanoh Haim  do {\
3108b52a31eSHanoh Haim    ::testing::TestPartResultArray gtest_failures;\
3118b52a31eSHanoh Haim    ::testing::internal::SingleFailureChecker gtest_checker(\
3128b52a31eSHanoh Haim        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
3138b52a31eSHanoh Haim        (substr));\
3148b52a31eSHanoh Haim    {\
3158b52a31eSHanoh Haim      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
3168b52a31eSHanoh Haim          ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS,\
3178b52a31eSHanoh Haim          &gtest_failures);\
3188b52a31eSHanoh Haim      if (::testing::internal::AlwaysTrue()) { statement; }\
3198b52a31eSHanoh Haim    }\
3208b52a31eSHanoh Haim  } while (::testing::internal::AlwaysFalse())
3218b52a31eSHanoh Haim
3228b52a31eSHanoh Haim#endif  // GTEST_INCLUDE_GTEST_GTEST_SPI_H_
3238b52a31eSHanoh Haim
3248b52a31eSHanoh Haim#include <ctype.h>
3258b52a31eSHanoh Haim#include <math.h>
3268b52a31eSHanoh Haim#include <stdarg.h>
3278b52a31eSHanoh Haim#include <stdio.h>
3288b52a31eSHanoh Haim#include <stdlib.h>
3298b52a31eSHanoh Haim#include <wchar.h>
3308b52a31eSHanoh Haim#include <wctype.h>
3318b52a31eSHanoh Haim
3328b52a31eSHanoh Haim#include <algorithm>
3338b52a31eSHanoh Haim#include <ostream>
3348b52a31eSHanoh Haim#include <sstream>
3358b52a31eSHanoh Haim#include <vector>
3368b52a31eSHanoh Haim
3378b52a31eSHanoh Haim#if GTEST_OS_LINUX
3388b52a31eSHanoh Haim
3398b52a31eSHanoh Haim// TODO(kenton@google.com): Use autoconf to detect availability of
3408b52a31eSHanoh Haim// gettimeofday().
3418b52a31eSHanoh Haim#define GTEST_HAS_GETTIMEOFDAY_ 1
3428b52a31eSHanoh Haim
3438b52a31eSHanoh Haim#include <fcntl.h>
3448b52a31eSHanoh Haim#include <limits.h>
3458b52a31eSHanoh Haim#include <sched.h>
3468b52a31eSHanoh Haim// Declares vsnprintf().  This header is not available on Windows.
3478b52a31eSHanoh Haim#include <strings.h>
3488b52a31eSHanoh Haim#include <sys/mman.h>
3498b52a31eSHanoh Haim#include <sys/time.h>
3508b52a31eSHanoh Haim#include <unistd.h>
3518b52a31eSHanoh Haim#include <string>
3528b52a31eSHanoh Haim#include <vector>
3538b52a31eSHanoh Haim
3548b52a31eSHanoh Haim#elif GTEST_OS_SYMBIAN
3558b52a31eSHanoh Haim#define GTEST_HAS_GETTIMEOFDAY_ 1
3568b52a31eSHanoh Haim#include <sys/time.h>  // NOLINT
3578b52a31eSHanoh Haim
3588b52a31eSHanoh Haim#elif GTEST_OS_ZOS
3598b52a31eSHanoh Haim#define GTEST_HAS_GETTIMEOFDAY_ 1
3608b52a31eSHanoh Haim#include <sys/time.h>  // NOLINT
3618b52a31eSHanoh Haim
3628b52a31eSHanoh Haim// On z/OS we additionally need strings.h for strcasecmp.
3638b52a31eSHanoh Haim#include <strings.h>  // NOLINT
3648b52a31eSHanoh Haim
3658b52a31eSHanoh Haim#elif GTEST_OS_WINDOWS_MOBILE  // We are on Windows CE.
3668b52a31eSHanoh Haim
3678b52a31eSHanoh Haim#include <windows.h>  // NOLINT
3688b52a31eSHanoh Haim
3698b52a31eSHanoh Haim#elif GTEST_OS_WINDOWS  // We are on Windows proper.
3708b52a31eSHanoh Haim
3718b52a31eSHanoh Haim#include <io.h>  // NOLINT
3728b52a31eSHanoh Haim#include <sys/timeb.h>  // NOLINT
3738b52a31eSHanoh Haim#include <sys/types.h>  // NOLINT
3748b52a31eSHanoh Haim#include <sys/stat.h>  // NOLINT
3758b52a31eSHanoh Haim
3768b52a31eSHanoh Haim#if GTEST_OS_WINDOWS_MINGW
3778b52a31eSHanoh Haim// MinGW has gettimeofday() but not _ftime64().
3788b52a31eSHanoh Haim// TODO(kenton@google.com): Use autoconf to detect availability of
3798b52a31eSHanoh Haim//   gettimeofday().
3808b52a31eSHanoh Haim// TODO(kenton@google.com): There are other ways to get the time on
3818b52a31eSHanoh Haim//   Windows, like GetTickCount() or GetSystemTimeAsFileTime().  MinGW
3828b52a31eSHanoh Haim//   supports these.  consider using them instead.
3838b52a31eSHanoh Haim#define GTEST_HAS_GETTIMEOFDAY_ 1
3848b52a31eSHanoh Haim#include <sys/time.h>  // NOLINT
3858b52a31eSHanoh Haim#endif  // GTEST_OS_WINDOWS_MINGW
3868b52a31eSHanoh Haim
3878b52a31eSHanoh Haim// cpplint thinks that the header is already included, so we want to
3888b52a31eSHanoh Haim// silence it.
3898b52a31eSHanoh Haim#include <windows.h>  // NOLINT
3908b52a31eSHanoh Haim
3918b52a31eSHanoh Haim#else
3928b52a31eSHanoh Haim
3938b52a31eSHanoh Haim// Assume other platforms have gettimeofday().
3948b52a31eSHanoh Haim// TODO(kenton@google.com): Use autoconf to detect availability of
3958b52a31eSHanoh Haim//   gettimeofday().
3968b52a31eSHanoh Haim#define GTEST_HAS_GETTIMEOFDAY_ 1
3978b52a31eSHanoh Haim
3988b52a31eSHanoh Haim// cpplint thinks that the header is already included, so we want to
3998b52a31eSHanoh Haim// silence it.
4008b52a31eSHanoh Haim#include <sys/time.h>  // NOLINT
4018b52a31eSHanoh Haim#include <unistd.h>  // NOLINT
4028b52a31eSHanoh Haim
4038b52a31eSHanoh Haim#endif  // GTEST_OS_LINUX
4048b52a31eSHanoh Haim
4058b52a31eSHanoh Haim#if GTEST_HAS_EXCEPTIONS
4068b52a31eSHanoh Haim#include <stdexcept>
4078b52a31eSHanoh Haim#endif
4088b52a31eSHanoh Haim
4098b52a31eSHanoh Haim// Indicates that this translation unit is part of Google Test's
4108b52a31eSHanoh Haim// implementation.  It must come before gtest-internal-inl.h is
4118b52a31eSHanoh Haim// included, or there will be a compiler error.  This trick is to
4128b52a31eSHanoh Haim// prevent a user from accidentally including gtest-internal-inl.h in
4138b52a31eSHanoh Haim// his code.
4148b52a31eSHanoh Haim#define GTEST_IMPLEMENTATION_ 1
4158b52a31eSHanoh Haim// Copyright 2005, Google Inc.
4168b52a31eSHanoh Haim// All rights reserved.
4178b52a31eSHanoh Haim//
4188b52a31eSHanoh Haim// Redistribution and use in source and binary forms, with or without
4198b52a31eSHanoh Haim// modification, are permitted provided that the following conditions are
4208b52a31eSHanoh Haim// met:
4218b52a31eSHanoh Haim//
4228b52a31eSHanoh Haim//     * Redistributions of source code must retain the above copyright
4238b52a31eSHanoh Haim// notice, this list of conditions and the following disclaimer.
4248b52a31eSHanoh Haim//     * Redistributions in binary form must reproduce the above
4258b52a31eSHanoh Haim// copyright notice, this list of conditions and the following disclaimer
4268b52a31eSHanoh Haim// in the documentation and/or other materials provided with the
4278b52a31eSHanoh Haim// distribution.
4288b52a31eSHanoh Haim//     * Neither the name of Google Inc. nor the names of its
4298b52a31eSHanoh Haim// contributors may be used to endorse or promote products derived from
4308b52a31eSHanoh Haim// this software without specific prior written permission.
4318b52a31eSHanoh Haim//
4328b52a31eSHanoh Haim// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4338b52a31eSHanoh Haim// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4348b52a31eSHanoh Haim// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4358b52a31eSHanoh Haim// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4368b52a31eSHanoh Haim// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4378b52a31eSHanoh Haim// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
4388b52a31eSHanoh Haim// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4398b52a31eSHanoh Haim// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
4408b52a31eSHanoh Haim// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4418b52a31eSHanoh Haim// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
4428b52a31eSHanoh Haim// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4438b52a31eSHanoh Haim
4448b52a31eSHanoh Haim// Utility functions and classes used by the Google C++ testing framework.
4458b52a31eSHanoh Haim//
4468b52a31eSHanoh Haim// Author: wan@google.com (Zhanyong Wan)
4478b52a31eSHanoh Haim//
4488b52a31eSHanoh Haim// This file contains purely Google Test's internal implementation.  Please
4498b52a31eSHanoh Haim// DO NOT #INCLUDE IT IN A USER PROGRAM.
4508b52a31eSHanoh Haim
4518b52a31eSHanoh Haim#ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
4528b52a31eSHanoh Haim#define GTEST_SRC_GTEST_INTERNAL_INL_H_
4538b52a31eSHanoh Haim
4548b52a31eSHanoh Haim// GTEST_IMPLEMENTATION_ is defined to 1 iff the current translation unit is
4558b52a31eSHanoh Haim// part of Google Test's implementation; otherwise it's undefined.
4568b52a31eSHanoh Haim#if !GTEST_IMPLEMENTATION_
4578b52a31eSHanoh Haim// A user is trying to include this from his code - just say no.
4588b52a31eSHanoh Haim#error "gtest-internal-inl.h is part of Google Test's internal implementation."
4598b52a31eSHanoh Haim#error "It must not be included except by Google Test itself."
4608b52a31eSHanoh Haim#endif  // GTEST_IMPLEMENTATION_
4618b52a31eSHanoh Haim
4628b52a31eSHanoh Haim#ifndef _WIN32_WCE
4638b52a31eSHanoh Haim#include <errno.h>
4648b52a31eSHanoh Haim#endif  // !_WIN32_WCE
4658b52a31eSHanoh Haim#include <stddef.h>
4668b52a31eSHanoh Haim#include <stdlib.h>  // For strtoll/_strtoul64/malloc/free.
4678b52a31eSHanoh Haim#include <string.h>  // For memmove.
4688b52a31eSHanoh Haim
4698b52a31eSHanoh Haim#include <algorithm>
4708b52a31eSHanoh Haim#include <string>
4718b52a31eSHanoh Haim#include <vector>
4728b52a31eSHanoh Haim
4738b52a31eSHanoh Haim
4748b52a31eSHanoh Haim#if GTEST_OS_WINDOWS
4758b52a31eSHanoh Haim#include <windows.h>  // For DWORD.
4768b52a31eSHanoh Haim#endif  // GTEST_OS_WINDOWS
4778b52a31eSHanoh Haim
4788b52a31eSHanoh Haim
4798b52a31eSHanoh Haimnamespace testing {
4808b52a31eSHanoh Haim
4818b52a31eSHanoh Haim// Declares the flags.
4828b52a31eSHanoh Haim//
4838b52a31eSHanoh Haim// We don't want the users to modify this flag in the code, but want
4848b52a31eSHanoh Haim// Google Test's own unit tests to be able to access it. Therefore we
4858b52a31eSHanoh Haim// declare it here as opposed to in gtest.h.
4868b52a31eSHanoh HaimGTEST_DECLARE_bool_(death_test_use_fork);
4878b52a31eSHanoh Haim
4888b52a31eSHanoh Haimnamespace internal {
4898b52a31eSHanoh Haim
4908b52a31eSHanoh Haim// The value of GetTestTypeId() as seen from within the Google Test
4918b52a31eSHanoh Haim// library.  This is solely for testing GetTestTypeId().
4928b52a31eSHanoh HaimGTEST_API_ extern const TypeId kTestTypeIdInGoogleTest;
4938b52a31eSHanoh Haim
4948b52a31eSHanoh Haim// Names of the flags (needed for parsing Google Test flags).
4958b52a31eSHanoh Haimconst char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests";
4968b52a31eSHanoh Haimconst char kBreakOnFailureFlag[] = "break_on_failure";
4978b52a31eSHanoh Haimconst char kCatchExceptionsFlag[] = "catch_exceptions";
4988b52a31eSHanoh Haimconst char kColorFlag[] = "color";
4998b52a31eSHanoh Haimconst char kFilterFlag[] = "filter";
5008b52a31eSHanoh Haimconst char kListTestsFlag[] = "list_tests";
5018b52a31eSHanoh Haimconst char kOutputFlag[] = "output";
5028b52a31eSHanoh Haimconst char kPrintTimeFlag[] = "print_time";
5038b52a31eSHanoh Haimconst char kRandomSeedFlag[] = "random_seed";
5048b52a31eSHanoh Haimconst char kRepeatFlag[] = "repeat";
5058b52a31eSHanoh Haimconst char kShuffleFlag[] = "shuffle";
5068b52a31eSHanoh Haimconst char kStackTraceDepthFlag[] = "stack_trace_depth";
5078b52a31eSHanoh Haimconst char kThrowOnFailureFlag[] = "throw_on_failure";
5088b52a31eSHanoh Haim
5098b52a31eSHanoh Haim// A valid random seed must be in [1, kMaxRandomSeed].
5108b52a31eSHanoh Haimconst int kMaxRandomSeed = 99999;
5118b52a31eSHanoh Haim
5128b52a31eSHanoh Haim// g_help_flag is true iff the --help flag or an equivalent form is
5138b52a31eSHanoh Haim// specified on the command line.
5148b52a31eSHanoh HaimGTEST_API_ extern bool g_help_flag;
5158b52a31eSHanoh Haim
5168b52a31eSHanoh Haim// Returns the current time in milliseconds.
5178b52a31eSHanoh HaimGTEST_API_ TimeInMillis GetTimeInMillis();
5188b52a31eSHanoh Haim
5198b52a31eSHanoh Haim// Returns true iff Google Test should use colors in the output.
5208b52a31eSHanoh HaimGTEST_API_ bool ShouldUseColor(bool stdout_is_tty);
5218b52a31eSHanoh Haim
5228b52a31eSHanoh Haim// Formats the given time in milliseconds as seconds.
5238b52a31eSHanoh HaimGTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);
5248b52a31eSHanoh Haim
5258b52a31eSHanoh Haim// Parses a string for an Int32 flag, in the form of "--flag=value".
5268b52a31eSHanoh Haim//
5278b52a31eSHanoh Haim// On success, stores the value of the flag in *value, and returns
5288b52a31eSHanoh Haim// true.  On failure, returns false without changing *value.
5298b52a31eSHanoh HaimGTEST_API_ bool ParseInt32Flag(
5308b52a31eSHanoh Haim    const char* str, const char* flag, Int32* value);
5318b52a31eSHanoh Haim
5328b52a31eSHanoh Haim// Returns a random seed in range [1, kMaxRandomSeed] based on the
5338b52a31eSHanoh Haim// given --gtest_random_seed flag value.
5348b52a31eSHanoh Haiminline int GetRandomSeedFromFlag(Int32 random_seed_flag) {
5358b52a31eSHanoh Haim  const unsigned int raw_seed = (random_seed_flag == 0) ?
5368b52a31eSHanoh Haim      static_cast<unsigned int>(GetTimeInMillis()) :
5378b52a31eSHanoh Haim      static_cast<unsigned int>(random_seed_flag);
5388b52a31eSHanoh Haim
5398b52a31eSHanoh Haim  // Normalizes the actual seed to range [1, kMaxRandomSeed] such that
5408b52a31eSHanoh Haim  // it's easy to type.
5418b52a31eSHanoh Haim  const int normalized_seed =
5428b52a31eSHanoh Haim      static_cast<int>((raw_seed - 1U) %
5438b52a31eSHanoh Haim                       static_cast<unsigned int>(kMaxRandomSeed)) + 1;
5448b52a31eSHanoh Haim  return normalized_seed;
5458b52a31eSHanoh Haim}
5468b52a31eSHanoh Haim
5478b52a31eSHanoh Haim// Returns the first valid random seed after 'seed'.  The behavior is
5488b52a31eSHanoh Haim// undefined if 'seed' is invalid.  The seed after kMaxRandomSeed is
5498b52a31eSHanoh Haim// considered to be 1.
5508b52a31eSHanoh Haiminline int GetNextRandomSeed(int seed) {
5518b52a31eSHanoh Haim  GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)
5528b52a31eSHanoh Haim      << "Invalid random seed " << seed << " - must be in [1, "
5538b52a31eSHanoh Haim      << kMaxRandomSeed << "].";
5548b52a31eSHanoh Haim  const int next_seed = seed + 1;
5558b52a31eSHanoh Haim  return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
5568b52a31eSHanoh Haim}
5578b52a31eSHanoh Haim
5588b52a31eSHanoh Haim// This class saves the values of all Google Test flags in its c'tor, and
5598b52a31eSHanoh Haim// restores them in its d'tor.
5608b52a31eSHanoh Haimclass GTestFlagSaver {
5618b52a31eSHanoh Haim public:
5628b52a31eSHanoh Haim  // The c'tor.
5638b52a31eSHanoh Haim  GTestFlagSaver() {
5648b52a31eSHanoh Haim    also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests);
5658b52a31eSHanoh Haim    break_on_failure_ = GTEST_FLAG(break_on_failure);
5668b52a31eSHanoh Haim    catch_exceptions_ = GTEST_FLAG(catch_exceptions);
5678b52a31eSHanoh Haim    color_ = GTEST_FLAG(color);
5688b52a31eSHanoh Haim    death_test_style_ = GTEST_FLAG(death_test_style);
5698b52a31eSHanoh Haim    death_test_use_fork_ = GTEST_FLAG(death_test_use_fork);
5708b52a31eSHanoh Haim    filter_ = GTEST_FLAG(filter);
5718b52a31eSHanoh Haim    internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
5728b52a31eSHanoh Haim    list_tests_ = GTEST_FLAG(list_tests);
5738b52a31eSHanoh Haim    output_ = GTEST_FLAG(output);
5748b52a31eSHanoh Haim    print_time_ = GTEST_FLAG(print_time);
5758b52a31eSHanoh Haim    random_seed_ = GTEST_FLAG(random_seed);
5768b52a31eSHanoh Haim    repeat_ = GTEST_FLAG(repeat);
5778b52a31eSHanoh Haim    shuffle_ = GTEST_FLAG(shuffle);
5788b52a31eSHanoh Haim    stack_trace_depth_ = GTEST_FLAG(stack_trace_depth);
5798b52a31eSHanoh Haim    throw_on_failure_ = GTEST_FLAG(throw_on_failure);
5808b52a31eSHanoh Haim  }
5818b52a31eSHanoh Haim
5828b52a31eSHanoh Haim  // The d'tor is not virtual.  DO NOT INHERIT FROM THIS CLASS.
5838b52a31eSHanoh Haim  ~GTestFlagSaver() {
5848b52a31eSHanoh Haim    GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
5858b52a31eSHanoh Haim    GTEST_FLAG(break_on_failure) = break_on_failure_;
5868b52a31eSHanoh Haim    GTEST_FLAG(catch_exceptions) = catch_exceptions_;
5878b52a31eSHanoh Haim    GTEST_FLAG(color) = color_;
5888b52a31eSHanoh Haim    GTEST_FLAG(death_test_style) = death_test_style_;
5898b52a31eSHanoh Haim    GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
5908b52a31eSHanoh Haim    GTEST_FLAG(filter) = filter_;
5918b52a31eSHanoh Haim    GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
5928b52a31eSHanoh Haim    GTEST_FLAG(list_tests) = list_tests_;
5938b52a31eSHanoh Haim    GTEST_FLAG(output) = output_;
5948b52a31eSHanoh Haim    GTEST_FLAG(print_time) = print_time_;
5958b52a31eSHanoh Haim    GTEST_FLAG(random_seed) = random_seed_;
5968b52a31eSHanoh Haim    GTEST_FLAG(repeat) = repeat_;
5978b52a31eSHanoh Haim    GTEST_FLAG(shuffle) = shuffle_;
5988b52a31eSHanoh Haim    GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
5998b52a31eSHanoh Haim    GTEST_FLAG(throw_on_failure) = throw_on_failure_;
6008b52a31eSHanoh Haim  }
6018b52a31eSHanoh Haim private:
6028b52a31eSHanoh Haim  // Fields for saving the original values of flags.
6038b52a31eSHanoh Haim  bool also_run_disabled_tests_;
6048b52a31eSHanoh Haim  bool break_on_failure_;
6058b52a31eSHanoh Haim  bool catch_exceptions_;
6068b52a31eSHanoh Haim  String color_;
6078b52a31eSHanoh Haim  String death_test_style_;
6088b52a31eSHanoh Haim  bool death_test_use_fork_;
6098b52a31eSHanoh Haim  String filter_;
6108b52a31eSHanoh Haim  String internal_run_death_test_;
6118b52a31eSHanoh Haim  bool list_tests_;
6128b52a31eSHanoh Haim  String output_;
6138b52a31eSHanoh Haim  bool print_time_;
6148b52a31eSHanoh Haim  bool pretty_;
6158b52a31eSHanoh Haim  internal::Int32 random_seed_;
6168b52a31eSHanoh Haim  internal::Int32 repeat_;
6178b52a31eSHanoh Haim  bool shuffle_;
6188b52a31eSHanoh Haim  internal::Int32 stack_trace_depth_;
6198b52a31eSHanoh Haim  bool throw_on_failure_;
6208b52a31eSHanoh Haim} GTEST_ATTRIBUTE_UNUSED_;
6218b52a31eSHanoh Haim
6228b52a31eSHanoh Haim// Converts a Unicode code point to a narrow string in UTF-8 encoding.
6238b52a31eSHanoh Haim// code_point parameter is of type UInt32 because wchar_t may not be
6248b52a31eSHanoh Haim// wide enough to contain a code point.
6258b52a31eSHanoh Haim// The output buffer str must containt at least 32 characters.
6268b52a31eSHanoh Haim// The function returns the address of the output buffer.
6278b52a31eSHanoh Haim// If the code_point is not a valid Unicode code point
6288b52a31eSHanoh Haim// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be output
6298b52a31eSHanoh Haim// as '(Invalid Unicode 0xXXXXXXXX)'.
6308b52a31eSHanoh HaimGTEST_API_ char* CodePointToUtf8(UInt32 code_point, char* str);
6318b52a31eSHanoh Haim
6328b52a31eSHanoh Haim// Converts a wide string to a narrow string in UTF-8 encoding.
6338b52a31eSHanoh Haim// The wide string is assumed to have the following encoding:
6348b52a31eSHanoh Haim//   UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS)
6358b52a31eSHanoh Haim//   UTF-32 if sizeof(wchar_t) == 4 (on Linux)
6368b52a31eSHanoh Haim// Parameter str points to a null-terminated wide string.
6378b52a31eSHanoh Haim// Parameter num_chars may additionally limit the number
6388b52a31eSHanoh Haim// of wchar_t characters processed. -1 is used when the entire string
6398b52a31eSHanoh Haim// should be processed.
6408b52a31eSHanoh Haim// If the string contains code points that are not valid Unicode code points
6418b52a31eSHanoh Haim// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output
6428b52a31eSHanoh Haim// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding
6438b52a31eSHanoh Haim// and contains invalid UTF-16 surrogate pairs, values in those pairs
6448b52a31eSHanoh Haim// will be encoded as individual Unicode characters from Basic Normal Plane.
6458b52a31eSHanoh HaimGTEST_API_ String WideStringToUtf8(const wchar_t* str, int num_chars);
6468b52a31eSHanoh Haim
6478b52a31eSHanoh Haim// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file
6488b52a31eSHanoh Haim// if the variable is present. If a file already exists at this location, this
6498b52a31eSHanoh Haim// function will write over it. If the variable is present, but the file cannot
6508b52a31eSHanoh Haim// be created, prints an error and exits.
6518b52a31eSHanoh Haimvoid WriteToShardStatusFileIfNeeded();
6528b52a31eSHanoh Haim
6538b52a31eSHanoh Haim// Checks whether sharding is enabled by examining the relevant
6548b52a31eSHanoh Haim// environment variable values. If the variables are present,
6558b52a31eSHanoh Haim// but inconsistent (e.g., shard_index >= total_shards), prints
6568b52a31eSHanoh Haim// an error and exits. If in_subprocess_for_death_test, sharding is
6578b52a31eSHanoh Haim// disabled because it must only be applied to the original test
6588b52a31eSHanoh Haim// process. Otherwise, we could filter out death tests we intended to execute.
6598b52a31eSHanoh HaimGTEST_API_ bool ShouldShard(const char* total_shards_str,
6608b52a31eSHanoh Haim                            const char* shard_index_str,
6618b52a31eSHanoh Haim                            bool in_subprocess_for_death_test);
6628b52a31eSHanoh Haim
6638b52a31eSHanoh Haim// Parses the environment variable var as an Int32. If it is unset,
6648b52a31eSHanoh Haim// returns default_val. If it is not an Int32, prints an error and
6658b52a31eSHanoh Haim// and aborts.
6668b52a31eSHanoh HaimGTEST_API_ Int32 Int32FromEnvOrDie(const char* env_var, Int32 default_val);
6678b52a31eSHanoh Haim
6688b52a31eSHanoh Haim// Given the total number of shards, the shard index, and the test id,
6698b52a31eSHanoh Haim// returns true iff the test should be run on this shard. The test id is
6708b52a31eSHanoh Haim// some arbitrary but unique non-negative integer assigned to each test
6718b52a31eSHanoh Haim// method. Assumes that 0 <= shard_index < total_shards.
6728b52a31eSHanoh HaimGTEST_API_ bool ShouldRunTestOnShard(
6738b52a31eSHanoh Haim    int total_shards, int shard_index, int test_id);
6748b52a31eSHanoh Haim
6758b52a31eSHanoh Haim// STL container utilities.
6768b52a31eSHanoh Haim
6778b52a31eSHanoh Haim// Returns the number of elements in the given container that satisfy
6788b52a31eSHanoh Haim// the given predicate.
6798b52a31eSHanoh Haimtemplate <class Container, typename Predicate>
6808b52a31eSHanoh Haiminline int CountIf(const Container& c, Predicate predicate) {
6818b52a31eSHanoh Haim  return static_cast<int>(std::count_if(c.begin(), c.end(), predicate));
6828b52a31eSHanoh Haim}
6838b52a31eSHanoh Haim
6848b52a31eSHanoh Haim// Applies a function/functor to each element in the container.
6858b52a31eSHanoh Haimtemplate <class Container, typename Functor>
6868b52a31eSHanoh Haimvoid ForEach(const Container& c, Functor functor) {
6878b52a31eSHanoh Haim  std::for_each(c.begin(), c.end(), functor);
6888b52a31eSHanoh Haim}
6898b52a31eSHanoh Haim
6908b52a31eSHanoh Haim// Returns the i-th element of the vector, or default_value if i is not
6918b52a31eSHanoh Haim// in range [0, v.size()).
6928b52a31eSHanoh Haimtemplate <typename E>
6938b52a31eSHanoh Haiminline E GetElementOr(const std::vector<E>& v, int i, E default_value) {
6948b52a31eSHanoh Haim  return (i < 0 || i >= static_cast<int>(v.size())) ? default_value : v[i];
6958b52a31eSHanoh Haim}
6968b52a31eSHanoh Haim
6978b52a31eSHanoh Haim// Performs an in-place shuffle of a range of the vector's elements.
6988b52a31eSHanoh Haim// 'begin' and 'end' are element indices as an STL-style range;
6998b52a31eSHanoh Haim// i.e. [begin, end) are shuffled, where 'end' == size() means to
7008b52a31eSHanoh Haim// shuffle to the end of the vector.
7018b52a31eSHanoh Haimtemplate <typename E>
7028b52a31eSHanoh Haimvoid ShuffleRange(internal::Random* random, int begin, int end,
7038b52a31eSHanoh Haim                  std::vector<E>* v) {
7048b52a31eSHanoh Haim  const int size = static_cast<int>(v->size());
7058b52a31eSHanoh Haim  GTEST_CHECK_(0 <= begin && begin <= size)
7068b52a31eSHanoh Haim      << "Invalid shuffle range start " << begin << ": must be in range [0, "
7078b52a31eSHanoh Haim      << size << "].";
7088b52a31eSHanoh Haim  GTEST_CHECK_(begin <= end && end <= size)
7098b52a31eSHanoh Haim      << "Invalid shuffle range finish " << end << ": must be in range ["
7108b52a31eSHanoh Haim      << begin << ", " << size << "].";
7118b52a31eSHanoh Haim
7128b52a31eSHanoh Haim  // Fisher-Yates shuffle, from
7138b52a31eSHanoh Haim  // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
7148b52a31eSHanoh Haim  for (int range_width = end - begin; range_width >= 2; range_width--) {
7158b52a31eSHanoh Haim    const int last_in_range = begin + range_width - 1;
7168b52a31eSHanoh Haim    const int selected = begin + random->Generate(range_width);
7178b52a31eSHanoh Haim    std::swap((*v)[selected], (*v)[last_in_range]);
7188b52a31eSHanoh Haim  }
7198b52a31eSHanoh Haim}
7208b52a31eSHanoh Haim
7218b52a31eSHanoh Haim// Performs an in-place shuffle of the vector's elements.
7228b52a31eSHanoh Haimtemplate <typename E>
7238b52a31eSHanoh Haiminline void Shuffle(internal::Random* random, std::vector<E>* v) {
7248b52a31eSHanoh Haim  ShuffleRange(random, 0, static_cast<int>(v->size()), v);
7258b52a31eSHanoh Haim}
7268b52a31eSHanoh Haim
7278b52a31eSHanoh Haim// A function for deleting an object.  Handy for being used as a
7288b52a31eSHanoh Haim// functor.
7298b52a31eSHanoh Haimtemplate <typename T>
7308b52a31eSHanoh Haimstatic void Delete(T* x) {
7318b52a31eSHanoh Haim  delete x;
7328b52a31eSHanoh Haim}
7338b52a31eSHanoh Haim
7348b52a31eSHanoh Haim// A predicate that checks the key of a TestProperty against a known key.
7358b52a31eSHanoh Haim//
7368b52a31eSHanoh Haim// TestPropertyKeyIs is copyable.
7378b52a31eSHanoh Haimclass TestPropertyKeyIs {
7388b52a31eSHanoh Haim public:
7398b52a31eSHanoh Haim  // Constructor.
7408b52a31eSHanoh Haim  //
7418b52a31eSHanoh Haim  // TestPropertyKeyIs has NO default constructor.
7428b52a31eSHanoh Haim  explicit TestPropertyKeyIs(const char* key)
7438b52a31eSHanoh Haim      : key_(key) {}
7448b52a31eSHanoh Haim
7458b52a31eSHanoh Haim  // Returns true iff the test name of test property matches on key_.
7468b52a31eSHanoh Haim  bool operator()(const TestProperty& test_property) const {
7478b52a31eSHanoh Haim    return String(test_property.key()).Compare(key_) == 0;
7488b52a31eSHanoh Haim  }
7498b52a31eSHanoh Haim
7508b52a31eSHanoh Haim private:
7518b52a31eSHanoh Haim  String key_;
7528b52a31eSHanoh Haim};
7538b52a31eSHanoh Haim
7548b52a31eSHanoh Haimclass TestInfoImpl {
7558b52a31eSHanoh Haim public:
7568b52a31eSHanoh Haim  TestInfoImpl(TestInfo* parent, const char* test_case_name,
7578b52a31eSHanoh Haim               const char* name, const char* test_case_comment,
7588b52a31eSHanoh Haim               const char* comment, TypeId fixture_class_id,
7598b52a31eSHanoh Haim               internal::TestFactoryBase* factory);
7608b52a31eSHanoh Haim  ~TestInfoImpl();
7618b52a31eSHanoh Haim
7628b52a31eSHanoh Haim  // Returns true if this test should run.
7638b52a31eSHanoh Haim  bool should_run() const { return should_run_; }
7648b52a31eSHanoh Haim
7658b52a31eSHanoh Haim  // Sets the should_run member.
7668b52a31eSHanoh Haim  void set_should_run(bool should) { should_run_ = should; }
7678b52a31eSHanoh Haim
7688b52a31eSHanoh Haim  // Returns true if this test is disabled. Disabled tests are not run.
7698b52a31eSHanoh Haim  bool is_disabled() const { return is_disabled_; }
7708b52a31eSHanoh Haim
7718b52a31eSHanoh Haim  // Sets the is_disabled member.
7728b52a31eSHanoh Haim  void set_is_disabled(bool is) { is_disabled_ = is; }
7738b52a31eSHanoh Haim
7748b52a31eSHanoh Haim  // Returns true if this test matches the filter specified by the user.
7758b52a31eSHanoh Haim  bool matches_filter() const { return matches_filter_; }
7768b52a31eSHanoh Haim
7778b52a31eSHanoh Haim  // Sets the matches_filter member.
7788b52a31eSHanoh Haim  void set_matches_filter(bool matches) { matches_filter_ = matches; }
7798b52a31eSHanoh Haim
7808b52a31eSHanoh Haim  // Returns the test case name.
7818b52a31eSHanoh Haim  const char* test_case_name() const { return test_case_name_.c_str(); }
7828b52a31eSHanoh Haim
7838b52a31eSHanoh Haim  // Returns the test name.
7848b52a31eSHanoh Haim  const char* name() const { return name_.c_str(); }
7858b52a31eSHanoh Haim
7868b52a31eSHanoh Haim  // Returns the test case comment.
7878b52a31eSHanoh Haim  const char* test_case_comment() const { return test_case_comment_.c_str(); }
7888b52a31eSHanoh Haim
7898b52a31eSHanoh Haim  // Returns the test comment.
7908b52a31eSHanoh Haim  const char* comment() const { return comment_.c_str(); }
7918b52a31eSHanoh Haim
7928b52a31eSHanoh Haim  // Returns the ID of the test fixture class.
7938b52a31eSHanoh Haim  TypeId fixture_class_id() const { return fixture_class_id_; }
7948b52a31eSHanoh Haim
7958b52a31eSHanoh Haim  // Returns the test result.
7968b52a31eSHanoh Haim  TestResult* result() { return &result_; }
7978b52a31eSHanoh Haim  const TestResult* result() const { return &result_; }
7988b52a31eSHanoh Haim
7998b52a31eSHanoh Haim  // Creates the test object, runs it, records its result, and then
8008b52a31eSHanoh Haim  // deletes it.
8018b52a31eSHanoh Haim  void Run();
8028b52a31eSHanoh Haim
8038b52a31eSHanoh Haim  // Clears the test result.
8048b52a31eSHanoh Haim  void ClearResult() { result_.Clear(); }
8058b52a31eSHanoh Haim
8068b52a31eSHanoh Haim  // Clears the test result in the given TestInfo object.
8078b52a31eSHanoh Haim  static void ClearTestResult(TestInfo * test_info) {
8088b52a31eSHanoh Haim    test_info->impl()->ClearResult();
8098b52a31eSHanoh Haim  }
8108b52a31eSHanoh Haim
8118b52a31eSHanoh Haim private:
8128b52a31eSHanoh Haim  // These fields are immutable properties of the test.
8138b52a31eSHanoh Haim  TestInfo* const parent_;          // The owner of this object
8148b52a31eSHanoh Haim  const String test_case_name_;     // Test case name
8158b52a31eSHanoh Haim  const String name_;               // Test name
8168b52a31eSHanoh Haim  const String test_case_comment_;  // Test case comment
8178b52a31eSHanoh Haim  const String comment_;            // Test comment
8188b52a31eSHanoh Haim  const TypeId fixture_class_id_;   // ID of the test fixture class
8198b52a31eSHanoh Haim  bool should_run_;                 // True iff this test should run
8208b52a31eSHanoh Haim  bool is_disabled_;                // True iff this test is disabled
8218b52a31eSHanoh Haim  bool matches_filter_;             // True if this test matches the
8228b52a31eSHanoh Haim                                    // user-specified filter.
8238b52a31eSHanoh Haim  internal::TestFactoryBase* const factory_;  // The factory that creates
8248b52a31eSHanoh Haim                                              // the test object
8258b52a31eSHanoh Haim
8268b52a31eSHanoh Haim  // This field is mutable and needs to be reset before running the
8278b52a31eSHanoh Haim  // test for the second time.
8288b52a31eSHanoh Haim  TestResult result_;
8298b52a31eSHanoh Haim
8308b52a31eSHanoh Haim  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfoImpl);
8318b52a31eSHanoh Haim};
8328b52a31eSHanoh Haim
8338b52a31eSHanoh Haim// Class UnitTestOptions.
8348b52a31eSHanoh Haim//
8358b52a31eSHanoh Haim// This class contains functions for processing options the user
8368b52a31eSHanoh Haim// specifies when running the tests.  It has only static members.
8378b52a31eSHanoh Haim//
8388b52a31eSHanoh Haim// In most cases, the user can specify an option using either an
8398b52a31eSHanoh Haim// environment variable or a command line flag.  E.g. you can set the
8408b52a31eSHanoh Haim// test filter using either GTEST_FILTER or --gtest_filter.  If both
8418b52a31eSHanoh Haim// the variable and the flag are present, the latter overrides the
8428b52a31eSHanoh Haim// former.
8438b52a31eSHanoh Haimclass GTEST_API_ UnitTestOptions {
8448b52a31eSHanoh Haim public:
8458b52a31eSHanoh Haim  // Functions for processing the gtest_output flag.
8468b52a31eSHanoh Haim
8478b52a31eSHanoh Haim  // Returns the output format, or "" for normal printed output.
8488b52a31eSHanoh Haim  static String GetOutputFormat();
8498b52a31eSHanoh Haim
8508b52a31eSHanoh Haim  // Returns the absolute path of the requested output file, or the
8518b52a31eSHanoh Haim  // default (test_detail.xml in the original working directory) if
8528b52a31eSHanoh Haim  // none was explicitly specified.
8538b52a31eSHanoh Haim  static String GetAbsolutePathToOutputFile();
8548b52a31eSHanoh Haim
8558b52a31eSHanoh Haim  // Functions for processing the gtest_filter flag.
8568b52a31eSHanoh Haim
8578b52a31eSHanoh Haim  // Returns true iff the wildcard pattern matches the string.  The
8588b52a31eSHanoh Haim  // first ':' or '\0' character in pattern marks the end of it.
8598b52a31eSHanoh Haim  //
8608b52a31eSHanoh Haim  // This recursive algorithm isn't very efficient, but is clear and
8618b52a31eSHanoh Haim  // works well enough for matching test names, which are short.
8628b52a31eSHanoh Haim  static bool PatternMatchesString(const char *pattern, const char *str);
8638b52a31eSHanoh Haim
8648b52a31eSHanoh Haim  // Returns true iff the user-specified filter matches the test case
8658b52a31eSHanoh Haim  // name and the test name.
8668b52a31eSHanoh Haim  static bool FilterMatchesTest(const String &test_case_name,
8678b52a31eSHanoh Haim                                const String &test_name);
8688b52a31eSHanoh Haim
8698b52a31eSHanoh Haim#if GTEST_OS_WINDOWS
8708b52a31eSHanoh Haim  // Function for supporting the gtest_catch_exception flag.
8718b52a31eSHanoh Haim
8728b52a31eSHanoh Haim  // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
8738b52a31eSHanoh Haim  // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
8748b52a31eSHanoh Haim  // This function is useful as an __except condition.
8758b52a31eSHanoh Haim  static int GTestShouldProcessSEH(DWORD exception_code);
8768b52a31eSHanoh Haim#endif  // GTEST_OS_WINDOWS
8778b52a31eSHanoh Haim
8788b52a31eSHanoh Haim  // Returns true if "name" matches the ':' separated list of glob-style
8798b52a31eSHanoh Haim  // filters in "filter".
8808b52a31eSHanoh Haim  static bool MatchesFilter(const String& name, const char* filter);
8818b52a31eSHanoh Haim};
8828b52a31eSHanoh Haim
8838b52a31eSHanoh Haim// Returns the current application's name, removing directory path if that
8848b52a31eSHanoh Haim// is present.  Used by UnitTestOptions::GetOutputFile.
8858b52a31eSHanoh HaimGTEST_API_ FilePath GetCurrentExecutableName();
8868b52a31eSHanoh Haim
8878b52a31eSHanoh Haim// The role interface for getting the OS stack trace as a string.
8888b52a31eSHanoh Haimclass OsStackTraceGetterInterface {
8898b52a31eSHanoh Haim public:
8908b52a31eSHanoh Haim  OsStackTraceGetterInterface() {}
8918b52a31eSHanoh Haim  virtual ~OsStackTraceGetterInterface() {}
8928b52a31eSHanoh Haim
8938b52a31eSHanoh Haim  // Returns the current OS stack trace as a String.  Parameters:
8948b52a31eSHanoh Haim  //
8958b52a31eSHanoh Haim  //   max_depth  - the maximum number of stack frames to be included
8968b52a31eSHanoh Haim  //                in the trace.
8978b52a31eSHanoh Haim  //   skip_count - the number of top frames to be skipped; doesn't count
8988b52a31eSHanoh Haim  //                against max_depth.
8998b52a31eSHanoh Haim  virtual String CurrentStackTrace(int max_depth, int skip_count) = 0;
9008b52a31eSHanoh Haim
9018b52a31eSHanoh Haim  // UponLeavingGTest() should be called immediately before Google Test calls
9028b52a31eSHanoh Haim  // user code. It saves some information about the current stack that
9038b52a31eSHanoh Haim  // CurrentStackTrace() will use to find and hide Google Test stack frames.
9048b52a31eSHanoh Haim  virtual void UponLeavingGTest() = 0;
9058b52a31eSHanoh Haim
9068b52a31eSHanoh Haim private:
9078b52a31eSHanoh Haim  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface);
9088b52a31eSHanoh Haim};
9098b52a31eSHanoh Haim
9108b52a31eSHanoh Haim// A working implementation of the OsStackTraceGetterInterface interface.
9118b52a31eSHanoh Haimclass OsStackTraceGetter : public OsStackTraceGetterInterface {
9128b52a31eSHanoh Haim public:
9138b52a31eSHanoh Haim  OsStackTraceGetter() : caller_frame_(NULL) {}
9148b52a31eSHanoh Haim  virtual String CurrentStackTrace(int max_depth, int skip_count);
9158b52a31eSHanoh Haim  virtual void UponLeavingGTest();
9168b52a31eSHanoh Haim
9178b52a31eSHanoh Haim  // This string is inserted in place of stack frames that are part of
9188b52a31eSHanoh Haim  // Google Test's implementation.
9198b52a31eSHanoh Haim  static const char* const kElidedFramesMarker;
9208b52a31eSHanoh Haim
9218b52a31eSHanoh Haim private:
9228b52a31eSHanoh Haim  Mutex mutex_;  // protects all internal state
9238b52a31eSHanoh Haim
9248b52a31eSHanoh Haim  // We save the stack frame below the frame that calls user code.
9258b52a31eSHanoh Haim  // We do this because the address of the frame immediately below
9268b52a31eSHanoh Haim  // the user code changes between the call to UponLeavingGTest()
9278b52a31eSHanoh Haim  // and any calls to CurrentStackTrace() from within the user code.
9288b52a31eSHanoh Haim  void* caller_frame_;
9298b52a31eSHanoh Haim
9308b52a31eSHanoh Haim  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter);
9318b52a31eSHanoh Haim};
9328b52a31eSHanoh Haim
9338b52a31eSHanoh Haim// Information about a Google Test trace point.
9348b52a31eSHanoh Haimstruct TraceInfo {
9358b52a31eSHanoh Haim  const char* file;
9368b52a31eSHanoh Haim  int line;
9378b52a31eSHanoh Haim  String message;
9388b52a31eSHanoh Haim};
9398b52a31eSHanoh Haim
9408b52a31eSHanoh Haim// This is the default global test part result reporter used in UnitTestImpl.
9418b52a31eSHanoh Haim// This class should only be used by UnitTestImpl.
9428b52a31eSHanoh Haimclass DefaultGlobalTestPartResultReporter
9438b52a31eSHanoh Haim  : public TestPartResultReporterInterface {
9448b52a31eSHanoh Haim public:
9458b52a31eSHanoh Haim  explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
9468b52a31eSHanoh Haim  // Implements the TestPartResultReporterInterface. Reports the test part
9478b52a31eSHanoh Haim  // result in the current test.
9488b52a31eSHanoh Haim  virtual void ReportTestPartResult(const TestPartResult& result);
9498b52a31eSHanoh Haim
9508b52a31eSHanoh Haim private:
9518b52a31eSHanoh Haim  UnitTestImpl* const unit_test_;
9528b52a31eSHanoh Haim
9538b52a31eSHanoh Haim  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);
9548b52a31eSHanoh Haim};
9558b52a31eSHanoh Haim
9568b52a31eSHanoh Haim// This is the default per thread test part result reporter used in
9578b52a31eSHanoh Haim// UnitTestImpl. This class should only be used by UnitTestImpl.
9588b52a31eSHanoh Haimclass DefaultPerThreadTestPartResultReporter
9598b52a31eSHanoh Haim    : public TestPartResultReporterInterface {
9608b52a31eSHanoh Haim public:
9618b52a31eSHanoh Haim  explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
9628b52a31eSHanoh Haim  // Implements the TestPartResultReporterInterface. The implementation just
9638b52a31eSHanoh Haim  // delegates to the current global test part result reporter of *unit_test_.
9648b52a31eSHanoh Haim  virtual void ReportTestPartResult(const TestPartResult& result);
9658b52a31eSHanoh Haim
9668b52a31eSHanoh Haim private:
9678b52a31eSHanoh Haim  UnitTestImpl* const unit_test_;
9688b52a31eSHanoh Haim
9698b52a31eSHanoh Haim  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter);
9708b52a31eSHanoh Haim};
9718b52a31eSHanoh Haim
9728b52a31eSHanoh Haim// The private implementation of the UnitTest class.  We don't protect
9738b52a31eSHanoh Haim// the methods under a mutex, as this class is not accessible by a
9748b52a31eSHanoh Haim// user and the UnitTest class that delegates work to this class does
9758b52a31eSHanoh Haim// proper locking.
9768b52a31eSHanoh Haimclass GTEST_API_ UnitTestImpl {
9778b52a31eSHanoh Haim public:
9788b52a31eSHanoh Haim  explicit UnitTestImpl(UnitTest* parent);
9798b52a31eSHanoh Haim  virtual ~UnitTestImpl();
9808b52a31eSHanoh Haim
9818b52a31eSHanoh Haim  // There are two different ways to register your own TestPartResultReporter.
9828b52a31eSHanoh Haim  // You can register your own repoter to listen either only for test results
9838b52a31eSHanoh Haim  // from the current thread or for results from all threads.
9848b52a31eSHanoh Haim  // By default, each per-thread test result repoter just passes a new
9858b52a31eSHanoh Haim  // TestPartResult to the global test result reporter, which registers the
9868b52a31eSHanoh Haim  // test part result for the currently running test.
9878b52a31eSHanoh Haim
9888b52a31eSHanoh Haim  // Returns the global test part result reporter.
9898b52a31eSHanoh Haim  TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
9908b52a31eSHanoh Haim
9918b52a31eSHanoh Haim  // Sets the global test part result reporter.
9928b52a31eSHanoh Haim  void SetGlobalTestPartResultReporter(
9938b52a31eSHanoh Haim      TestPartResultReporterInterface* reporter);
9948b52a31eSHanoh Haim
9958b52a31eSHanoh Haim  // Returns the test part result reporter for the current thread.
9968b52a31eSHanoh Haim  TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
9978b52a31eSHanoh Haim
9988b52a31eSHanoh Haim  // Sets the test part result reporter for the current thread.
9998b52a31eSHanoh Haim  void SetTestPartResultReporterForCurrentThread(
10008b52a31eSHanoh Haim      TestPartResultReporterInterface* reporter);
10018b52a31eSHanoh Haim
10028b52a31eSHanoh Haim  // Gets the number of successful test cases.
10038b52a31eSHanoh Haim  int successful_test_case_count() const;
10048b52a31eSHanoh Haim
10058b52a31eSHanoh Haim  // Gets the number of failed test cases.
10068b52a31eSHanoh Haim  int failed_test_case_count() const;
10078b52a31eSHanoh Haim
10088b52a31eSHanoh Haim  // Gets the number of all test cases.
10098b52a31eSHanoh Haim  int total_test_case_count() const;
10108b52a31eSHanoh Haim
10118b52a31eSHanoh Haim  // Gets the number of all test cases that contain at least one test
10128b52a31eSHanoh Haim  // that should run.
10138b52a31eSHanoh Haim  int test_case_to_run_count() const;
10148b52a31eSHanoh Haim
10158b52a31eSHanoh Haim  // Gets the number of successful tests.
10168b52a31eSHanoh Haim  int successful_test_count() const;
10178b52a31eSHanoh Haim
10188b52a31eSHanoh Haim  // Gets the number of failed tests.
10198b52a31eSHanoh Haim  int failed_test_count() const;
10208b52a31eSHanoh Haim
10218b52a31eSHanoh Haim  // Gets the number of disabled tests.
10228b52a31eSHanoh Haim  int disabled_test_count() const;
10238b52a31eSHanoh Haim
10248b52a31eSHanoh Haim  // Gets the number of all tests.
10258b52a31eSHanoh Haim  int total_test_count() const;
10268b52a31eSHanoh Haim
10278b52a31eSHanoh Haim  // Gets the number of tests that should run.
10288b52a31eSHanoh Haim  int test_to_run_count() const;
10298b52a31eSHanoh Haim
10308b52a31eSHanoh Haim  // Gets the elapsed time, in milliseconds.
10318b52a31eSHanoh Haim  TimeInMillis elapsed_time() const { return elapsed_time_; }
10328b52a31eSHanoh Haim
10338b52a31eSHanoh Haim  // Returns true iff the unit test passed (i.e. all test cases passed).
10348b52a31eSHanoh Haim  bool Passed() const { return !Failed(); }
10358b52a31eSHanoh Haim
10368b52a31eSHanoh Haim  // Returns true iff the unit test failed (i.e. some test case failed
10378b52a31eSHanoh Haim  // or something outside of all tests failed).
10388b52a31eSHanoh Haim  bool Failed() const {
10398b52a31eSHanoh Haim    return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
10408b52a31eSHanoh Haim  }
10418b52a31eSHanoh Haim
10428b52a31eSHanoh Haim  // Gets the i-th test case among all the test cases. i can range from 0 to
10438b52a31eSHanoh Haim  // total_test_case_count() - 1. If i is not in that range, returns NULL.
10448b52a31eSHanoh Haim  const TestCase* GetTestCase(int i) const {
10458b52a31eSHanoh Haim    const int index = GetElementOr(test_case_indices_, i, -1);
10468b52a31eSHanoh Haim    return index < 0 ? NULL : test_cases_[i];
10478b52a31eSHanoh Haim  }
10488b52a31eSHanoh Haim
10498b52a31eSHanoh Haim  // Gets the i-th test case among all the test cases. i can range from 0 to
10508b52a31eSHanoh Haim  // total_test_case_count() - 1. If i is not in that range, returns NULL.
10518b52a31eSHanoh Haim  TestCase* GetMutableTestCase(int i) {
10528b52a31eSHanoh Haim    const int index = GetElementOr(test_case_indices_, i, -1);
10538b52a31eSHanoh Haim    return index < 0 ? NULL : test_cases_[index];
10548b52a31eSHanoh Haim  }
10558b52a31eSHanoh Haim
10568b52a31eSHanoh Haim  // Provides access to the event listener list.
10578b52a31eSHanoh Haim  TestEventListeners* listeners() { return &listeners_; }
10588b52a31eSHanoh Haim
10598b52a31eSHanoh Haim  // Returns the TestResult for the test that's currently running, or
10608b52a31eSHanoh Haim  // the TestResult for the ad hoc test if no test is running.
10618b52a31eSHanoh Haim  TestResult* current_test_result();
10628b52a31eSHanoh Haim
10638b52a31eSHanoh Haim  // Returns the TestResult for the ad hoc test.
10648b52a31eSHanoh Haim  const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
10658b52a31eSHanoh Haim
10668b52a31eSHanoh Haim  // Sets the OS stack trace getter.
10678b52a31eSHanoh Haim  //
10688b52a31eSHanoh Haim  // Does nothing if the input and the current OS stack trace getter
10698b52a31eSHanoh Haim  // are the same; otherwise, deletes the old getter and makes the
10708b52a31eSHanoh Haim  // input the current getter.
10718b52a31eSHanoh Haim  void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
10728b52a31eSHanoh Haim
10738b52a31eSHanoh Haim  // Returns the current OS stack trace getter if it is not NULL;
10748b52a31eSHanoh Haim  // otherwise, creates an OsStackTraceGetter, makes it the current
10758b52a31eSHanoh Haim  // getter, and returns it.
10768b52a31eSHanoh Haim  OsStackTraceGetterInterface* os_stack_trace_getter();
10778b52a31eSHanoh Haim
10788b52a31eSHanoh Haim  // Returns the current OS stack trace as a String.
10798b52a31eSHanoh Haim  //
10808b52a31eSHanoh Haim  // The maximum number of stack frames to be included is specified by
10818b52a31eSHanoh Haim  // the gtest_stack_trace_depth flag.  The skip_count parameter
10828b52a31eSHanoh Haim  // specifies the number of top frames to be skipped, which doesn't
10838b52a31eSHanoh Haim  // count against the number of frames to be included.
10848b52a31eSHanoh Haim  //
10858b52a31eSHanoh Haim  // For example, if Foo() calls Bar(), which in turn calls
10868b52a31eSHanoh Haim  // CurrentOsStackTraceExceptTop(1), Foo() will be included in the
10878b52a31eSHanoh Haim  // trace but Bar() and CurrentOsStackTraceExceptTop() won't.
10888b52a31eSHanoh Haim  String CurrentOsStackTraceExceptTop(int skip_count);
10898b52a31eSHanoh Haim
10908b52a31eSHanoh Haim  // Finds and returns a TestCase with the given name.  If one doesn't
10918b52a31eSHanoh Haim  // exist, creates one and returns it.
10928b52a31eSHanoh Haim  //
10938b52a31eSHanoh Haim  // Arguments:
10948b52a31eSHanoh Haim  //
10958b52a31eSHanoh Haim  //   test_case_name: name of the test case
10968b52a31eSHanoh Haim  //   set_up_tc:      pointer to the function that sets up the test case
10978b52a31eSHanoh Haim  //   tear_down_tc:   pointer to the function that tears down the test case
10988b52a31eSHanoh Haim  TestCase* GetTestCase(const char* test_case_name,
10998b52a31eSHanoh Haim                        const char* comment,
11008b52a31eSHanoh Haim                        Test::SetUpTestCaseFunc set_up_tc,
11018b52a31eSHanoh Haim                        Test::TearDownTestCaseFunc tear_down_tc);
11028b52a31eSHanoh Haim
11038b52a31eSHanoh Haim  // Adds a TestInfo to the unit test.
11048b52a31eSHanoh Haim  //
11058b52a31eSHanoh Haim  // Arguments:
11068b52a31eSHanoh Haim  //
11078b52a31eSHanoh Haim  //   set_up_tc:    pointer to the function that sets up the test case
11088b52a31eSHanoh Haim  //   tear_down_tc: pointer to the function that tears down the test case
11098b52a31eSHanoh Haim  //   test_info:    the TestInfo object
11108b52a31eSHanoh Haim  void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc,
11118b52a31eSHanoh Haim                   Test::TearDownTestCaseFunc tear_down_tc,
11128b52a31eSHanoh Haim                   TestInfo * test_info) {
11138b52a31eSHanoh Haim    // In order to support thread-safe death tests, we need to
11148b52a31eSHanoh Haim    // remember the original working directory when the test program
11158b52a31eSHanoh Haim    // was first invoked.  We cannot do this in RUN_ALL_TESTS(), as
11168b52a31eSHanoh Haim    // the user may have changed the current directory before calling
11178b52a31eSHanoh Haim    // RUN_ALL_TESTS().  Therefore we capture the current directory in
11188b52a31eSHanoh Haim    // AddTestInfo(), which is called to register a TEST or TEST_F
11198b52a31eSHanoh Haim    // before main() is reached.
11208b52a31eSHanoh Haim    if (original_working_dir_.IsEmpty()) {
11218b52a31eSHanoh Haim      original_working_dir_.Set(FilePath::GetCurrentDir());
11228b52a31eSHanoh Haim      GTEST_CHECK_(!original_working_dir_.IsEmpty())
11238b52a31eSHanoh Haim          << "Failed to get the current working directory.";
11248b52a31eSHanoh Haim    }
11258b52a31eSHanoh Haim
11268b52a31eSHanoh Haim    GetTestCase(test_info->test_case_name(),
11278b52a31eSHanoh Haim                test_info->test_case_comment(),
11288b52a31eSHanoh Haim                set_up_tc,
11298b52a31eSHanoh Haim                tear_down_tc)->AddTestInfo(test_info);
11308b52a31eSHanoh Haim  }
11318b52a31eSHanoh Haim
11328b52a31eSHanoh Haim#if GTEST_HAS_PARAM_TEST
11338b52a31eSHanoh Haim  // Returns ParameterizedTestCaseRegistry object used to keep track of
11348b52a31eSHanoh Haim  // value-parameterized tests and instantiate and register them.
11358b52a31eSHanoh Haim  internal::ParameterizedTestCaseRegistry& parameterized_test_registry() {
11368b52a31eSHanoh Haim    return parameterized_test_registry_;
11378b52a31eSHanoh Haim  }
11388b52a31eSHanoh Haim#endif  // GTEST_HAS_PARAM_TEST
11398b52a31eSHanoh Haim
11408b52a31eSHanoh Haim  // Sets the TestCase object for the test that's currently running.
11418b52a31eSHanoh Haim  void set_current_test_case(TestCase* a_current_test_case) {
11428b52a31eSHanoh Haim    current_test_case_ = a_current_test_case;
11438b52a31eSHanoh Haim  }
11448b52a31eSHanoh Haim
11458b52a31eSHanoh Haim  // Sets the TestInfo object for the test that's currently running.  If
11468b52a31eSHanoh Haim  // current_test_info is NULL, the assertion results will be stored in
11478b52a31eSHanoh Haim  // ad_hoc_test_result_.
11488b52a31eSHanoh Haim  void set_current_test_info(TestInfo* a_current_test_info) {
11498b52a31eSHanoh Haim    current_test_info_ = a_current_test_info;
11508b52a31eSHanoh Haim  }
11518b52a31eSHanoh Haim
11528b52a31eSHanoh Haim  // Registers all parameterized tests defined using TEST_P and
11538b52a31eSHanoh Haim  // INSTANTIATE_TEST_P, creating regular tests for each test/parameter
11548b52a31eSHanoh Haim  // combination. This method can be called more then once; it has
11558b52a31eSHanoh Haim  // guards protecting from registering the tests more then once.
11568b52a31eSHanoh Haim  // If value-parameterized tests are disabled, RegisterParameterizedTests
11578b52a31eSHanoh Haim  // is present but does nothing.
11588b52a31eSHanoh Haim  void RegisterParameterizedTests();
11598b52a31eSHanoh Haim
11608b52a31eSHanoh Haim  // Runs all tests in this UnitTest object, prints the result, and
11618b52a31eSHanoh Haim  // returns 0 if all tests are successful, or 1 otherwise.  If any
11628b52a31eSHanoh Haim  // exception is thrown during a test on Windows, this test is
11638b52a31eSHanoh Haim  // considered to be failed, but the rest of the tests will still be
11648b52a31eSHanoh Haim  // run.  (We disable exceptions on Linux and Mac OS X, so the issue
11658b52a31eSHanoh Haim  // doesn't apply there.)
11668b52a31eSHanoh Haim  int RunAllTests();
11678b52a31eSHanoh Haim
11688b52a31eSHanoh Haim  // Clears the results of all tests, including the ad hoc test.
11698b52a31eSHanoh Haim  void ClearResult() {
11708b52a31eSHanoh Haim    ForEach(test_cases_, TestCase::ClearTestCaseResult);
11718b52a31eSHanoh Haim    ad_hoc_test_result_.Clear();
11728b52a31eSHanoh Haim  }
11738b52a31eSHanoh Haim
11748b52a31eSHanoh Haim  enum ReactionToSharding {
11758b52a31eSHanoh Haim    HONOR_SHARDING_PROTOCOL,
11768b52a31eSHanoh Haim    IGNORE_SHARDING_PROTOCOL
11778b52a31eSHanoh Haim  };
11788b52a31eSHanoh Haim
11798b52a31eSHanoh Haim  // Matches the full name of each test against the user-specified
11808b52a31eSHanoh Haim  // filter to decide whether the test should run, then records the
11818b52a31eSHanoh Haim  // result in each TestCase and TestInfo object.
11828b52a31eSHanoh Haim  // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests
11838b52a31eSHanoh Haim  // based on sharding variables in the environment.
11848b52a31eSHanoh Haim  // Returns the number of tests that should run.
11858b52a31eSHanoh Haim  int FilterTests(ReactionToSharding shard_tests);
11868b52a31eSHanoh Haim
11878b52a31eSHanoh Haim  // Prints the names of the tests matching the user-specified filter flag.
11888b52a31eSHanoh Haim  void ListTestsMatchingFilter();
11898b52a31eSHanoh Haim
11908b52a31eSHanoh Haim  const TestCase* current_test_case() const { return current_test_case_; }
11918b52a31eSHanoh Haim  TestInfo* current_test_info() { return current_test_info_; }
11928b52a31eSHanoh Haim  const TestInfo* current_test_info() const { return current_test_info_; }
11938b52a31eSHanoh Haim
11948b52a31eSHanoh Haim  // Returns the vector of environments that need to be set-up/torn-down
11958b52a31eSHanoh Haim  // before/after the tests are run.
11968b52a31eSHanoh Haim  std::vector<Environment*>& environments() { return environments_; }
11978b52a31eSHanoh Haim
11988b52a31eSHanoh Haim  // Getters for the per-thread Google Test trace stack.
11998b52a31eSHanoh Haim  std::vector<TraceInfo>& gtest_trace_stack() {
12008b52a31eSHanoh Haim    return *(gtest_trace_stack_.pointer());
12018b52a31eSHanoh Haim  }
12028b52a31eSHanoh Haim  const std::vector<TraceInfo>& gtest_trace_stack() const {
12038b52a31eSHanoh Haim    return gtest_trace_stack_.get();
12048b52a31eSHanoh Haim  }
12058b52a31eSHanoh Haim
12068b52a31eSHanoh Haim#if GTEST_HAS_DEATH_TEST
12078b52a31eSHanoh Haim  void InitDeathTestSubprocessControlInfo() {
12088b52a31eSHanoh Haim    internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
12098b52a31eSHanoh Haim  }
12108b52a31eSHanoh Haim  // Returns a pointer to the parsed --gtest_internal_run_death_test
12118b52a31eSHanoh Haim  // flag, or NULL if that flag was not specified.
12128b52a31eSHanoh Haim  // This information is useful only in a death test child process.
12138b52a31eSHanoh Haim  // Must not be called before a call to InitGoogleTest.
12148b52a31eSHanoh Haim  const InternalRunDeathTestFlag* internal_run_death_test_flag() const {
12158b52a31eSHanoh Haim    return internal_run_death_test_flag_.get();
12168b52a31eSHanoh Haim  }
12178b52a31eSHanoh Haim
12188b52a31eSHanoh Haim  // Returns a pointer to the current death test factory.
12198b52a31eSHanoh Haim  internal::DeathTestFactory* death_test_factory() {
12208b52a31eSHanoh Haim    return death_test_factory_.get();
12218b52a31eSHanoh Haim  }
12228b52a31eSHanoh Haim
12238b52a31eSHanoh Haim  void SuppressTestEventsIfInSubprocess();
12248b52a31eSHanoh Haim
12258b52a31eSHanoh Haim  friend class ReplaceDeathTestFactory;
12268b52a31eSHanoh Haim#endif  // GTEST_HAS_DEATH_TEST
12278b52a31eSHanoh Haim
12288b52a31eSHanoh Haim  // Initializes the event listener performing XML output as specified by
12298b52a31eSHanoh Haim  // UnitTestOptions. Must not be called before InitGoogleTest.
12308b52a31eSHanoh Haim  void ConfigureXmlOutput();
12318b52a31eSHanoh Haim
12328b52a31eSHanoh Haim  // Performs initialization dependent upon flag values obtained in
12338b52a31eSHanoh Haim  // ParseGoogleTestFlagsOnly.  Is called from InitGoogleTest after the call to
12348b52a31eSHanoh Haim  // ParseGoogleTestFlagsOnly.  In case a user neglects to call InitGoogleTest
12358b52a31eSHanoh Haim  // this function is also called from RunAllTests.  Since this function can be
12368b52a31eSHanoh Haim  // called more than once, it has to be idempotent.
12378b52a31eSHanoh Haim  void PostFlagParsingInit();
12388b52a31eSHanoh Haim
12398b52a31eSHanoh Haim  // Gets the random seed used at the start of the current test iteration.
12408b52a31eSHanoh Haim  int random_seed() const { return random_seed_; }
12418b52a31eSHanoh Haim
12428b52a31eSHanoh Haim  // Gets the random number generator.
12438b52a31eSHanoh Haim  internal::Random* random() { return &random_; }
12448b52a31eSHanoh Haim
12458b52a31eSHanoh Haim  // Shuffles all test cases, and the tests within each test case,
12468b52a31eSHanoh Haim  // making sure that death tests are still run first.
12478b52a31eSHanoh Haim  void ShuffleTests();
12488b52a31eSHanoh Haim
12498b52a31eSHanoh Haim  // Restores the test cases and tests to their order before the first shuffle.
12508b52a31eSHanoh Haim  void UnshuffleTests();
12518b52a31eSHanoh Haim
12528b52a31eSHanoh Haim private:
12538b52a31eSHanoh Haim  friend class ::testing::UnitTest;
12548b52a31eSHanoh Haim
12558b52a31eSHanoh Haim  // The UnitTest object that owns this implementation object.
12568b52a31eSHanoh Haim  UnitTest* const parent_;
12578b52a31eSHanoh Haim
12588b52a31eSHanoh Haim  // The working directory when the first TEST() or TEST_F() was
12598b52a31eSHanoh Haim  // executed.
12608b52a31eSHanoh Haim  internal::FilePath original_working_dir_;
12618b52a31eSHanoh Haim
12628b52a31eSHanoh Haim  // The default test part result reporters.
12638b52a31eSHanoh Haim  DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
12648b52a31eSHanoh Haim  DefaultPerThreadTestPartResultReporter
12658b52a31eSHanoh Haim      default_per_thread_test_part_result_reporter_;
12668b52a31eSHanoh Haim
12678b52a31eSHanoh Haim  // Points to (but doesn't own) the global test part result reporter.
12688b52a31eSHanoh Haim  TestPartResultReporterInterface* global_test_part_result_repoter_;
12698b52a31eSHanoh Haim
12708b52a31eSHanoh Haim  // Protects read and write access to global_test_part_result_reporter_.
12718b52a31eSHanoh Haim  internal::Mutex global_test_part_result_reporter_mutex_;
12728b52a31eSHanoh Haim
12738b52a31eSHanoh Haim  // Points to (but doesn't own) the per-thread test part result reporter.
12748b52a31eSHanoh Haim  internal::ThreadLocal<TestPartResultReporterInterface*>
12758b52a31eSHanoh Haim      per_thread_test_part_result_reporter_;
12768b52a31eSHanoh Haim
12778b52a31eSHanoh Haim  // The vector of environments that need to be set-up/torn-down
12788b52a31eSHanoh Haim  // before/after the tests are run.
12798b52a31eSHanoh Haim  std::vector<Environment*> environments_;
12808b52a31eSHanoh Haim
12818b52a31eSHanoh Haim  // The vector of TestCases in their original order.  It owns the
12828b52a31eSHanoh Haim  // elements in the vector.
12838b52a31eSHanoh Haim  std::vector<TestCase*> test_cases_;
12848b52a31eSHanoh Haim
12858b52a31eSHanoh Haim  // Provides a level of indirection for the test case list to allow
12868b52a31eSHanoh Haim  // easy shuffling and restoring the test case order.  The i-th
12878b52a31eSHanoh Haim  // element of this vector is the index of the i-th test case in the
12888b52a31eSHanoh Haim  // shuffled order.
12898b52a31eSHanoh Haim  std::vector<int> test_case_indices_;
12908b52a31eSHanoh Haim
12918b52a31eSHanoh Haim#if GTEST_HAS_PARAM_TEST
12928b52a31eSHanoh Haim  // ParameterizedTestRegistry object used to register value-parameterized
12938b52a31eSHanoh Haim  // tests.
12948b52a31eSHanoh Haim  internal::ParameterizedTestCaseRegistry parameterized_test_registry_;
12958b52a31eSHanoh Haim
12968b52a31eSHanoh Haim  // Indicates whether RegisterParameterizedTests() has been called already.
12978b52a31eSHanoh Haim  bool parameterized_tests_registered_;
12988b52a31eSHanoh Haim#endif  // GTEST_HAS_PARAM_TEST
12998b52a31eSHanoh Haim
13008b52a31eSHanoh Haim  // Index of the last death test case registered.  Initially -1.
13018b52a31eSHanoh Haim  int last_death_test_case_;
13028b52a31eSHanoh Haim
13038b52a31eSHanoh Haim  // This points to the TestCase for the currently running test.  It
13048b52a31eSHanoh Haim  // changes as Google Test goes through one test case after another.
13058b52a31eSHanoh Haim  // When no test is running, this is set to NULL and Google Test
13068b52a31eSHanoh Haim  // stores assertion results in ad_hoc_test_result_.  Initially NULL.
13078b52a31eSHanoh Haim  TestCase* current_test_case_;
13088b52a31eSHanoh Haim
13098b52a31eSHanoh Haim  // This points to the TestInfo for the currently running test.  It
13108b52a31eSHanoh Haim  // changes as Google Test goes through one test after another.  When
13118b52a31eSHanoh Haim  // no test is running, this is set to NULL and Google Test stores
13128b52a31eSHanoh Haim  // assertion results in ad_hoc_test_result_.  Initially NULL.
13138b52a31eSHanoh Haim  TestInfo* current_test_info_;
13148b52a31eSHanoh Haim
13158b52a31eSHanoh Haim  // Normally, a user only writes assertions inside a TEST or TEST_F,
13168b52a31eSHanoh Haim  // or inside a function called by a TEST or TEST_F.  Since Google
13178b52a31eSHanoh Haim  // Test keeps track of which test is current running, it can
13188b52a31eSHanoh Haim  // associate such an assertion with the test it belongs to.
13198b52a31eSHanoh Haim  //
13208b52a31eSHanoh Haim  // If an assertion is encountered when no TEST or TEST_F is running,
13218b52a31eSHanoh Haim  // Google Test attributes the assertion result to an imaginary "ad hoc"
13228b52a31eSHanoh Haim  // test, and records the result in ad_hoc_test_result_.
13238b52a31eSHanoh Haim  TestResult ad_hoc_test_result_;
13248b52a31eSHanoh Haim
13258b52a31eSHanoh Haim  // The list of event listeners that can be used to track events inside
13268b52a31eSHanoh Haim  // Google Test.
13278b52a31eSHanoh Haim  TestEventListeners listeners_;
13288b52a31eSHanoh Haim
13298b52a31eSHanoh Haim  // The OS stack trace getter.  Will be deleted when the UnitTest
13308b52a31eSHanoh Haim  // object is destructed.  By default, an OsStackTraceGetter is used,
13318b52a31eSHanoh Haim  // but the user can set this field to use a custom getter if that is
13328b52a31eSHanoh Haim  // desired.
13338b52a31eSHanoh Haim  OsStackTraceGetterInterface* os_stack_trace_getter_;
13348b52a31eSHanoh Haim
13358b52a31eSHanoh Haim  // True iff PostFlagParsingInit() has been called.
13368b52a31eSHanoh Haim  bool post_flag_parse_init_performed_;
13378b52a31eSHanoh Haim
13388b52a31eSHanoh Haim  // The random number seed used at the beginning of the test run.
13398b52a31eSHanoh Haim  int random_seed_;
13408b52a31eSHanoh Haim
13418b52a31eSHanoh Haim  // Our random number generator.
13428b52a31eSHanoh Haim  internal::Random random_;
13438b52a31eSHanoh Haim
13448b52a31eSHanoh Haim  // How long the test took to run, in milliseconds.
13458b52a31eSHanoh Haim  TimeInMillis elapsed_time_;
13468b52a31eSHanoh Haim
13478b52a31eSHanoh Haim#if GTEST_HAS_DEATH_TEST
13488b52a31eSHanoh Haim  // The decomposed components of the gtest_internal_run_death_test flag,
13498b52a31eSHanoh Haim  // parsed when RUN_ALL_TESTS is called.
13508b52a31eSHanoh Haim  internal::scoped_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
13518b52a31eSHanoh Haim  internal::scoped_ptr<internal::DeathTestFactory> death_test_factory_;
13528b52a31eSHanoh Haim#endif  // GTEST_HAS_DEATH_TEST
13538b52a31eSHanoh Haim
13548b52a31eSHanoh Haim  // A per-thread stack of traces created by the SCOPED_TRACE() macro.
13558b52a31eSHanoh Haim  internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
13568b52a31eSHanoh Haim
13578b52a31eSHanoh Haim  GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);
13588b52a31eSHanoh Haim};  // class UnitTestImpl
13598b52a31eSHanoh Haim
13608b52a31eSHanoh Haim// Convenience function for accessing the global UnitTest
13618b52a31eSHanoh Haim// implementation object.
13628b52a31eSHanoh Haiminline UnitTestImpl* GetUnitTestImpl() {
13638b52a31eSHanoh Haim  return UnitTest::GetInstance()->impl();
13648b52a31eSHanoh Haim}
13658b52a31eSHanoh Haim
13668b52a31eSHanoh Haim// Internal helper functions for implementing the simple regular
13678b52a31eSHanoh Haim// expression matcher.
13688b52a31eSHanoh HaimGTEST_API_ bool IsInSet(char ch, const char* str);
13698b52a31eSHanoh HaimGTEST_API_ bool IsDigit(char ch);
13708b52a31eSHanoh HaimGTEST_API_ bool IsPunct(char ch);
13718b52a31eSHanoh HaimGTEST_API_ bool IsRepeat(char ch);
13728b52a31eSHanoh HaimGTEST_API_ bool IsWhiteSpace(char ch);
13738b52a31eSHanoh HaimGTEST_API_ bool IsWordChar(char ch);
13748b52a31eSHanoh HaimGTEST_API_ bool IsValidEscape(char ch);
13758b52a31eSHanoh HaimGTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch);
13768b52a31eSHanoh HaimGTEST_API_ bool ValidateRegex(const char* regex);
13778b52a31eSHanoh HaimGTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str);
13788b52a31eSHanoh HaimGTEST_API_ bool MatchRepetitionAndRegexAtHead(
13798b52a31eSHanoh Haim    bool escaped, char ch, char repeat, const char* regex, const char* str);
13808b52a31eSHanoh HaimGTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str);
13818b52a31eSHanoh Haim
13828b52a31eSHanoh Haim// Parses the command line for Google Test flags, without initializing
13838b52a31eSHanoh Haim// other parts of Google Test.
13848b52a31eSHanoh HaimGTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv);
13858b52a31eSHanoh HaimGTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv);
13868b52a31eSHanoh Haim
13878b52a31eSHanoh Haim#if GTEST_HAS_DEATH_TEST
13888b52a31eSHanoh Haim
13898b52a31eSHanoh Haim// Returns the message describing the last system error, regardless of the
13908b52a31eSHanoh Haim// platform.
13918b52a31eSHanoh HaimString GetLastErrnoDescription();
13928b52a31eSHanoh Haim
13938b52a31eSHanoh Haim#if GTEST_OS_WINDOWS
13948b52a31eSHanoh Haim// Provides leak-safe Windows kernel handle ownership.
13958b52a31eSHanoh Haimclass AutoHandle {
13968b52a31eSHanoh Haim public:
13978b52a31eSHanoh Haim  AutoHandle() : handle_(INVALID_HANDLE_VALUE) {}
13988b52a31eSHanoh Haim  explicit AutoHandle(HANDLE handle) : handle_(handle) {}
13998b52a31eSHanoh Haim
14008b52a31eSHanoh Haim  ~AutoHandle() { Reset(); }
14018b52a31eSHanoh Haim
14028b52a31eSHanoh Haim  HANDLE Get() const { return handle_; }
14038b52a31eSHanoh Haim  void Reset() { Reset(INVALID_HANDLE_VALUE); }
14048b52a31eSHanoh Haim  void Reset(HANDLE handle) {
14058b52a31eSHanoh Haim    if (handle != handle_) {
14068b52a31eSHanoh Haim      if (handle_ != INVALID_HANDLE_VALUE)
14078b52a31eSHanoh Haim        ::CloseHandle(handle_);
14088b52a31eSHanoh Haim      handle_ = handle;
14098b52a31eSHanoh Haim    }
14108b52a31eSHanoh Haim  }
14118b52a31eSHanoh Haim
14128b52a31eSHanoh Haim private:
14138b52a31eSHanoh Haim  HANDLE handle_;
14148b52a31eSHanoh Haim
14158b52a31eSHanoh Haim  GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
14168b52a31eSHanoh Haim};
14178b52a31eSHanoh Haim#endif  // GTEST_OS_WINDOWS
14188b52a31eSHanoh Haim
14198b52a31eSHanoh Haim// Attempts to parse a string into a positive integer pointed to by the
14208b52a31eSHanoh Haim// number parameter.  Returns true if that is possible.
14218b52a31eSHanoh Haim// GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use
14228b52a31eSHanoh Haim// it here.
14238b52a31eSHanoh Haimtemplate <typename Integer>
14248b52a31eSHanoh Haimbool ParseNaturalNumber(const ::std::string& str, Integer* number) {
14258b52a31eSHanoh Haim  // Fail fast if the given string does not begin with a digit;
14268b52a31eSHanoh Haim  // this bypasses strtoXXX's "optional leading whitespace and plus
14278b52a31eSHanoh Haim  // or minus sign" semantics, which are undesirable here.
14288b52a31eSHanoh Haim  if (str.empty() || !isdigit(str[0])) {
14298b52a31eSHanoh Haim    return false;
14308b52a31eSHanoh Haim  }
14318b52a31eSHanoh Haim  errno = 0;
14328b52a31eSHanoh Haim
14338b52a31eSHanoh Haim  char* end;
14348b52a31eSHanoh Haim  // BiggestConvertible is the largest integer type that system-provided
14358b52a31eSHanoh Haim  // string-to-number conversion routines can return.
14368b52a31eSHanoh Haim#if GTEST_OS_WINDOWS && !defined(__GNUC__)
14378b52a31eSHanoh Haim  // MSVC and C++ Builder define __int64 instead of the standard long long.
14388b52a31eSHanoh Haim  typedef unsigned __int64 BiggestConvertible;
14398b52a31eSHanoh Haim  const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10);
14408b52a31eSHanoh Haim#else
14418b52a31eSHanoh Haim  typedef unsigned long long BiggestConvertible;  // NOLINT
14428b52a31eSHanoh Haim  const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
14438b52a31eSHanoh Haim#endif  // GTEST_OS_WINDOWS && !defined(__GNUC__)
14448b52a31eSHanoh Haim  const bool parse_success = *end == '\0' && errno == 0;
14458b52a31eSHanoh Haim
14468b52a31eSHanoh Haim  // TODO(vladl@google.com): Convert this to compile time assertion when it is
14478b52a31eSHanoh Haim  // available.
14488b52a31eSHanoh Haim  GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed));
14498b52a31eSHanoh Haim
14508b52a31eSHanoh Haim  const Integer result = static_cast<Integer>(parsed);
14518b52a31eSHanoh Haim  if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
14528b52a31eSHanoh Haim    *number = result;
14538b52a31eSHanoh Haim    return true;
14548b52a31eSHanoh Haim  }
14558b52a31eSHanoh Haim  return false;
14568b52a31eSHanoh Haim}
14578b52a31eSHanoh Haim#endif  // GTEST_HAS_DEATH_TEST
14588b52a31eSHanoh Haim
14598b52a31eSHanoh Haim// TestResult contains some private methods that should be hidden from
14608b52a31eSHanoh Haim// Google Test user but are required for testing. This class allow our tests
14618b52a31eSHanoh Haim// to access them.
14628b52a31eSHanoh Haim//
14638b52a31eSHanoh Haim// This class is supplied only for the purpose of testing Google Test's own
14648b52a31eSHanoh Haim// constructs. Do not use it in user tests, either directly or indirectly.
14658b52a31eSHanoh Haimclass TestResultAccessor {
14668b52a31eSHanoh Haim public:
14678b52a31eSHanoh Haim  static void RecordProperty(TestResult* test_result,
14688b52a31eSHanoh Haim                             const TestProperty& property) {
14698b52a31eSHanoh Haim    test_result->RecordProperty(property);
14708b52a31eSHanoh Haim  }
14718b52a31eSHanoh Haim
14728b52a31eSHanoh Haim  static void ClearTestPartResults(TestResult* test_result) {
14738b52a31eSHanoh Haim    test_result->ClearTestPartResults();
14748b52a31eSHanoh Haim  }
14758b52a31eSHanoh Haim
14768b52a31eSHanoh Haim  static const std::vector<testing::TestPartResult>& test_part_results(
14778b52a31eSHanoh Haim      const TestResult& test_result) {
14788b52a31eSHanoh Haim    return test_result.test_part_results();
14798b52a31eSHanoh Haim  }
14808b52a31eSHanoh Haim};
14818b52a31eSHanoh Haim
14828b52a31eSHanoh Haim}  // namespace internal
14838b52a31eSHanoh Haim}  // namespace testing
14848b52a31eSHanoh Haim
14858b52a31eSHanoh Haim#endif  // GTEST_SRC_GTEST_INTERNAL_INL_H_
14868b52a31eSHanoh Haim#undef GTEST_IMPLEMENTATION_
14878b52a31eSHanoh Haim
14888b52a31eSHanoh Haim#if GTEST_OS_WINDOWS
14898b52a31eSHanoh Haim#define vsnprintf _vsnprintf
14908b52a31eSHanoh Haim#endif  // GTEST_OS_WINDOWS
14918b52a31eSHanoh Haim
14928b52a31eSHanoh Haimnamespace testing {
14938b52a31eSHanoh Haim
14948b52a31eSHanoh Haimusing internal::CountIf;
14958b52a31eSHanoh Haimusing internal::ForEach;
14968b52a31eSHanoh Haimusing internal::GetElementOr;
14978b52a31eSHanoh Haimusing internal::Shuffle;
14988b52a31eSHanoh Haim
14998b52a31eSHanoh Haim// Constants.
15008b52a31eSHanoh Haim
15018b52a31eSHanoh Haim// A test whose test case name or test name matches this filter is
15028b52a31eSHanoh Haim// disabled and not run.
15038b52a31eSHanoh Haimstatic const char kDisableTestFilter[] = "DISABLED_*:*/DISABLED_*";
15048b52a31eSHanoh Haim
15058b52a31eSHanoh Haim// A test case whose name matches this filter is considered a death