34 #ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
35 #define GTEST_SRC_GTEST_INTERNAL_INL_H_
51 #if GTEST_CAN_STREAM_RESULTS_
52 # include <arpa/inet.h>
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";
100 const int kMaxRandomSeed = 99999;
110 GTEST_API_ bool ShouldUseColor(
bool stdout_is_tty);
126 const char* str,
const char* flag,
Int32* 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);
137 const int normalized_seed =
138 static_cast<int>((raw_seed - 1U) %
139 static_cast<unsigned int>(kMaxRandomSeed)) + 1;
140 return normalized_seed;
146 inline int GetNextRandomSeed(
int seed) {
148 <<
"Invalid random seed " << seed <<
" - must be in [1, "
149 << kMaxRandomSeed <<
"].";
150 const int next_seed = seed + 1;
151 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
156 class 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);
164 death_test_style_ =
GTEST_FLAG(death_test_style);
165 death_test_use_fork_ =
GTEST_FLAG(death_test_use_fork);
167 internal_run_death_test_ =
GTEST_FLAG(internal_run_death_test);
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);
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_;
186 GTEST_FLAG(death_test_style) = death_test_style_;
187 GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
189 GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
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_;
204 bool also_run_disabled_tests_;
205 bool break_on_failure_;
206 bool catch_exceptions_;
209 bool death_test_use_fork_;
221 bool throw_on_failure_;
251 void WriteToShardStatusFileIfNeeded();
259 GTEST_API_ bool ShouldShard(
const char* total_shards_str,
260 const char* shard_index_str,
261 bool in_subprocess_for_death_test);
273 int total_shards,
int shard_index,
int test_id);
279 template <
class Container,
typename Predicate>
280 inline int CountIf(
const Container& c, Predicate predicate) {
284 for (
typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
292 template <
class Container,
typename Functor>
293 void ForEach(
const Container& c, Functor functor) {
294 std::for_each(c.begin(), c.end(), functor);
299 template <
typename E>
300 inline E GetElementOr(
const std::vector<E>& v,
int i,
E default_value) {
301 return (i < 0 || i >=
static_cast<int>(v.size())) ? default_value : v[i];
308 template <
typename E>
311 const int size =
static_cast<int>(v->size());
313 <<
"Invalid shuffle range start " << begin <<
": must be in range [0, "
316 <<
"Invalid shuffle range finish " <<
end <<
": must be in range ["
317 << begin <<
", " << size <<
"].";
321 for (
int range_width =
end - begin; range_width >= 2; range_width--) {
322 const int last_in_range = begin + range_width - 1;
323 const int selected = begin + random->Generate(range_width);
324 std::swap((*v)[selected], (*v)[last_in_range]);
329 template <
typename E>
331 ShuffleRange(random, 0,
static_cast<int>(v->size()), v);
336 template <
typename T>
337 static void Delete(T* x) {
344 class TestPropertyKeyIs {
349 explicit TestPropertyKeyIs(
const std::string& key) : key_(key) {}
352 bool operator()(
const TestProperty& test_property)
const {
353 return test_property.key() == key_;
389 static bool PatternMatchesString(
const char *pattern,
const char *str);
393 static bool FilterMatchesTest(
const std::string& test_suite_name,
402 static int GTestShouldProcessSEH(DWORD exception_code);
407 static bool MatchesFilter(
const std::string& name,
const char* filter);
412 GTEST_API_ FilePath GetCurrentExecutableName();
415 class OsStackTraceGetterInterface {
417 OsStackTraceGetterInterface() {}
418 virtual ~OsStackTraceGetterInterface() {}
426 virtual std::string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
431 virtual void UponLeavingGTest() = 0;
435 static const char*
const kElidedFramesMarker;
442 class OsStackTraceGetter :
public OsStackTraceGetterInterface {
444 OsStackTraceGetter() {}
446 std::string CurrentStackTrace(
int max_depth,
int skip_count)
override;
447 void UponLeavingGTest()
override;
457 void* caller_frame_ =
nullptr;
472 class DefaultGlobalTestPartResultReporter
473 :
public TestPartResultReporterInterface {
475 explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);
478 void ReportTestPartResult(
const TestPartResult& result)
override;
481 UnitTestImpl*
const unit_test_;
488 class DefaultPerThreadTestPartResultReporter
489 :
public TestPartResultReporterInterface {
491 explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);
494 void ReportTestPartResult(
const TestPartResult& result)
override;
497 UnitTestImpl*
const unit_test_;
508 explicit UnitTestImpl(UnitTest* parent);
509 virtual ~UnitTestImpl();
519 TestPartResultReporterInterface* GetGlobalTestPartResultReporter();
522 void SetGlobalTestPartResultReporter(
523 TestPartResultReporterInterface* reporter);
526 TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();
529 void SetTestPartResultReporterForCurrentThread(
530 TestPartResultReporterInterface* reporter);
533 int successful_test_suite_count()
const;
536 int failed_test_suite_count()
const;
539 int total_test_suite_count()
const;
543 int test_suite_to_run_count()
const;
546 int successful_test_count()
const;
549 int skipped_test_count()
const;
552 int failed_test_count()
const;
555 int reportable_disabled_test_count()
const;
558 int disabled_test_count()
const;
561 int reportable_test_count()
const;
564 int total_test_count()
const;
567 int test_to_run_count()
const;
571 TimeInMillis start_timestamp()
const {
return start_timestamp_; }
574 TimeInMillis elapsed_time()
const {
return elapsed_time_; }
577 bool Passed()
const {
return !Failed(); }
581 bool Failed()
const {
582 return failed_test_suite_count() > 0 || ad_hoc_test_result()->Failed();
587 const TestSuite* GetTestSuite(
int i)
const {
588 const int index = GetElementOr(test_suite_indices_, i, -1);
589 return index < 0 ? nullptr : test_suites_[i];
593 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
594 const TestCase* GetTestCase(
int i)
const {
return GetTestSuite(i); }
599 TestSuite* GetMutableSuiteCase(
int i) {
600 const int index = GetElementOr(test_suite_indices_, i, -1);
601 return index < 0 ? nullptr : test_suites_[index];
605 TestEventListeners* listeners() {
return &listeners_; }
609 TestResult* current_test_result();
612 const TestResult* ad_hoc_test_result()
const {
return &ad_hoc_test_result_; }
619 void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);
624 OsStackTraceGetterInterface* os_stack_trace_getter();
648 TestSuite* GetTestSuite(
const char* test_suite_name,
const char* type_param,
653 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
654 TestCase* GetTestCase(
const char* test_case_name,
const char* type_param,
657 return GetTestSuite(test_case_name, type_param, set_up_tc, tear_down_tc);
670 TestInfo* test_info) {
678 if (original_working_dir_.IsEmpty()) {
679 original_working_dir_.Set(FilePath::GetCurrentDir());
681 <<
"Failed to get the current working directory.";
684 GetTestSuite(test_info->test_suite_name(), test_info->type_param(),
685 set_up_tc, tear_down_tc)
686 ->AddTestInfo(test_info);
691 internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() {
692 return parameterized_test_registry_;
696 void set_current_test_suite(TestSuite* a_current_test_suite) {
697 current_test_suite_ = a_current_test_suite;
703 void set_current_test_info(TestInfo* a_current_test_info) {
704 current_test_info_ = a_current_test_info;
713 void RegisterParameterizedTests();
722 void ClearNonAdHocTestResult() {
723 ForEach(test_suites_, TestSuite::ClearTestSuiteResult);
727 void ClearAdHocTestResult() {
728 ad_hoc_test_result_.Clear();
735 void RecordProperty(
const TestProperty& test_property);
737 enum ReactionToSharding {
738 HONOR_SHARDING_PROTOCOL,
739 IGNORE_SHARDING_PROTOCOL
748 int FilterTests(ReactionToSharding shard_tests);
751 void ListTestsMatchingFilter();
753 const TestSuite* current_test_suite()
const {
return current_test_suite_; }
754 TestInfo* current_test_info() {
return current_test_info_; }
755 const TestInfo* current_test_info()
const {
return current_test_info_; }
759 std::vector<Environment*>& environments() {
return environments_; }
762 std::vector<TraceInfo>& gtest_trace_stack() {
763 return *(gtest_trace_stack_.pointer());
765 const std::vector<TraceInfo>& gtest_trace_stack()
const {
766 return gtest_trace_stack_.get();
769 #if GTEST_HAS_DEATH_TEST
770 void InitDeathTestSubprocessControlInfo() {
771 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
777 const InternalRunDeathTestFlag* internal_run_death_test_flag()
const {
778 return internal_run_death_test_flag_.get();
782 internal::DeathTestFactory* death_test_factory() {
783 return death_test_factory_.get();
786 void SuppressTestEventsIfInSubprocess();
788 friend class ReplaceDeathTestFactory;
793 void ConfigureXmlOutput();
795 #if GTEST_CAN_STREAM_RESULTS_
798 void ConfigureStreamingOutput();
806 void PostFlagParsingInit();
809 int random_seed()
const {
return random_seed_; }
819 void UnshuffleTests();
823 bool catch_exceptions()
const {
return catch_exceptions_; }
826 friend class ::testing::UnitTest;
830 void set_catch_exceptions(
bool value) { catch_exceptions_ = value; }
833 UnitTest*
const parent_;
837 internal::FilePath original_working_dir_;
840 DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;
841 DefaultPerThreadTestPartResultReporter
842 default_per_thread_test_part_result_reporter_;
845 TestPartResultReporterInterface* global_test_part_result_repoter_;
848 internal::Mutex global_test_part_result_reporter_mutex_;
851 internal::ThreadLocal<TestPartResultReporterInterface*>
852 per_thread_test_part_result_reporter_;
856 std::vector<Environment*> environments_;
860 std::vector<TestSuite*> test_suites_;
866 std::vector<int> test_suite_indices_;
870 internal::ParameterizedTestSuiteRegistry parameterized_test_registry_;
873 bool parameterized_tests_registered_;
876 int last_death_test_suite_;
882 TestSuite* current_test_suite_;
888 TestInfo* current_test_info_;
898 TestResult ad_hoc_test_result_;
902 TestEventListeners listeners_;
908 OsStackTraceGetterInterface* os_stack_trace_getter_;
911 bool post_flag_parse_init_performed_;
926 #if GTEST_HAS_DEATH_TEST
929 std::unique_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;
930 std::unique_ptr<internal::DeathTestFactory> death_test_factory_;
934 internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;
938 bool catch_exceptions_;
945 inline UnitTestImpl* GetUnitTestImpl() {
946 return UnitTest::GetInstance()->impl();
949 #if GTEST_USES_SIMPLE_RE
953 GTEST_API_ bool IsInSet(
char ch,
const char* str);
960 GTEST_API_ bool AtomMatchesChar(
bool escaped,
char pattern,
char ch);
961 GTEST_API_ bool ValidateRegex(
const char* regex);
962 GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char* str);
963 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
964 bool escaped,
char ch,
char repeat,
const char* regex,
const char* str);
965 GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char* str);
971 GTEST_API_ void ParseGoogleTestFlagsOnly(
int* argc,
char** argv);
972 GTEST_API_ void ParseGoogleTestFlagsOnly(
int* argc,
wchar_t** argv);
974 #if GTEST_HAS_DEATH_TEST
984 template <
typename Integer>
989 if (str.empty() || !
IsDigit(str[0])) {
998 # if GTEST_OS_WINDOWS && !defined(__GNUC__)
1001 typedef unsigned __int64 BiggestConvertible;
1002 const BiggestConvertible parsed = _strtoui64(str.c_str(), &
end, 10);
1006 typedef unsigned long long BiggestConvertible;
1007 const BiggestConvertible parsed = strtoull(str.c_str(), &
end, 10);
1011 const bool parse_success = *
end ==
'\0' && errno == 0;
1015 const Integer result =
static_cast<Integer
>(parsed);
1016 if (parse_success &&
static_cast<BiggestConvertible
>(result) == parsed) {
1030 class TestResultAccessor {
1032 static void RecordProperty(TestResult* test_result,
1034 const TestProperty& property) {
1035 test_result->RecordProperty(xml_element, property);
1038 static void ClearTestPartResults(TestResult* test_result) {
1039 test_result->ClearTestPartResults();
1042 static const std::vector<testing::TestPartResult>& test_part_results(
1043 const TestResult& test_result) {
1044 return test_result.test_part_results();
1048 #if GTEST_CAN_STREAM_RESULTS_
1051 class StreamingListener :
public EmptyTestEventListener {
1054 class AbstractSocketWriter {
1056 virtual ~AbstractSocketWriter() {}
1059 virtual void Send(
const std::string& message) = 0;
1062 virtual void CloseConnection() {}
1065 void SendLn(
const std::string& message) { Send(message +
"\n"); }
1069 class SocketWriter :
public AbstractSocketWriter {
1072 : sockfd_(-1), host_name_(host), port_num_(port) {
1076 ~SocketWriter()
override {
1084 <<
"Send() can be called only when there is a connection.";
1086 const int len =
static_cast<int>(message.length());
1087 if (write(sockfd_, message.c_str(), len) != len) {
1089 <<
"stream_result_to: failed to stream to "
1090 << host_name_ <<
":" << port_num_;
1096 void MakeConnection();
1099 void CloseConnection()
override {
1101 <<
"CloseConnection() can be called only when there is a connection.";
1118 : socket_writer_(
new SocketWriter(host, port)) {
1122 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1123 : socket_writer_(socket_writer) { Start(); }
1125 void OnTestProgramStart(
const UnitTest& )
override {
1126 SendLn(
"event=TestProgramStart");
1129 void OnTestProgramEnd(
const UnitTest& unit_test)
override {
1132 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1135 socket_writer_->CloseConnection();
1138 void OnTestIterationStart(
const UnitTest& ,
1139 int iteration)
override {
1140 SendLn(
"event=TestIterationStart&iteration=" +
1144 void OnTestIterationEnd(
const UnitTest& unit_test,
1146 SendLn(
"event=TestIterationEnd&passed=" +
1147 FormatBool(unit_test.Passed()) +
"&elapsed_time=" +
1153 void OnTestCaseStart(
const TestCase& test_case)
override {
1154 SendLn(
std::string(
"event=TestCaseStart&name=") + test_case.name());
1159 void OnTestCaseEnd(
const TestCase& test_case)
override {
1160 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_case.Passed()) +
1165 void OnTestStart(
const TestInfo& test_info)
override {
1166 SendLn(
std::string(
"event=TestStart&name=") + test_info.name());
1169 void OnTestEnd(
const TestInfo& test_info)
override {
1170 SendLn(
"event=TestEnd&passed=" +
1171 FormatBool((test_info.result())->Passed()) +
1176 void OnTestPartResult(
const TestPartResult& test_part_result)
override {
1177 const char*
file_name = test_part_result.file_name();
1179 SendLn(
"event=TestPartResult&file=" + UrlEncode(
file_name) +
1181 "&message=" + UrlEncode(test_part_result.message()));
1186 void SendLn(
const std::string& message) { socket_writer_->SendLn(message); }
1190 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1192 std::string FormatBool(
bool value) {
return value ?
"1" :
"0"; }
1194 const std::unique_ptr<AbstractSocketWriter> socket_writer_;
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251) namespace testing
#define GTEST_DECLARE_bool_(name)
#define GTEST_LOG_(severity)
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
#define GTEST_CHECK_(condition)
#define GTEST_ATTRIBUTE_UNUSED_
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
constexpr const char * file_name(const char *str)
__device__ Real Random(cuRNGState &state, Real a, Real b)
Return a random number between a and b.
TypeWithSize< 4 >::UInt UInt32
void(*)() TearDownTestSuiteFunc
void(*)() SetUpTestSuiteFunc
TypeWithSize< 4 >::Int Int32
std::string StreamableToString(const T &streamable)
internal::TimeInMillis TimeInMillis
DEVICEHOST void swap(Real &a, Real &b)