gtest-internal-inl.h revision 41d0579e8de9ef4ff178fc4991043c61a19943f7
15fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Copyright 2005, Google Inc.
25fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// All rights reserved.
35fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//
45fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Redistribution and use in source and binary forms, with or without
55fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// modification, are permitted provided that the following conditions are
65fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// met:
75fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//
85fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//     * Redistributions of source code must retain the above copyright
95fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// notice, this list of conditions and the following disclaimer.
105fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//     * Redistributions in binary form must reproduce the above
115fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// copyright notice, this list of conditions and the following disclaimer
125fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// in the documentation and/or other materials provided with the
135fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// distribution.
145fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//     * Neither the name of Google Inc. nor the names of its
155fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// contributors may be used to endorse or promote products derived from
165fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// this software without specific prior written permission.
175fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//
185fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
305fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Utility functions and classes used by the Google C++ testing framework.
315fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//
325fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Author: wan@google.com (Zhanyong Wan)
335fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//
345fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// This file contains purely Google Test's internal implementation.  Please
355fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// DO NOT #INCLUDE IT IN A USER PROGRAM.
365fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
375fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
385fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#define GTEST_SRC_GTEST_INTERNAL_INL_H_
395fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
405fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// GTEST_IMPLEMENTATION_ is defined to 1 iff the current translation unit is
415fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// part of Google Test's implementation; otherwise it's undefined.
425fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#if !GTEST_IMPLEMENTATION_
435fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// A user is trying to include this from his code - just say no.
445fd6d3161cc7407718c7e230f94db421543273b8Kenny Root# error "gtest-internal-inl.h is part of Google Test's internal implementation."
455fd6d3161cc7407718c7e230f94db421543273b8Kenny Root# error "It must not be included except by Google Test itself."
465fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#endif  // GTEST_IMPLEMENTATION_
475fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
485fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#ifndef _WIN32_WCE
495fd6d3161cc7407718c7e230f94db421543273b8Kenny Root# include <errno.h>
505fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#endif  // !_WIN32_WCE
515fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#include <stddef.h>
525fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#include <stdlib.h>  // For strtoll/_strtoul64/malloc/free.
535fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#include <string.h>  // For memmove.
545fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
555fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#include <algorithm>
565fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#include <string>
575fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#include <vector>
585fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
595fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#include "gtest/internal/gtest-port.h"
605fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
615fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#if GTEST_OS_WINDOWS
625fd6d3161cc7407718c7e230f94db421543273b8Kenny Root# include <windows.h>  // NOLINT
635fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#endif  // GTEST_OS_WINDOWS
645fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
655fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#include "gtest/gtest.h"  // NOLINT
665fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#include "gtest/gtest-spi.h"
675fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
685fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootnamespace testing {
695fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
705fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Declares the flags.
715fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//
725fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// We don't want the users to modify this flag in the code, but want
735fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Google Test's own unit tests to be able to access it. Therefore we
745fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// declare it here as opposed to in gtest.h.
755fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_DECLARE_bool_(death_test_use_fork);
765fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
775fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootnamespace internal {
785fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
795fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// The value of GetTestTypeId() as seen from within the Google Test
805fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// library.  This is solely for testing GetTestTypeId().
815fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ extern const TypeId kTestTypeIdInGoogleTest;
825fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
835fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Names of the flags (needed for parsing Google Test flags).
845fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests";
855fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kBreakOnFailureFlag[] = "break_on_failure";
865fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kCatchExceptionsFlag[] = "catch_exceptions";
875fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kColorFlag[] = "color";
885fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kFilterFlag[] = "filter";
895fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kListTestsFlag[] = "list_tests";
905fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kOutputFlag[] = "output";
915fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kPrintTimeFlag[] = "print_time";
925fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kRandomSeedFlag[] = "random_seed";
935fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kRepeatFlag[] = "repeat";
945fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kShuffleFlag[] = "shuffle";
955fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kStackTraceDepthFlag[] = "stack_trace_depth";
965fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kStreamResultToFlag[] = "stream_result_to";
975fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst char kThrowOnFailureFlag[] = "throw_on_failure";
985fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
995fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// A valid random seed must be in [1, kMaxRandomSeed].
1005fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootconst int kMaxRandomSeed = 99999;
1015fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
1025fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// g_help_flag is true iff the --help flag or an equivalent form is
1035fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// specified on the command line.
1045fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ extern bool g_help_flag;
1055fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
1065fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Returns the current time in milliseconds.
1075fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ TimeInMillis GetTimeInMillis();
1085fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
1095fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Returns true iff Google Test should use colors in the output.
1105fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ bool ShouldUseColor(bool stdout_is_tty);
1115fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
1125fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Formats the given time in milliseconds as seconds.
1135fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);
1145fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
1155fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Parses a string for an Int32 flag, in the form of "--flag=value".
1165fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//
1175fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// On success, stores the value of the flag in *value, and returns
1185fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// true.  On failure, returns false without changing *value.
1195fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ bool ParseInt32Flag(
1205fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    const char* str, const char* flag, Int32* value);
1215fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
1225fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Returns a random seed in range [1, kMaxRandomSeed] based on the
1235fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// given --gtest_random_seed flag value.
1245fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootinline int GetRandomSeedFromFlag(Int32 random_seed_flag) {
1255fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  const unsigned int raw_seed = (random_seed_flag == 0) ?
1265fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      static_cast<unsigned int>(GetTimeInMillis()) :
1275fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      static_cast<unsigned int>(random_seed_flag);
1285fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
1295fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Normalizes the actual seed to range [1, kMaxRandomSeed] such that
1305fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // it's easy to type.
1315fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  const int normalized_seed =
1325fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      static_cast<int>((raw_seed - 1U) %
1335fd6d3161cc7407718c7e230f94db421543273b8Kenny Root                       static_cast<unsigned int>(kMaxRandomSeed)) + 1;
1345fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  return normalized_seed;
1355fd6d3161cc7407718c7e230f94db421543273b8Kenny Root}
1365fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
1375fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Returns the first valid random seed after 'seed'.  The behavior is
1385fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// undefined if 'seed' is invalid.  The seed after kMaxRandomSeed is
1395fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// considered to be 1.
1405fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootinline int GetNextRandomSeed(int seed) {
1415fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)
1425fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      << "Invalid random seed " << seed << " - must be in [1, "
1435fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      << kMaxRandomSeed << "].";
1445fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  const int next_seed = seed + 1;
1455fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
1465fd6d3161cc7407718c7e230f94db421543273b8Kenny Root}
1475fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
1485fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// This class saves the values of all Google Test flags in its c'tor, and
1495fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// restores them in its d'tor.
1505fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootclass GTestFlagSaver {
1515fd6d3161cc7407718c7e230f94db421543273b8Kenny Root public:
1525fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // The c'tor.
1535fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  GTestFlagSaver() {
1545fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests);
1555fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    break_on_failure_ = GTEST_FLAG(break_on_failure);
1565fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    catch_exceptions_ = GTEST_FLAG(catch_exceptions);
1575fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    color_ = GTEST_FLAG(color);
1585fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    death_test_style_ = GTEST_FLAG(death_test_style);
1595fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    death_test_use_fork_ = GTEST_FLAG(death_test_use_fork);
1605fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    filter_ = GTEST_FLAG(filter);
1615fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
1625fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    list_tests_ = GTEST_FLAG(list_tests);
1635fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    output_ = GTEST_FLAG(output);
1645fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    print_time_ = GTEST_FLAG(print_time);
1655fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    random_seed_ = GTEST_FLAG(random_seed);
1665fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    repeat_ = GTEST_FLAG(repeat);
1675fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    shuffle_ = GTEST_FLAG(shuffle);
1685fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    stack_trace_depth_ = GTEST_FLAG(stack_trace_depth);
1695fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    stream_result_to_ = GTEST_FLAG(stream_result_to);
1705fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    throw_on_failure_ = GTEST_FLAG(throw_on_failure);
1715fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  }
1725fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
1735fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // The d'tor is not virtual.  DO NOT INHERIT FROM THIS CLASS.
1745fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  ~GTestFlagSaver() {
1755fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
1765fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(break_on_failure) = break_on_failure_;
1775fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(catch_exceptions) = catch_exceptions_;
1785fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(color) = color_;
1795fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(death_test_style) = death_test_style_;
1805fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
1815fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(filter) = filter_;
1825fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
1835fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(list_tests) = list_tests_;
1845fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(output) = output_;
1855fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(print_time) = print_time_;
1865fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(random_seed) = random_seed_;
1875fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(repeat) = repeat_;
1885fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(shuffle) = shuffle_;
1895fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
1905fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(stream_result_to) = stream_result_to_;
1915fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    GTEST_FLAG(throw_on_failure) = throw_on_failure_;
1925fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  }
1935fd6d3161cc7407718c7e230f94db421543273b8Kenny Root private:
1945fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Fields for saving the original values of flags.
1955fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  bool also_run_disabled_tests_;
1965fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  bool break_on_failure_;
1975fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  bool catch_exceptions_;
1985fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  String color_;
1995fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  String death_test_style_;
2005fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  bool death_test_use_fork_;
2015fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  String filter_;
2025fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  String internal_run_death_test_;
2035fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  bool list_tests_;
2045fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  String output_;
2055fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  bool print_time_;
2065fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  bool pretty_;
2075fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  internal::Int32 random_seed_;
2085fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  internal::Int32 repeat_;
2095fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  bool shuffle_;
2105fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  internal::Int32 stack_trace_depth_;
2115fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  String stream_result_to_;
2125fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  bool throw_on_failure_;
2135fd6d3161cc7407718c7e230f94db421543273b8Kenny Root} GTEST_ATTRIBUTE_UNUSED_;
2145fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2155fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Converts a Unicode code point to a narrow string in UTF-8 encoding.
2165fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// code_point parameter is of type UInt32 because wchar_t may not be
2175fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// wide enough to contain a code point.
2185fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// The output buffer str must containt at least 32 characters.
2195fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// The function returns the address of the output buffer.
2205fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// If the code_point is not a valid Unicode code point
2215fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be output
2225fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// as '(Invalid Unicode 0xXXXXXXXX)'.
2235fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ char* CodePointToUtf8(UInt32 code_point, char* str);
2245fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2255fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Converts a wide string to a narrow string in UTF-8 encoding.
2265fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// The wide string is assumed to have the following encoding:
2275fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//   UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS)
2285fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//   UTF-32 if sizeof(wchar_t) == 4 (on Linux)
2295fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Parameter str points to a null-terminated wide string.
2305fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Parameter num_chars may additionally limit the number
2315fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// of wchar_t characters processed. -1 is used when the entire string
2325fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// should be processed.
2335fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// If the string contains code points that are not valid Unicode code points
2345fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output
2355fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding
2365fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// and contains invalid UTF-16 surrogate pairs, values in those pairs
2375fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// will be encoded as individual Unicode characters from Basic Normal Plane.
2385fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ String WideStringToUtf8(const wchar_t* str, int num_chars);
2395fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2405fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file
2415fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// if the variable is present. If a file already exists at this location, this
2425fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// function will write over it. If the variable is present, but the file cannot
2435fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// be created, prints an error and exits.
2445fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootvoid WriteToShardStatusFileIfNeeded();
2455fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2465fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Checks whether sharding is enabled by examining the relevant
2475fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// environment variable values. If the variables are present,
2485fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// but inconsistent (e.g., shard_index >= total_shards), prints
2495fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// an error and exits. If in_subprocess_for_death_test, sharding is
2505fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// disabled because it must only be applied to the original test
2515fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// process. Otherwise, we could filter out death tests we intended to execute.
2525fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ bool ShouldShard(const char* total_shards_str,
2535fd6d3161cc7407718c7e230f94db421543273b8Kenny Root                            const char* shard_index_str,
2545fd6d3161cc7407718c7e230f94db421543273b8Kenny Root                            bool in_subprocess_for_death_test);
2555fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2565fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Parses the environment variable var as an Int32. If it is unset,
2575fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// returns default_val. If it is not an Int32, prints an error and
2585fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// and aborts.
2595fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ Int32 Int32FromEnvOrDie(const char* env_var, Int32 default_val);
2605fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2615fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Given the total number of shards, the shard index, and the test id,
2625fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// returns true iff the test should be run on this shard. The test id is
2635fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// some arbitrary but unique non-negative integer assigned to each test
2645fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// method. Assumes that 0 <= shard_index < total_shards.
2655a43d1928da29b4ec7b911fe865ed78ee3c2ca26Kenny RootGTEST_API_ bool ShouldRunTestOnShard(
2665fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    int total_shards, int shard_index, int test_id);
2675fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2685fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// STL container utilities.
2695fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2705fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Returns the number of elements in the given container that satisfy
2715fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// the given predicate.
2725fd6d3161cc7407718c7e230f94db421543273b8Kenny Roottemplate <class Container, typename Predicate>
2735fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootinline int CountIf(const Container& c, Predicate predicate) {
2745fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Implemented as an explicit loop since std::count_if() in libCstd on
2755fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Solaris has a non-standard signature.
2765fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  int count = 0;
2775fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
2785fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    if (predicate(*it))
2795fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      ++count;
2805fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  }
2815fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  return count;
2825fd6d3161cc7407718c7e230f94db421543273b8Kenny Root}
2835fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2845fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Applies a function/functor to each element in the container.
2855fd6d3161cc7407718c7e230f94db421543273b8Kenny Roottemplate <class Container, typename Functor>
2865fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootvoid ForEach(const Container& c, Functor functor) {
2875fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  std::for_each(c.begin(), c.end(), functor);
2885fd6d3161cc7407718c7e230f94db421543273b8Kenny Root}
2895fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2905fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Returns the i-th element of the vector, or default_value if i is not
2915fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// in range [0, v.size()).
2925fd6d3161cc7407718c7e230f94db421543273b8Kenny Roottemplate <typename E>
2935fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootinline E GetElementOr(const std::vector<E>& v, int i, E default_value) {
2945fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  return (i < 0 || i >= static_cast<int>(v.size())) ? default_value : v[i];
2955fd6d3161cc7407718c7e230f94db421543273b8Kenny Root}
2965fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
2975fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Performs an in-place shuffle of a range of the vector's elements.
2985fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// 'begin' and 'end' are element indices as an STL-style range;
2995fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// i.e. [begin, end) are shuffled, where 'end' == size() means to
3005fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// shuffle to the end of the vector.
3015fd6d3161cc7407718c7e230f94db421543273b8Kenny Roottemplate <typename E>
3025fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootvoid ShuffleRange(internal::Random* random, int begin, int end,
3035fd6d3161cc7407718c7e230f94db421543273b8Kenny Root                  std::vector<E>* v) {
3045fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  const int size = static_cast<int>(v->size());
3055fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  GTEST_CHECK_(0 <= begin && begin <= size)
3065fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      << "Invalid shuffle range start " << begin << ": must be in range [0, "
3075fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      << size << "].";
3085fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  GTEST_CHECK_(begin <= end && end <= size)
3095fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      << "Invalid shuffle range finish " << end << ": must be in range ["
3105fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      << begin << ", " << size << "].";
3115fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3125fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Fisher-Yates shuffle, from
3135fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
3145fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  for (int range_width = end - begin; range_width >= 2; range_width--) {
3155fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    const int last_in_range = begin + range_width - 1;
3165fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    const int selected = begin + random->Generate(range_width);
3175fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    std::swap((*v)[selected], (*v)[last_in_range]);
3185fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  }
3195fd6d3161cc7407718c7e230f94db421543273b8Kenny Root}
3205fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3215fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Performs an in-place shuffle of the vector's elements.
3225fd6d3161cc7407718c7e230f94db421543273b8Kenny Roottemplate <typename E>
3235fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootinline void Shuffle(internal::Random* random, std::vector<E>* v) {
3245fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  ShuffleRange(random, 0, static_cast<int>(v->size()), v);
3255fd6d3161cc7407718c7e230f94db421543273b8Kenny Root}
3265fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3275a43d1928da29b4ec7b911fe865ed78ee3c2ca26Kenny Root// A function for deleting an object.  Handy for being used as a
3285a43d1928da29b4ec7b911fe865ed78ee3c2ca26Kenny Root// functor.
3295a43d1928da29b4ec7b911fe865ed78ee3c2ca26Kenny Roottemplate <typename T>
3305a43d1928da29b4ec7b911fe865ed78ee3c2ca26Kenny Rootstatic void Delete(T* x) {
3315a43d1928da29b4ec7b911fe865ed78ee3c2ca26Kenny Root  delete x;
3325a43d1928da29b4ec7b911fe865ed78ee3c2ca26Kenny Root}
3335a43d1928da29b4ec7b911fe865ed78ee3c2ca26Kenny Root
3345a43d1928da29b4ec7b911fe865ed78ee3c2ca26Kenny Root// A predicate that checks the key of a TestProperty against a known key.
3355a43d1928da29b4ec7b911fe865ed78ee3c2ca26Kenny Root//
3365fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// TestPropertyKeyIs is copyable.
3375fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootclass TestPropertyKeyIs {
3385fd6d3161cc7407718c7e230f94db421543273b8Kenny Root public:
3395fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Constructor.
3405fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  //
3415fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // TestPropertyKeyIs has NO default constructor.
3425fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  explicit TestPropertyKeyIs(const char* key)
3435fd6d3161cc7407718c7e230f94db421543273b8Kenny Root      : key_(key) {}
3445fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3455fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Returns true iff the test name of test property matches on key_.
3465fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  bool operator()(const TestProperty& test_property) const {
3475fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    return String(test_property.key()).Compare(key_) == 0;
3485fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  }
3495fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3505fd6d3161cc7407718c7e230f94db421543273b8Kenny Root private:
3515fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  String key_;
3525fd6d3161cc7407718c7e230f94db421543273b8Kenny Root};
3535fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3545fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Class UnitTestOptions.
3555fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//
3565fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// This class contains functions for processing options the user
3575fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// specifies when running the tests.  It has only static members.
3585fd6d3161cc7407718c7e230f94db421543273b8Kenny Root//
3595fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// In most cases, the user can specify an option using either an
3605fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// environment variable or a command line flag.  E.g. you can set the
3615fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// test filter using either GTEST_FILTER or --gtest_filter.  If both
3625fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// the variable and the flag are present, the latter overrides the
3635fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// former.
3645fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootclass GTEST_API_ UnitTestOptions {
3655fd6d3161cc7407718c7e230f94db421543273b8Kenny Root public:
3665fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Functions for processing the gtest_output flag.
3675fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3685fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Returns the output format, or "" for normal printed output.
3695fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  static String GetOutputFormat();
3705fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3715fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Returns the absolute path of the requested output file, or the
3725fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // default (test_detail.xml in the original working directory) if
3735fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // none was explicitly specified.
3745fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  static String GetAbsolutePathToOutputFile();
3755fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3765fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Functions for processing the gtest_filter flag.
3775fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3785fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Returns true iff the wildcard pattern matches the string.  The
3795fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // first ':' or '\0' character in pattern marks the end of it.
3805fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  //
3815fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // This recursive algorithm isn't very efficient, but is clear and
3825fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // works well enough for matching test names, which are short.
3835fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  static bool PatternMatchesString(const char *pattern, const char *str);
3845fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3855fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Returns true iff the user-specified filter matches the test case
3865fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // name and the test name.
3875fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  static bool FilterMatchesTest(const String &test_case_name,
3885fd6d3161cc7407718c7e230f94db421543273b8Kenny Root                                const String &test_name);
3895fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3905fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#if GTEST_OS_WINDOWS
3915fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Function for supporting the gtest_catch_exception flag.
3925fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3935fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
3945fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
3955fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // This function is useful as an __except condition.
3965fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  static int GTestShouldProcessSEH(DWORD exception_code);
3975fd6d3161cc7407718c7e230f94db421543273b8Kenny Root#endif  // GTEST_OS_WINDOWS
3985fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
3995fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Returns true if "name" matches the ':' separated list of glob-style
4005fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // filters in "filter".
4015fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  static bool MatchesFilter(const String& name, const char* filter);
4025fd6d3161cc7407718c7e230f94db421543273b8Kenny Root};
4035fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4045fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Returns the current application's name, removing directory path if that
4055fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// is present.  Used by UnitTestOptions::GetOutputFile.
4065fd6d3161cc7407718c7e230f94db421543273b8Kenny RootGTEST_API_ FilePath GetCurrentExecutableName();
4075fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4085fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// The role interface for getting the OS stack trace as a string.
4095fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootclass OsStackTraceGetterInterface {
4105fd6d3161cc7407718c7e230f94db421543273b8Kenny Root public:
4115fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  OsStackTraceGetterInterface() {}
4125fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  virtual ~OsStackTraceGetterInterface() {}
4135fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4145fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Returns the current OS stack trace as a String.  Parameters:
4155fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  //
4165fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  //   max_depth  - the maximum number of stack frames to be included
4175fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  //                in the trace.
4185fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  //   skip_count - the number of top frames to be skipped; doesn't count
4195fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  //                against max_depth.
4205fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  virtual String CurrentStackTrace(int max_depth, int skip_count) = 0;
4215fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4225fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // UponLeavingGTest() should be called immediately before Google Test calls
4235fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // user code. It saves some information about the current stack that
4245fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // CurrentStackTrace() will use to find and hide Google Test stack frames.
4255fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  virtual void UponLeavingGTest() = 0;
4265fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4275fd6d3161cc7407718c7e230f94db421543273b8Kenny Root private:
4285fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface);
4295fd6d3161cc7407718c7e230f94db421543273b8Kenny Root};
4305fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4315fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// A working implementation of the OsStackTraceGetterInterface interface.
4325fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootclass OsStackTraceGetter : public OsStackTraceGetterInterface {
4335fd6d3161cc7407718c7e230f94db421543273b8Kenny Root public:
4345fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  OsStackTraceGetter() : caller_frame_(NULL) {}
4355fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  virtual String CurrentStackTrace(int max_depth, int skip_count);
4365fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  virtual void UponLeavingGTest();
4375fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4385fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // This string is inserted in place of stack frames that are part of
4395fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Google Test's implementation.
4405fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  static const char* const kElidedFramesMarker;
4415fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4425fd6d3161cc7407718c7e230f94db421543273b8Kenny Root private:
4435fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  Mutex mutex_;  // protects all internal state
4445fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4455fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // We save the stack frame below the frame that calls user code.
4465fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // We do this because the address of the frame immediately below
4475fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // the user code changes between the call to UponLeavingGTest()
4485fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // and any calls to CurrentStackTrace() from within the user code.
4495fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  void* caller_frame_;
4505fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4515fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter);
4525fd6d3161cc7407718c7e230f94db421543273b8Kenny Root};
4535fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4545fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// Information about a Google Test trace point.
4555fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootstruct TraceInfo {
4565fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  const char* file;
4575fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  int line;
4585fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  String message;
4595fd6d3161cc7407718c7e230f94db421543273b8Kenny Root};
4605fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4615fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// This is the default global test part result reporter used in UnitTestImpl.
4625fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// This class should only be used by UnitTestImpl.
4635fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootclass DefaultGlobalTestPartResultReporter
4645fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  : public TestPartResultReporterInterface {
4655fd6d3161cc7407718c7e230f94db421543273b8Kenny Root public:
4665fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
4675fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Implements the TestPartResultReporterInterface. Reports the test part
4685fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // result in the current test.
4695fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  virtual void ReportTestPartResult(const TestPartResult& result);
4705fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4715fd6d3161cc7407718c7e230f94db421543273b8Kenny Root private:
4725fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  UnitTestImpl* const unit_test_;
4735fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4745fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);
4755fd6d3161cc7407718c7e230f94db421543273b8Kenny Root};
4765fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4775fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// This is the default per thread test part result reporter used in
4785fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// UnitTestImpl. This class should only be used by UnitTestImpl.
4795fd6d3161cc7407718c7e230f94db421543273b8Kenny Rootclass DefaultPerThreadTestPartResultReporter
4805fd6d3161cc7407718c7e230f94db421543273b8Kenny Root    : public TestPartResultReporterInterface {
4815fd6d3161cc7407718c7e230f94db421543273b8Kenny Root public:
4825fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
4835fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // Implements the TestPartResultReporterInterface. The implementation just
4845fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  // delegates to the current global test part result reporter of *unit_test_.
4855fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  virtual void ReportTestPartResult(const TestPartResult& result);
4865fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4875fd6d3161cc7407718c7e230f94db421543273b8Kenny Root private:
4885fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  UnitTestImpl* const unit_test_;
4895fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4905fd6d3161cc7407718c7e230f94db421543273b8Kenny Root  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter);
4915fd6d3161cc7407718c7e230f94db421543273b8Kenny Root};
4925fd6d3161cc7407718c7e230f94db421543273b8Kenny Root
4935fd6d3161cc7407718c7e230f94db421543273b8Kenny Root// The private implementation of the UnitTest class.  We don't protect
494// the methods under a mutex, as this class is not accessible by a
495// user and the UnitTest class that delegates work to this class does
496// proper locking.
497class GTEST_API_ UnitTestImpl {
498 public:
499  explicit UnitTestImpl(UnitTest* parent);
500  virtual ~UnitTestImpl();
501
502  // There are two different ways to register your own TestPartResultReporter.
503  // You can register your own repoter to listen either only for test results
504  // from the current thread or for results from all threads.
505  // By default, each per-thread test result repoter just passes a new
506  // TestPartResult to the global test result reporter, which registers the
507  // test part result for the currently running test.
508
509  // Returns the global test part result reporter.
510  TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
511
512  // Sets the global test part result reporter.
513  void SetGlobalTestPartResultReporter(
514      TestPartResultReporterInterface* reporter);
515
516  // Returns the test part result reporter for the current thread.
517  TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
518
519  // Sets the test part result reporter for the current thread.
520  void SetTestPartResultReporterForCurrentThread(
521      TestPartResultReporterInterface* reporter);
522
523  // Gets the number of successful test cases.
524  int successful_test_case_count() const;
525
526  // Gets the number of failed test cases.
527  int failed_test_case_count() const;
528
529  // Gets the number of all test cases.
530  int total_test_case_count() const;
531
532  // Gets the number of all test cases that contain at least one test
533  // that should run.
534  int test_case_to_run_count() const;
535
536  // Gets the number of successful tests.
537  int successful_test_count() const;
538
539  // Gets the number of failed tests.
540  int failed_test_count() const;
541
542  // Gets the number of disabled tests.
543  int disabled_test_count() const;
544
545  // Gets the number of all tests.
546  int total_test_count() const;
547
548  // Gets the number of tests that should run.
549  int test_to_run_count() const;
550
551  // Gets the elapsed time, in milliseconds.
552  TimeInMillis elapsed_time() const { return elapsed_time_; }
553
554  // Returns true iff the unit test passed (i.e. all test cases passed).
555  bool Passed() const { return !Failed(); }
556
557  // Returns true iff the unit test failed (i.e. some test case failed
558  // or something outside of all tests failed).
559  bool Failed() const {
560    return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
561  }
562
563  // Gets the i-th test case among all the test cases. i can range from 0 to
564  // total_test_case_count() - 1. If i is not in that range, returns NULL.
565  const TestCase* GetTestCase(int i) const {
566    const int index = GetElementOr(test_case_indices_, i, -1);
567    return index < 0 ? NULL : test_cases_[i];
568  }
569
570  // Gets the i-th test case among all the test cases. i can range from 0 to
571  // total_test_case_count() - 1. If i is not in that range, returns NULL.
572  TestCase* GetMutableTestCase(int i) {
573    const int index = GetElementOr(test_case_indices_, i, -1);
574    return index < 0 ? NULL : test_cases_[index];
575  }
576
577  // Provides access to the event listener list.
578  TestEventListeners* listeners() { return &listeners_; }
579
580  // Returns the TestResult for the test that's currently running, or
581  // the TestResult for the ad hoc test if no test is running.
582  TestResult* current_test_result();
583
584  // Returns the TestResult for the ad hoc test.
585  const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
586
587  // Sets the OS stack trace getter.
588  //
589  // Does nothing if the input and the current OS stack trace getter
590  // are the same; otherwise, deletes the old getter and makes the
591  // input the current getter.
592  void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
593
594  // Returns the current OS stack trace getter if it is not NULL;
595  // otherwise, creates an OsStackTraceGetter, makes it the current
596  // getter, and returns it.
597  OsStackTraceGetterInterface* os_stack_trace_getter();
598
599  // Returns the current OS stack trace as a String.
600  //
601  // The maximum number of stack frames to be included is specified by
602  // the gtest_stack_trace_depth flag.  The skip_count parameter
603  // specifies the number of top frames to be skipped, which doesn't
604  // count against the number of frames to be included.
605  //
606  // For example, if Foo() calls Bar(), which in turn calls
607  // CurrentOsStackTraceExceptTop(1), Foo() will be included in the
608  // trace but Bar() and CurrentOsStackTraceExceptTop() won't.
609  String CurrentOsStackTraceExceptTop(int skip_count);
610
611  // Finds and returns a TestCase with the given name.  If one doesn't
612  // exist, creates one and returns it.
613  //
614  // Arguments:
615  //
616  //   test_case_name: name of the test case
617  //   type_param:     the name of the test's type parameter, or NULL if
618  //                   this is not a typed or a type-parameterized test.
619  //   set_up_tc:      pointer to the function that sets up the test case
620  //   tear_down_tc:   pointer to the function that tears down the test case
621  TestCase* GetTestCase(const char* test_case_name,
622                        const char* type_param,
623                        Test::SetUpTestCaseFunc set_up_tc,
624                        Test::TearDownTestCaseFunc tear_down_tc);
625
626  // Adds a TestInfo to the unit test.
627  //
628  // Arguments:
629  //
630  //   set_up_tc:    pointer to the function that sets up the test case
631  //   tear_down_tc: pointer to the function that tears down the test case
632  //   test_info:    the TestInfo object
633  void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc,
634                   Test::TearDownTestCaseFunc tear_down_tc,
635                   TestInfo* test_info) {
636    // In order to support thread-safe death tests, we need to
637    // remember the original working directory when the test program
638    // was first invoked.  We cannot do this in RUN_ALL_TESTS(), as
639    // the user may have changed the current directory before calling
640    // RUN_ALL_TESTS().  Therefore we capture the current directory in
641    // AddTestInfo(), which is called to register a TEST or TEST_F
642    // before main() is reached.
643    if (original_working_dir_.IsEmpty()) {
644      original_working_dir_.Set(FilePath::GetCurrentDir());
645      GTEST_CHECK_(!original_working_dir_.IsEmpty())
646          << "Failed to get the current working directory.";
647    }
648
649    GetTestCase(test_info->test_case_name(),
650                test_info->type_param(),
651                set_up_tc,
652                tear_down_tc)->AddTestInfo(test_info);
653  }
654
655#if GTEST_HAS_PARAM_TEST
656  // Returns ParameterizedTestCaseRegistry object used to keep track of
657  // value-parameterized tests and instantiate and register them.
658  internal::ParameterizedTestCaseRegistry& parameterized_test_registry() {
659    return parameterized_test_registry_;
660  }
661#endif  // GTEST_HAS_PARAM_TEST
662
663  // Sets the TestCase object for the test that's currently running.
664  void set_current_test_case(TestCase* a_current_test_case) {
665    current_test_case_ = a_current_test_case;
666  }
667
668  // Sets the TestInfo object for the test that's currently running.  If
669  // current_test_info is NULL, the assertion results will be stored in
670  // ad_hoc_test_result_.
671  void set_current_test_info(TestInfo* a_current_test_info) {
672    current_test_info_ = a_current_test_info;
673  }
674
675  // Registers all parameterized tests defined using TEST_P and
676  // INSTANTIATE_TEST_CASE_P, creating regular tests for each test/parameter
677  // combination. This method can be called more then once; it has guards
678  // protecting from registering the tests more then once.  If
679  // value-parameterized tests are disabled, RegisterParameterizedTests is
680  // present but does nothing.
681  void RegisterParameterizedTests();
682
683  // Runs all tests in this UnitTest object, prints the result, and
684  // returns true if all tests are successful.  If any exception is
685  // thrown during a test, this test is considered to be failed, but
686  // the rest of the tests will still be run.
687  bool RunAllTests();
688
689  // Clears the results of all tests, except the ad hoc tests.
690  void ClearNonAdHocTestResult() {
691    ForEach(test_cases_, TestCase::ClearTestCaseResult);
692  }
693
694  // Clears the results of ad-hoc test assertions.
695  void ClearAdHocTestResult() {
696    ad_hoc_test_result_.Clear();
697  }
698
699  enum ReactionToSharding {
700    HONOR_SHARDING_PROTOCOL,
701    IGNORE_SHARDING_PROTOCOL
702  };
703
704  // Matches the full name of each test against the user-specified
705  // filter to decide whether the test should run, then records the
706  // result in each TestCase and TestInfo object.
707  // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests
708  // based on sharding variables in the environment.
709  // Returns the number of tests that should run.
710  int FilterTests(ReactionToSharding shard_tests);
711
712  // Prints the names of the tests matching the user-specified filter flag.
713  void ListTestsMatchingFilter();
714
715  const TestCase* current_test_case() const { return current_test_case_; }
716  TestInfo* current_test_info() { return current_test_info_; }
717  const TestInfo* current_test_info() const { return current_test_info_; }
718
719  // Returns the vector of environments that need to be set-up/torn-down
720  // before/after the tests are run.
721  std::vector<Environment*>& environments() { return environments_; }
722
723  // Getters for the per-thread Google Test trace stack.
724  std::vector<TraceInfo>& gtest_trace_stack() {
725    return *(gtest_trace_stack_.pointer());
726  }
727  const std::vector<TraceInfo>& gtest_trace_stack() const {
728    return gtest_trace_stack_.get();
729  }
730
731#if GTEST_HAS_DEATH_TEST
732  void InitDeathTestSubprocessControlInfo() {
733    internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
734  }
735  // Returns a pointer to the parsed --gtest_internal_run_death_test
736  // flag, or NULL if that flag was not specified.
737  // This information is useful only in a death test child process.
738  // Must not be called before a call to InitGoogleTest.
739  const InternalRunDeathTestFlag* internal_run_death_test_flag() const {
740    return internal_run_death_test_flag_.get();
741  }
742
743  // Returns a pointer to the current death test factory.
744  internal::DeathTestFactory* death_test_factory() {
745    return death_test_factory_.get();
746  }
747
748  void SuppressTestEventsIfInSubprocess();
749
750  friend class ReplaceDeathTestFactory;
751#endif  // GTEST_HAS_DEATH_TEST
752
753  // Initializes the event listener performing XML output as specified by
754  // UnitTestOptions. Must not be called before InitGoogleTest.
755  void ConfigureXmlOutput();
756
757#if GTEST_CAN_STREAM_RESULTS_
758  // Initializes the event listener for streaming test results to a socket.
759  // Must not be called before InitGoogleTest.
760  void ConfigureStreamingOutput();
761#endif
762
763  // Performs initialization dependent upon flag values obtained in
764  // ParseGoogleTestFlagsOnly.  Is called from InitGoogleTest after the call to
765  // ParseGoogleTestFlagsOnly.  In case a user neglects to call InitGoogleTest
766  // this function is also called from RunAllTests.  Since this function can be
767  // called more than once, it has to be idempotent.
768  void PostFlagParsingInit();
769
770  // Gets the random seed used at the start of the current test iteration.
771  int random_seed() const { return random_seed_; }
772
773  // Gets the random number generator.
774  internal::Random* random() { return &random_; }
775
776  // Shuffles all test cases, and the tests within each test case,
777  // making sure that death tests are still run first.
778  void ShuffleTests();
779
780  // Restores the test cases and tests to their order before the first shuffle.
781  void UnshuffleTests();
782
783  // Returns the value of GTEST_FLAG(catch_exceptions) at the moment
784  // UnitTest::Run() starts.
785  bool catch_exceptions() const { return catch_exceptions_; }
786
787 private:
788  friend class ::testing::UnitTest;
789
790  // Used by UnitTest::Run() to capture the state of
791  // GTEST_FLAG(catch_exceptions) at the moment it starts.
792  void set_catch_exceptions(bool value) { catch_exceptions_ = value; }
793
794  // The UnitTest object that owns this implementation object.
795  UnitTest* const parent_;
796
797  // The working directory when the first TEST() or TEST_F() was
798  // executed.
799  internal::FilePath original_working_dir_;
800
801  // The default test part result reporters.
802  DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
803  DefaultPerThreadTestPartResultReporter
804      default_per_thread_test_part_result_reporter_;
805
806  // Points to (but doesn't own) the global test part result reporter.
807  TestPartResultReporterInterface* global_test_part_result_repoter_;
808
809  // Protects read and write access to global_test_part_result_reporter_.
810  internal::Mutex global_test_part_result_reporter_mutex_;
811
812  // Points to (but doesn't own) the per-thread test part result reporter.
813  internal::ThreadLocal<TestPartResultReporterInterface*>
814      per_thread_test_part_result_reporter_;
815
816  // The vector of environments that need to be set-up/torn-down
817  // before/after the tests are run.
818  std::vector<Environment*> environments_;
819
820  // The vector of TestCases in their original order.  It owns the
821  // elements in the vector.
822  std::vector<TestCase*> test_cases_;
823
824  // Provides a level of indirection for the test case list to allow
825  // easy shuffling and restoring the test case order.  The i-th
826  // element of this vector is the index of the i-th test case in the
827  // shuffled order.
828  std::vector<int> test_case_indices_;
829
830#if GTEST_HAS_PARAM_TEST
831  // ParameterizedTestRegistry object used to register value-parameterized
832  // tests.
833  internal::ParameterizedTestCaseRegistry parameterized_test_registry_;
834
835  // Indicates whether RegisterParameterizedTests() has been called already.
836  bool parameterized_tests_registered_;
837#endif  // GTEST_HAS_PARAM_TEST
838
839  // Index of the last death test case registered.  Initially -1.
840  int last_death_test_case_;
841
842  // This points to the TestCase for the currently running test.  It
843  // changes as Google Test goes through one test case after another.
844  // When no test is running, this is set to NULL and Google Test
845  // stores assertion results in ad_hoc_test_result_.  Initially NULL.
846  TestCase* current_test_case_;
847
848  // This points to the TestInfo for the currently running test.  It
849  // changes as Google Test goes through one test after another.  When
850  // no test is running, this is set to NULL and Google Test stores
851  // assertion results in ad_hoc_test_result_.  Initially NULL.
852  TestInfo* current_test_info_;
853
854  // Normally, a user only writes assertions inside a TEST or TEST_F,
855  // or inside a function called by a TEST or TEST_F.  Since Google
856  // Test keeps track of which test is current running, it can
857  // associate such an assertion with the test it belongs to.
858  //
859  // If an assertion is encountered when no TEST or TEST_F is running,
860  // Google Test attributes the assertion result to an imaginary "ad hoc"
861  // test, and records the result in ad_hoc_test_result_.
862  TestResult ad_hoc_test_result_;
863
864  // The list of event listeners that can be used to track events inside
865  // Google Test.
866  TestEventListeners listeners_;
867
868  // The OS stack trace getter.  Will be deleted when the UnitTest
869  // object is destructed.  By default, an OsStackTraceGetter is used,
870  // but the user can set this field to use a custom getter if that is
871  // desired.
872  OsStackTraceGetterInterface* os_stack_trace_getter_;
873
874  // True iff PostFlagParsingInit() has been called.
875  bool post_flag_parse_init_performed_;
876
877  // The random number seed used at the beginning of the test run.
878  int random_seed_;
879
880  // Our random number generator.
881  internal::Random random_;
882
883  // How long the test took to run, in milliseconds.
884  TimeInMillis elapsed_time_;
885
886#if GTEST_HAS_DEATH_TEST
887  // The decomposed components of the gtest_internal_run_death_test flag,
888  // parsed when RUN_ALL_TESTS is called.
889  internal::scoped_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
890  internal::scoped_ptr<internal::DeathTestFactory> death_test_factory_;
891#endif  // GTEST_HAS_DEATH_TEST
892
893  // A per-thread stack of traces created by the SCOPED_TRACE() macro.
894  internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
895
896  // The value of GTEST_FLAG(catch_exceptions) at the moment RunAllTests()
897  // starts.
898  bool catch_exceptions_;
899
900  GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);
901};  // class UnitTestImpl
902
903// Convenience function for accessing the global UnitTest
904// implementation object.
905inline UnitTestImpl* GetUnitTestImpl() {
906  return UnitTest::GetInstance()->impl();
907}
908
909#if GTEST_USES_SIMPLE_RE
910
911// Internal helper functions for implementing the simple regular
912// expression matcher.
913GTEST_API_ bool IsInSet(char ch, const char* str);
914GTEST_API_ bool IsAsciiDigit(char ch);
915GTEST_API_ bool IsAsciiPunct(char ch);
916GTEST_API_ bool IsRepeat(char ch);
917GTEST_API_ bool IsAsciiWhiteSpace(char ch);
918GTEST_API_ bool IsAsciiWordChar(char ch);
919GTEST_API_ bool IsValidEscape(char ch);
920GTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch);
921GTEST_API_ bool ValidateRegex(const char* regex);
922GTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str);
923GTEST_API_ bool MatchRepetitionAndRegexAtHead(
924    bool escaped, char ch, char repeat, const char* regex, const char* str);
925GTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str);
926
927#endif  // GTEST_USES_SIMPLE_RE
928
929// Parses the command line for Google Test flags, without initializing
930// other parts of Google Test.
931GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv);
932GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv);
933
934#if GTEST_HAS_DEATH_TEST
935
936// Returns the message describing the last system error, regardless of the
937// platform.
938GTEST_API_ String GetLastErrnoDescription();
939
940# if GTEST_OS_WINDOWS
941// Provides leak-safe Windows kernel handle ownership.
942class AutoHandle {
943 public:
944  AutoHandle() : handle_(INVALID_HANDLE_VALUE) {}
945  explicit AutoHandle(HANDLE handle) : handle_(handle) {}
946
947  ~AutoHandle() { Reset(); }
948
949  HANDLE Get() const { return handle_; }
950  void Reset() { Reset(INVALID_HANDLE_VALUE); }
951  void Reset(HANDLE handle) {
952    if (handle != handle_) {
953      if (handle_ != INVALID_HANDLE_VALUE)
954        ::CloseHandle(handle_);
955      handle_ = handle;
956    }
957  }
958
959 private:
960  HANDLE handle_;
961
962  GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
963};
964# endif  // GTEST_OS_WINDOWS
965
966// Attempts to parse a string into a positive integer pointed to by the
967// number parameter.  Returns true if that is possible.
968// GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use
969// it here.
970template <typename Integer>
971bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
972  // Fail fast if the given string does not begin with a digit;
973  // this bypasses strtoXXX's "optional leading whitespace and plus
974  // or minus sign" semantics, which are undesirable here.
975  if (str.empty() || !IsDigit(str[0])) {
976    return false;
977  }
978  errno = 0;
979
980  char* end;
981  // BiggestConvertible is the largest integer type that system-provided
982  // string-to-number conversion routines can return.
983
984# if GTEST_OS_WINDOWS && !defined(__GNUC__)
985
986  // MSVC and C++ Builder define __int64 instead of the standard long long.
987  typedef unsigned __int64 BiggestConvertible;
988  const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10);
989
990# else
991
992  typedef unsigned long long BiggestConvertible;  // NOLINT
993  const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
994
995# endif  // GTEST_OS_WINDOWS && !defined(__GNUC__)
996
997  const bool parse_success = *end == '\0' && errno == 0;
998
999  // TODO(vladl@google.com): Convert this to compile time assertion when it is
1000  // available.
1001  GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed));
1002
1003  const Integer result = static_cast<Integer>(parsed);
1004  if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
1005    *number = result;
1006    return true;
1007  }
1008  return false;
1009}
1010#endif  // GTEST_HAS_DEATH_TEST
1011
1012// TestResult contains some private methods that should be hidden from
1013// Google Test user but are required for testing. This class allow our tests
1014// to access them.
1015//
1016// This class is supplied only for the purpose of testing Google Test's own
1017// constructs. Do not use it in user tests, either directly or indirectly.
1018class TestResultAccessor {
1019 public:
1020  static void RecordProperty(TestResult* test_result,
1021                             const TestProperty& property) {
1022    test_result->RecordProperty(property);
1023  }
1024
1025  static void ClearTestPartResults(TestResult* test_result) {
1026    test_result->ClearTestPartResults();
1027  }
1028
1029  static const std::vector<testing::TestPartResult>& test_part_results(
1030      const TestResult& test_result) {
1031    return test_result.test_part_results();
1032  }
1033};
1034
1035}  // namespace internal
1036}  // namespace testing
1037
1038#endif  // GTEST_SRC_GTEST_INTERNAL_INL_H_
1039