31
31
// This file contains purely Google Test's internal implementation. Please
32
32
// DO NOT #INCLUDE IT IN A USER PROGRAM.
34
#ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
35
#define GTEST_SRC_GTEST_INTERNAL_INL_H_
34
#ifndef GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
35
#define GOOGLETEST_SRC_GTEST_INTERNAL_INL_H_
39
39
#endif // !_WIN32_WCE
40
40
#include <stddef.h>
41
41
#include <stdlib.h> // For strtoll/_strtoul64/malloc/free.
42
42
#include <string.h> // For memmove.
44
44
#include <algorithm>
49
51
#include "gtest/internal/gtest-port.h"
51
53
#if GTEST_CAN_STREAM_RESULTS_
52
# include <arpa/inet.h> // NOLINT
53
# include <netdb.h> // NOLINT
54
#include <arpa/inet.h> // NOLINT
55
#include <netdb.h> // NOLINT
56
58
#if GTEST_OS_WINDOWS
57
# include <windows.h> // NOLINT
58
#endif // GTEST_OS_WINDOWS
59
#include <windows.h> // NOLINT
60
#endif // GTEST_OS_WINDOWS
62
#include "gtest/gtest-spi.h"
60
63
#include "gtest/gtest.h"
61
#include "gtest/gtest-spi.h"
63
65
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
64
66
/* class A needs to have dll-interface to be used by clients of class B */)
68
68
// Declares the flags.
70
70
// We don't want the users to modify this flag in the code, but want
72
72
// declare it here as opposed to in gtest.h.
73
73
GTEST_DECLARE_bool_(death_test_use_fork);
75
76
namespace internal {
77
78
// The value of GetTestTypeId() as seen from within the Google Test
78
79
// library. This is solely for testing GetTestTypeId().
79
80
GTEST_API_ extern const TypeId kTestTypeIdInGoogleTest;
81
// Names of the flags (needed for parsing Google Test flags).
82
const char kAlsoRunDisabledTestsFlag[] = "also_run_disabled_tests";
83
const char kBreakOnFailureFlag[] = "break_on_failure";
84
const char kCatchExceptionsFlag[] = "catch_exceptions";
85
const char kColorFlag[] = "color";
86
const char kFilterFlag[] = "filter";
87
const char kListTestsFlag[] = "list_tests";
88
const char kOutputFlag[] = "output";
89
const char kPrintTimeFlag[] = "print_time";
90
const char kPrintUTF8Flag[] = "print_utf8";
91
const char kRandomSeedFlag[] = "random_seed";
92
const char kRepeatFlag[] = "repeat";
93
const char kShuffleFlag[] = "shuffle";
94
const char kStackTraceDepthFlag[] = "stack_trace_depth";
95
const char kStreamResultToFlag[] = "stream_result_to";
96
const char kThrowOnFailureFlag[] = "throw_on_failure";
97
const char kFlagfileFlag[] = "flagfile";
99
82
// A valid random seed must be in [1, kMaxRandomSeed].
100
83
const int kMaxRandomSeed = 99999;
123
106
// On success, stores the value of the flag in *value, and returns
124
107
// true. On failure, returns false without changing *value.
125
GTEST_API_ bool ParseInt32Flag(
126
const char* str, const char* flag, Int32* value);
108
GTEST_API_ bool ParseFlag(const char* str, const char* flag, int32_t* value);
128
110
// Returns a random seed in range [1, kMaxRandomSeed] based on the
129
111
// given --gtest_random_seed flag value.
130
inline int GetRandomSeedFromFlag(Int32 random_seed_flag) {
131
const unsigned int raw_seed = (random_seed_flag == 0) ?
132
static_cast<unsigned int>(GetTimeInMillis()) :
133
static_cast<unsigned int>(random_seed_flag);
112
inline int GetRandomSeedFromFlag(int32_t random_seed_flag) {
113
const unsigned int raw_seed =
114
(random_seed_flag == 0) ? static_cast<unsigned int>(GetTimeInMillis())
115
: static_cast<unsigned int>(random_seed_flag);
135
117
// Normalizes the actual seed to range [1, kMaxRandomSeed] such that
136
118
// it's easy to type.
137
119
const int normalized_seed =
138
120
static_cast<int>((raw_seed - 1U) %
139
static_cast<unsigned int>(kMaxRandomSeed)) + 1;
121
static_cast<unsigned int>(kMaxRandomSeed)) +
140
123
return normalized_seed;
159
142
GTestFlagSaver() {
160
also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests);
161
break_on_failure_ = GTEST_FLAG(break_on_failure);
162
catch_exceptions_ = GTEST_FLAG(catch_exceptions);
163
color_ = GTEST_FLAG(color);
164
death_test_style_ = GTEST_FLAG(death_test_style);
165
death_test_use_fork_ = GTEST_FLAG(death_test_use_fork);
166
filter_ = GTEST_FLAG(filter);
167
internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);
168
list_tests_ = GTEST_FLAG(list_tests);
169
output_ = GTEST_FLAG(output);
170
print_time_ = GTEST_FLAG(print_time);
171
print_utf8_ = GTEST_FLAG(print_utf8);
172
random_seed_ = GTEST_FLAG(random_seed);
173
repeat_ = GTEST_FLAG(repeat);
174
shuffle_ = GTEST_FLAG(shuffle);
175
stack_trace_depth_ = GTEST_FLAG(stack_trace_depth);
176
stream_result_to_ = GTEST_FLAG(stream_result_to);
177
throw_on_failure_ = GTEST_FLAG(throw_on_failure);
143
also_run_disabled_tests_ = GTEST_FLAG_GET(also_run_disabled_tests);
144
break_on_failure_ = GTEST_FLAG_GET(break_on_failure);
145
catch_exceptions_ = GTEST_FLAG_GET(catch_exceptions);
146
color_ = GTEST_FLAG_GET(color);
147
death_test_style_ = GTEST_FLAG_GET(death_test_style);
148
death_test_use_fork_ = GTEST_FLAG_GET(death_test_use_fork);
149
fail_fast_ = GTEST_FLAG_GET(fail_fast);
150
filter_ = GTEST_FLAG_GET(filter);
151
internal_run_death_test_ = GTEST_FLAG_GET(internal_run_death_test);
152
list_tests_ = GTEST_FLAG_GET(list_tests);
153
output_ = GTEST_FLAG_GET(output);
154
brief_ = GTEST_FLAG_GET(brief);
155
print_time_ = GTEST_FLAG_GET(print_time);
156
print_utf8_ = GTEST_FLAG_GET(print_utf8);
157
random_seed_ = GTEST_FLAG_GET(random_seed);
158
repeat_ = GTEST_FLAG_GET(repeat);
159
recreate_environments_when_repeating_ =
160
GTEST_FLAG_GET(recreate_environments_when_repeating);
161
shuffle_ = GTEST_FLAG_GET(shuffle);
162
stack_trace_depth_ = GTEST_FLAG_GET(stack_trace_depth);
163
stream_result_to_ = GTEST_FLAG_GET(stream_result_to);
164
throw_on_failure_ = GTEST_FLAG_GET(throw_on_failure);
180
167
// The d'tor is not virtual. DO NOT INHERIT FROM THIS CLASS.
181
168
~GTestFlagSaver() {
182
GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
183
GTEST_FLAG(break_on_failure) = break_on_failure_;
184
GTEST_FLAG(catch_exceptions) = catch_exceptions_;
185
GTEST_FLAG(color) = color_;
186
GTEST_FLAG(death_test_style) = death_test_style_;
187
GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
188
GTEST_FLAG(filter) = filter_;
189
GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
190
GTEST_FLAG(list_tests) = list_tests_;
191
GTEST_FLAG(output) = output_;
192
GTEST_FLAG(print_time) = print_time_;
193
GTEST_FLAG(print_utf8) = print_utf8_;
194
GTEST_FLAG(random_seed) = random_seed_;
195
GTEST_FLAG(repeat) = repeat_;
196
GTEST_FLAG(shuffle) = shuffle_;
197
GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
198
GTEST_FLAG(stream_result_to) = stream_result_to_;
199
GTEST_FLAG(throw_on_failure) = throw_on_failure_;
169
GTEST_FLAG_SET(also_run_disabled_tests, also_run_disabled_tests_);
170
GTEST_FLAG_SET(break_on_failure, break_on_failure_);
171
GTEST_FLAG_SET(catch_exceptions, catch_exceptions_);
172
GTEST_FLAG_SET(color, color_);
173
GTEST_FLAG_SET(death_test_style, death_test_style_);
174
GTEST_FLAG_SET(death_test_use_fork, death_test_use_fork_);
175
GTEST_FLAG_SET(filter, filter_);
176
GTEST_FLAG_SET(fail_fast, fail_fast_);
177
GTEST_FLAG_SET(internal_run_death_test, internal_run_death_test_);
178
GTEST_FLAG_SET(list_tests, list_tests_);
179
GTEST_FLAG_SET(output, output_);
180
GTEST_FLAG_SET(brief, brief_);
181
GTEST_FLAG_SET(print_time, print_time_);
182
GTEST_FLAG_SET(print_utf8, print_utf8_);
183
GTEST_FLAG_SET(random_seed, random_seed_);
184
GTEST_FLAG_SET(repeat, repeat_);
185
GTEST_FLAG_SET(recreate_environments_when_repeating,
186
recreate_environments_when_repeating_);
187
GTEST_FLAG_SET(shuffle, shuffle_);
188
GTEST_FLAG_SET(stack_trace_depth, stack_trace_depth_);
189
GTEST_FLAG_SET(stream_result_to, stream_result_to_);
190
GTEST_FLAG_SET(throw_on_failure, throw_on_failure_);
260
254
const char* shard_index_str,
261
255
bool in_subprocess_for_death_test);
263
// Parses the environment variable var as an Int32. If it is unset,
264
// returns default_val. If it is not an Int32, prints an error and
257
// Parses the environment variable var as a 32-bit integer. If it is unset,
258
// returns default_val. If it is not a 32-bit integer, prints an error and
266
GTEST_API_ Int32 Int32FromEnvOrDie(const char* env_var, Int32 default_val);
260
GTEST_API_ int32_t Int32FromEnvOrDie(const char* env_var, int32_t default_val);
268
262
// Given the total number of shards, the shard index, and the test id,
269
263
// returns true if and only if the test should be run on this shard. The test id
270
264
// is some arbitrary but unique non-negative integer assigned to each test
271
265
// method. Assumes that 0 <= shard_index < total_shards.
272
GTEST_API_ bool ShouldRunTestOnShard(
273
int total_shards, int shard_index, int test_id);
266
GTEST_API_ bool ShouldRunTestOnShard(int total_shards, int shard_index,
275
269
// STL container utilities.
648
646
// test_suite_name: name of the test suite
649
// type_param: the name of the test's type parameter, or NULL if
650
// this is not a typed or a type-parameterized test.
651
// set_up_tc: pointer to the function that sets up the test suite
652
// tear_down_tc: pointer to the function that tears down the test suite
647
// type_param: the name of the test's type parameter, or NULL if
648
// this is not a typed or a type-parameterized test.
649
// set_up_tc: pointer to the function that sets up the test suite
650
// tear_down_tc: pointer to the function that tears down the test suite
653
651
TestSuite* GetTestSuite(const char* test_suite_name, const char* type_param,
654
652
internal::SetUpTestSuiteFunc set_up_tc,
655
653
internal::TearDownTestSuiteFunc tear_down_tc);
1000
1016
// BiggestConvertible is the largest integer type that system-provided
1001
1017
// string-to-number conversion routines can return.
1003
# if GTEST_OS_WINDOWS && !defined(__GNUC__)
1005
// MSVC and C++ Builder define __int64 instead of the standard long long.
1006
typedef unsigned __int64 BiggestConvertible;
1007
const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10);
1011
typedef unsigned long long BiggestConvertible; // NOLINT
1012
const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
1014
# endif // GTEST_OS_WINDOWS && !defined(__GNUC__)
1018
using BiggestConvertible = unsigned long long; // NOLINT
1020
const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10); // NOLINT
1016
1021
const bool parse_success = *end == '\0' && errno == 0;
1018
1023
GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed));
1149
1155
void OnTestIterationEnd(const UnitTest& unit_test,
1150
1156
int /* iteration */) override {
1151
SendLn("event=TestIterationEnd&passed=" +
1152
FormatBool(unit_test.Passed()) + "&elapsed_time=" +
1153
StreamableToString(unit_test.elapsed_time()) + "ms");
1157
SendLn("event=TestIterationEnd&passed=" + FormatBool(unit_test.Passed()) +
1158
"&elapsed_time=" + StreamableToString(unit_test.elapsed_time()) +
1156
1162
// Note that "event=TestCaseStart" is a wire format and has to remain
1157
// "case" for compatibilty
1158
void OnTestCaseStart(const TestCase& test_case) override {
1159
SendLn(std::string("event=TestCaseStart&name=") + test_case.name());
1163
// "case" for compatibility
1164
void OnTestSuiteStart(const TestSuite& test_suite) override {
1165
SendLn(std::string("event=TestCaseStart&name=") + test_suite.name());
1162
1168
// Note that "event=TestCaseEnd" is a wire format and has to remain
1163
// "case" for compatibilty
1164
void OnTestCaseEnd(const TestCase& test_case) override {
1165
SendLn("event=TestCaseEnd&passed=" + FormatBool(test_case.Passed()) +
1166
"&elapsed_time=" + StreamableToString(test_case.elapsed_time()) +
1169
// "case" for compatibility
1170
void OnTestSuiteEnd(const TestSuite& test_suite) override {
1171
SendLn("event=TestCaseEnd&passed=" + FormatBool(test_suite.Passed()) +
1172
"&elapsed_time=" + StreamableToString(test_suite.elapsed_time()) +