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