10a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Copyright 2005, Google Inc.
20a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// All rights reserved.
30a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
40a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Redistribution and use in source and binary forms, with or without
50a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// modification, are permitted provided that the following conditions are
60a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// met:
70a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
80a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//     * Redistributions of source code must retain the above copyright
90a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// notice, this list of conditions and the following disclaimer.
100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//     * Redistributions in binary form must reproduce the above
110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// copyright notice, this list of conditions and the following disclaimer
120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// in the documentation and/or other materials provided with the
130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// distribution.
140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//     * Neither the name of Google Inc. nor the names of its
150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// contributors may be used to endorse or promote products derived from
160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// this software without specific prior written permission.
170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Utility functions and classes used by the Google C++ testing framework.
310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Author: wan@google.com (Zhanyong Wan)
330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This file contains purely Google Test's internal implementation.  Please
350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// DO NOT #INCLUDE IT IN A USER PROGRAM.
360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#define GTEST_SRC_GTEST_INTERNAL_INL_H_
390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// GTEST_IMPLEMENTATION_ is defined to 1 iff the current translation unit is
410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// part of Google Test's implementation; otherwise it's undefined.
420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if !GTEST_IMPLEMENTATION_
430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// A user is trying to include this from his code - just say no.
440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#error "gtest-internal-inl.h is part of Google Test's internal implementation."
450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#error "It must not be included except by Google Test itself."
460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_IMPLEMENTATION_
470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#ifndef _WIN32_WCE
490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <errno.h>
500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // !_WIN32_WCE
510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <stddef.h>
520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <stdlib.h>  // For strtoll/_strtoul64/malloc/free.
530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <string.h>  // For memmove.
540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <string>
560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <gtest/internal/gtest-port.h>
580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if GTEST_OS_WINDOWS
600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <windows.h>  // For DWORD.
610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_OS_WINDOWS
620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <gtest/gtest.h>
640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#include <gtest/gtest-spi.h>
650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathnamespace testing {
670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Declares the flags.
690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// We don't want the users to modify this flag in the code, but want
710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Google Test's own unit tests to be able to access it. Therefore we
720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// declare it here as opposed to in gtest.h.
730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathGTEST_DECLARE_bool_(death_test_use_fork);
740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathnamespace internal {
760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The value of GetTestTypeId() as seen from within the Google Test
780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// library.  This is solely for testing GetTestTypeId().
790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathextern const TypeId kTestTypeIdInGoogleTest;
800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Names of the flags (needed for parsing Google Test flags).
820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests";
830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kBreakOnFailureFlag[] = "break_on_failure";
840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kCatchExceptionsFlag[] = "catch_exceptions";
850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kColorFlag[] = "color";
860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kFilterFlag[] = "filter";
870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kListTestsFlag[] = "list_tests";
880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kOutputFlag[] = "output";
890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kPrintTimeFlag[] = "print_time";
900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kRandomSeedFlag[] = "random_seed";
910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kRepeatFlag[] = "repeat";
920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kShuffleFlag[] = "shuffle";
930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kStackTraceDepthFlag[] = "stack_trace_depth";
940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst char kThrowOnFailureFlag[] = "throw_on_failure";
950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// A valid random seed must be in [1, kMaxRandomSeed].
970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathconst int kMaxRandomSeed = 99999;
980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Returns the current time in milliseconds.
1000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathTimeInMillis GetTimeInMillis();
1010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Returns a random seed in range [1, kMaxRandomSeed] based on the
1030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// given --gtest_random_seed flag value.
1040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathinline int GetRandomSeedFromFlag(Int32 random_seed_flag) {
1050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const unsigned int raw_seed = (random_seed_flag == 0) ?
1060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      static_cast<unsigned int>(GetTimeInMillis()) :
1070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      static_cast<unsigned int>(random_seed_flag);
1080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Normalizes the actual seed to range [1, kMaxRandomSeed] such that
1100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // it's easy to type.
1110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const int normalized_seed =
1120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      static_cast<int>((raw_seed - 1U) %
1130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                       static_cast<unsigned int>(kMaxRandomSeed)) + 1;
1140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  return normalized_seed;
1150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}
1160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Returns the first valid random seed after 'seed'.  The behavior is
1180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// undefined if 'seed' is invalid.  The seed after kMaxRandomSeed is
1190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// considered to be 1.
1200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathinline int GetNextRandomSeed(int seed) {
1210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)
1220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      << "Invalid random seed " << seed << " - must be in [1, "
1230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      << kMaxRandomSeed << "].";
1240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const int next_seed = seed + 1;
1250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
1260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}
1270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This class saves the values of all Google Test flags in its c'tor, and
1290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// restores them in its d'tor.
1300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass GTestFlagSaver {
1310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
1320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The c'tor.
1330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTestFlagSaver() {
1340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests);
1350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    break_on_failure_ = GTEST_FLAG(break_on_failure);
1360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    catch_exceptions_ = GTEST_FLAG(catch_exceptions);
1370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    color_ = GTEST_FLAG(color);
1380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    death_test_style_ = GTEST_FLAG(death_test_style);
1390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    death_test_use_fork_ = GTEST_FLAG(death_test_use_fork);
1400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    filter_ = GTEST_FLAG(filter);
1410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
1420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    list_tests_ = GTEST_FLAG(list_tests);
1430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    output_ = GTEST_FLAG(output);
1440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    print_time_ = GTEST_FLAG(print_time);
1450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    random_seed_ = GTEST_FLAG(random_seed);
1460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    repeat_ = GTEST_FLAG(repeat);
1470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    shuffle_ = GTEST_FLAG(shuffle);
1480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    stack_trace_depth_ = GTEST_FLAG(stack_trace_depth);
1490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    throw_on_failure_ = GTEST_FLAG(throw_on_failure);
1500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
1510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The d'tor is not virtual.  DO NOT INHERIT FROM THIS CLASS.
1530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  ~GTestFlagSaver() {
1540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
1550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(break_on_failure) = break_on_failure_;
1560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(catch_exceptions) = catch_exceptions_;
1570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(color) = color_;
1580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(death_test_style) = death_test_style_;
1590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
1600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(filter) = filter_;
1610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
1620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(list_tests) = list_tests_;
1630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(output) = output_;
1640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(print_time) = print_time_;
1650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(random_seed) = random_seed_;
1660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(repeat) = repeat_;
1670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(shuffle) = shuffle_;
1680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
1690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_FLAG(throw_on_failure) = throw_on_failure_;
1700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
1710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
1720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Fields for saving the original values of flags.
1730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool also_run_disabled_tests_;
1740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool break_on_failure_;
1750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool catch_exceptions_;
1760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  String color_;
1770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  String death_test_style_;
1780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool death_test_use_fork_;
1790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  String filter_;
1800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  String internal_run_death_test_;
1810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool list_tests_;
1820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  String output_;
1830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool print_time_;
1840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool pretty_;
1850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Int32 random_seed_;
1860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Int32 repeat_;
1870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool shuffle_;
1880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Int32 stack_trace_depth_;
1890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool throw_on_failure_;
1900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath} GTEST_ATTRIBUTE_UNUSED_;
1910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
1920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Converts a Unicode code point to a narrow string in UTF-8 encoding.
1930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// code_point parameter is of type UInt32 because wchar_t may not be
1940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// wide enough to contain a code point.
1950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The output buffer str must containt at least 32 characters.
1960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The function returns the address of the output buffer.
1970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// If the code_point is not a valid Unicode code point
1980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be output
1990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// as '(Invalid Unicode 0xXXXXXXXX)'.
2000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathchar* CodePointToUtf8(UInt32 code_point, char* str);
2010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Converts a wide string to a narrow string in UTF-8 encoding.
2030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The wide string is assumed to have the following encoding:
2040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//   UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS)
2050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//   UTF-32 if sizeof(wchar_t) == 4 (on Linux)
2060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Parameter str points to a null-terminated wide string.
2070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Parameter num_chars may additionally limit the number
2080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// of wchar_t characters processed. -1 is used when the entire string
2090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// should be processed.
2100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// If the string contains code points that are not valid Unicode code points
2110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output
2120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding
2130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// and contains invalid UTF-16 surrogate pairs, values in those pairs
2140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// will be encoded as individual Unicode characters from Basic Normal Plane.
2150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathString WideStringToUtf8(const wchar_t* str, int num_chars);
2160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Returns the number of active threads, or 0 when there is an error.
2180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathsize_t GetThreadCount();
2190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file
2210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// if the variable is present. If a file already exists at this location, this
2220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// function will write over it. If the variable is present, but the file cannot
2230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// be created, prints an error and exits.
2240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathvoid WriteToShardStatusFileIfNeeded();
2250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Checks whether sharding is enabled by examining the relevant
2270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// environment variable values. If the variables are present,
2280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// but inconsistent (e.g., shard_index >= total_shards), prints
2290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// an error and exits. If in_subprocess_for_death_test, sharding is
2300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// disabled because it must only be applied to the original test
2310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// process. Otherwise, we could filter out death tests we intended to execute.
2320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool ShouldShard(const char* total_shards_str, const char* shard_index_str,
2330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                 bool in_subprocess_for_death_test);
2340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Parses the environment variable var as an Int32. If it is unset,
2360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// returns default_val. If it is not an Int32, prints an error and
2370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// and aborts.
2380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathInt32 Int32FromEnvOrDie(const char* env_var, Int32 default_val);
2390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Given the total number of shards, the shard index, and the test id,
2410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// returns true iff the test should be run on this shard. The test id is
2420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// some arbitrary but unique non-negative integer assigned to each test
2430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// method. Assumes that 0 <= shard_index < total_shards.
2440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id);
2450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Vector is an ordered container that supports random access to the
2470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// elements.
2480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
2490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// We cannot use std::vector, as Visual C++ 7.1's implementation of
2500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// STL has problems compiling when exceptions are disabled.  There is
2510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// a hack to work around the problems, but we've seen cases where the
2520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// hack fails to work.
2530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
2540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The element type must support copy constructor and operator=.
2550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtemplate <typename E>  // E is the element type.
2560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass Vector {
2570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
2580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Creates an empty Vector.
2590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  Vector() : elements_(NULL), capacity_(0), size_(0) {}
2600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // D'tor.
2620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual ~Vector() { Clear(); }
2630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Clears the Vector.
2650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void Clear() {
2660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    if (elements_ != NULL) {
2670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      for (int i = 0; i < size_; i++) {
2680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        delete elements_[i];
2690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      }
2700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      free(elements_);
2720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      elements_ = NULL;
2730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      capacity_ = size_ = 0;
2740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    }
2750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
2760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the number of elements.
2780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int size() const { return size_; }
2790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Adds an element to the end of the Vector.  A copy of the element
2810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // is created using the copy constructor, and then stored in the
2820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Vector.  Changes made to the element in the Vector doesn't affect
2830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // the source object, and vice versa.
2840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void PushBack(const E& element) { Insert(element, size_); }
2850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Adds an element to the beginning of this Vector.
2870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void PushFront(const E& element) { Insert(element, 0); }
2880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Removes an element from the beginning of this Vector.  If the
2900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // result argument is not NULL, the removed element is stored in the
2910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // memory it points to.  Otherwise the element is thrown away.
2920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns true iff the vector wasn't empty before the operation.
2930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool PopFront(E* result) {
2940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    if (size_ == 0)
2950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      return false;
2960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
2970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    if (result != NULL)
2980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      *result = GetElement(0);
2990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    Erase(0);
3010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return true;
3020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
3030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Inserts an element at the given index.  It's the caller's
3050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // responsibility to ensure that the given index is in the range [0,
3060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // size()].
3070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void Insert(const E& element, int index) {
3080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GrowIfNeeded();
3090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    MoveElements(index, size_ - index, index + 1);
3100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    elements_[index] = new E(element);
3110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    size_++;
3120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
3130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Erases the element at the specified index, or aborts the program if the
3150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // index is not in range [0, size()).
3160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void Erase(int index) {
3170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_CHECK_(0 <= index && index < size_)
3180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << "Invalid Vector index " << index << ": must be in range [0, "
3190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << (size_ - 1) << "].";
3200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    delete elements_[index];
3220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    MoveElements(index + 1, size_ - index - 1, index);
3230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    size_--;
3240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
3250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the number of elements that satisfy a given predicate.
3270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The parameter 'predicate' is a Boolean function or functor that
3280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // accepts a 'const E &', where E is the element type.
3290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  template <typename P>  // P is the type of the predicate function/functor
3300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int CountIf(P predicate) const {
3310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    int count = 0;
3320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    for (int i = 0; i < size_; i++) {
3330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      if (predicate(*(elements_[i]))) {
3340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        count++;
3350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      }
3360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    }
3370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return count;
3390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
3400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Applies a function/functor to each element in the Vector.  The
3420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // parameter 'functor' is a function/functor that accepts a 'const
3430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // E &', where E is the element type.  This method does not change
3440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // the elements.
3450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  template <typename F>  // F is the type of the function/functor
3460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void ForEach(F functor) const {
3470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    for (int i = 0; i < size_; i++) {
3480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      functor(*(elements_[i]));
3490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    }
3500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
3510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the first node whose element satisfies a given predicate,
3530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // or NULL if none is found.  The parameter 'predicate' is a
3540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // function/functor that accepts a 'const E &', where E is the
3550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // element type.  This method does not change the elements.
3560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  template <typename P>  // P is the type of the predicate function/functor.
3570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const E* FindIf(P predicate) const {
3580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    for (int i = 0; i < size_; i++) {
3590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      if (predicate(*elements_[i])) {
3600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        return elements_[i];
3610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      }
3620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    }
3630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return NULL;
3640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
3650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  template <typename P>
3670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  E* FindIf(P predicate) {
3680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    for (int i = 0; i < size_; i++) {
3690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      if (predicate(*elements_[i])) {
3700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        return elements_[i];
3710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      }
3720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    }
3730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return NULL;
3740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
3750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the i-th element of the Vector, or aborts the program if i
3770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // is not in range [0, size()).
3780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const E& GetElement(int i) const {
3790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_CHECK_(0 <= i && i < size_)
3800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << "Invalid Vector index " << i << ": must be in range [0, "
3810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << (size_ - 1) << "].";
3820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return *(elements_[i]);
3840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
3850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns a mutable reference to the i-th element of the Vector, or
3870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // aborts the program if i is not in range [0, size()).
3880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  E& GetMutableElement(int i) {
3890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_CHECK_(0 <= i && i < size_)
3900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << "Invalid Vector index " << i << ": must be in range [0, "
3910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << (size_ - 1) << "].";
3920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return *(elements_[i]);
3940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
3950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
3960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the i-th element of the Vector, or default_value if i is not
3970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // in range [0, size()).
3980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  E GetElementOr(int i, E default_value) const {
3990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return (i < 0 || i >= size_) ? default_value : *(elements_[i]);
4000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
4010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Swaps the i-th and j-th elements of the Vector.  Crashes if i or
4030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // j is invalid.
4040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void Swap(int i, int j) {
4050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_CHECK_(0 <= i && i < size_)
4060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << "Invalid first swap element " << i << ": must be in range [0, "
4070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << (size_ - 1) << "].";
4080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_CHECK_(0 <= j && j < size_)
4090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << "Invalid second swap element " << j << ": must be in range [0, "
4100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << (size_ - 1) << "].";
4110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    E* const temp = elements_[i];
4130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    elements_[i] = elements_[j];
4140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    elements_[j] = temp;
4150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
4160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Performs an in-place shuffle of a range of this Vector's nodes.
4180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // 'begin' and 'end' are element indices as an STL-style range;
4190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // i.e. [begin, end) are shuffled, where 'end' == size() means to
4200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // shuffle to the end of the Vector.
4210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void ShuffleRange(internal::Random* random, int begin, int end) {
4220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_CHECK_(0 <= begin && begin <= size_)
4230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << "Invalid shuffle range start " << begin << ": must be in range [0, "
4240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << size_ << "].";
4250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_CHECK_(begin <= end && end <= size_)
4260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << "Invalid shuffle range finish " << end << ": must be in range ["
4270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << begin << ", " << size_ << "].";
4280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // Fisher-Yates shuffle, from
4300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
4310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    for (int range_width = end - begin; range_width >= 2; range_width--) {
4320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      const int last_in_range = begin + range_width - 1;
4330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      const int selected = begin + random->Generate(range_width);
4340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      Swap(selected, last_in_range);
4350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    }
4360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
4370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Performs an in-place shuffle of this Vector's nodes.
4390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void Shuffle(internal::Random* random) {
4400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    ShuffleRange(random, 0, size());
4410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
4420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns a copy of this Vector.
4440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  Vector* Clone() const {
4450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    Vector* const clone = new Vector;
4460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    clone->Reserve(size_);
4470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    for (int i = 0; i < size_; i++) {
4480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      clone->PushBack(GetElement(i));
4490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    }
4500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return clone;
4510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
4520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
4540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Makes sure this Vector's capacity is at least the given value.
4550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void Reserve(int new_capacity) {
4560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    if (new_capacity <= capacity_)
4570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      return;
4580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    capacity_ = new_capacity;
4600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    elements_ = static_cast<E**>(
4610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        realloc(elements_, capacity_*sizeof(elements_[0])));
4620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
4630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Grows the buffer if it is not big enough to hold one more element.
4650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void GrowIfNeeded() {
4660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    if (size_ < capacity_)
4670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      return;
4680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // Exponential bump-up is necessary to ensure that inserting N
4700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // elements is O(N) instead of O(N^2).  The factor 3/2 means that
4710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // no more than 1/3 of the slots are wasted.
4720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    const int new_capacity = 3*(capacity_/2 + 1);
4730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GTEST_CHECK_(new_capacity > capacity_)  // Does the new capacity overflow?
4740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        << "Cannot grow a Vector with " << capacity_ << " elements already.";
4750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    Reserve(new_capacity);
4760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
4770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Moves the give consecutive elements to a new index in the Vector.
4790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void MoveElements(int source, int count, int dest) {
4800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    memmove(elements_ + dest, elements_ + source, count*sizeof(elements_[0]));
4810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
4820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  E** elements_;
4840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int capacity_;  // The number of elements allocated for elements_.
4850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int size_;      // The number of elements; in the range [0, capacity_].
4860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // We disallow copying Vector.
4880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTEST_DISALLOW_COPY_AND_ASSIGN_(Vector);
4890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};  // class Vector
4900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// A function for deleting an object.  Handy for being used as a
4920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// functor.
4930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtemplate <typename T>
4940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathstatic void Delete(T * x) {
4950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  delete x;
4960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}
4970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
4980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// A predicate that checks the key of a TestProperty against a known key.
4990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
5000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// TestPropertyKeyIs is copyable.
5010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass TestPropertyKeyIs {
5020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
5030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Constructor.
5040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
5050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // TestPropertyKeyIs has NO default constructor.
5060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  explicit TestPropertyKeyIs(const char* key)
5070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      : key_(key) {}
5080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns true iff the test name of test property matches on key_.
5100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool operator()(const TestProperty& test_property) const {
5110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return String(test_property.key()).Compare(key_) == 0;
5120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
5130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
5150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  String key_;
5160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
5170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass TestInfoImpl {
5190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
5200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestInfoImpl(TestInfo* parent, const char* test_case_name,
5210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath               const char* name, const char* test_case_comment,
5220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath               const char* comment, TypeId fixture_class_id,
5230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath               internal::TestFactoryBase* factory);
5240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  ~TestInfoImpl();
5250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns true if this test should run.
5270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool should_run() const { return should_run_; }
5280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Sets the should_run member.
5300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void set_should_run(bool should) { should_run_ = should; }
5310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns true if this test is disabled. Disabled tests are not run.
5330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool is_disabled() const { return is_disabled_; }
5340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Sets the is_disabled member.
5360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void set_is_disabled(bool is) { is_disabled_ = is; }
5370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns true if this test matches the filter specified by the user.
5390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool matches_filter() const { return matches_filter_; }
5400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Sets the matches_filter member.
5420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void set_matches_filter(bool matches) { matches_filter_ = matches; }
5430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the test case name.
5450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const char* test_case_name() const { return test_case_name_.c_str(); }
5460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the test name.
5480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const char* name() const { return name_.c_str(); }
5490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the test case comment.
5510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const char* test_case_comment() const { return test_case_comment_.c_str(); }
5520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the test comment.
5540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const char* comment() const { return comment_.c_str(); }
5550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the ID of the test fixture class.
5570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TypeId fixture_class_id() const { return fixture_class_id_; }
5580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the test result.
5600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestResult* result() { return &result_; }
5610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const TestResult* result() const { return &result_; }
5620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Creates the test object, runs it, records its result, and then
5640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // deletes it.
5650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void Run();
5660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Clears the test result.
5680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void ClearResult() { result_.Clear(); }
5690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Clears the test result in the given TestInfo object.
5710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static void ClearTestResult(TestInfo * test_info) {
5720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    test_info->impl()->ClearResult();
5730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
5740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
5760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // These fields are immutable properties of the test.
5770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestInfo* const parent_;          // The owner of this object
5780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const String test_case_name_;     // Test case name
5790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const String name_;               // Test name
5800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const String test_case_comment_;  // Test case comment
5810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const String comment_;            // Test comment
5820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const TypeId fixture_class_id_;   // ID of the test fixture class
5830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool should_run_;                 // True iff this test should run
5840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool is_disabled_;                // True iff this test is disabled
5850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool matches_filter_;             // True if this test matches the
5860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                                    // user-specified filter.
5870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::TestFactoryBase* const factory_;  // The factory that creates
5880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                                              // the test object
5890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // This field is mutable and needs to be reset before running the
5910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // test for the second time.
5920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestResult result_;
5930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfoImpl);
5950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
5960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
5970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Class UnitTestOptions.
5980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
5990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This class contains functions for processing options the user
6000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// specifies when running the tests.  It has only static members.
6010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath//
6020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// In most cases, the user can specify an option using either an
6030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// environment variable or a command line flag.  E.g. you can set the
6040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// test filter using either GTEST_FILTER or --gtest_filter.  If both
6050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// the variable and the flag are present, the latter overrides the
6060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// former.
6070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass UnitTestOptions {
6080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
6090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Functions for processing the gtest_output flag.
6100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the output format, or "" for normal printed output.
6120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static String GetOutputFormat();
6130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the absolute path of the requested output file, or the
6150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // default (test_detail.xml in the original working directory) if
6160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // none was explicitly specified.
6170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static String GetAbsolutePathToOutputFile();
6180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Functions for processing the gtest_filter flag.
6200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns true iff the wildcard pattern matches the string.  The
6220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // first ':' or '\0' character in pattern marks the end of it.
6230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
6240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // This recursive algorithm isn't very efficient, but is clear and
6250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // works well enough for matching test names, which are short.
6260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static bool PatternMatchesString(const char *pattern, const char *str);
6270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns true iff the user-specified filter matches the test case
6290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // name and the test name.
6300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static bool FilterMatchesTest(const String &test_case_name,
6310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                                const String &test_name);
6320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if GTEST_OS_WINDOWS
6340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Function for supporting the gtest_catch_exception flag.
6350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
6370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
6380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // This function is useful as an __except condition.
6390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static int GTestShouldProcessSEH(DWORD exception_code);
6400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_OS_WINDOWS
6410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns true if "name" matches the ':' separated list of glob-style
6430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // filters in "filter".
6440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static bool MatchesFilter(const String& name, const char* filter);
6450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
6460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Returns the current application's name, removing directory path if that
6480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// is present.  Used by UnitTestOptions::GetOutputFile.
6490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathFilePath GetCurrentExecutableName();
6500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The role interface for getting the OS stack trace as a string.
6520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass OsStackTraceGetterInterface {
6530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
6540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  OsStackTraceGetterInterface() {}
6550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual ~OsStackTraceGetterInterface() {}
6560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the current OS stack trace as a String.  Parameters:
6580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
6590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //   max_depth  - the maximum number of stack frames to be included
6600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //                in the trace.
6610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //   skip_count - the number of top frames to be skipped; doesn't count
6620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //                against max_depth.
6630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual String CurrentStackTrace(int max_depth, int skip_count) = 0;
6640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // UponLeavingGTest() should be called immediately before Google Test calls
6660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // user code. It saves some information about the current stack that
6670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // CurrentStackTrace() will use to find and hide Google Test stack frames.
6680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void UponLeavingGTest() = 0;
6690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
6710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface);
6720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
6730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// A working implementation of the OsStackTraceGetterInterface interface.
6750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass OsStackTraceGetter : public OsStackTraceGetterInterface {
6760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
6770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  OsStackTraceGetter() : caller_frame_(NULL) {}
6780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual String CurrentStackTrace(int max_depth, int skip_count);
6790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void UponLeavingGTest();
6800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // This string is inserted in place of stack frames that are part of
6820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Google Test's implementation.
6830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static const char* const kElidedFramesMarker;
6840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
6860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  Mutex mutex_;  // protects all internal state
6870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // We save the stack frame below the frame that calls user code.
6890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // We do this because the address of the frame immediately below
6900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // the user code changes between the call to UponLeavingGTest()
6910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // and any calls to CurrentStackTrace() from within the user code.
6920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void* caller_frame_;
6930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter);
6950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
6960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
6970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Information about a Google Test trace point.
6980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathstruct TraceInfo {
6990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const char* file;
7000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int line;
7010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  String message;
7020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
7030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This is the default global test part result reporter used in UnitTestImpl.
7050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This class should only be used by UnitTestImpl.
7060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass DefaultGlobalTestPartResultReporter
7070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  : public TestPartResultReporterInterface {
7080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
7090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
7100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Implements the TestPartResultReporterInterface. Reports the test part
7110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // result in the current test.
7120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void ReportTestPartResult(const TestPartResult& result);
7130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
7150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  UnitTestImpl* const unit_test_;
7160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);
7180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
7190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// This is the default per thread test part result reporter used in
7210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// UnitTestImpl. This class should only be used by UnitTestImpl.
7220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass DefaultPerThreadTestPartResultReporter
7230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    : public TestPartResultReporterInterface {
7240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
7250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
7260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Implements the TestPartResultReporterInterface. The implementation just
7270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // delegates to the current global test part result reporter of *unit_test_.
7280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual void ReportTestPartResult(const TestPartResult& result);
7290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
7310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  UnitTestImpl* const unit_test_;
7320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter);
7340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
7350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// The private implementation of the UnitTest class.  We don't protect
7370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// the methods under a mutex, as this class is not accessible by a
7380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// user and the UnitTest class that delegates work to this class does
7390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// proper locking.
7400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass UnitTestImpl {
7410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
7420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  explicit UnitTestImpl(UnitTest* parent);
7430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  virtual ~UnitTestImpl();
7440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // There are two different ways to register your own TestPartResultReporter.
7460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // You can register your own repoter to listen either only for test results
7470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // from the current thread or for results from all threads.
7480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // By default, each per-thread test result repoter just passes a new
7490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // TestPartResult to the global test result reporter, which registers the
7500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // test part result for the currently running test.
7510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the global test part result reporter.
7530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
7540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Sets the global test part result reporter.
7560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void SetGlobalTestPartResultReporter(
7570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      TestPartResultReporterInterface* reporter);
7580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the test part result reporter for the current thread.
7600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
7610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Sets the test part result reporter for the current thread.
7630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void SetTestPartResultReporterForCurrentThread(
7640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      TestPartResultReporterInterface* reporter);
7650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the number of successful test cases.
7670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int successful_test_case_count() const;
7680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the number of failed test cases.
7700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int failed_test_case_count() const;
7710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the number of all test cases.
7730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int total_test_case_count() const;
7740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the number of all test cases that contain at least one test
7760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // that should run.
7770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int test_case_to_run_count() const;
7780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the number of successful tests.
7800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int successful_test_count() const;
7810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the number of failed tests.
7830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int failed_test_count() const;
7840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the number of disabled tests.
7860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int disabled_test_count() const;
7870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the number of all tests.
7890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int total_test_count() const;
7900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the number of tests that should run.
7920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int test_to_run_count() const;
7930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the elapsed time, in milliseconds.
7950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TimeInMillis elapsed_time() const { return elapsed_time_; }
7960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
7970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns true iff the unit test passed (i.e. all test cases passed).
7980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool Passed() const { return !Failed(); }
7990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns true iff the unit test failed (i.e. some test case failed
8010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // or something outside of all tests failed).
8020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool Failed() const {
8030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
8040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
8050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the i-th test case among all the test cases. i can range from 0 to
8070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // total_test_case_count() - 1. If i is not in that range, returns NULL.
8080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const TestCase* GetTestCase(int i) const {
8090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    const int index = test_case_indices_.GetElementOr(i, -1);
8100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return index < 0 ? NULL : test_cases_.GetElement(i);
8110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
8120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the i-th test case among all the test cases. i can range from 0 to
8140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // total_test_case_count() - 1. If i is not in that range, returns NULL.
8150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestCase* GetMutableTestCase(int i) {
8160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    const int index = test_case_indices_.GetElementOr(i, -1);
8170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return index < 0 ? NULL : test_cases_.GetElement(index);
8180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
8190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Provides access to the event listener list.
8210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestEventListeners* listeners() { return &listeners_; }
8220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the TestResult for the test that's currently running, or
8240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // the TestResult for the ad hoc test if no test is running.
8250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestResult* current_test_result();
8260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the TestResult for the ad hoc test.
8280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
8290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Sets the OS stack trace getter.
8310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
8320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Does nothing if the input and the current OS stack trace getter
8330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // are the same; otherwise, deletes the old getter and makes the
8340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // input the current getter.
8350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
8360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the current OS stack trace getter if it is not NULL;
8380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // otherwise, creates an OsStackTraceGetter, makes it the current
8390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // getter, and returns it.
8400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  OsStackTraceGetterInterface* os_stack_trace_getter();
8410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the current OS stack trace as a String.
8430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
8440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The maximum number of stack frames to be included is specified by
8450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // the gtest_stack_trace_depth flag.  The skip_count parameter
8460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // specifies the number of top frames to be skipped, which doesn't
8470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // count against the number of frames to be included.
8480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
8490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // For example, if Foo() calls Bar(), which in turn calls
8500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // CurrentOsStackTraceExceptTop(1), Foo() will be included in the
8510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // trace but Bar() and CurrentOsStackTraceExceptTop() won't.
8520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  String CurrentOsStackTraceExceptTop(int skip_count);
8530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Finds and returns a TestCase with the given name.  If one doesn't
8550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // exist, creates one and returns it.
8560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
8570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Arguments:
8580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
8590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //   test_case_name: name of the test case
8600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //   set_up_tc:      pointer to the function that sets up the test case
8610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //   tear_down_tc:   pointer to the function that tears down the test case
8620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestCase* GetTestCase(const char* test_case_name,
8630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                        const char* comment,
8640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                        Test::SetUpTestCaseFunc set_up_tc,
8650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                        Test::TearDownTestCaseFunc tear_down_tc);
8660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Adds a TestInfo to the unit test.
8680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
8690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Arguments:
8700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
8710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //   set_up_tc:    pointer to the function that sets up the test case
8720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //   tear_down_tc: pointer to the function that tears down the test case
8730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //   test_info:    the TestInfo object
8740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc,
8750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                   Test::TearDownTestCaseFunc tear_down_tc,
8760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                   TestInfo * test_info) {
8770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // In order to support thread-safe death tests, we need to
8780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // remember the original working directory when the test program
8790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // was first invoked.  We cannot do this in RUN_ALL_TESTS(), as
8800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // the user may have changed the current directory before calling
8810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // RUN_ALL_TESTS().  Therefore we capture the current directory in
8820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // AddTestInfo(), which is called to register a TEST or TEST_F
8830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    // before main() is reached.
8840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    if (original_working_dir_.IsEmpty()) {
8850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      original_working_dir_.Set(FilePath::GetCurrentDir());
8860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      GTEST_CHECK_(!original_working_dir_.IsEmpty())
8870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath          << "Failed to get the current working directory.";
8880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    }
8890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    GetTestCase(test_info->test_case_name(),
8910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                test_info->test_case_comment(),
8920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                set_up_tc,
8930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                tear_down_tc)->AddTestInfo(test_info);
8940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
8950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
8960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if GTEST_HAS_PARAM_TEST
8970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns ParameterizedTestCaseRegistry object used to keep track of
8980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // value-parameterized tests and instantiate and register them.
8990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::ParameterizedTestCaseRegistry& parameterized_test_registry() {
9000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return parameterized_test_registry_;
9010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
9020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_HAS_PARAM_TEST
9030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Sets the TestCase object for the test that's currently running.
9050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void set_current_test_case(TestCase* current_test_case) {
9060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    current_test_case_ = current_test_case;
9070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
9080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Sets the TestInfo object for the test that's currently running.  If
9100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // current_test_info is NULL, the assertion results will be stored in
9110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // ad_hoc_test_result_.
9120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void set_current_test_info(TestInfo* current_test_info) {
9130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    current_test_info_ = current_test_info;
9140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
9150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Registers all parameterized tests defined using TEST_P and
9170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // INSTANTIATE_TEST_P, creating regular tests for each test/parameter
9180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // combination. This method can be called more then once; it has
9190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // guards protecting from registering the tests more then once.
9200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // If value-parameterized tests are disabled, RegisterParameterizedTests
9210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // is present but does nothing.
9220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void RegisterParameterizedTests();
9230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Runs all tests in this UnitTest object, prints the result, and
9250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // returns 0 if all tests are successful, or 1 otherwise.  If any
9260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // exception is thrown during a test on Windows, this test is
9270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // considered to be failed, but the rest of the tests will still be
9280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // run.  (We disable exceptions on Linux and Mac OS X, so the issue
9290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // doesn't apply there.)
9300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int RunAllTests();
9310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Clears the results of all tests, including the ad hoc test.
9330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void ClearResult() {
9340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    test_cases_.ForEach(TestCase::ClearTestCaseResult);
9350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    ad_hoc_test_result_.Clear();
9360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
9370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  enum ReactionToSharding {
9390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    HONOR_SHARDING_PROTOCOL,
9400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    IGNORE_SHARDING_PROTOCOL
9410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  };
9420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Matches the full name of each test against the user-specified
9440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // filter to decide whether the test should run, then records the
9450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // result in each TestCase and TestInfo object.
9460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests
9470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // based on sharding variables in the environment.
9480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the number of tests that should run.
9490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int FilterTests(ReactionToSharding shard_tests);
9500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Prints the names of the tests matching the user-specified filter flag.
9520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void ListTestsMatchingFilter();
9530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const TestCase* current_test_case() const { return current_test_case_; }
9550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestInfo* current_test_info() { return current_test_info_; }
9560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const TestInfo* current_test_info() const { return current_test_info_; }
9570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns the vector of environments that need to be set-up/torn-down
9590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // before/after the tests are run.
9600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Vector<Environment*>* environments() { return &environments_; }
9610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Vector<Environment*>* environments_in_reverse_order() {
9620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return &environments_in_reverse_order_;
9630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
9640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Getters for the per-thread Google Test trace stack.
9660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Vector<TraceInfo>* gtest_trace_stack() {
9670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return gtest_trace_stack_.pointer();
9680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
9690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const internal::Vector<TraceInfo>* gtest_trace_stack() const {
9700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return gtest_trace_stack_.pointer();
9710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
9720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if GTEST_HAS_DEATH_TEST
9740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void InitDeathTestSubprocessControlInfo() {
9750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
9760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
9770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns a pointer to the parsed --gtest_internal_run_death_test
9780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // flag, or NULL if that flag was not specified.
9790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // This information is useful only in a death test child process.
9800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Must not be called before a call to InitGoogleTest.
9810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const InternalRunDeathTestFlag* internal_run_death_test_flag() const {
9820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return internal_run_death_test_flag_.get();
9830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
9840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Returns a pointer to the current death test factory.
9860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::DeathTestFactory* death_test_factory() {
9870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return death_test_factory_.get();
9880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
9890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void SuppressTestEventsIfInSubprocess();
9910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  friend class ReplaceDeathTestFactory;
9930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_HAS_DEATH_TEST
9940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Initializes the event listener performing XML output as specified by
9960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // UnitTestOptions. Must not be called before InitGoogleTest.
9970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void ConfigureXmlOutput();
9980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
9990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Performs initialization dependent upon flag values obtained in
10000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // ParseGoogleTestFlagsOnly.  Is called from InitGoogleTest after the call to
10010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // ParseGoogleTestFlagsOnly.  In case a user neglects to call InitGoogleTest
10020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // this function is also called from RunAllTests.  Since this function can be
10030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // called more than once, it has to be idempotent.
10040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void PostFlagParsingInit();
10050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the random seed used at the start of the current test iteration.
10070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int random_seed() const { return random_seed_; }
10080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Gets the random number generator.
10100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Random* random() { return &random_; }
10110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Shuffles all test cases, and the tests within each test case,
10130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // making sure that death tests are still run first.
10140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void ShuffleTests();
10150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Restores the test cases and tests to their order before the first shuffle.
10170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void UnshuffleTests();
10180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
10200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  friend class ::testing::UnitTest;
10210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The UnitTest object that owns this implementation object.
10230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  UnitTest* const parent_;
10240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The working directory when the first TEST() or TEST_F() was
10260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // executed.
10270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::FilePath original_working_dir_;
10280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The default test part result reporters.
10300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
10310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  DefaultPerThreadTestPartResultReporter
10320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      default_per_thread_test_part_result_reporter_;
10330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Points to (but doesn't own) the global test part result reporter.
10350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestPartResultReporterInterface* global_test_part_result_repoter_;
10360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Protects read and write access to global_test_part_result_reporter_.
10380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Mutex global_test_part_result_reporter_mutex_;
10390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Points to (but doesn't own) the per-thread test part result reporter.
10410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::ThreadLocal<TestPartResultReporterInterface*>
10420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      per_thread_test_part_result_reporter_;
10430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The vector of environments that need to be set-up/torn-down
10450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // before/after the tests are run.  environments_in_reverse_order_
10460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // simply mirrors environments_ in reverse order.
10470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Vector<Environment*> environments_;
10480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Vector<Environment*> environments_in_reverse_order_;
10490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The vector of TestCases in their original order.  It owns the
10510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // elements in the vector.
10520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Vector<TestCase*> test_cases_;
10530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Provides a level of indirection for the test case list to allow
10550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // easy shuffling and restoring the test case order.  The i-th
10560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // element of this vector is the index of the i-th test case in the
10570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // shuffled order.
10580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Vector<int> test_case_indices_;
10590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if GTEST_HAS_PARAM_TEST
10610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // ParameterizedTestRegistry object used to register value-parameterized
10620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // tests.
10630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::ParameterizedTestCaseRegistry parameterized_test_registry_;
10640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Indicates whether RegisterParameterizedTests() has been called already.
10660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool parameterized_tests_registered_;
10670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_HAS_PARAM_TEST
10680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Index of the last death test case registered.  Initially -1.
10700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int last_death_test_case_;
10710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // This points to the TestCase for the currently running test.  It
10730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // changes as Google Test goes through one test case after another.
10740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // When no test is running, this is set to NULL and Google Test
10750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // stores assertion results in ad_hoc_test_result_.  Initially NULL.
10760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestCase* current_test_case_;
10770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // This points to the TestInfo for the currently running test.  It
10790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // changes as Google Test goes through one test after another.  When
10800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // no test is running, this is set to NULL and Google Test stores
10810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // assertion results in ad_hoc_test_result_.  Initially NULL.
10820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestInfo* current_test_info_;
10830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Normally, a user only writes assertions inside a TEST or TEST_F,
10850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // or inside a function called by a TEST or TEST_F.  Since Google
10860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Test keeps track of which test is current running, it can
10870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // associate such an assertion with the test it belongs to.
10880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  //
10890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // If an assertion is encountered when no TEST or TEST_F is running,
10900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Google Test attributes the assertion result to an imaginary "ad hoc"
10910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // test, and records the result in ad_hoc_test_result_.
10920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestResult ad_hoc_test_result_;
10930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The list of event listeners that can be used to track events inside
10950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Google Test.
10960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TestEventListeners listeners_;
10970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
10980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The OS stack trace getter.  Will be deleted when the UnitTest
10990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // object is destructed.  By default, an OsStackTraceGetter is used,
11000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // but the user can set this field to use a custom getter if that is
11010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // desired.
11020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  OsStackTraceGetterInterface* os_stack_trace_getter_;
11030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // True iff PostFlagParsingInit() has been called.
11050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  bool post_flag_parse_init_performed_;
11060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The random number seed used at the beginning of the test run.
11080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  int random_seed_;
11090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Our random number generator.
11110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::Random random_;
11120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // How long the test took to run, in milliseconds.
11140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  TimeInMillis elapsed_time_;
11150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if GTEST_HAS_DEATH_TEST
11170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // The decomposed components of the gtest_internal_run_death_test flag,
11180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // parsed when RUN_ALL_TESTS is called.
11190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::scoped_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
11200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::scoped_ptr<internal::DeathTestFactory> death_test_factory_;
11210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_HAS_DEATH_TEST
11220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // A per-thread stack of traces created by the SCOPED_TRACE() macro.
11240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  internal::ThreadLocal<internal::Vector<TraceInfo> > gtest_trace_stack_;
11250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);
11270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};  // class UnitTestImpl
11280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Convenience function for accessing the global UnitTest
11300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// implementation object.
11310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathinline UnitTestImpl* GetUnitTestImpl() {
11320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  return UnitTest::GetInstance()->impl();
11330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}
11340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Internal helper functions for implementing the simple regular
11360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// expression matcher.
11370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool IsInSet(char ch, const char* str);
11380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool IsDigit(char ch);
11390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool IsPunct(char ch);
11400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool IsRepeat(char ch);
11410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool IsWhiteSpace(char ch);
11420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool IsWordChar(char ch);
11430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool IsValidEscape(char ch);
11440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool AtomMatchesChar(bool escaped, char pattern, char ch);
11450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool ValidateRegex(const char* regex);
11460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool MatchRegexAtHead(const char* regex, const char* str);
11470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool MatchRepetitionAndRegexAtHead(
11480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    bool escaped, char ch, char repeat, const char* regex, const char* str);
11490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool MatchRegexAnywhere(const char* regex, const char* str);
11500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Parses the command line for Google Test flags, without initializing
11520a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// other parts of Google Test.
11530a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathvoid ParseGoogleTestFlagsOnly(int* argc, char** argv);
11540a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathvoid ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv);
11550a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11560a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if GTEST_HAS_DEATH_TEST
11570a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11580a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Returns the message describing the last system error, regardless of the
11590a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// platform.
11600a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan KamathString GetLastErrnoDescription();
11610a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11620a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if GTEST_OS_WINDOWS
11630a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Provides leak-safe Windows kernel handle ownership.
11640a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass AutoHandle {
11650a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
11660a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  AutoHandle() : handle_(INVALID_HANDLE_VALUE) {}
11670a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  explicit AutoHandle(HANDLE handle) : handle_(handle) {}
11680a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11690a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  ~AutoHandle() { Reset(); }
11700a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11710a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  HANDLE Get() const { return handle_; }
11720a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void Reset() { Reset(INVALID_HANDLE_VALUE); }
11730a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  void Reset(HANDLE handle) {
11740a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    if (handle != handle_) {
11750a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      if (handle_ != INVALID_HANDLE_VALUE)
11760a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath        ::CloseHandle(handle_);
11770a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      handle_ = handle;
11780a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    }
11790a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
11800a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11810a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath private:
11820a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  HANDLE handle_;
11830a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11840a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
11850a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
11860a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_OS_WINDOWS
11870a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
11880a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Attempts to parse a string into a positive integer pointed to by the
11890a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// number parameter.  Returns true if that is possible.
11900a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use
11910a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// it here.
11920a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathtemplate <typename Integer>
11930a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathbool ParseNaturalNumber(const ::std::string& str, Integer* number) {
11940a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // Fail fast if the given string does not begin with a digit;
11950a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // this bypasses strtoXXX's "optional leading whitespace and plus
11960a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // or minus sign" semantics, which are undesirable here.
11970a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  if (str.empty() || !isdigit(str[0])) {
11980a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return false;
11990a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
12000a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  errno = 0;
12010a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
12020a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  char* end;
12030a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // BiggestConvertible is the largest integer type that system-provided
12040a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // string-to-number conversion routines can return.
12050a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#if GTEST_OS_WINDOWS && !defined(__GNUC__)
12060a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // MSVC and C++ Builder define __int64 instead of the standard long long.
12070a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  typedef unsigned __int64 BiggestConvertible;
12080a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10);
12090a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#else
12100a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  typedef unsigned long long BiggestConvertible;  // NOLINT
12110a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
12120a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_OS_WINDOWS && !defined(__GNUC__)
12130a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const bool parse_success = *end == '\0' && errno == 0;
12140a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
12150a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // TODO(vladl@google.com): Convert this to compile time assertion when it is
12160a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  // available.
12170a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed));
12180a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
12190a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  const Integer result = static_cast<Integer>(parsed);
12200a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
12210a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    *number = result;
12220a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return true;
12230a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
12240a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  return false;
12250a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}
12260a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_HAS_DEATH_TEST
12270a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
12280a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// TestResult contains some private methods that should be hidden from
12290a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// Google Test user but are required for testing. This class allow our tests
12300a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath// to access them.
12310a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamathclass TestResultAccessor {
12320a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath public:
12330a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static void RecordProperty(TestResult* test_result,
12340a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath                             const TestProperty& property) {
12350a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    test_result->RecordProperty(property);
12360a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
12370a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
12380a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static void ClearTestPartResults(TestResult* test_result) {
12390a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    test_result->ClearTestPartResults();
12400a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
12410a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
12420a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  static const Vector<testing::TestPartResult>& test_part_results(
12430a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath      const TestResult& test_result) {
12440a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath    return test_result.test_part_results();
12450a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath  }
12460a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath};
12470a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
12480a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}  // namespace internal
12490a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath}  // namespace testing
12500a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath
12510a58c5c2f73e5047b36f12b5f12b12d6f2a9f69dNarayan Kamath#endif  // GTEST_SRC_GTEST_INTERNAL_INL_H_
1252