37#ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_
38#define GTEST_SRC_GTEST_INTERNAL_INL_H_
53#if GTEST_CAN_STREAM_RESULTS_
54# include <arpa/inet.h>
99const int kMaxRandomSeed = 99999;
125 const char* str,
const char* flag,
Int32*
value);
130 const unsigned int raw_seed = (random_seed_flag == 0) ?
132 static_cast<unsigned int>(random_seed_flag);
136 const int normalized_seed =
137 static_cast<int>((raw_seed - 1U) %
139 return normalized_seed;
147 <<
"Invalid random seed " << seed <<
" - must be in [1, "
148 << kMaxRandomSeed <<
"].";
149 const int next_seed = seed + 1;
150 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
159 also_run_disabled_tests_ =
GTEST_FLAG(also_run_disabled_tests);
160 break_on_failure_ =
GTEST_FLAG(break_on_failure);
161 catch_exceptions_ =
GTEST_FLAG(catch_exceptions);
163 death_test_style_ =
GTEST_FLAG(death_test_style);
164 death_test_use_fork_ =
GTEST_FLAG(death_test_use_fork);
166 internal_run_death_test_ =
GTEST_FLAG(internal_run_death_test);
174 stack_trace_depth_ =
GTEST_FLAG(stack_trace_depth);
175 stream_result_to_ =
GTEST_FLAG(stream_result_to);
176 throw_on_failure_ =
GTEST_FLAG(throw_on_failure);
181 GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;
182 GTEST_FLAG(break_on_failure) = break_on_failure_;
183 GTEST_FLAG(catch_exceptions) = catch_exceptions_;
185 GTEST_FLAG(death_test_style) = death_test_style_;
186 GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;
188 GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;
196 GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;
197 GTEST_FLAG(stream_result_to) = stream_result_to_;
198 GTEST_FLAG(throw_on_failure) = throw_on_failure_;
203 bool also_run_disabled_tests_;
204 bool break_on_failure_;
205 bool catch_exceptions_;
207 std::string death_test_style_;
208 bool death_test_use_fork_;
210 std::string internal_run_death_test_;
219 std::string stream_result_to_;
220 bool throw_on_failure_;
259 const char* shard_index_str,
260 bool in_subprocess_for_death_test);
272 int total_shards,
int shard_index,
int test_id);
278template <
class Container,
typename Predicate>
279inline int CountIf(
const Container& c, Predicate predicate) {
283 for (
typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
291template <
class Container,
typename Functor>
292void ForEach(
const Container& c, Functor functor) {
293 std::for_each(c.begin(), c.end(), functor);
299inline E
GetElementOr(
const std::vector<E>& v,
int i, E default_value) {
300 return (i < 0 || i >=
static_cast<int>(v.size())) ? default_value : v[i];
310 const int size =
static_cast<int>(v->size());
312 <<
"Invalid shuffle range start " << begin <<
": must be in range [0, "
315 <<
"Invalid shuffle range finish " << end <<
": must be in range ["
316 << begin <<
", " << size <<
"].";
320 for (
int range_width = end - begin; range_width >= 2; range_width--) {
321 const int last_in_range = begin + range_width - 1;
322 const int selected = begin +
random->Generate(range_width);
323 std::swap((*v)[selected], (*v)[last_in_range]);
336static void Delete(
T* x) {
352 return test_property.
key() == key_;
374 static std::string GetOutputFormat();
379 static std::string GetAbsolutePathToOutputFile();
388 static bool PatternMatchesString(
const char *pattern,
const char *str);
392 static bool FilterMatchesTest(
const std::string &test_case_name,
393 const std::string &test_name);
401 static int GTestShouldProcessSEH(DWORD exception_code);
406 static bool MatchesFilter(
const std::string&
name,
const char* filter);
511 void SetGlobalTestPartResultReporter(
518 void SetTestPartResultReporterForCurrentThread(
522 int successful_test_case_count()
const;
525 int failed_test_case_count()
const;
528 int total_test_case_count()
const;
532 int test_case_to_run_count()
const;
535 int successful_test_count()
const;
538 int failed_test_count()
const;
541 int reportable_disabled_test_count()
const;
544 int disabled_test_count()
const;
547 int reportable_test_count()
const;
550 int total_test_count()
const;
553 int test_to_run_count()
const;
563 bool Passed()
const {
return !Failed(); }
568 return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
574 const int index = GetElementOr(test_case_indices_, i, -1);
575 return index < 0 ? NULL : test_cases_[i];
581 const int index = GetElementOr(test_case_indices_, i, -1);
582 return index < 0 ? NULL : test_cases_[index];
629 TestCase* GetTestCase(
const char* test_case_name,
630 const char* type_param,
654 <<
"Failed to get the current working directory.";
660 tear_down_tc)->AddTestInfo(test_info);
666 return parameterized_test_registry_;
671 current_test_case_ = a_current_test_case;
678 current_test_info_ = a_current_test_info;
687 void RegisterParameterizedTests();
697 ForEach(test_cases_, TestCase::ClearTestCaseResult);
702 ad_hoc_test_result_.Clear();
713 IGNORE_SHARDING_PROTOCOL
722 int FilterTests(ReactionToSharding shard_tests);
725 void ListTestsMatchingFilter();
737 return *(gtest_trace_stack_.pointer());
740 return gtest_trace_stack_.get();
743#if GTEST_HAS_DEATH_TEST
744 void InitDeathTestSubprocessControlInfo() {
745 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
751 const InternalRunDeathTestFlag* internal_run_death_test_flag()
const {
752 return internal_run_death_test_flag_.get();
756 internal::DeathTestFactory* death_test_factory() {
757 return death_test_factory_.get();
760 void SuppressTestEventsIfInSubprocess();
762 friend class ReplaceDeathTestFactory;
767 void ConfigureXmlOutput();
769#if GTEST_CAN_STREAM_RESULTS_
772 void ConfigureStreamingOutput();
780 void PostFlagParsingInit();
793 void UnshuffleTests();
800 friend class ::testing::UnitTest;
804 void set_catch_exceptions(
bool value) { catch_exceptions_ =
value; }
816 default_per_thread_test_part_result_reporter_;
826 per_thread_test_part_result_reporter_;
830 std::vector<Environment*> environments_;
834 std::vector<TestCase*> test_cases_;
840 std::vector<int> test_case_indices_;
847 bool parameterized_tests_registered_;
850 int last_death_test_case_;
885 bool post_flag_parse_init_performed_;
900#if GTEST_HAS_DEATH_TEST
912 bool catch_exceptions_;
923#if GTEST_USES_SIMPLE_RE
927GTEST_API_ bool IsInSet(
char ch,
const char* str);
934GTEST_API_ bool AtomMatchesChar(
bool escaped,
char pattern,
char ch);
935GTEST_API_ bool ValidateRegex(
const char* regex);
936GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char* str);
938 bool escaped,
char ch,
char repeat,
const char* regex,
const char* str);
939GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char* str);
948#if GTEST_HAS_DEATH_TEST
952GTEST_API_ std::string GetLastErrnoDescription();
958template <
typename Integer>
959bool ParseNaturalNumber(const ::std::string& str, Integer* number) {
963 if (str.empty() || !
IsDigit(str[0])) {
972# if GTEST_OS_WINDOWS && !defined(__GNUC__)
975 typedef unsigned __int64 BiggestConvertible;
976 const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10);
980 typedef unsigned long long BiggestConvertible;
981 const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);
985 const bool parse_success = *end ==
'\0' && errno == 0;
991 const Integer result =
static_cast<Integer
>(parsed);
992 if (parse_success &&
static_cast<BiggestConvertible
>(result) == parsed) {
1009 const std::string& xml_element,
1011 test_result->RecordProperty(xml_element, property);
1015 test_result->ClearTestPartResults();
1020 return test_result.test_part_results();
1024#if GTEST_CAN_STREAM_RESULTS_
1030 class AbstractSocketWriter {
1032 virtual ~AbstractSocketWriter() {}
1035 virtual void Send(
const std::string& message) = 0;
1038 virtual void CloseConnection() {}
1041 void SendLn(
const std::string& message) { Send(message +
"\n"); }
1045 class SocketWriter :
public AbstractSocketWriter {
1047 SocketWriter(
const std::string& host,
const std::string& port)
1048 : sockfd_(-1), host_name_(host), port_num_(port) {
1052 virtual ~SocketWriter() {
1058 virtual void Send(
const std::string& message) {
1060 <<
"Send() can be called only when there is a connection.";
1062 const int len =
static_cast<int>(message.length());
1063 if (write(sockfd_, message.c_str(),
len) !=
len) {
1065 <<
"stream_result_to: failed to stream to "
1066 << host_name_ <<
":" << port_num_;
1072 void MakeConnection();
1075 void CloseConnection() {
1077 <<
"CloseConnection() can be called only when there is a connection.";
1084 const std::string host_name_;
1085 const std::string port_num_;
1091 static std::string UrlEncode(
const char* str);
1093 StreamingListener(
const std::string& host,
const std::string& port)
1094 : socket_writer_(new SocketWriter(host, port)) {
1098 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1099 : socket_writer_(socket_writer) { Start(); }
1101 void OnTestProgramStart(
const UnitTest& ) {
1102 SendLn(
"event=TestProgramStart");
1105 void OnTestProgramEnd(
const UnitTest& unit_test) {
1108 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.Passed()));
1111 socket_writer_->CloseConnection();
1114 void OnTestIterationStart(
const UnitTest& ,
int iteration) {
1115 SendLn(
"event=TestIterationStart&iteration=" +
1116 StreamableToString(iteration));
1119 void OnTestIterationEnd(
const UnitTest& unit_test,
int ) {
1120 SendLn(
"event=TestIterationEnd&passed=" +
1121 FormatBool(unit_test.Passed()) +
"&elapsed_time=" +
1122 StreamableToString(unit_test.elapsed_time()) +
"ms");
1125 void OnTestCaseStart(
const TestCase& test_case) {
1126 SendLn(std::string(
"event=TestCaseStart&name=") + test_case.name());
1129 void OnTestCaseEnd(
const TestCase& test_case) {
1130 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_case.Passed())
1131 +
"&elapsed_time=" + StreamableToString(test_case.elapsed_time())
1135 void OnTestStart(
const TestInfo& test_info) {
1136 SendLn(std::string(
"event=TestStart&name=") + test_info.name());
1139 void OnTestEnd(
const TestInfo& test_info) {
1140 SendLn(
"event=TestEnd&passed=" +
1141 FormatBool((test_info.result())->Passed()) +
1143 StreamableToString((test_info.result())->elapsed_time()) +
"ms");
1146 void OnTestPartResult(
const TestPartResult& test_part_result) {
1147 const char* file_name = test_part_result.file_name();
1148 if (file_name == NULL)
1150 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1151 "&line=" + StreamableToString(test_part_result.line_number()) +
1152 "&message=" + UrlEncode(test_part_result.message()));
1157 void SendLn(
const std::string& message) { socket_writer_->SendLn(message); }
1161 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1163 std::string FormatBool(
bool value) {
return value ?
"1" :
"0"; }
1165 const scoped_ptr<AbstractSocketWriter> socket_writer_;
internal::SetUpTestCaseFunc SetUpTestCaseFunc
internal::TearDownTestCaseFunc TearDownTestCaseFunc
const char * type_param() const
const char * test_case_name() const
static UnitTest * GetInstance()
DefaultGlobalTestPartResultReporter(UnitTestImpl *unit_test)
virtual void ReportTestPartResult(const TestPartResult &result)
DefaultPerThreadTestPartResultReporter(UnitTestImpl *unit_test)
virtual void ReportTestPartResult(const TestPartResult &result)
virtual std::string CurrentStackTrace(int max_depth, int skip_count)
virtual void UponLeavingGTest()
virtual std::string CurrentStackTrace(int max_depth, int skip_count)=0
virtual ~OsStackTraceGetterInterface()
static const char *const kElidedFramesMarker
virtual void UponLeavingGTest()=0
OsStackTraceGetterInterface()
TestPropertyKeyIs(const std::string &key)
bool operator()(const TestProperty &test_property) const
static void ClearTestPartResults(TestResult *test_result)
static const std::vector< testing::TestPartResult > & test_part_results(const TestResult &test_result)
static void RecordProperty(TestResult *test_result, const std::string &xml_element, const TestProperty &property)
const std::vector< TraceInfo > & gtest_trace_stack() const
TestCase * GetMutableTestCase(int i)
internal::Random * random()
void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc, Test::TearDownTestCaseFunc tear_down_tc, TestInfo *test_info)
TestEventListeners * listeners()
TimeInMillis elapsed_time() const
const TestResult * ad_hoc_test_result() const
void set_current_test_case(TestCase *a_current_test_case)
void ClearNonAdHocTestResult()
bool catch_exceptions() const
TestInfo * current_test_info()
const TestInfo * current_test_info() const
void set_current_test_info(TestInfo *a_current_test_info)
TimeInMillis start_timestamp() const
const TestCase * GetTestCase(int i) const
std::vector< Environment * > & environments()
void ClearAdHocTestResult()
std::vector< TraceInfo > & gtest_trace_stack()
@ HONOR_SHARDING_PROTOCOL
internal::ParameterizedTestCaseRegistry & parameterized_test_registry()
const TestCase * current_test_case() const
FilePath original_working_dir_
#define GTEST_DECLARE_bool_(name)
#define GTEST_LOG_(severity)
#define GTEST_CHECK_(condition)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
void close(T *e, websocketpp::connection_hdl hdl)
void swap(picojson::value &x, picojson::value &y)
GTEST_API_ std::string WideStringToUtf8(const wchar_t *str, int num_chars)
GTEST_API_ std::string CodePointToUtf8(UInt32 code_point)
void ShuffleRange(internal::Random *random, int begin, int end, std::vector< E > *v)
GTEST_API_ bool ShouldShard(const char *total_shards_str, const char *shard_index_str, bool in_subprocess_for_death_test)
void WriteToShardStatusFileIfNeeded()
const char kListTestsFlag[]
int CountIf(const Container &c, Predicate predicate)
TypeWithSize< 4 >::UInt UInt32
GTEST_API_ bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id)
GTEST_API_ void ParseGoogleTestFlagsOnly(int *argc, char **argv)
void ForEach(const Container &c, Functor functor)
class testing::internal::GTestFlagSaver GTEST_ATTRIBUTE_UNUSED_
GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms)
TypeWithSize< 8 >::Int TimeInMillis
int GetNextRandomSeed(int seed)
E GetElementOr(const std::vector< E > &v, int i, E default_value)
GTEST_API_ FilePath GetCurrentExecutableName()
const char kBreakOnFailureFlag[]
const char kStreamResultToFlag[]
TypeWithSize< 4 >::Int Int32
GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms)
void Shuffle(internal::Random *random, std::vector< E > *v)
GTEST_API_ bool g_help_flag
const char kRandomSeedFlag[]
class UnitTestImpl * GetUnitTestImpl()
const char kFlagfileFlag[]
const char kPrintTimeFlag[]
GTEST_API_ Int32 Int32FromEnvOrDie(const char *env_var, Int32 default_val)
const char kPrintUTF8Flag[]
GTEST_API_ bool ShouldUseColor(bool stdout_is_tty)
GTEST_API_ const TypeId kTestTypeIdInGoogleTest
const char kAlsoRunDisabledTestsFlag[]
const char kStackTraceDepthFlag[]
const char kThrowOnFailureFlag[]
GTEST_API_ bool ParseInt32Flag(const char *str, const char *flag, Int32 *value)
GTEST_API_ TimeInMillis GetTimeInMillis()
int GetRandomSeedFromFlag(Int32 random_seed_flag)
const char kCatchExceptionsFlag[]
const char kShuffleFlag[]
#define T(meth, val, expected)