1fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Copyright 2005, Google Inc.
2fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// All rights reserved.
3fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
4fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Redistribution and use in source and binary forms, with or without
5fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// modification, are permitted provided that the following conditions are
6fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// met:
7fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
8fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions of source code must retain the above copyright
9fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// notice, this list of conditions and the following disclaimer.
10fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Redistributions in binary form must reproduce the above
11fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// copyright notice, this list of conditions and the following disclaimer
12fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// in the documentation and/or other materials provided with the
13fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// distribution.
14fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//     * Neither the name of Google Inc. nor the names of its
15fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// contributors may be used to endorse or promote products derived from
16fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// this software without specific prior written permission.
17fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
18fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
30fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Utility functions and classes used by the Google C++ testing framework.
31fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
32fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Author: wan@google.com (Zhanyong Wan)
33fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
34fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This file contains purely Google Test's internal implementation.  Please
35fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// DO NOT #INCLUDE IT IN A USER PROGRAM.
36fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
37fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
38fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#define GTEST_SRC_GTEST_INTERNAL_INL_H_
39fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
40fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// GTEST_IMPLEMENTATION_ is defined to 1 iff the current translation unit is
41fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// part of Google Test's implementation; otherwise it's undefined.
42fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if !GTEST_IMPLEMENTATION_
43fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A user is trying to include this from his code - just say no.
44fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#error "gtest-internal-inl.h is part of Google Test's internal implementation."
45fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#error "It must not be included except by Google Test itself."
46fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_IMPLEMENTATION_
47fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
48fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#ifndef _WIN32_WCE
49fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <errno.h>
50fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // !_WIN32_WCE
51fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <stddef.h>
52fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <stdlib.h>  // For strtoll/_strtoul64/malloc/free.
53fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <string.h>  // For memmove.
54fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
55fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <string>
56fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
57fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/internal/gtest-port.h>
58fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
59fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if GTEST_OS_WINDOWS
60fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <windows.h>  // For DWORD.
61fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_OS_WINDOWS
62fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
63fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/gtest.h>
64fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#include <gtest/gtest-spi.h>
65fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
66fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace testing {
67fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
68fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Declares the flags.
69fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
70fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// We don't want the users to modify this flag in the code, but want
71fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Google Test's own unit tests to be able to access it. Therefore we
72fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// declare it here as opposed to in gtest.h.
73fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleGTEST_DECLARE_bool_(death_test_use_fork);
74fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
75fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillenamespace internal {
76fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
77fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The value of GetTestTypeId() as seen from within the Google Test
78fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// library.  This is solely for testing GetTestTypeId().
79fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleextern const TypeId kTestTypeIdInGoogleTest;
80fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
81fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Names of the flags (needed for parsing Google Test flags).
82fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests";
83fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kBreakOnFailureFlag[] = "break_on_failure";
84fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kCatchExceptionsFlag[] = "catch_exceptions";
85fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kColorFlag[] = "color";
86fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kFilterFlag[] = "filter";
87fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kListTestsFlag[] = "list_tests";
88fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kOutputFlag[] = "output";
89fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kPrintTimeFlag[] = "print_time";
90fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kRandomSeedFlag[] = "random_seed";
91fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kRepeatFlag[] = "repeat";
92fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kShuffleFlag[] = "shuffle";
93fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst char kThrowOnFailureFlag[] = "throw_on_failure";
94fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
95fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A valid random seed must be in [1, kMaxRandomSeed].
96fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleconst int kMaxRandomSeed = 99999;
97fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
98fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns the current time in milliseconds.
99fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleTimeInMillis GetTimeInMillis();
100fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
101fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns a random seed in range [1, kMaxRandomSeed] based on the
102fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// given --gtest_random_seed flag value.
103fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline int GetRandomSeedFromFlag(Int32 random_seed_flag) {
104fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const unsigned int raw_seed = (random_seed_flag == 0) ?
105fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      static_cast<unsigned int>(GetTimeInMillis()) :
106fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      static_cast<unsigned int>(random_seed_flag);
107fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
108fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Normalizes the actual seed to range [1, kMaxRandomSeed] such that
109fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // it's easy to type.
110fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const int normalized_seed =
111fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      static_cast<int>((raw_seed - 1U) %
112fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                       static_cast<unsigned int>(kMaxRandomSeed)) + 1;
113fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return normalized_seed;
114fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
115fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
116fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns the first valid random seed after 'seed'.  The behavior is
117fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// undefined if 'seed' is invalid.  The seed after kMaxRandomSeed is
118fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// considered to be 1.
119fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleinline int GetNextRandomSeed(int seed) {
120fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)
121fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      << "Invalid random seed " << seed << " - must be in [1, "
122fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      << kMaxRandomSeed << "].";
123fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const int next_seed = seed + 1;
124fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
125fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
126fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
127fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This class saves the values of all Google Test flags in its c'tor, and
128fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// restores them in its d'tor.
129fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass GTestFlagSaver {
130fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
131fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The c'tor.
132fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTestFlagSaver() {
133fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests);
134fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    break_on_failure_ = GTEST_FLAG(break_on_failure);
135fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    catch_exceptions_ = GTEST_FLAG(catch_exceptions);
136fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    color_ = GTEST_FLAG(color);
137fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    death_test_style_ = GTEST_FLAG(death_test_style);
138fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    death_test_use_fork_ = GTEST_FLAG(death_test_use_fork);
139fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    filter_ = GTEST_FLAG(filter);
140fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
141fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    list_tests_ = GTEST_FLAG(list_tests);
142fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    output_ = GTEST_FLAG(output);
143fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    print_time_ = GTEST_FLAG(print_time);
144fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    random_seed_ = GTEST_FLAG(random_seed);
145fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    repeat_ = GTEST_FLAG(repeat);
146fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    shuffle_ = GTEST_FLAG(shuffle);
147fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    throw_on_failure_ = GTEST_FLAG(throw_on_failure);
148fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
149fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
150fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The d'tor is not virtual.  DO NOT INHERIT FROM THIS CLASS.
151fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  ~GTestFlagSaver() {
152fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
153fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(break_on_failure) = break_on_failure_;
154fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(catch_exceptions) = catch_exceptions_;
155fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(color) = color_;
156fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(death_test_style) = death_test_style_;
157fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
158fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(filter) = filter_;
159fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
160fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(list_tests) = list_tests_;
161fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(output) = output_;
162fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(print_time) = print_time_;
163fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(random_seed) = random_seed_;
164fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(repeat) = repeat_;
165fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(shuffle) = shuffle_;
166fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_FLAG(throw_on_failure) = throw_on_failure_;
167fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
168fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
169fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Fields for saving the original values of flags.
170fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool also_run_disabled_tests_;
171fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool break_on_failure_;
172fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool catch_exceptions_;
173fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  String color_;
174fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  String death_test_style_;
175fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool death_test_use_fork_;
176fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  String filter_;
177fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  String internal_run_death_test_;
178fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool list_tests_;
179fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  String output_;
180fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool print_time_;
181fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool pretty_;
182fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  internal::Int32 random_seed_;
183fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  internal::Int32 repeat_;
184fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool shuffle_;
185fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool throw_on_failure_;
186fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville} GTEST_ATTRIBUTE_UNUSED_;
187fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
188fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Converts a Unicode code point to a narrow string in UTF-8 encoding.
189fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// code_point parameter is of type UInt32 because wchar_t may not be
190fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// wide enough to contain a code point.
191fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The output buffer str must containt at least 32 characters.
192fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The function returns the address of the output buffer.
193fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// If the code_point is not a valid Unicode code point
194fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be output
195fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// as '(Invalid Unicode 0xXXXXXXXX)'.
196fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillechar* CodePointToUtf8(UInt32 code_point, char* str);
197fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
198fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Converts a wide string to a narrow string in UTF-8 encoding.
199fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The wide string is assumed to have the following encoding:
200fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS)
201fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//   UTF-32 if sizeof(wchar_t) == 4 (on Linux)
202fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Parameter str points to a null-terminated wide string.
203fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Parameter num_chars may additionally limit the number
204fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// of wchar_t characters processed. -1 is used when the entire string
205fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// should be processed.
206fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// If the string contains code points that are not valid Unicode code points
207fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output
208fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding
209fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// and contains invalid UTF-16 surrogate pairs, values in those pairs
210fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// will be encoded as individual Unicode characters from Basic Normal Plane.
211fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleString WideStringToUtf8(const wchar_t* str, int num_chars);
212fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
213fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns the number of active threads, or 0 when there is an error.
214fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillesize_t GetThreadCount();
215fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
216fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file
217fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// if the variable is present. If a file already exists at this location, this
218fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// function will write over it. If the variable is present, but the file cannot
219fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// be created, prints an error and exits.
220fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillevoid WriteToShardStatusFileIfNeeded();
221fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
222fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Checks whether sharding is enabled by examining the relevant
223fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// environment variable values. If the variables are present,
224fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// but inconsistent (e.g., shard_index >= total_shards), prints
225fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// an error and exits. If in_subprocess_for_death_test, sharding is
226fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// disabled because it must only be applied to the original test
227fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// process. Otherwise, we could filter out death tests we intended to execute.
228fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillebool ShouldShard(const char* total_shards_str, const char* shard_index_str,
229fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                 bool in_subprocess_for_death_test);
230fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
231fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Parses the environment variable var as an Int32. If it is unset,
232fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// returns default_val. If it is not an Int32, prints an error and
233fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// and aborts.
234fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleInt32 Int32FromEnvOrDie(const char* env_var, Int32 default_val);
235fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
236fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Given the total number of shards, the shard index, and the test id,
237fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// returns true iff the test should be run on this shard. The test id is
238fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// some arbitrary but unique non-negative integer assigned to each test
239fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// method. Assumes that 0 <= shard_index < total_shards.
240fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillebool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id);
241fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
242fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Vector is an ordered container that supports random access to the
243fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// elements.
244fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
245fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// We cannot use std::vector, as Visual C++ 7.1's implementation of
246fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// STL has problems compiling when exceptions are disabled.  There is
247fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// a hack to work around the problems, but we've seen cases where the
248fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// hack fails to work.
249fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
250fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The element type must support copy constructor and operator=.
251fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename E>  // E is the element type.
252fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass Vector {
253fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
254fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Creates an empty Vector.
255fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  Vector() : elements_(NULL), capacity_(0), size_(0) {}
256fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
257fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // D'tor.
258fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual ~Vector() { Clear(); }
259fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
260fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Clears the Vector.
261fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void Clear() {
262fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (elements_ != NULL) {
263fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      for (int i = 0; i < size_; i++) {
264fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        delete elements_[i];
265fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      }
266fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
267fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      free(elements_);
268fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      elements_ = NULL;
269fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      capacity_ = size_ = 0;
270fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
271fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
272fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
273fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the number of elements.
274fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int size() const { return size_; }
275fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
276fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Adds an element to the end of the Vector.  A copy of the element
277fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // is created using the copy constructor, and then stored in the
278fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Vector.  Changes made to the element in the Vector doesn't affect
279fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // the source object, and vice versa.
280d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  void PushBack(const E& element) { Insert(element, size_); }
281fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
282fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Adds an element to the beginning of this Vector.
283fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void PushFront(const E& element) { Insert(element, 0); }
284fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
285fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Removes an element from the beginning of this Vector.  If the
286fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // result argument is not NULL, the removed element is stored in the
287fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // memory it points to.  Otherwise the element is thrown away.
288fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true iff the vector wasn't empty before the operation.
289fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool PopFront(E* result) {
290fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (size_ == 0)
291fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      return false;
292fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
293fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (result != NULL)
294fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      *result = GetElement(0);
295fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
296fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    Erase(0);
297fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return true;
298fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
299fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
300fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Inserts an element at the given index.  It's the caller's
301fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // responsibility to ensure that the given index is in the range [0,
302fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // size()].
303fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void Insert(const E& element, int index) {
304fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GrowIfNeeded();
305fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    MoveElements(index, size_ - index, index + 1);
306fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    elements_[index] = new E(element);
307fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    size_++;
308fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
309fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
310fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Erases the element at the specified index, or aborts the program if the
311fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // index is not in range [0, size()).
312fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void Erase(int index) {
313fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_CHECK_(0 <= index && index < size_)
314fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        << "Invalid Vector index " << index << ": must be in range [0, "
315fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        << (size_ - 1) << "].";
316fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
317fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    delete elements_[index];
318fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    MoveElements(index + 1, size_ - index - 1, index);
319fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    size_--;
320fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
321fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
322fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the number of elements that satisfy a given predicate.
323fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The parameter 'predicate' is a Boolean function or functor that
324fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // accepts a 'const E &', where E is the element type.
325fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  template <typename P>  // P is the type of the predicate function/functor
326fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int CountIf(P predicate) const {
327fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    int count = 0;
328fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    for (int i = 0; i < size_; i++) {
329fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      if (predicate(*(elements_[i]))) {
330fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        count++;
331fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      }
332fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
333fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
334fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return count;
335fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
336fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
337fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Applies a function/functor to each element in the Vector.  The
338fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // parameter 'functor' is a function/functor that accepts a 'const
339fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // E &', where E is the element type.  This method does not change
340fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // the elements.
341fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  template <typename F>  // F is the type of the function/functor
342fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void ForEach(F functor) const {
343fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    for (int i = 0; i < size_; i++) {
344fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      functor(*(elements_[i]));
345fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
346fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
347fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
348fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the first node whose element satisfies a given predicate,
349fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // or NULL if none is found.  The parameter 'predicate' is a
350fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // function/functor that accepts a 'const E &', where E is the
351fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // element type.  This method does not change the elements.
352fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  template <typename P>  // P is the type of the predicate function/functor.
353fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const E* FindIf(P predicate) const {
354fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    for (int i = 0; i < size_; i++) {
355fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      if (predicate(*elements_[i])) {
356fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        return elements_[i];
357fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      }
358fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
359fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return NULL;
360fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
361fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
362fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  template <typename P>
363fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  E* FindIf(P predicate) {
364fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    for (int i = 0; i < size_; i++) {
365fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      if (predicate(*elements_[i])) {
366fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        return elements_[i];
367fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      }
368fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    }
369fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return NULL;
370fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
371fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
372d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Returns the i-th element of the Vector, or aborts the program if i
373fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // is not in range [0, size()).
374fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const E& GetElement(int i) const {
375fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_CHECK_(0 <= i && i < size_)
376fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        << "Invalid Vector index " << i << ": must be in range [0, "
377fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        << (size_ - 1) << "].";
378fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
379fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return *(elements_[i]);
380fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
381fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
382d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Returns a mutable reference to the i-th element of the Vector, or
383d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // aborts the program if i is not in range [0, size()).
384d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  E& GetMutableElement(int i) {
385d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    GTEST_CHECK_(0 <= i && i < size_)
386d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        << "Invalid Vector index " << i << ": must be in range [0, "
387d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        << (size_ - 1) << "].";
388d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
389d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    return *(elements_[i]);
390d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  }
391d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
392d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Returns the i-th element of the Vector, or default_value if i is not
393fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // in range [0, size()).
394fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  E GetElementOr(int i, E default_value) const {
395fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return (i < 0 || i >= size_) ? default_value : *(elements_[i]);
396fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
397fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
398d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Swaps the i-th and j-th elements of the Vector.  Crashes if i or
399d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // j is invalid.
400d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  void Swap(int i, int j) {
401d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    GTEST_CHECK_(0 <= i && i < size_)
402d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        << "Invalid first swap element " << i << ": must be in range [0, "
403d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        << (size_ - 1) << "].";
404d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    GTEST_CHECK_(0 <= j && j < size_)
405d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        << "Invalid second swap element " << j << ": must be in range [0, "
406d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        << (size_ - 1) << "].";
407d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
408d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    E* const temp = elements_[i];
409d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    elements_[i] = elements_[j];
410d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    elements_[j] = temp;
411d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  }
412d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
413d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Performs an in-place shuffle of a range of this Vector's nodes.
414d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // 'begin' and 'end' are element indices as an STL-style range;
415d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // i.e. [begin, end) are shuffled, where 'end' == size() means to
416d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // shuffle to the end of the Vector.
417d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  void ShuffleRange(internal::Random* random, int begin, int end) {
418d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    GTEST_CHECK_(0 <= begin && begin <= size_)
419d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        << "Invalid shuffle range start " << begin << ": must be in range [0, "
420d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        << size_ << "].";
421d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    GTEST_CHECK_(begin <= end && end <= size_)
422d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        << "Invalid shuffle range finish " << end << ": must be in range ["
423d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        << begin << ", " << size_ << "].";
424d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
425d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    // Fisher-Yates shuffle, from
426d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
427d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    for (int range_width = end - begin; range_width >= 2; range_width--) {
428d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville      const int last_in_range = begin + range_width - 1;
429d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville      const int selected = begin + random->Generate(range_width);
430d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville      Swap(selected, last_in_range);
431d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    }
432d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  }
433d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
434d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Performs an in-place shuffle of this Vector's nodes.
435d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  void Shuffle(internal::Random* random) {
436d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    ShuffleRange(random, 0, size());
437d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  }
438d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
439d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Returns a copy of this Vector.
440d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  Vector* Clone() const {
441d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    Vector* const clone = new Vector;
442d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    clone->Reserve(size_);
443d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    for (int i = 0; i < size_; i++) {
444d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville      clone->PushBack(GetElement(i));
445d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    }
446d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    return clone;
447d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  }
448d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
449fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
450d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Makes sure this Vector's capacity is at least the given value.
451d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  void Reserve(int new_capacity) {
452d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    if (new_capacity <= capacity_)
453d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville      return;
454d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
455d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    capacity_ = new_capacity;
456d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    elements_ = static_cast<E**>(
457d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville        realloc(elements_, capacity_*sizeof(elements_[0])));
458d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  }
459d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
460fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Grows the buffer if it is not big enough to hold one more element.
461fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void GrowIfNeeded() {
462fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (size_ < capacity_)
463fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      return;
464fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
465fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // Exponential bump-up is necessary to ensure that inserting N
466fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // elements is O(N) instead of O(N^2).  The factor 3/2 means that
467fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // no more than 1/3 of the slots are wasted.
468fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    const int new_capacity = 3*(capacity_/2 + 1);
469fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    GTEST_CHECK_(new_capacity > capacity_)  // Does the new capacity overflow?
470fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville        << "Cannot grow a Vector with " << capacity_ << " elements already.";
471d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    Reserve(new_capacity);
472fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
473fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
474fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Moves the give consecutive elements to a new index in the Vector.
475fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void MoveElements(int source, int count, int dest) {
476fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    memmove(elements_ + dest, elements_ + source, count*sizeof(elements_[0]));
477fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
478fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
479fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  E** elements_;
480fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int capacity_;  // The number of elements allocated for elements_.
481fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int size_;      // The number of elements; in the range [0, capacity_].
482fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
483fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // We disallow copying Vector.
484fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_DISALLOW_COPY_AND_ASSIGN_(Vector);
485fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};  // class Vector
486fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
487fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A function for deleting an object.  Handy for being used as a
488fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// functor.
489fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilletemplate <typename T>
490fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillestatic void Delete(T * x) {
491fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  delete x;
492fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville}
493fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
494fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A predicate that checks the key of a TestProperty against a known key.
495fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
496fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// TestPropertyKeyIs is copyable.
497fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TestPropertyKeyIs {
498fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
499fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Constructor.
500fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
501fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // TestPropertyKeyIs has NO default constructor.
502fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  explicit TestPropertyKeyIs(const char* key)
503fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      : key_(key) {}
504fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
505fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true iff the test name of test property matches on key_.
506fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool operator()(const TestProperty& test_property) const {
507fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return String(test_property.key()).Compare(key_) == 0;
508fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
509fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
510fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
511fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  String key_;
512fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
513fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
514fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass TestInfoImpl {
515fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
516fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TestInfoImpl(TestInfo* parent, const char* test_case_name,
517fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville               const char* name, const char* test_case_comment,
518fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville               const char* comment, TypeId fixture_class_id,
519fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville               internal::TestFactoryBase* factory);
520fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  ~TestInfoImpl();
521fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
522fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true if this test should run.
523fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool should_run() const { return should_run_; }
524fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
525fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Sets the should_run member.
526fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void set_should_run(bool should) { should_run_ = should; }
527fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
528fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true if this test is disabled. Disabled tests are not run.
529fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool is_disabled() const { return is_disabled_; }
530fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
531fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Sets the is_disabled member.
532fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void set_is_disabled(bool is) { is_disabled_ = is; }
533fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
534fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true if this test matches the filter specified by the user.
535fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool matches_filter() const { return matches_filter_; }
536fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
537fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Sets the matches_filter member.
538fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void set_matches_filter(bool matches) { matches_filter_ = matches; }
539fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
540fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the test case name.
541fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const char* test_case_name() const { return test_case_name_.c_str(); }
542fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
543fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the test name.
544fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const char* name() const { return name_.c_str(); }
545fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
546fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the test case comment.
547fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const char* test_case_comment() const { return test_case_comment_.c_str(); }
548fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
549fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the test comment.
550fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const char* comment() const { return comment_.c_str(); }
551fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
552fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the ID of the test fixture class.
553fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TypeId fixture_class_id() const { return fixture_class_id_; }
554fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
555fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the test result.
556fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TestResult* result() { return &result_; }
557fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const TestResult* result() const { return &result_; }
558fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
559fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Creates the test object, runs it, records its result, and then
560fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // deletes it.
561fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void Run();
562fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
563fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Clears the test result.
564fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void ClearResult() { result_.Clear(); }
565fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
566fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Clears the test result in the given TestInfo object.
567fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static void ClearTestResult(TestInfo * test_info) {
568fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    test_info->impl()->ClearResult();
569fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
570fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
571fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
572fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // These fields are immutable properties of the test.
573fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TestInfo* const parent_;          // The owner of this object
574fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const String test_case_name_;     // Test case name
575fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const String name_;               // Test name
576fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const String test_case_comment_;  // Test case comment
577fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const String comment_;            // Test comment
578fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const TypeId fixture_class_id_;   // ID of the test fixture class
579fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool should_run_;                 // True iff this test should run
580fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool is_disabled_;                // True iff this test is disabled
581fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool matches_filter_;             // True if this test matches the
582fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                                    // user-specified filter.
583fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  internal::TestFactoryBase* const factory_;  // The factory that creates
584fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                                              // the test object
585fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
586fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // This field is mutable and needs to be reset before running the
587fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // test for the second time.
588fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TestResult result_;
589fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
590fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfoImpl);
591fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
592fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
593fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Class UnitTestOptions.
594fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
595fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This class contains functions for processing options the user
596fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// specifies when running the tests.  It has only static members.
597fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville//
598fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// In most cases, the user can specify an option using either an
599fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// environment variable or a command line flag.  E.g. you can set the
600fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// test filter using either GTEST_FILTER or --gtest_filter.  If both
601fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// the variable and the flag are present, the latter overrides the
602fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// former.
603fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass UnitTestOptions {
604fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
605fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Functions for processing the gtest_output flag.
606fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
607fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the output format, or "" for normal printed output.
608fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static String GetOutputFormat();
609fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
610fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the absolute path of the requested output file, or the
611fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // default (test_detail.xml in the original working directory) if
612fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // none was explicitly specified.
613fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static String GetAbsolutePathToOutputFile();
614fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
615fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Functions for processing the gtest_filter flag.
616fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
617fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true iff the wildcard pattern matches the string.  The
618fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // first ':' or '\0' character in pattern marks the end of it.
619fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
620fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // This recursive algorithm isn't very efficient, but is clear and
621fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // works well enough for matching test names, which are short.
622fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static bool PatternMatchesString(const char *pattern, const char *str);
623fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
624fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true iff the user-specified filter matches the test case
625fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // name and the test name.
626fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static bool FilterMatchesTest(const String &test_case_name,
627fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                                const String &test_name);
628fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
629fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#if GTEST_OS_WINDOWS
630fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Function for supporting the gtest_catch_exception flag.
631fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
632fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
633fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
634fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // This function is useful as an __except condition.
635fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static int GTestShouldProcessSEH(DWORD exception_code);
636fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville#endif  // GTEST_OS_WINDOWS
637fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
638fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true if "name" matches the ':' separated list of glob-style
639fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // filters in "filter".
640fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static bool MatchesFilter(const String& name, const char* filter);
641fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
642fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
643fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Returns the current application's name, removing directory path if that
644fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// is present.  Used by UnitTestOptions::GetOutputFile.
645fbaaef999ba563838ebd00874ed8a1c01fbf286dWink SavilleFilePath GetCurrentExecutableName();
646fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
647fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The role interface for getting the OS stack trace as a string.
648fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass OsStackTraceGetterInterface {
649fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
650fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  OsStackTraceGetterInterface() {}
651fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual ~OsStackTraceGetterInterface() {}
652fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
653fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the current OS stack trace as a String.  Parameters:
654fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
655fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   max_depth  - the maximum number of stack frames to be included
656fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //                in the trace.
657fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   skip_count - the number of top frames to be skipped; doesn't count
658fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //                against max_depth.
659fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual String CurrentStackTrace(int max_depth, int skip_count) = 0;
660fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
661fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // UponLeavingGTest() should be called immediately before Google Test calls
662fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // user code. It saves some information about the current stack that
663fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // CurrentStackTrace() will use to find and hide Google Test stack frames.
664fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual void UponLeavingGTest() = 0;
665fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
666fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
667fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface);
668fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
669fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
670fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// A working implementation of the OsStackTraceGetterInterface interface.
671fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass OsStackTraceGetter : public OsStackTraceGetterInterface {
672fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
673d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  OsStackTraceGetter() : caller_frame_(NULL) {}
674fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual String CurrentStackTrace(int max_depth, int skip_count);
675fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual void UponLeavingGTest();
676fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
677fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // This string is inserted in place of stack frames that are part of
678fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Google Test's implementation.
679fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  static const char* const kElidedFramesMarker;
680fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
681fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
682fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  Mutex mutex_;  // protects all internal state
683fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
684fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // We save the stack frame below the frame that calls user code.
685fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // We do this because the address of the frame immediately below
686fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // the user code changes between the call to UponLeavingGTest()
687fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // and any calls to CurrentStackTrace() from within the user code.
688fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void* caller_frame_;
689fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
690fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter);
691fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
692fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
693fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// Information about a Google Test trace point.
694fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savillestruct TraceInfo {
695fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const char* file;
696fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int line;
697fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  String message;
698fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
699fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
700fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This is the default global test part result reporter used in UnitTestImpl.
701fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This class should only be used by UnitTestImpl.
702fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass DefaultGlobalTestPartResultReporter
703fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  : public TestPartResultReporterInterface {
704fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
705fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
706fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Implements the TestPartResultReporterInterface. Reports the test part
707fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // result in the current test.
708fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual void ReportTestPartResult(const TestPartResult& result);
709fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
710fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
711fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  UnitTestImpl* const unit_test_;
712fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
713fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);
714fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
715fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
716fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// This is the default per thread test part result reporter used in
717fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// UnitTestImpl. This class should only be used by UnitTestImpl.
718fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass DefaultPerThreadTestPartResultReporter
719fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    : public TestPartResultReporterInterface {
720fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
721fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
722fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Implements the TestPartResultReporterInterface. The implementation just
723fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // delegates to the current global test part result reporter of *unit_test_.
724fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual void ReportTestPartResult(const TestPartResult& result);
725fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
726fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville private:
727fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  UnitTestImpl* const unit_test_;
728fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
729fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter);
730fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville};
731fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
732fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// The private implementation of the UnitTest class.  We don't protect
733fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// the methods under a mutex, as this class is not accessible by a
734fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// user and the UnitTest class that delegates work to this class does
735fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville// proper locking.
736fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Savilleclass UnitTestImpl {
737fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville public:
738fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  explicit UnitTestImpl(UnitTest* parent);
739fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  virtual ~UnitTestImpl();
740fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
741fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // There are two different ways to register your own TestPartResultReporter.
742fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // You can register your own repoter to listen either only for test results
743fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // from the current thread or for results from all threads.
744fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // By default, each per-thread test result repoter just passes a new
745fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // TestPartResult to the global test result reporter, which registers the
746fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // test part result for the currently running test.
747fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
748fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the global test part result reporter.
749fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
750fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
751fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Sets the global test part result reporter.
752fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void SetGlobalTestPartResultReporter(
753fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      TestPartResultReporterInterface* reporter);
754fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
755fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the test part result reporter for the current thread.
756fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
757fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
758fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Sets the test part result reporter for the current thread.
759fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void SetTestPartResultReporterForCurrentThread(
760fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      TestPartResultReporterInterface* reporter);
761fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
762fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the number of successful test cases.
763fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int successful_test_case_count() const;
764fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
765fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the number of failed test cases.
766fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int failed_test_case_count() const;
767fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
768fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the number of all test cases.
769fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int total_test_case_count() const;
770fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
771fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the number of all test cases that contain at least one test
772fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // that should run.
773fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int test_case_to_run_count() const;
774fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
775fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the number of successful tests.
776fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int successful_test_count() const;
777fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
778fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the number of failed tests.
779fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int failed_test_count() const;
780fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
781fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the number of disabled tests.
782fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int disabled_test_count() const;
783fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
784fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the number of all tests.
785fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int total_test_count() const;
786fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
787fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the number of tests that should run.
788fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  int test_to_run_count() const;
789fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
790fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the elapsed time, in milliseconds.
791fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TimeInMillis elapsed_time() const { return elapsed_time_; }
792fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
793fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true iff the unit test passed (i.e. all test cases passed).
794fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool Passed() const { return !Failed(); }
795fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
796fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns true iff the unit test failed (i.e. some test case failed
797fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // or something outside of all tests failed).
798fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  bool Failed() const {
799fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
800fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
801fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
802fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Gets the i-th test case among all the test cases. i can range from 0 to
803fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // total_test_case_count() - 1. If i is not in that range, returns NULL.
804fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const TestCase* GetTestCase(int i) const {
805d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    const int index = test_case_indices_.GetElementOr(i, -1);
806d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    return index < 0 ? NULL : test_cases_.GetElement(i);
807d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  }
808d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
809d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Gets the i-th test case among all the test cases. i can range from 0 to
810d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // total_test_case_count() - 1. If i is not in that range, returns NULL.
811d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  TestCase* GetMutableTestCase(int i) {
812d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    const int index = test_case_indices_.GetElementOr(i, -1);
813d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville    return index < 0 ? NULL : test_cases_.GetElement(index);
814fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  }
815fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
816d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  // Provides access to the event listener list.
817d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville  TestEventListeners* listeners() { return &listeners_; }
818d0332953cda33fb4f8e24ebff9c49159b69c43d6Wink Saville
819fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the TestResult for the test that's currently running, or
820fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // the TestResult for the ad hoc test if no test is running.
821fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TestResult* current_test_result();
822fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
823fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the TestResult for the ad hoc test.
824fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
825fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
826fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Sets the OS stack trace getter.
827fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
828fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Does nothing if the input and the current OS stack trace getter
829fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // are the same; otherwise, deletes the old getter and makes the
830fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // input the current getter.
831fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
832fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
833fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the current OS stack trace getter if it is not NULL;
834fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // otherwise, creates an OsStackTraceGetter, makes it the current
835fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // getter, and returns it.
836fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  OsStackTraceGetterInterface* os_stack_trace_getter();
837fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
838fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Returns the current OS stack trace as a String.
839fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
840fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // The maximum number of stack frames to be included is specified by
841fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // the gtest_stack_trace_depth flag.  The skip_count parameter
842fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // specifies the number of top frames to be skipped, which doesn't
843fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // count against the number of frames to be included.
844fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
845fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // For example, if Foo() calls Bar(), which in turn calls
846fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // CurrentOsStackTraceExceptTop(1), Foo() will be included in the
847fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // trace but Bar() and CurrentOsStackTraceExceptTop() won't.
848fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  String CurrentOsStackTraceExceptTop(int skip_count);
849fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
850fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Finds and returns a TestCase with the given name.  If one doesn't
851fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // exist, creates one and returns it.
852fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
853fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Arguments:
854fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
855fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   test_case_name: name of the test case
856fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   set_up_tc:      pointer to the function that sets up the test case
857fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   tear_down_tc:   pointer to the function that tears down the test case
858fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  TestCase* GetTestCase(const char* test_case_name,
859fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                        const char* comment,
860fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                        Test::SetUpTestCaseFunc set_up_tc,
861fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                        Test::TearDownTestCaseFunc tear_down_tc);
862fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville
863fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Adds a TestInfo to the unit test.
864fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
865fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  // Arguments:
866fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //
867fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   set_up_tc:    pointer to the function that sets up the test case
868fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   tear_down_tc: pointer to the function that tears down the test case
869fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  //   test_info:    the TestInfo object
870fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville  void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc,
871fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                   Test::TearDownTestCaseFunc tear_down_tc,
872fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville                   TestInfo * test_info) {
873fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // In order to support thread-safe death tests, we need to
874fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // remember the original working directory when the test program
875fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // was first invoked.  We cannot do this in RUN_ALL_TESTS(), as
876fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // the user may have changed the current directory before calling
877fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // RUN_ALL_TESTS().  Therefore we capture the current directory in
878fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // AddTestInfo(), which is called to register a TEST or TEST_F
879fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    // before main() is reached.
880fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville    if (original_working_dir_.IsEmpty()) {
881fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      original_working_dir_.Set(FilePath::GetCurrentDir());
882fbaaef999ba563838ebd00874ed8a1c01fbf286dWink Saville      GTEST_CHECK_(!original_working_dir_.IsEmpty())
883