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