1*f4a2713aSLionel Sambuc // Copyright 2005, Google Inc.
2*f4a2713aSLionel Sambuc // All rights reserved.
3*f4a2713aSLionel Sambuc //
4*f4a2713aSLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*f4a2713aSLionel Sambuc // modification, are permitted provided that the following conditions are
6*f4a2713aSLionel Sambuc // met:
7*f4a2713aSLionel Sambuc //
8*f4a2713aSLionel Sambuc //     * Redistributions of source code must retain the above copyright
9*f4a2713aSLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*f4a2713aSLionel Sambuc //     * Redistributions in binary form must reproduce the above
11*f4a2713aSLionel Sambuc // copyright notice, this list of conditions and the following disclaimer
12*f4a2713aSLionel Sambuc // in the documentation and/or other materials provided with the
13*f4a2713aSLionel Sambuc // distribution.
14*f4a2713aSLionel Sambuc //     * Neither the name of Google Inc. nor the names of its
15*f4a2713aSLionel Sambuc // contributors may be used to endorse or promote products derived from
16*f4a2713aSLionel Sambuc // this software without specific prior written permission.
17*f4a2713aSLionel Sambuc //
18*f4a2713aSLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*f4a2713aSLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*f4a2713aSLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*f4a2713aSLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*f4a2713aSLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*f4a2713aSLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*f4a2713aSLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*f4a2713aSLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*f4a2713aSLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*f4a2713aSLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*f4a2713aSLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc // Utility functions and classes used by the Google C++ testing framework.
31*f4a2713aSLionel Sambuc //
32*f4a2713aSLionel Sambuc // Author: wan@google.com (Zhanyong Wan)
33*f4a2713aSLionel Sambuc //
34*f4a2713aSLionel Sambuc // This file contains purely Google Test's internal implementation.  Please
35*f4a2713aSLionel Sambuc // DO NOT #INCLUDE IT IN A USER PROGRAM.
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc #ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
38*f4a2713aSLionel Sambuc #define GTEST_SRC_GTEST_INTERNAL_INL_H_
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc // GTEST_IMPLEMENTATION_ is defined to 1 iff the current translation unit is
41*f4a2713aSLionel Sambuc // part of Google Test's implementation; otherwise it's undefined.
42*f4a2713aSLionel Sambuc #if !GTEST_IMPLEMENTATION_
43*f4a2713aSLionel Sambuc // A user is trying to include this from his code - just say no.
44*f4a2713aSLionel Sambuc # error "gtest-internal-inl.h is part of Google Test's internal implementation."
45*f4a2713aSLionel Sambuc # error "It must not be included except by Google Test itself."
46*f4a2713aSLionel Sambuc #endif  // GTEST_IMPLEMENTATION_
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc #ifndef _WIN32_WCE
49*f4a2713aSLionel Sambuc # include <errno.h>
50*f4a2713aSLionel Sambuc #endif  // !_WIN32_WCE
51*f4a2713aSLionel Sambuc #include <stddef.h>
52*f4a2713aSLionel Sambuc #include <stdlib.h>  // For strtoll/_strtoul64/malloc/free.
53*f4a2713aSLionel Sambuc #include <string.h>  // For memmove.
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc #include <algorithm>
56*f4a2713aSLionel Sambuc #include <string>
57*f4a2713aSLionel Sambuc #include <vector>
58*f4a2713aSLionel Sambuc 
59*f4a2713aSLionel Sambuc #include "gtest/internal/gtest-port.h"
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc #if GTEST_OS_WINDOWS
62*f4a2713aSLionel Sambuc # include <windows.h>  // NOLINT
63*f4a2713aSLionel Sambuc #endif  // GTEST_OS_WINDOWS
64*f4a2713aSLionel Sambuc 
65*f4a2713aSLionel Sambuc #include "gtest/gtest.h"  // NOLINT
66*f4a2713aSLionel Sambuc #include "gtest/gtest-spi.h"
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc namespace testing {
69*f4a2713aSLionel Sambuc 
70*f4a2713aSLionel Sambuc // Declares the flags.
71*f4a2713aSLionel Sambuc //
72*f4a2713aSLionel Sambuc // We don't want the users to modify this flag in the code, but want
73*f4a2713aSLionel Sambuc // Google Test's own unit tests to be able to access it. Therefore we
74*f4a2713aSLionel Sambuc // declare it here as opposed to in gtest.h.
75*f4a2713aSLionel Sambuc GTEST_DECLARE_bool_(death_test_use_fork);
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc namespace internal {
78*f4a2713aSLionel Sambuc 
79*f4a2713aSLionel Sambuc // The value of GetTestTypeId() as seen from within the Google Test
80*f4a2713aSLionel Sambuc // library.  This is solely for testing GetTestTypeId().
81*f4a2713aSLionel Sambuc GTEST_API_ extern const TypeId kTestTypeIdInGoogleTest;
82*f4a2713aSLionel Sambuc 
83*f4a2713aSLionel Sambuc // Names of the flags (needed for parsing Google Test flags).
84*f4a2713aSLionel Sambuc const char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests";
85*f4a2713aSLionel Sambuc const char kBreakOnFailureFlag[] = "break_on_failure";
86*f4a2713aSLionel Sambuc const char kCatchExceptionsFlag[] = "catch_exceptions";
87*f4a2713aSLionel Sambuc const char kColorFlag[] = "color";
88*f4a2713aSLionel Sambuc const char kFilterFlag[] = "filter";
89*f4a2713aSLionel Sambuc const char kListTestsFlag[] = "list_tests";
90*f4a2713aSLionel Sambuc const char kOutputFlag[] = "output";
91*f4a2713aSLionel Sambuc const char kPrintTimeFlag[] = "print_time";
92*f4a2713aSLionel Sambuc const char kRandomSeedFlag[] = "random_seed";
93*f4a2713aSLionel Sambuc const char kRepeatFlag[] = "repeat";
94*f4a2713aSLionel Sambuc const char kShuffleFlag[] = "shuffle";
95*f4a2713aSLionel Sambuc const char kStackTraceDepthFlag[] = "stack_trace_depth";
96*f4a2713aSLionel Sambuc const char kStreamResultToFlag[] = "stream_result_to";
97*f4a2713aSLionel Sambuc const char kThrowOnFailureFlag[] = "throw_on_failure";
98*f4a2713aSLionel Sambuc 
99*f4a2713aSLionel Sambuc // A valid random seed must be in [1, kMaxRandomSeed].
100*f4a2713aSLionel Sambuc const int kMaxRandomSeed = 99999;
101*f4a2713aSLionel Sambuc 
102*f4a2713aSLionel Sambuc // g_help_flag is true iff the --help flag or an equivalent form is
103*f4a2713aSLionel Sambuc // specified on the command line.
104*f4a2713aSLionel Sambuc GTEST_API_ extern bool g_help_flag;
105*f4a2713aSLionel Sambuc 
106*f4a2713aSLionel Sambuc // Returns the current time in milliseconds.
107*f4a2713aSLionel Sambuc GTEST_API_ TimeInMillis GetTimeInMillis();
108*f4a2713aSLionel Sambuc 
109*f4a2713aSLionel Sambuc // Returns true iff Google Test should use colors in the output.
110*f4a2713aSLionel Sambuc GTEST_API_ bool ShouldUseColor(bool stdout_is_tty);
111*f4a2713aSLionel Sambuc 
112*f4a2713aSLionel Sambuc // Formats the given time in milliseconds as seconds.
113*f4a2713aSLionel Sambuc GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);
114*f4a2713aSLionel Sambuc 
115*f4a2713aSLionel Sambuc // Parses a string for an Int32 flag, in the form of "--flag=value".
116*f4a2713aSLionel Sambuc //
117*f4a2713aSLionel Sambuc // On success, stores the value of the flag in *value, and returns
118*f4a2713aSLionel Sambuc // true.  On failure, returns false without changing *value.
119*f4a2713aSLionel Sambuc GTEST_API_ bool ParseInt32Flag(
120*f4a2713aSLionel Sambuc     const char* str, const char* flag, Int32* value);
121*f4a2713aSLionel Sambuc 
122*f4a2713aSLionel Sambuc // Returns a random seed in range [1, kMaxRandomSeed] based on the
123*f4a2713aSLionel Sambuc // given --gtest_random_seed flag value.
GetRandomSeedFromFlag(Int32 random_seed_flag)124*f4a2713aSLionel Sambuc inline int GetRandomSeedFromFlag(Int32 random_seed_flag) {
125*f4a2713aSLionel Sambuc   const unsigned int raw_seed = (random_seed_flag == 0) ?
126*f4a2713aSLionel Sambuc       static_cast<unsigned int>(GetTimeInMillis()) :
127*f4a2713aSLionel Sambuc       static_cast<unsigned int>(random_seed_flag);
128*f4a2713aSLionel Sambuc 
129*f4a2713aSLionel Sambuc   // Normalizes the actual seed to range [1, kMaxRandomSeed] such that
130*f4a2713aSLionel Sambuc   // it's easy to type.
131*f4a2713aSLionel Sambuc   const int normalized_seed =
132*f4a2713aSLionel Sambuc       static_cast<int>((raw_seed - 1U) %
133*f4a2713aSLionel Sambuc                        static_cast<unsigned int>(kMaxRandomSeed)) + 1;
134*f4a2713aSLionel Sambuc   return normalized_seed;
135*f4a2713aSLionel Sambuc }
136*f4a2713aSLionel Sambuc 
137*f4a2713aSLionel Sambuc // Returns the first valid random seed after 'seed'.  The behavior is
138*f4a2713aSLionel Sambuc // undefined if 'seed' is invalid.  The seed after kMaxRandomSeed is
139*f4a2713aSLionel Sambuc // considered to be 1.
GetNextRandomSeed(int seed)140*f4a2713aSLionel Sambuc inline int GetNextRandomSeed(int seed) {
141*f4a2713aSLionel Sambuc   GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)
142*f4a2713aSLionel Sambuc       << "Invalid random seed " << seed << " - must be in [1, "
143*f4a2713aSLionel Sambuc       << kMaxRandomSeed << "].";
144*f4a2713aSLionel Sambuc   const int next_seed = seed + 1;
145*f4a2713aSLionel Sambuc   return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
146*f4a2713aSLionel Sambuc }
147*f4a2713aSLionel Sambuc 
148*f4a2713aSLionel Sambuc // This class saves the values of all Google Test flags in its c'tor, and
149*f4a2713aSLionel Sambuc // restores them in its d'tor.
150*f4a2713aSLionel Sambuc class GTestFlagSaver {
151*f4a2713aSLionel Sambuc  public:
152*f4a2713aSLionel Sambuc   // The c'tor.
GTestFlagSaver()153*f4a2713aSLionel Sambuc   GTestFlagSaver() {
154*f4a2713aSLionel Sambuc     also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests);
155*f4a2713aSLionel Sambuc     break_on_failure_ = GTEST_FLAG(break_on_failure);
156*f4a2713aSLionel Sambuc     catch_exceptions_ = GTEST_FLAG(catch_exceptions);
157*f4a2713aSLionel Sambuc     color_ = GTEST_FLAG(color);
158*f4a2713aSLionel Sambuc     death_test_style_ = GTEST_FLAG(death_test_style);
159*f4a2713aSLionel Sambuc     death_test_use_fork_ = GTEST_FLAG(death_test_use_fork);
160*f4a2713aSLionel Sambuc     filter_ = GTEST_FLAG(filter);
161*f4a2713aSLionel Sambuc     internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
162*f4a2713aSLionel Sambuc     list_tests_ = GTEST_FLAG(list_tests);
163*f4a2713aSLionel Sambuc     output_ = GTEST_FLAG(output);
164*f4a2713aSLionel Sambuc     print_time_ = GTEST_FLAG(print_time);
165*f4a2713aSLionel Sambuc     random_seed_ = GTEST_FLAG(random_seed);
166*f4a2713aSLionel Sambuc     repeat_ = GTEST_FLAG(repeat);
167*f4a2713aSLionel Sambuc     shuffle_ = GTEST_FLAG(shuffle);
168*f4a2713aSLionel Sambuc     stack_trace_depth_ = GTEST_FLAG(stack_trace_depth);
169*f4a2713aSLionel Sambuc     stream_result_to_ = GTEST_FLAG(stream_result_to);
170*f4a2713aSLionel Sambuc     throw_on_failure_ = GTEST_FLAG(throw_on_failure);
171*f4a2713aSLionel Sambuc   }
172*f4a2713aSLionel Sambuc 
173*f4a2713aSLionel Sambuc   // The d'tor is not virtual.  DO NOT INHERIT FROM THIS CLASS.
~GTestFlagSaver()174*f4a2713aSLionel Sambuc   ~GTestFlagSaver() {
175*f4a2713aSLionel Sambuc     GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
176*f4a2713aSLionel Sambuc     GTEST_FLAG(break_on_failure) = break_on_failure_;
177*f4a2713aSLionel Sambuc     GTEST_FLAG(catch_exceptions) = catch_exceptions_;
178*f4a2713aSLionel Sambuc     GTEST_FLAG(color) = color_;
179*f4a2713aSLionel Sambuc     GTEST_FLAG(death_test_style) = death_test_style_;
180*f4a2713aSLionel Sambuc     GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
181*f4a2713aSLionel Sambuc     GTEST_FLAG(filter) = filter_;
182*f4a2713aSLionel Sambuc     GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
183*f4a2713aSLionel Sambuc     GTEST_FLAG(list_tests) = list_tests_;
184*f4a2713aSLionel Sambuc     GTEST_FLAG(output) = output_;
185*f4a2713aSLionel Sambuc     GTEST_FLAG(print_time) = print_time_;
186*f4a2713aSLionel Sambuc     GTEST_FLAG(random_seed) = random_seed_;
187*f4a2713aSLionel Sambuc     GTEST_FLAG(repeat) = repeat_;
188*f4a2713aSLionel Sambuc     GTEST_FLAG(shuffle) = shuffle_;
189*f4a2713aSLionel Sambuc     GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
190*f4a2713aSLionel Sambuc     GTEST_FLAG(stream_result_to) = stream_result_to_;
191*f4a2713aSLionel Sambuc     GTEST_FLAG(throw_on_failure) = throw_on_failure_;
192*f4a2713aSLionel Sambuc   }
193*f4a2713aSLionel Sambuc  private:
194*f4a2713aSLionel Sambuc   // Fields for saving the original values of flags.
195*f4a2713aSLionel Sambuc   bool also_run_disabled_tests_;
196*f4a2713aSLionel Sambuc   bool break_on_failure_;
197*f4a2713aSLionel Sambuc   bool catch_exceptions_;
198*f4a2713aSLionel Sambuc   String color_;
199*f4a2713aSLionel Sambuc   String death_test_style_;
200*f4a2713aSLionel Sambuc   bool death_test_use_fork_;
201*f4a2713aSLionel Sambuc   String filter_;
202*f4a2713aSLionel Sambuc   String internal_run_death_test_;
203*f4a2713aSLionel Sambuc   bool list_tests_;
204*f4a2713aSLionel Sambuc   String output_;
205*f4a2713aSLionel Sambuc   bool print_time_;
206*f4a2713aSLionel Sambuc   internal::Int32 random_seed_;
207*f4a2713aSLionel Sambuc   internal::Int32 repeat_;
208*f4a2713aSLionel Sambuc   bool shuffle_;
209*f4a2713aSLionel Sambuc   internal::Int32 stack_trace_depth_;
210*f4a2713aSLionel Sambuc   String stream_result_to_;
211*f4a2713aSLionel Sambuc   bool throw_on_failure_;
212*f4a2713aSLionel Sambuc } GTEST_ATTRIBUTE_UNUSED_;
213*f4a2713aSLionel Sambuc 
214*f4a2713aSLionel Sambuc // Converts a Unicode code point to a narrow string in UTF-8 encoding.
215*f4a2713aSLionel Sambuc // code_point parameter is of type UInt32 because wchar_t may not be
216*f4a2713aSLionel Sambuc // wide enough to contain a code point.
217*f4a2713aSLionel Sambuc // The output buffer str must containt at least 32 characters.
218*f4a2713aSLionel Sambuc // The function returns the address of the output buffer.
219*f4a2713aSLionel Sambuc // If the code_point is not a valid Unicode code point
220*f4a2713aSLionel Sambuc // (i.e. outside of Unicode range U+0 to U+10FFFF) it will be output
221*f4a2713aSLionel Sambuc // as '(Invalid Unicode 0xXXXXXXXX)'.
222*f4a2713aSLionel Sambuc GTEST_API_ char* CodePointToUtf8(UInt32 code_point, char* str);
223*f4a2713aSLionel Sambuc 
224*f4a2713aSLionel Sambuc // Converts a wide string to a narrow string in UTF-8 encoding.
225*f4a2713aSLionel Sambuc // The wide string is assumed to have the following encoding:
226*f4a2713aSLionel Sambuc //   UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS)
227*f4a2713aSLionel Sambuc //   UTF-32 if sizeof(wchar_t) == 4 (on Linux)
228*f4a2713aSLionel Sambuc // Parameter str points to a null-terminated wide string.
229*f4a2713aSLionel Sambuc // Parameter num_chars may additionally limit the number
230*f4a2713aSLionel Sambuc // of wchar_t characters processed. -1 is used when the entire string
231*f4a2713aSLionel Sambuc // should be processed.
232*f4a2713aSLionel Sambuc // If the string contains code points that are not valid Unicode code points
233*f4a2713aSLionel Sambuc // (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output
234*f4a2713aSLionel Sambuc // as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding
235*f4a2713aSLionel Sambuc // and contains invalid UTF-16 surrogate pairs, values in those pairs
236*f4a2713aSLionel Sambuc // will be encoded as individual Unicode characters from Basic Normal Plane.
237*f4a2713aSLionel Sambuc GTEST_API_ String WideStringToUtf8(const wchar_t* str, int num_chars);
238*f4a2713aSLionel Sambuc 
239*f4a2713aSLionel Sambuc // Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file
240*f4a2713aSLionel Sambuc // if the variable is present. If a file already exists at this location, this
241*f4a2713aSLionel Sambuc // function will write over it. If the variable is present, but the file cannot
242*f4a2713aSLionel Sambuc // be created, prints an error and exits.
243*f4a2713aSLionel Sambuc void WriteToShardStatusFileIfNeeded();
244*f4a2713aSLionel Sambuc 
245*f4a2713aSLionel Sambuc // Checks whether sharding is enabled by examining the relevant
246*f4a2713aSLionel Sambuc // environment variable values. If the variables are present,
247*f4a2713aSLionel Sambuc // but inconsistent (e.g., shard_index >= total_shards), prints
248*f4a2713aSLionel Sambuc // an error and exits. If in_subprocess_for_death_test, sharding is
249*f4a2713aSLionel Sambuc // disabled because it must only be applied to the original test
250*f4a2713aSLionel Sambuc // process. Otherwise, we could filter out death tests we intended to execute.
251*f4a2713aSLionel Sambuc GTEST_API_ bool ShouldShard(const char* total_shards_str,
252*f4a2713aSLionel Sambuc                             const char* shard_index_str,
253*f4a2713aSLionel Sambuc                             bool in_subprocess_for_death_test);
254*f4a2713aSLionel Sambuc 
255*f4a2713aSLionel Sambuc // Parses the environment variable var as an Int32. If it is unset,
256*f4a2713aSLionel Sambuc // returns default_val. If it is not an Int32, prints an error and
257*f4a2713aSLionel Sambuc // and aborts.
258*f4a2713aSLionel Sambuc GTEST_API_ Int32 Int32FromEnvOrDie(const char* env_var, Int32 default_val);
259*f4a2713aSLionel Sambuc 
260*f4a2713aSLionel Sambuc // Given the total number of shards, the shard index, and the test id,
261*f4a2713aSLionel Sambuc // returns true iff the test should be run on this shard. The test id is
262*f4a2713aSLionel Sambuc // some arbitrary but unique non-negative integer assigned to each test
263*f4a2713aSLionel Sambuc // method. Assumes that 0 <= shard_index < total_shards.
264*f4a2713aSLionel Sambuc GTEST_API_ bool ShouldRunTestOnShard(
265*f4a2713aSLionel Sambuc     int total_shards, int shard_index, int test_id);
266*f4a2713aSLionel Sambuc 
267*f4a2713aSLionel Sambuc // STL container utilities.
268*f4a2713aSLionel Sambuc 
269*f4a2713aSLionel Sambuc // Returns the number of elements in the given container that satisfy
270*f4a2713aSLionel Sambuc // the given predicate.
271*f4a2713aSLionel Sambuc template <class Container, typename Predicate>
CountIf(const Container & c,Predicate predicate)272*f4a2713aSLionel Sambuc inline int CountIf(const Container& c, Predicate predicate) {
273*f4a2713aSLionel Sambuc   // Implemented as an explicit loop since std::count_if() in libCstd on
274*f4a2713aSLionel Sambuc   // Solaris has a non-standard signature.
275*f4a2713aSLionel Sambuc   int count = 0;
276*f4a2713aSLionel Sambuc   for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
277*f4a2713aSLionel Sambuc     if (predicate(*it))
278*f4a2713aSLionel Sambuc       ++count;
279*f4a2713aSLionel Sambuc   }
280*f4a2713aSLionel Sambuc   return count;
281*f4a2713aSLionel Sambuc }
282*f4a2713aSLionel Sambuc 
283*f4a2713aSLionel Sambuc // Applies a function/functor to each element in the container.
284*f4a2713aSLionel Sambuc template <class Container, typename Functor>
ForEach(const Container & c,Functor functor)285*f4a2713aSLionel Sambuc void ForEach(const Container& c, Functor functor) {
286*f4a2713aSLionel Sambuc   std::for_each(c.begin(), c.end(), functor);
287*f4a2713aSLionel Sambuc }
288*f4a2713aSLionel Sambuc 
289*f4a2713aSLionel Sambuc // Returns the i-th element of the vector, or default_value if i is not
290*f4a2713aSLionel Sambuc // in range [0, v.size()).
291*f4a2713aSLionel Sambuc template <typename E>
GetElementOr(const std::vector<E> & v,int i,E default_value)292*f4a2713aSLionel Sambuc inline E GetElementOr(const std::vector<E>& v, int i, E default_value) {
293*f4a2713aSLionel Sambuc   return (i < 0 || i >= static_cast<int>(v.size())) ? default_value : v[i];
294*f4a2713aSLionel Sambuc }
295*f4a2713aSLionel Sambuc 
296*f4a2713aSLionel Sambuc // Performs an in-place shuffle of a range of the vector's elements.
297*f4a2713aSLionel Sambuc // 'begin' and 'end' are element indices as an STL-style range;
298*f4a2713aSLionel Sambuc // i.e. [begin, end) are shuffled, where 'end' == size() means to
299*f4a2713aSLionel Sambuc // shuffle to the end of the vector.
300*f4a2713aSLionel Sambuc template <typename E>
ShuffleRange(internal::Random * random,int begin,int end,std::vector<E> * v)301*f4a2713aSLionel Sambuc void ShuffleRange(internal::Random* random, int begin, int end,
302*f4a2713aSLionel Sambuc                   std::vector<E>* v) {
303*f4a2713aSLionel Sambuc   const int size = static_cast<int>(v->size());
304*f4a2713aSLionel Sambuc   GTEST_CHECK_(0 <= begin && begin <= size)
305*f4a2713aSLionel Sambuc       << "Invalid shuffle range start " << begin << ": must be in range [0, "
306*f4a2713aSLionel Sambuc       << size << "].";
307*f4a2713aSLionel Sambuc   GTEST_CHECK_(begin <= end && end <= size)
308*f4a2713aSLionel Sambuc       << "Invalid shuffle range finish " << end << ": must be in range ["
309*f4a2713aSLionel Sambuc       << begin << ", " << size << "].";
310*f4a2713aSLionel Sambuc 
311*f4a2713aSLionel Sambuc   // Fisher-Yates shuffle, from
312*f4a2713aSLionel Sambuc   // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
313*f4a2713aSLionel Sambuc   for (int range_width = end - begin; range_width >= 2; range_width--) {
314*f4a2713aSLionel Sambuc     const int last_in_range = begin + range_width - 1;
315*f4a2713aSLionel Sambuc     const int selected = begin + random->Generate(range_width);
316*f4a2713aSLionel Sambuc     std::swap((*v)[selected], (*v)[last_in_range]);
317*f4a2713aSLionel Sambuc   }
318*f4a2713aSLionel Sambuc }
319*f4a2713aSLionel Sambuc 
320*f4a2713aSLionel Sambuc // Performs an in-place shuffle of the vector's elements.
321*f4a2713aSLionel Sambuc template <typename E>
Shuffle(internal::Random * random,std::vector<E> * v)322*f4a2713aSLionel Sambuc inline void Shuffle(internal::Random* random, std::vector<E>* v) {
323*f4a2713aSLionel Sambuc   ShuffleRange(random, 0, static_cast<int>(v->size()), v);
324*f4a2713aSLionel Sambuc }
325*f4a2713aSLionel Sambuc 
326*f4a2713aSLionel Sambuc // A function for deleting an object.  Handy for being used as a
327*f4a2713aSLionel Sambuc // functor.
328*f4a2713aSLionel Sambuc template <typename T>
Delete(T * x)329*f4a2713aSLionel Sambuc static void Delete(T* x) {
330*f4a2713aSLionel Sambuc   delete x;
331*f4a2713aSLionel Sambuc }
332*f4a2713aSLionel Sambuc 
333*f4a2713aSLionel Sambuc // A predicate that checks the key of a TestProperty against a known key.
334*f4a2713aSLionel Sambuc //
335*f4a2713aSLionel Sambuc // TestPropertyKeyIs is copyable.
336*f4a2713aSLionel Sambuc class TestPropertyKeyIs {
337*f4a2713aSLionel Sambuc  public:
338*f4a2713aSLionel Sambuc   // Constructor.
339*f4a2713aSLionel Sambuc   //
340*f4a2713aSLionel Sambuc   // TestPropertyKeyIs has NO default constructor.
TestPropertyKeyIs(const char * key)341*f4a2713aSLionel Sambuc   explicit TestPropertyKeyIs(const char* key)
342*f4a2713aSLionel Sambuc       : key_(key) {}
343*f4a2713aSLionel Sambuc 
344*f4a2713aSLionel Sambuc   // Returns true iff the test name of test property matches on key_.
operator()345*f4a2713aSLionel Sambuc   bool operator()(const TestProperty& test_property) const {
346*f4a2713aSLionel Sambuc     return String(test_property.key()).Compare(key_) == 0;
347*f4a2713aSLionel Sambuc   }
348*f4a2713aSLionel Sambuc 
349*f4a2713aSLionel Sambuc  private:
350*f4a2713aSLionel Sambuc   String key_;
351*f4a2713aSLionel Sambuc };
352*f4a2713aSLionel Sambuc 
353*f4a2713aSLionel Sambuc // Class UnitTestOptions.
354*f4a2713aSLionel Sambuc //
355*f4a2713aSLionel Sambuc // This class contains functions for processing options the user
356*f4a2713aSLionel Sambuc // specifies when running the tests.  It has only static members.
357*f4a2713aSLionel Sambuc //
358*f4a2713aSLionel Sambuc // In most cases, the user can specify an option using either an
359*f4a2713aSLionel Sambuc // environment variable or a command line flag.  E.g. you can set the
360*f4a2713aSLionel Sambuc // test filter using either GTEST_FILTER or --gtest_filter.  If both
361*f4a2713aSLionel Sambuc // the variable and the flag are present, the latter overrides the
362*f4a2713aSLionel Sambuc // former.
363*f4a2713aSLionel Sambuc class GTEST_API_ UnitTestOptions {
364*f4a2713aSLionel Sambuc  public:
365*f4a2713aSLionel Sambuc   // Functions for processing the gtest_output flag.
366*f4a2713aSLionel Sambuc 
367*f4a2713aSLionel Sambuc   // Returns the output format, or "" for normal printed output.
368*f4a2713aSLionel Sambuc   static String GetOutputFormat();
369*f4a2713aSLionel Sambuc 
370*f4a2713aSLionel Sambuc   // Returns the absolute path of the requested output file, or the
371*f4a2713aSLionel Sambuc   // default (test_detail.xml in the original working directory) if
372*f4a2713aSLionel Sambuc   // none was explicitly specified.
373*f4a2713aSLionel Sambuc   static String GetAbsolutePathToOutputFile();
374*f4a2713aSLionel Sambuc 
375*f4a2713aSLionel Sambuc   // Functions for processing the gtest_filter flag.
376*f4a2713aSLionel Sambuc 
377*f4a2713aSLionel Sambuc   // Returns true iff the wildcard pattern matches the string.  The
378*f4a2713aSLionel Sambuc   // first ':' or '\0' character in pattern marks the end of it.
379*f4a2713aSLionel Sambuc   //
380*f4a2713aSLionel Sambuc   // This recursive algorithm isn't very efficient, but is clear and
381*f4a2713aSLionel Sambuc   // works well enough for matching test names, which are short.
382*f4a2713aSLionel Sambuc   static bool PatternMatchesString(const char *pattern, const char *str);
383*f4a2713aSLionel Sambuc 
384*f4a2713aSLionel Sambuc   // Returns true iff the user-specified filter matches the test case
385*f4a2713aSLionel Sambuc   // name and the test name.
386*f4a2713aSLionel Sambuc   static bool FilterMatchesTest(const String &test_case_name,
387*f4a2713aSLionel Sambuc                                 const String &test_name);
388*f4a2713aSLionel Sambuc 
389*f4a2713aSLionel Sambuc #if GTEST_OS_WINDOWS
390*f4a2713aSLionel Sambuc   // Function for supporting the gtest_catch_exception flag.
391*f4a2713aSLionel Sambuc 
392*f4a2713aSLionel Sambuc   // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the
393*f4a2713aSLionel Sambuc   // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.
394*f4a2713aSLionel Sambuc   // This function is useful as an __except condition.
395*f4a2713aSLionel Sambuc   static int GTestShouldProcessSEH(DWORD exception_code);
396*f4a2713aSLionel Sambuc #endif  // GTEST_OS_WINDOWS
397*f4a2713aSLionel Sambuc 
398*f4a2713aSLionel Sambuc   // Returns true if "name" matches the ':' separated list of glob-style
399*f4a2713aSLionel Sambuc   // filters in "filter".
400*f4a2713aSLionel Sambuc   static bool MatchesFilter(const String& name, const char* filter);
401*f4a2713aSLionel Sambuc };
402*f4a2713aSLionel Sambuc 
403*f4a2713aSLionel Sambuc // Returns the current application's name, removing directory path if that
404*f4a2713aSLionel Sambuc // is present.  Used by UnitTestOptions::GetOutputFile.
405*f4a2713aSLionel Sambuc GTEST_API_ FilePath GetCurrentExecutableName();
406*f4a2713aSLionel Sambuc 
407*f4a2713aSLionel Sambuc // The role interface for getting the OS stack trace as a string.
408*f4a2713aSLionel Sambuc class OsStackTraceGetterInterface {
409*f4a2713aSLionel Sambuc  public:
OsStackTraceGetterInterface()410*f4a2713aSLionel Sambuc   OsStackTraceGetterInterface() {}
411*f4a2713aSLionel Sambuc   virtual ~OsStackTraceGetterInterface();
412*f4a2713aSLionel Sambuc 
413*f4a2713aSLionel Sambuc   // Returns the current OS stack trace as a String.  Parameters:
414*f4a2713aSLionel Sambuc   //
415*f4a2713aSLionel Sambuc   //   max_depth  - the maximum number of stack frames to be included
416*f4a2713aSLionel Sambuc   //                in the trace.
417*f4a2713aSLionel Sambuc   //   skip_count - the number of top frames to be skipped; doesn't count
418*f4a2713aSLionel Sambuc   //                against max_depth.
419*f4a2713aSLionel Sambuc   virtual String CurrentStackTrace(int max_depth, int skip_count) = 0;
420*f4a2713aSLionel Sambuc 
421*f4a2713aSLionel Sambuc   // UponLeavingGTest() should be called immediately before Google Test calls
422*f4a2713aSLionel Sambuc   // user code. It saves some information about the current stack that
423*f4a2713aSLionel Sambuc   // CurrentStackTrace() will use to find and hide Google Test stack frames.
424*f4a2713aSLionel Sambuc   virtual void UponLeavingGTest() = 0;
425*f4a2713aSLionel Sambuc 
426*f4a2713aSLionel Sambuc  private:
427*f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface);
428*f4a2713aSLionel Sambuc };
429*f4a2713aSLionel Sambuc 
430*f4a2713aSLionel Sambuc // A working implementation of the OsStackTraceGetterInterface interface.
431*f4a2713aSLionel Sambuc class OsStackTraceGetter : public OsStackTraceGetterInterface {
432*f4a2713aSLionel Sambuc  public:
OsStackTraceGetter()433*f4a2713aSLionel Sambuc   OsStackTraceGetter() : caller_frame_(NULL) {}
434*f4a2713aSLionel Sambuc   virtual String CurrentStackTrace(int max_depth, int skip_count);
435*f4a2713aSLionel Sambuc   virtual void UponLeavingGTest();
436*f4a2713aSLionel Sambuc 
437*f4a2713aSLionel Sambuc   // This string is inserted in place of stack frames that are part of
438*f4a2713aSLionel Sambuc   // Google Test's implementation.
439*f4a2713aSLionel Sambuc   static const char* const kElidedFramesMarker;
440*f4a2713aSLionel Sambuc 
441*f4a2713aSLionel Sambuc  private:
442*f4a2713aSLionel Sambuc   Mutex mutex_;  // protects all internal state
443*f4a2713aSLionel Sambuc 
444*f4a2713aSLionel Sambuc   // We save the stack frame below the frame that calls user code.
445*f4a2713aSLionel Sambuc   // We do this because the address of the frame immediately below
446*f4a2713aSLionel Sambuc   // the user code changes between the call to UponLeavingGTest()
447*f4a2713aSLionel Sambuc   // and any calls to CurrentStackTrace() from within the user code.
448*f4a2713aSLionel Sambuc   void* caller_frame_;
449*f4a2713aSLionel Sambuc 
450*f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter);
451*f4a2713aSLionel Sambuc };
452*f4a2713aSLionel Sambuc 
453*f4a2713aSLionel Sambuc // Information about a Google Test trace point.
454*f4a2713aSLionel Sambuc struct TraceInfo {
455*f4a2713aSLionel Sambuc   const char* file;
456*f4a2713aSLionel Sambuc   int line;
457*f4a2713aSLionel Sambuc   String message;
458*f4a2713aSLionel Sambuc };
459*f4a2713aSLionel Sambuc 
460*f4a2713aSLionel Sambuc // This is the default global test part result reporter used in UnitTestImpl.
461*f4a2713aSLionel Sambuc // This class should only be used by UnitTestImpl.
462*f4a2713aSLionel Sambuc class DefaultGlobalTestPartResultReporter
463*f4a2713aSLionel Sambuc   : public TestPartResultReporterInterface {
464*f4a2713aSLionel Sambuc  public:
465*f4a2713aSLionel Sambuc   explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
466*f4a2713aSLionel Sambuc   // Implements the TestPartResultReporterInterface. Reports the test part
467*f4a2713aSLionel Sambuc   // result in the current test.
468*f4a2713aSLionel Sambuc   virtual void ReportTestPartResult(const TestPartResult& result);
469*f4a2713aSLionel Sambuc 
470*f4a2713aSLionel Sambuc  private:
471*f4a2713aSLionel Sambuc   UnitTestImpl* const unit_test_;
472*f4a2713aSLionel Sambuc 
473*f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);
474*f4a2713aSLionel Sambuc };
475*f4a2713aSLionel Sambuc 
476*f4a2713aSLionel Sambuc // This is the default per thread test part result reporter used in
477*f4a2713aSLionel Sambuc // UnitTestImpl. This class should only be used by UnitTestImpl.
478*f4a2713aSLionel Sambuc class DefaultPerThreadTestPartResultReporter
479*f4a2713aSLionel Sambuc     : public TestPartResultReporterInterface {
480*f4a2713aSLionel Sambuc  public:
481*f4a2713aSLionel Sambuc   explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
482*f4a2713aSLionel Sambuc   // Implements the TestPartResultReporterInterface. The implementation just
483*f4a2713aSLionel Sambuc   // delegates to the current global test part result reporter of *unit_test_.
484*f4a2713aSLionel Sambuc   virtual void ReportTestPartResult(const TestPartResult& result);
485*f4a2713aSLionel Sambuc 
486*f4a2713aSLionel Sambuc  private:
487*f4a2713aSLionel Sambuc   UnitTestImpl* const unit_test_;
488*f4a2713aSLionel Sambuc 
489*f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter);
490*f4a2713aSLionel Sambuc };
491*f4a2713aSLionel Sambuc 
492*f4a2713aSLionel Sambuc // The private implementation of the UnitTest class.  We don't protect
493*f4a2713aSLionel Sambuc // the methods under a mutex, as this class is not accessible by a
494*f4a2713aSLionel Sambuc // user and the UnitTest class that delegates work to this class does
495*f4a2713aSLionel Sambuc // proper locking.
496*f4a2713aSLionel Sambuc class GTEST_API_ UnitTestImpl {
497*f4a2713aSLionel Sambuc  public:
498*f4a2713aSLionel Sambuc   explicit UnitTestImpl(UnitTest* parent);
499*f4a2713aSLionel Sambuc   virtual ~UnitTestImpl();
500*f4a2713aSLionel Sambuc 
501*f4a2713aSLionel Sambuc   // There are two different ways to register your own TestPartResultReporter.
502*f4a2713aSLionel Sambuc   // You can register your own repoter to listen either only for test results
503*f4a2713aSLionel Sambuc   // from the current thread or for results from all threads.
504*f4a2713aSLionel Sambuc   // By default, each per-thread test result repoter just passes a new
505*f4a2713aSLionel Sambuc   // TestPartResult to the global test result reporter, which registers the
506*f4a2713aSLionel Sambuc   // test part result for the currently running test.
507*f4a2713aSLionel Sambuc 
508*f4a2713aSLionel Sambuc   // Returns the global test part result reporter.
509*f4a2713aSLionel Sambuc   TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
510*f4a2713aSLionel Sambuc 
511*f4a2713aSLionel Sambuc   // Sets the global test part result reporter.
512*f4a2713aSLionel Sambuc   void SetGlobalTestPartResultReporter(
513*f4a2713aSLionel Sambuc       TestPartResultReporterInterface* reporter);
514*f4a2713aSLionel Sambuc 
515*f4a2713aSLionel Sambuc   // Returns the test part result reporter for the current thread.
516*f4a2713aSLionel Sambuc   TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
517*f4a2713aSLionel Sambuc 
518*f4a2713aSLionel Sambuc   // Sets the test part result reporter for the current thread.
519*f4a2713aSLionel Sambuc   void SetTestPartResultReporterForCurrentThread(
520*f4a2713aSLionel Sambuc       TestPartResultReporterInterface* reporter);
521*f4a2713aSLionel Sambuc 
522*f4a2713aSLionel Sambuc   // Gets the number of successful test cases.
523*f4a2713aSLionel Sambuc   int successful_test_case_count() const;
524*f4a2713aSLionel Sambuc 
525*f4a2713aSLionel Sambuc   // Gets the number of failed test cases.
526*f4a2713aSLionel Sambuc   int failed_test_case_count() const;
527*f4a2713aSLionel Sambuc 
528*f4a2713aSLionel Sambuc   // Gets the number of all test cases.
529*f4a2713aSLionel Sambuc   int total_test_case_count() const;
530*f4a2713aSLionel Sambuc 
531*f4a2713aSLionel Sambuc   // Gets the number of all test cases that contain at least one test
532*f4a2713aSLionel Sambuc   // that should run.
533*f4a2713aSLionel Sambuc   int test_case_to_run_count() const;
534*f4a2713aSLionel Sambuc 
535*f4a2713aSLionel Sambuc   // Gets the number of successful tests.
536*f4a2713aSLionel Sambuc   int successful_test_count() const;
537*f4a2713aSLionel Sambuc 
538*f4a2713aSLionel Sambuc   // Gets the number of failed tests.
539*f4a2713aSLionel Sambuc   int failed_test_count() const;
540*f4a2713aSLionel Sambuc 
541*f4a2713aSLionel Sambuc   // Gets the number of disabled tests.
542*f4a2713aSLionel Sambuc   int disabled_test_count() const;
543*f4a2713aSLionel Sambuc 
544*f4a2713aSLionel Sambuc   // Gets the number of all tests.
545*f4a2713aSLionel Sambuc   int total_test_count() const;
546*f4a2713aSLionel Sambuc 
547*f4a2713aSLionel Sambuc   // Gets the number of tests that should run.
548*f4a2713aSLionel Sambuc   int test_to_run_count() const;
549*f4a2713aSLionel Sambuc 
550*f4a2713aSLionel Sambuc   // Gets the elapsed time, in milliseconds.
elapsed_time()551*f4a2713aSLionel Sambuc   TimeInMillis elapsed_time() const { return elapsed_time_; }
552*f4a2713aSLionel Sambuc 
553*f4a2713aSLionel Sambuc   // Returns true iff the unit test passed (i.e. all test cases passed).
Passed()554*f4a2713aSLionel Sambuc   bool Passed() const { return !Failed(); }
555*f4a2713aSLionel Sambuc 
556*f4a2713aSLionel Sambuc   // Returns true iff the unit test failed (i.e. some test case failed
557*f4a2713aSLionel Sambuc   // or something outside of all tests failed).
Failed()558*f4a2713aSLionel Sambuc   bool Failed() const {
559*f4a2713aSLionel Sambuc     return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
560*f4a2713aSLionel Sambuc   }
561*f4a2713aSLionel Sambuc 
562*f4a2713aSLionel Sambuc   // Gets the i-th test case among all the test cases. i can range from 0 to
563*f4a2713aSLionel Sambuc   // total_test_case_count() - 1. If i is not in that range, returns NULL.
GetTestCase(int i)564*f4a2713aSLionel Sambuc   const TestCase* GetTestCase(int i) const {
565*f4a2713aSLionel Sambuc     const int index = GetElementOr(test_case_indices_, i, -1);
566*f4a2713aSLionel Sambuc     return index < 0 ? NULL : test_cases_[i];
567*f4a2713aSLionel Sambuc   }
568*f4a2713aSLionel Sambuc 
569*f4a2713aSLionel Sambuc   // Gets the i-th test case among all the test cases. i can range from 0 to
570*f4a2713aSLionel Sambuc   // total_test_case_count() - 1. If i is not in that range, returns NULL.
GetMutableTestCase(int i)571*f4a2713aSLionel Sambuc   TestCase* GetMutableTestCase(int i) {
572*f4a2713aSLionel Sambuc     const int index = GetElementOr(test_case_indices_, i, -1);
573*f4a2713aSLionel Sambuc     return index < 0 ? NULL : test_cases_[index];
574*f4a2713aSLionel Sambuc   }
575*f4a2713aSLionel Sambuc 
576*f4a2713aSLionel Sambuc   // Provides access to the event listener list.
listeners()577*f4a2713aSLionel Sambuc   TestEventListeners* listeners() { return &listeners_; }
578*f4a2713aSLionel Sambuc 
579*f4a2713aSLionel Sambuc   // Returns the TestResult for the test that's currently running, or
580*f4a2713aSLionel Sambuc   // the TestResult for the ad hoc test if no test is running.
581*f4a2713aSLionel Sambuc   TestResult* current_test_result();
582*f4a2713aSLionel Sambuc 
583*f4a2713aSLionel Sambuc   // Returns the TestResult for the ad hoc test.
ad_hoc_test_result()584*f4a2713aSLionel Sambuc   const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }
585*f4a2713aSLionel Sambuc 
586*f4a2713aSLionel Sambuc   // Sets the OS stack trace getter.
587*f4a2713aSLionel Sambuc   //
588*f4a2713aSLionel Sambuc   // Does nothing if the input and the current OS stack trace getter
589*f4a2713aSLionel Sambuc   // are the same; otherwise, deletes the old getter and makes the
590*f4a2713aSLionel Sambuc   // input the current getter.
591*f4a2713aSLionel Sambuc   void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
592*f4a2713aSLionel Sambuc 
593*f4a2713aSLionel Sambuc   // Returns the current OS stack trace getter if it is not NULL;
594*f4a2713aSLionel Sambuc   // otherwise, creates an OsStackTraceGetter, makes it the current
595*f4a2713aSLionel Sambuc   // getter, and returns it.
596*f4a2713aSLionel Sambuc   OsStackTraceGetterInterface* os_stack_trace_getter();
597*f4a2713aSLionel Sambuc 
598*f4a2713aSLionel Sambuc   // Returns the current OS stack trace as a String.
599*f4a2713aSLionel Sambuc   //
600*f4a2713aSLionel Sambuc   // The maximum number of stack frames to be included is specified by
601*f4a2713aSLionel Sambuc   // the gtest_stack_trace_depth flag.  The skip_count parameter
602*f4a2713aSLionel Sambuc   // specifies the number of top frames to be skipped, which doesn't
603*f4a2713aSLionel Sambuc   // count against the number of frames to be included.
604*f4a2713aSLionel Sambuc   //
605*f4a2713aSLionel Sambuc   // For example, if Foo() calls Bar(), which in turn calls
606*f4a2713aSLionel Sambuc   // CurrentOsStackTraceExceptTop(1), Foo() will be included in the
607*f4a2713aSLionel Sambuc   // trace but Bar() and CurrentOsStackTraceExceptTop() won't.
608*f4a2713aSLionel Sambuc   String CurrentOsStackTraceExceptTop(int skip_count);
609*f4a2713aSLionel Sambuc 
610*f4a2713aSLionel Sambuc   // Finds and returns a TestCase with the given name.  If one doesn't
611*f4a2713aSLionel Sambuc   // exist, creates one and returns it.
612*f4a2713aSLionel Sambuc   //
613*f4a2713aSLionel Sambuc   // Arguments:
614*f4a2713aSLionel Sambuc   //
615*f4a2713aSLionel Sambuc   //   test_case_name: name of the test case
616*f4a2713aSLionel Sambuc   //   type_param:     the name of the test's type parameter, or NULL if
617*f4a2713aSLionel Sambuc   //                   this is not a typed or a type-parameterized test.
618*f4a2713aSLionel Sambuc   //   set_up_tc:      pointer to the function that sets up the test case
619*f4a2713aSLionel Sambuc   //   tear_down_tc:   pointer to the function that tears down the test case
620*f4a2713aSLionel Sambuc   TestCase* GetTestCase(const char* test_case_name,
621*f4a2713aSLionel Sambuc                         const char* type_param,
622*f4a2713aSLionel Sambuc                         Test::SetUpTestCaseFunc set_up_tc,
623*f4a2713aSLionel Sambuc                         Test::TearDownTestCaseFunc tear_down_tc);
624*f4a2713aSLionel Sambuc 
625*f4a2713aSLionel Sambuc   // Adds a TestInfo to the unit test.
626*f4a2713aSLionel Sambuc   //
627*f4a2713aSLionel Sambuc   // Arguments:
628*f4a2713aSLionel Sambuc   //
629*f4a2713aSLionel Sambuc   //   set_up_tc:    pointer to the function that sets up the test case
630*f4a2713aSLionel Sambuc   //   tear_down_tc: pointer to the function that tears down the test case
631*f4a2713aSLionel Sambuc   //   test_info:    the TestInfo object
AddTestInfo(Test::SetUpTestCaseFunc set_up_tc,Test::TearDownTestCaseFunc tear_down_tc,TestInfo * test_info)632*f4a2713aSLionel Sambuc   void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc,
633*f4a2713aSLionel Sambuc                    Test::TearDownTestCaseFunc tear_down_tc,
634*f4a2713aSLionel Sambuc                    TestInfo* test_info) {
635*f4a2713aSLionel Sambuc     // In order to support thread-safe death tests, we need to
636*f4a2713aSLionel Sambuc     // remember the original working directory when the test program
637*f4a2713aSLionel Sambuc     // was first invoked.  We cannot do this in RUN_ALL_TESTS(), as
638*f4a2713aSLionel Sambuc     // the user may have changed the current directory before calling
639*f4a2713aSLionel Sambuc     // RUN_ALL_TESTS().  Therefore we capture the current directory in
640*f4a2713aSLionel Sambuc     // AddTestInfo(), which is called to register a TEST or TEST_F
641*f4a2713aSLionel Sambuc     // before main() is reached.
642*f4a2713aSLionel Sambuc     if (original_working_dir_.IsEmpty()) {
643*f4a2713aSLionel Sambuc       original_working_dir_.Set(FilePath::GetCurrentDir());
644*f4a2713aSLionel Sambuc       GTEST_CHECK_(!original_working_dir_.IsEmpty())
645*f4a2713aSLionel Sambuc           << "Failed to get the current working directory.";
646*f4a2713aSLionel Sambuc     }
647*f4a2713aSLionel Sambuc 
648*f4a2713aSLionel Sambuc     GetTestCase(test_info->test_case_name(),
649*f4a2713aSLionel Sambuc                 test_info->type_param(),
650*f4a2713aSLionel Sambuc                 set_up_tc,
651*f4a2713aSLionel Sambuc                 tear_down_tc)->AddTestInfo(test_info);
652*f4a2713aSLionel Sambuc   }
653*f4a2713aSLionel Sambuc 
654*f4a2713aSLionel Sambuc #if GTEST_HAS_PARAM_TEST
655*f4a2713aSLionel Sambuc   // Returns ParameterizedTestCaseRegistry object used to keep track of
656*f4a2713aSLionel Sambuc   // value-parameterized tests and instantiate and register them.
parameterized_test_registry()657*f4a2713aSLionel Sambuc   internal::ParameterizedTestCaseRegistry& parameterized_test_registry() {
658*f4a2713aSLionel Sambuc     return parameterized_test_registry_;
659*f4a2713aSLionel Sambuc   }
660*f4a2713aSLionel Sambuc #endif  // GTEST_HAS_PARAM_TEST
661*f4a2713aSLionel Sambuc 
662*f4a2713aSLionel Sambuc   // Sets the TestCase object for the test that's currently running.
set_current_test_case(TestCase * a_current_test_case)663*f4a2713aSLionel Sambuc   void set_current_test_case(TestCase* a_current_test_case) {
664*f4a2713aSLionel Sambuc     current_test_case_ = a_current_test_case;
665*f4a2713aSLionel Sambuc   }
666*f4a2713aSLionel Sambuc 
667*f4a2713aSLionel Sambuc   // Sets the TestInfo object for the test that's currently running.  If
668*f4a2713aSLionel Sambuc   // current_test_info is NULL, the assertion results will be stored in
669*f4a2713aSLionel Sambuc   // ad_hoc_test_result_.
set_current_test_info(TestInfo * a_current_test_info)670*f4a2713aSLionel Sambuc   void set_current_test_info(TestInfo* a_current_test_info) {
671*f4a2713aSLionel Sambuc     current_test_info_ = a_current_test_info;
672*f4a2713aSLionel Sambuc   }
673*f4a2713aSLionel Sambuc 
674*f4a2713aSLionel Sambuc   // Registers all parameterized tests defined using TEST_P and
675*f4a2713aSLionel Sambuc   // INSTANTIATE_TEST_CASE_P, creating regular tests for each test/parameter
676*f4a2713aSLionel Sambuc   // combination. This method can be called more then once; it has guards
677*f4a2713aSLionel Sambuc   // protecting from registering the tests more then once.  If
678*f4a2713aSLionel Sambuc   // value-parameterized tests are disabled, RegisterParameterizedTests is
679*f4a2713aSLionel Sambuc   // present but does nothing.
680*f4a2713aSLionel Sambuc   void RegisterParameterizedTests();
681*f4a2713aSLionel Sambuc 
682*f4a2713aSLionel Sambuc   // Runs all tests in this UnitTest object, prints the result, and
683*f4a2713aSLionel Sambuc   // returns true if all tests are successful.  If any exception is
684*f4a2713aSLionel Sambuc   // thrown during a test, this test is considered to be failed, but
685*f4a2713aSLionel Sambuc   // the rest of the tests will still be run.
686*f4a2713aSLionel Sambuc   bool RunAllTests();
687*f4a2713aSLionel Sambuc 
688*f4a2713aSLionel Sambuc   // Clears the results of all tests, except the ad hoc tests.
ClearNonAdHocTestResult()689*f4a2713aSLionel Sambuc   void ClearNonAdHocTestResult() {
690*f4a2713aSLionel Sambuc     ForEach(test_cases_, TestCase::ClearTestCaseResult);
691*f4a2713aSLionel Sambuc   }
692*f4a2713aSLionel Sambuc 
693*f4a2713aSLionel Sambuc   // Clears the results of ad-hoc test assertions.
ClearAdHocTestResult()694*f4a2713aSLionel Sambuc   void ClearAdHocTestResult() {
695*f4a2713aSLionel Sambuc     ad_hoc_test_result_.Clear();
696*f4a2713aSLionel Sambuc   }
697*f4a2713aSLionel Sambuc 
698*f4a2713aSLionel Sambuc   enum ReactionToSharding {
699*f4a2713aSLionel Sambuc     HONOR_SHARDING_PROTOCOL,
700*f4a2713aSLionel Sambuc     IGNORE_SHARDING_PROTOCOL
701*f4a2713aSLionel Sambuc   };
702*f4a2713aSLionel Sambuc 
703*f4a2713aSLionel Sambuc   // Matches the full name of each test against the user-specified
704*f4a2713aSLionel Sambuc   // filter to decide whether the test should run, then records the
705*f4a2713aSLionel Sambuc   // result in each TestCase and TestInfo object.
706*f4a2713aSLionel Sambuc   // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests
707*f4a2713aSLionel Sambuc   // based on sharding variables in the environment.
708*f4a2713aSLionel Sambuc   // Returns the number of tests that should run.
709*f4a2713aSLionel Sambuc   int FilterTests(ReactionToSharding shard_tests);
710*f4a2713aSLionel Sambuc 
711*f4a2713aSLionel Sambuc   // Prints the names of the tests matching the user-specified filter flag.
712*f4a2713aSLionel Sambuc   void ListTestsMatchingFilter();
713*f4a2713aSLionel Sambuc 
current_test_case()714*f4a2713aSLionel Sambuc   const TestCase* current_test_case() const { return current_test_case_; }
current_test_info()715*f4a2713aSLionel Sambuc   TestInfo* current_test_info() { return current_test_info_; }
current_test_info()716*f4a2713aSLionel Sambuc   const TestInfo* current_test_info() const { return current_test_info_; }
717*f4a2713aSLionel Sambuc 
718*f4a2713aSLionel Sambuc   // Returns the vector of environments that need to be set-up/torn-down
719*f4a2713aSLionel Sambuc   // before/after the tests are run.
environments()720*f4a2713aSLionel Sambuc   std::vector<Environment*>& environments() { return environments_; }
721*f4a2713aSLionel Sambuc 
722*f4a2713aSLionel Sambuc   // Getters for the per-thread Google Test trace stack.
gtest_trace_stack()723*f4a2713aSLionel Sambuc   std::vector<TraceInfo>& gtest_trace_stack() {
724*f4a2713aSLionel Sambuc     return *(gtest_trace_stack_.pointer());
725*f4a2713aSLionel Sambuc   }
gtest_trace_stack()726*f4a2713aSLionel Sambuc   const std::vector<TraceInfo>& gtest_trace_stack() const {
727*f4a2713aSLionel Sambuc     return gtest_trace_stack_.get();
728*f4a2713aSLionel Sambuc   }
729*f4a2713aSLionel Sambuc 
730*f4a2713aSLionel Sambuc #if GTEST_HAS_DEATH_TEST
InitDeathTestSubprocessControlInfo()731*f4a2713aSLionel Sambuc   void InitDeathTestSubprocessControlInfo() {
732*f4a2713aSLionel Sambuc     internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
733*f4a2713aSLionel Sambuc   }
734*f4a2713aSLionel Sambuc   // Returns a pointer to the parsed --gtest_internal_run_death_test
735*f4a2713aSLionel Sambuc   // flag, or NULL if that flag was not specified.
736*f4a2713aSLionel Sambuc   // This information is useful only in a death test child process.
737*f4a2713aSLionel Sambuc   // Must not be called before a call to InitGoogleTest.
internal_run_death_test_flag()738*f4a2713aSLionel Sambuc   const InternalRunDeathTestFlag* internal_run_death_test_flag() const {
739*f4a2713aSLionel Sambuc     return internal_run_death_test_flag_.get();
740*f4a2713aSLionel Sambuc   }
741*f4a2713aSLionel Sambuc 
742*f4a2713aSLionel Sambuc   // Returns a pointer to the current death test factory.
death_test_factory()743*f4a2713aSLionel Sambuc   internal::DeathTestFactory* death_test_factory() {
744*f4a2713aSLionel Sambuc     return death_test_factory_.get();
745*f4a2713aSLionel Sambuc   }
746*f4a2713aSLionel Sambuc 
747*f4a2713aSLionel Sambuc   void SuppressTestEventsIfInSubprocess();
748*f4a2713aSLionel Sambuc 
749*f4a2713aSLionel Sambuc   friend class ReplaceDeathTestFactory;
750*f4a2713aSLionel Sambuc #endif  // GTEST_HAS_DEATH_TEST
751*f4a2713aSLionel Sambuc 
752*f4a2713aSLionel Sambuc   // Initializes the event listener performing XML output as specified by
753*f4a2713aSLionel Sambuc   // UnitTestOptions. Must not be called before InitGoogleTest.
754*f4a2713aSLionel Sambuc   void ConfigureXmlOutput();
755*f4a2713aSLionel Sambuc 
756*f4a2713aSLionel Sambuc #if GTEST_CAN_STREAM_RESULTS_
757*f4a2713aSLionel Sambuc   // Initializes the event listener for streaming test results to a socket.
758*f4a2713aSLionel Sambuc   // Must not be called before InitGoogleTest.
759*f4a2713aSLionel Sambuc   void ConfigureStreamingOutput();
760*f4a2713aSLionel Sambuc #endif
761*f4a2713aSLionel Sambuc 
762*f4a2713aSLionel Sambuc   // Performs initialization dependent upon flag values obtained in
763*f4a2713aSLionel Sambuc   // ParseGoogleTestFlagsOnly.  Is called from InitGoogleTest after the call to
764*f4a2713aSLionel Sambuc   // ParseGoogleTestFlagsOnly.  In case a user neglects to call InitGoogleTest
765*f4a2713aSLionel Sambuc   // this function is also called from RunAllTests.  Since this function can be
766*f4a2713aSLionel Sambuc   // called more than once, it has to be idempotent.
767*f4a2713aSLionel Sambuc   void PostFlagParsingInit();
768*f4a2713aSLionel Sambuc 
769*f4a2713aSLionel Sambuc   // Gets the random seed used at the start of the current test iteration.
random_seed()770*f4a2713aSLionel Sambuc   int random_seed() const { return random_seed_; }
771*f4a2713aSLionel Sambuc 
772*f4a2713aSLionel Sambuc   // Gets the random number generator.
random()773*f4a2713aSLionel Sambuc   internal::Random* random() { return &random_; }
774*f4a2713aSLionel Sambuc 
775*f4a2713aSLionel Sambuc   // Shuffles all test cases, and the tests within each test case,
776*f4a2713aSLionel Sambuc   // making sure that death tests are still run first.
777*f4a2713aSLionel Sambuc   void ShuffleTests();
778*f4a2713aSLionel Sambuc 
779*f4a2713aSLionel Sambuc   // Restores the test cases and tests to their order before the first shuffle.
780*f4a2713aSLionel Sambuc   void UnshuffleTests();
781*f4a2713aSLionel Sambuc 
782*f4a2713aSLionel Sambuc   // Returns the value of GTEST_FLAG(catch_exceptions) at the moment
783*f4a2713aSLionel Sambuc   // UnitTest::Run() starts.
catch_exceptions()784*f4a2713aSLionel Sambuc   bool catch_exceptions() const { return catch_exceptions_; }
785*f4a2713aSLionel Sambuc 
786*f4a2713aSLionel Sambuc  private:
787*f4a2713aSLionel Sambuc   friend class ::testing::UnitTest;
788*f4a2713aSLionel Sambuc 
789*f4a2713aSLionel Sambuc   // Used by UnitTest::Run() to capture the state of
790*f4a2713aSLionel Sambuc   // GTEST_FLAG(catch_exceptions) at the moment it starts.
set_catch_exceptions(bool value)791*f4a2713aSLionel Sambuc   void set_catch_exceptions(bool value) { catch_exceptions_ = value; }
792*f4a2713aSLionel Sambuc 
793*f4a2713aSLionel Sambuc   // The UnitTest object that owns this implementation object.
794*f4a2713aSLionel Sambuc   UnitTest* const parent_;
795*f4a2713aSLionel Sambuc 
796*f4a2713aSLionel Sambuc   // The working directory when the first TEST() or TEST_F() was
797*f4a2713aSLionel Sambuc   // executed.
798*f4a2713aSLionel Sambuc   internal::FilePath original_working_dir_;
799*f4a2713aSLionel Sambuc 
800*f4a2713aSLionel Sambuc   // The default test part result reporters.
801*f4a2713aSLionel Sambuc   DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
802*f4a2713aSLionel Sambuc   DefaultPerThreadTestPartResultReporter
803*f4a2713aSLionel Sambuc       default_per_thread_test_part_result_reporter_;
804*f4a2713aSLionel Sambuc 
805*f4a2713aSLionel Sambuc   // Points to (but doesn't own) the global test part result reporter.
806*f4a2713aSLionel Sambuc   TestPartResultReporterInterface* global_test_part_result_repoter_;
807*f4a2713aSLionel Sambuc 
808*f4a2713aSLionel Sambuc   // Protects read and write access to global_test_part_result_reporter_.
809*f4a2713aSLionel Sambuc   internal::Mutex global_test_part_result_reporter_mutex_;
810*f4a2713aSLionel Sambuc 
811*f4a2713aSLionel Sambuc   // Points to (but doesn't own) the per-thread test part result reporter.
812*f4a2713aSLionel Sambuc   internal::ThreadLocal<TestPartResultReporterInterface*>
813*f4a2713aSLionel Sambuc       per_thread_test_part_result_reporter_;
814*f4a2713aSLionel Sambuc 
815*f4a2713aSLionel Sambuc   // The vector of environments that need to be set-up/torn-down
816*f4a2713aSLionel Sambuc   // before/after the tests are run.
817*f4a2713aSLionel Sambuc   std::vector<Environment*> environments_;
818*f4a2713aSLionel Sambuc 
819*f4a2713aSLionel Sambuc   // The vector of TestCases in their original order.  It owns the
820*f4a2713aSLionel Sambuc   // elements in the vector.
821*f4a2713aSLionel Sambuc   std::vector<TestCase*> test_cases_;
822*f4a2713aSLionel Sambuc 
823*f4a2713aSLionel Sambuc   // Provides a level of indirection for the test case list to allow
824*f4a2713aSLionel Sambuc   // easy shuffling and restoring the test case order.  The i-th
825*f4a2713aSLionel Sambuc   // element of this vector is the index of the i-th test case in the
826*f4a2713aSLionel Sambuc   // shuffled order.
827*f4a2713aSLionel Sambuc   std::vector<int> test_case_indices_;
828*f4a2713aSLionel Sambuc 
829*f4a2713aSLionel Sambuc #if GTEST_HAS_PARAM_TEST
830*f4a2713aSLionel Sambuc   // ParameterizedTestRegistry object used to register value-parameterized
831*f4a2713aSLionel Sambuc   // tests.
832*f4a2713aSLionel Sambuc   internal::ParameterizedTestCaseRegistry parameterized_test_registry_;
833*f4a2713aSLionel Sambuc 
834*f4a2713aSLionel Sambuc   // Indicates whether RegisterParameterizedTests() has been called already.
835*f4a2713aSLionel Sambuc   bool parameterized_tests_registered_;
836*f4a2713aSLionel Sambuc #endif  // GTEST_HAS_PARAM_TEST
837*f4a2713aSLionel Sambuc 
838*f4a2713aSLionel Sambuc   // Index of the last death test case registered.  Initially -1.
839*f4a2713aSLionel Sambuc   int last_death_test_case_;
840*f4a2713aSLionel Sambuc 
841*f4a2713aSLionel Sambuc   // This points to the TestCase for the currently running test.  It
842*f4a2713aSLionel Sambuc   // changes as Google Test goes through one test case after another.
843*f4a2713aSLionel Sambuc   // When no test is running, this is set to NULL and Google Test
844*f4a2713aSLionel Sambuc   // stores assertion results in ad_hoc_test_result_.  Initially NULL.
845*f4a2713aSLionel Sambuc   TestCase* current_test_case_;
846*f4a2713aSLionel Sambuc 
847*f4a2713aSLionel Sambuc   // This points to the TestInfo for the currently running test.  It
848*f4a2713aSLionel Sambuc   // changes as Google Test goes through one test after another.  When
849*f4a2713aSLionel Sambuc   // no test is running, this is set to NULL and Google Test stores
850*f4a2713aSLionel Sambuc   // assertion results in ad_hoc_test_result_.  Initially NULL.
851*f4a2713aSLionel Sambuc   TestInfo* current_test_info_;
852*f4a2713aSLionel Sambuc 
853*f4a2713aSLionel Sambuc   // Normally, a user only writes assertions inside a TEST or TEST_F,
854*f4a2713aSLionel Sambuc   // or inside a function called by a TEST or TEST_F.  Since Google
855*f4a2713aSLionel Sambuc   // Test keeps track of which test is current running, it can
856*f4a2713aSLionel Sambuc   // associate such an assertion with the test it belongs to.
857*f4a2713aSLionel Sambuc   //
858*f4a2713aSLionel Sambuc   // If an assertion is encountered when no TEST or TEST_F is running,
859*f4a2713aSLionel Sambuc   // Google Test attributes the assertion result to an imaginary "ad hoc"
860*f4a2713aSLionel Sambuc   // test, and records the result in ad_hoc_test_result_.
861*f4a2713aSLionel Sambuc   TestResult ad_hoc_test_result_;
862*f4a2713aSLionel Sambuc 
863*f4a2713aSLionel Sambuc   // The list of event listeners that can be used to track events inside
864*f4a2713aSLionel Sambuc   // Google Test.
865*f4a2713aSLionel Sambuc   TestEventListeners listeners_;
866*f4a2713aSLionel Sambuc 
867*f4a2713aSLionel Sambuc   // The OS stack trace getter.  Will be deleted when the UnitTest
868*f4a2713aSLionel Sambuc   // object is destructed.  By default, an OsStackTraceGetter is used,
869*f4a2713aSLionel Sambuc   // but the user can set this field to use a custom getter if that is
870*f4a2713aSLionel Sambuc   // desired.
871*f4a2713aSLionel Sambuc   OsStackTraceGetterInterface* os_stack_trace_getter_;
872*f4a2713aSLionel Sambuc 
873*f4a2713aSLionel Sambuc   // True iff PostFlagParsingInit() has been called.
874*f4a2713aSLionel Sambuc   bool post_flag_parse_init_performed_;
875*f4a2713aSLionel Sambuc 
876*f4a2713aSLionel Sambuc   // The random number seed used at the beginning of the test run.
877*f4a2713aSLionel Sambuc   int random_seed_;
878*f4a2713aSLionel Sambuc 
879*f4a2713aSLionel Sambuc   // Our random number generator.
880*f4a2713aSLionel Sambuc   internal::Random random_;
881*f4a2713aSLionel Sambuc 
882*f4a2713aSLionel Sambuc   // How long the test took to run, in milliseconds.
883*f4a2713aSLionel Sambuc   TimeInMillis elapsed_time_;
884*f4a2713aSLionel Sambuc 
885*f4a2713aSLionel Sambuc #if GTEST_HAS_DEATH_TEST
886*f4a2713aSLionel Sambuc   // The decomposed components of the gtest_internal_run_death_test flag,
887*f4a2713aSLionel Sambuc   // parsed when RUN_ALL_TESTS is called.
888*f4a2713aSLionel Sambuc   internal::scoped_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
889*f4a2713aSLionel Sambuc   internal::scoped_ptr<internal::DeathTestFactory> death_test_factory_;
890*f4a2713aSLionel Sambuc #endif  // GTEST_HAS_DEATH_TEST
891*f4a2713aSLionel Sambuc 
892*f4a2713aSLionel Sambuc   // A per-thread stack of traces created by the SCOPED_TRACE() macro.
893*f4a2713aSLionel Sambuc   internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
894*f4a2713aSLionel Sambuc 
895*f4a2713aSLionel Sambuc   // The value of GTEST_FLAG(catch_exceptions) at the moment RunAllTests()
896*f4a2713aSLionel Sambuc   // starts.
897*f4a2713aSLionel Sambuc   bool catch_exceptions_;
898*f4a2713aSLionel Sambuc 
899*f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);
900*f4a2713aSLionel Sambuc };  // class UnitTestImpl
901*f4a2713aSLionel Sambuc 
902*f4a2713aSLionel Sambuc // Convenience function for accessing the global UnitTest
903*f4a2713aSLionel Sambuc // implementation object.
GetUnitTestImpl()904*f4a2713aSLionel Sambuc inline UnitTestImpl* GetUnitTestImpl() {
905*f4a2713aSLionel Sambuc   return UnitTest::GetInstance()->impl();
906*f4a2713aSLionel Sambuc }
907*f4a2713aSLionel Sambuc 
908*f4a2713aSLionel Sambuc #if GTEST_USES_SIMPLE_RE
909*f4a2713aSLionel Sambuc 
910*f4a2713aSLionel Sambuc // Internal helper functions for implementing the simple regular
911*f4a2713aSLionel Sambuc // expression matcher.
912*f4a2713aSLionel Sambuc GTEST_API_ bool IsInSet(char ch, const char* str);
913*f4a2713aSLionel Sambuc GTEST_API_ bool IsAsciiDigit(char ch);
914*f4a2713aSLionel Sambuc GTEST_API_ bool IsAsciiPunct(char ch);
915*f4a2713aSLionel Sambuc GTEST_API_ bool IsRepeat(char ch);
916*f4a2713aSLionel Sambuc GTEST_API_ bool IsAsciiWhiteSpace(char ch);
917*f4a2713aSLionel Sambuc GTEST_API_ bool IsAsciiWordChar(char ch);
918*f4a2713aSLionel Sambuc GTEST_API_ bool IsValidEscape(char ch);
919*f4a2713aSLionel Sambuc GTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch);
920*f4a2713aSLionel Sambuc GTEST_API_ bool ValidateRegex(const char* regex);
921*f4a2713aSLionel Sambuc GTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str);
922*f4a2713aSLionel Sambuc GTEST_API_ bool MatchRepetitionAndRegexAtHead(
923*f4a2713aSLionel Sambuc     bool escaped, char ch, char repeat, const char* regex, const char* str);
924*f4a2713aSLionel Sambuc GTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str);
925*f4a2713aSLionel Sambuc 
926*f4a2713aSLionel Sambuc #endif  // GTEST_USES_SIMPLE_RE
927*f4a2713aSLionel Sambuc 
928*f4a2713aSLionel Sambuc // Parses the command line for Google Test flags, without initializing
929*f4a2713aSLionel Sambuc // other parts of Google Test.
930*f4a2713aSLionel Sambuc GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv);
931*f4a2713aSLionel Sambuc GTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv);
932*f4a2713aSLionel Sambuc 
933*f4a2713aSLionel Sambuc #if GTEST_HAS_DEATH_TEST
934*f4a2713aSLionel Sambuc 
935*f4a2713aSLionel Sambuc // Returns the message describing the last system error, regardless of the
936*f4a2713aSLionel Sambuc // platform.
937*f4a2713aSLionel Sambuc GTEST_API_ String GetLastErrnoDescription();
938*f4a2713aSLionel Sambuc 
939*f4a2713aSLionel Sambuc # if GTEST_OS_WINDOWS
940*f4a2713aSLionel Sambuc // Provides leak-safe Windows kernel handle ownership.
941*f4a2713aSLionel Sambuc class AutoHandle {
942*f4a2713aSLionel Sambuc  public:
AutoHandle()943*f4a2713aSLionel Sambuc   AutoHandle() : handle_(INVALID_HANDLE_VALUE) {}
AutoHandle(HANDLE handle)944*f4a2713aSLionel Sambuc   explicit AutoHandle(HANDLE handle) : handle_(handle) {}
945*f4a2713aSLionel Sambuc 
~AutoHandle()946*f4a2713aSLionel Sambuc   ~AutoHandle() { Reset(); }
947*f4a2713aSLionel Sambuc 
Get()948*f4a2713aSLionel Sambuc   HANDLE Get() const { return handle_; }
Reset()949*f4a2713aSLionel Sambuc   void Reset() { Reset(INVALID_HANDLE_VALUE); }
Reset(HANDLE handle)950*f4a2713aSLionel Sambuc   void Reset(HANDLE handle) {
951*f4a2713aSLionel Sambuc     if (handle != handle_) {
952*f4a2713aSLionel Sambuc       if (handle_ != INVALID_HANDLE_VALUE)
953*f4a2713aSLionel Sambuc         ::CloseHandle(handle_);
954*f4a2713aSLionel Sambuc       handle_ = handle;
955*f4a2713aSLionel Sambuc     }
956*f4a2713aSLionel Sambuc   }
957*f4a2713aSLionel Sambuc 
958*f4a2713aSLionel Sambuc  private:
959*f4a2713aSLionel Sambuc   HANDLE handle_;
960*f4a2713aSLionel Sambuc 
961*f4a2713aSLionel Sambuc   GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);
962*f4a2713aSLionel Sambuc };
963*f4a2713aSLionel Sambuc # endif  // GTEST_OS_WINDOWS
964*f4a2713aSLionel Sambuc 
965*f4a2713aSLionel Sambuc // Attempts to parse a string into a positive integer pointed to by the
966*f4a2713aSLionel Sambuc // number parameter.  Returns true if that is possible.
967*f4a2713aSLionel Sambuc // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use
968*f4a2713aSLionel Sambuc // it here.
969*f4a2713aSLionel Sambuc template <typename Integer>
ParseNaturalNumber(const::std::string & str,Integer * number)970*f4a2713aSLionel Sambuc bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
971*f4a2713aSLionel Sambuc   // Fail fast if the given string does not begin with a digit;
972*f4a2713aSLionel Sambuc   // this bypasses strtoXXX's "optional leading whitespace and plus
973*f4a2713aSLionel Sambuc   // or minus sign" semantics, which are undesirable here.
974*f4a2713aSLionel Sambuc   if (str.empty() || !IsDigit(str[0])) {
975*f4a2713aSLionel Sambuc     return false;
976*f4a2713aSLionel Sambuc   }
977*f4a2713aSLionel Sambuc   errno = 0;
978*f4a2713aSLionel Sambuc 
979*f4a2713aSLionel Sambuc   char* end;
980*f4a2713aSLionel Sambuc   // BiggestConvertible is the largest integer type that system-provided
981*f4a2713aSLionel Sambuc   // string-to-number conversion routines can return.
982*f4a2713aSLionel Sambuc 
983*f4a2713aSLionel Sambuc # if GTEST_OS_WINDOWS && !defined(__GNUC__)
984*f4a2713aSLionel Sambuc 
985*f4a2713aSLionel Sambuc   // MSVC and C++ Builder define __int64 instead of the standard long long.
986*f4a2713aSLionel Sambuc   typedef unsigned __int64 BiggestConvertible;
987*f4a2713aSLionel Sambuc   const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10);
988*f4a2713aSLionel Sambuc 
989*f4a2713aSLionel Sambuc # else
990*f4a2713aSLionel Sambuc 
991*f4a2713aSLionel Sambuc   typedef unsigned long long BiggestConvertible;  // NOLINT
992*f4a2713aSLionel Sambuc   const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
993*f4a2713aSLionel Sambuc 
994*f4a2713aSLionel Sambuc # endif  // GTEST_OS_WINDOWS && !defined(__GNUC__)
995*f4a2713aSLionel Sambuc 
996*f4a2713aSLionel Sambuc   const bool parse_success = *end == '\0' && errno == 0;
997*f4a2713aSLionel Sambuc 
998*f4a2713aSLionel Sambuc   // TODO(vladl@google.com): Convert this to compile time assertion when it is
999*f4a2713aSLionel Sambuc   // available.
1000*f4a2713aSLionel Sambuc   GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed));
1001*f4a2713aSLionel Sambuc 
1002*f4a2713aSLionel Sambuc   const Integer result = static_cast<Integer>(parsed);
1003*f4a2713aSLionel Sambuc   if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
1004*f4a2713aSLionel Sambuc     *number = result;
1005*f4a2713aSLionel Sambuc     return true;
1006*f4a2713aSLionel Sambuc   }
1007*f4a2713aSLionel Sambuc   return false;
1008*f4a2713aSLionel Sambuc }
1009*f4a2713aSLionel Sambuc #endif  // GTEST_HAS_DEATH_TEST
1010*f4a2713aSLionel Sambuc 
1011*f4a2713aSLionel Sambuc // TestResult contains some private methods that should be hidden from
1012*f4a2713aSLionel Sambuc // Google Test user but are required for testing. This class allow our tests
1013*f4a2713aSLionel Sambuc // to access them.
1014*f4a2713aSLionel Sambuc //
1015*f4a2713aSLionel Sambuc // This class is supplied only for the purpose of testing Google Test's own
1016*f4a2713aSLionel Sambuc // constructs. Do not use it in user tests, either directly or indirectly.
1017*f4a2713aSLionel Sambuc class TestResultAccessor {
1018*f4a2713aSLionel Sambuc  public:
RecordProperty(TestResult * test_result,const TestProperty & property)1019*f4a2713aSLionel Sambuc   static void RecordProperty(TestResult* test_result,
1020*f4a2713aSLionel Sambuc                              const TestProperty& property) {
1021*f4a2713aSLionel Sambuc     test_result->RecordProperty(property);
1022*f4a2713aSLionel Sambuc   }
1023*f4a2713aSLionel Sambuc 
ClearTestPartResults(TestResult * test_result)1024*f4a2713aSLionel Sambuc   static void ClearTestPartResults(TestResult* test_result) {
1025*f4a2713aSLionel Sambuc     test_result->ClearTestPartResults();
1026*f4a2713aSLionel Sambuc   }
1027*f4a2713aSLionel Sambuc 
test_part_results(const TestResult & test_result)1028*f4a2713aSLionel Sambuc   static const std::vector<testing::TestPartResult>& test_part_results(
1029*f4a2713aSLionel Sambuc       const TestResult& test_result) {
1030*f4a2713aSLionel Sambuc     return test_result.test_part_results();
1031*f4a2713aSLionel Sambuc   }
1032*f4a2713aSLionel Sambuc };
1033*f4a2713aSLionel Sambuc 
1034*f4a2713aSLionel Sambuc }  // namespace internal
1035*f4a2713aSLionel Sambuc }  // namespace testing
1036*f4a2713aSLionel Sambuc 
1037*f4a2713aSLionel Sambuc #endif  // GTEST_SRC_GTEST_INTERNAL_INL_H_
1038