37 #ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_ 38 #define GTEST_SRC_GTEST_INTERNAL_INL_H_ 42 #if !GTEST_IMPLEMENTATION_ 44 # error "gtest-internal-inl.h is part of Google Test's internal implementation." 45 # error "It must not be included except by Google Test itself." 46 #endif // GTEST_IMPLEMENTATION_ 59 #include "gtest/internal/gtest-port.h" 61 #if GTEST_CAN_STREAM_RESULTS_ 62 # include <arpa/inet.h> 68 #endif // GTEST_OS_WINDOWS 70 #include "gtest/gtest.h" 71 #include "gtest/gtest-spi.h" 133 const char * str,
const char * flag,
Int32 * value);
139 const unsigned int raw_seed = (random_seed_flag == 0) ?
141 static_cast<unsigned int>(random_seed_flag);
145 const int normalized_seed =
146 static_cast<int>((raw_seed - 1U) %
147 static_cast<unsigned int>(kMaxRandomSeed)) + 1;
148 return normalized_seed;
157 <<
"Invalid random seed " << seed <<
" - must be in [1, " 158 << kMaxRandomSeed <<
"].";
159 const int next_seed = seed + 1;
160 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
269 const char * shard_index_str,
270 bool in_subprocess_for_death_test);
282 int total_shards,
int shard_index,
int test_id);
288 template <
class Container,
typename Predicate>
289 inline int CountIf(
const Container &
c, Predicate predicate)
295 for (
typename Container::const_iterator it = c.begin(); it != c.end(); ++it)
305 template <
class Container,
typename Functor>
306 void ForEach(
const Container & c, Functor functor)
308 std::for_each(c.begin(), c.end(), functor);
313 template <
typename E>
316 return (i < 0 || i >= static_cast<int>(v.size())) ? default_value : v[i];
323 template <
typename E>
327 const int size =
static_cast<int>(v->size());
329 <<
"Invalid shuffle range start " << begin <<
": must be in range [0, " 332 <<
"Invalid shuffle range finish " << end <<
": must be in range [" 333 << begin <<
", " << size <<
"].";
337 for (
int range_width = end - begin; range_width >= 2; range_width--)
339 const int last_in_range = begin + range_width - 1;
340 const int selected = begin + random->
Generate(range_width);
341 std::swap((*v)[selected], (*v)[last_in_range]);
346 template <
typename E>
349 ShuffleRange(random, 0, static_cast<int>(v->size()), v);
354 template <
typename T>
374 return test_property.
key() == key_;
411 static bool PatternMatchesString(
const char * pattern,
const char * str);
415 static bool FilterMatchesTest(
const std::string & test_case_name,
424 static int GTestShouldProcessSEH(DWORD exception_code);
425 #endif // GTEST_OS_WINDOWS 429 static bool MatchesFilter(
const std::string &
name,
const char * filter);
449 virtual string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
454 virtual void UponLeavingGTest() = 0;
466 virtual string CurrentStackTrace(
int max_depth,
int skip_count)
473 static const char *
const kElidedFramesMarker;
482 void * caller_frame_;
536 explicit UnitTestImpl(
UnitTest * parent);
537 virtual ~UnitTestImpl();
550 void SetGlobalTestPartResultReporter(
557 void SetTestPartResultReporterForCurrentThread(
561 int successful_test_case_count()
const;
564 int failed_test_case_count()
const;
567 int total_test_case_count()
const;
571 int test_case_to_run_count()
const;
574 int successful_test_count()
const;
577 int failed_test_count()
const;
580 int reportable_disabled_test_count()
const;
583 int disabled_test_count()
const;
586 int reportable_test_count()
const;
589 int total_test_count()
const;
592 int test_to_run_count()
const;
602 bool Passed()
const {
return !Failed(); }
608 return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
615 const int index =
GetElementOr(test_case_indices_, i, -1);
616 return index < 0 ? NULL : test_cases_[i];
623 const int index =
GetElementOr(test_case_indices_, i, -1);
624 return index < 0 ? NULL : test_cases_[index];
671 TestCase * GetTestCase(
const char * test_case_name,
672 const char * type_param,
698 <<
"Failed to get the current working directory.";
704 tear_down_tc)->AddTestInfo(test_info);
707 #if GTEST_HAS_PARAM_TEST 712 return parameterized_test_registry_;
714 #endif // GTEST_HAS_PARAM_TEST 719 current_test_case_ = a_current_test_case;
727 current_test_info_ = a_current_test_info;
736 void RegisterParameterizedTests();
753 ad_hoc_test_result_.Clear();
760 void RecordProperty(
const TestProperty & test_property);
764 HONOR_SHARDING_PROTOCOL,
765 IGNORE_SHARDING_PROTOCOL
777 void ListTestsMatchingFilter();
790 return *(gtest_trace_stack_.pointer());
794 return gtest_trace_stack_.get();
797 #if GTEST_HAS_DEATH_TEST 798 void InitDeathTestSubprocessControlInfo()
800 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
806 const InternalRunDeathTestFlag * internal_run_death_test_flag()
const 808 return internal_run_death_test_flag_.get();
812 internal::DeathTestFactory * death_test_factory()
814 return death_test_factory_.get();
817 void SuppressTestEventsIfInSubprocess();
819 friend class ReplaceDeathTestFactory;
820 #endif // GTEST_HAS_DEATH_TEST 824 void ConfigureXmlOutput();
826 #if GTEST_CAN_STREAM_RESULTS_ 829 void ConfigureStreamingOutput();
837 void PostFlagParsingInit();
850 void UnshuffleTests();
857 friend class ::testing::UnitTest;
873 default_per_thread_test_part_result_reporter_;
883 per_thread_test_part_result_reporter_;
887 std::vector<Environment *> environments_;
891 std::vector<TestCase *> test_cases_;
897 std::vector<int> test_case_indices_;
899 #if GTEST_HAS_PARAM_TEST 905 bool parameterized_tests_registered_;
906 #endif // GTEST_HAS_PARAM_TEST 909 int last_death_test_case_;
944 bool post_flag_parse_init_performed_;
959 #if GTEST_HAS_DEATH_TEST 964 #endif // GTEST_HAS_DEATH_TEST 983 #if GTEST_USES_SIMPLE_RE 987 GTEST_API_ bool IsInSet(
char ch,
const char * str);
994 GTEST_API_ bool AtomMatchesChar(
bool escaped,
char pattern,
char ch);
995 GTEST_API_ bool ValidateRegex(
const char * regex);
996 GTEST_API_ bool MatchRegexAtHead(
const char * regex,
const char * str);
997 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
998 bool escaped,
char ch,
char repeat,
const char * regex,
const char * str);
999 GTEST_API_ bool MatchRegexAnywhere(
const char * regex,
const char * str);
1001 #endif // GTEST_USES_SIMPLE_RE 1008 #if GTEST_HAS_DEATH_TEST 1014 # if GTEST_OS_WINDOWS 1019 AutoHandle() : handle_(INVALID_HANDLE_VALUE) {}
1020 explicit AutoHandle(HANDLE
handle) : handle_(handle) {}
1022 ~AutoHandle() { Reset(); }
1024 HANDLE Get()
const {
return handle_; }
1025 void Reset() { Reset(INVALID_HANDLE_VALUE); }
1026 void Reset(HANDLE handle)
1028 if (handle != handle_)
1030 if (handle_ != INVALID_HANDLE_VALUE)
1031 { ::CloseHandle(handle_); }
1042 # endif // GTEST_OS_WINDOWS 1048 template <
typename Integer>
1054 if (str.empty() || !
IsDigit(str[0]))
1065 # if GTEST_OS_WINDOWS && !defined(__GNUC__) 1068 typedef unsigned __int64 BiggestConvertible;
1069 const BiggestConvertible parsed = _strtoui64(str.c_str(), &
end, 10);
1073 typedef unsigned long long BiggestConvertible;
1074 const BiggestConvertible parsed = strtoull(str.c_str(), &
end, 10);
1076 # endif // GTEST_OS_WINDOWS && !defined(__GNUC__) 1078 const bool parse_success = *end ==
'\0' && errno == 0;
1084 const Integer result =
static_cast<Integer
>(parsed);
1086 if (parse_success && static_cast<BiggestConvertible>(result) == parsed)
1094 #endif // GTEST_HAS_DEATH_TEST 1124 #if GTEST_CAN_STREAM_RESULTS_ 1131 class AbstractSocketWriter
1134 virtual ~AbstractSocketWriter() {}
1137 virtual void Send(
const string &
message) = 0;
1140 virtual void CloseConnection() {}
1143 void SendLn(
const string & message)
1145 Send(message +
"\n");
1150 class SocketWriter :
public AbstractSocketWriter
1153 SocketWriter(
const string & host,
const string & port)
1154 : sockfd_(-1), host_name_(host), port_num_(port)
1159 virtual ~SocketWriter()
1162 { CloseConnection(); }
1166 virtual void Send(
const string &
message)
1169 <<
"Send() can be called only when there is a connection.";
1171 const int len =
static_cast<int>(message.length());
1173 if (
write(sockfd_, message.c_str(), len) != len)
1176 <<
"stream_result_to: failed to stream to " 1177 << host_name_ <<
":" << port_num_;
1183 void MakeConnection();
1186 void CloseConnection()
1189 <<
"CloseConnection() can be called only when there is a connection.";
1196 const string host_name_;
1197 const string port_num_;
1203 static string UrlEncode(
const char * str);
1205 StreamingListener(
const string & host,
const string & port)
1206 : socket_writer_(
new SocketWriter(host, port)) { Start(); }
1208 explicit StreamingListener(AbstractSocketWriter * socket_writer)
1209 : socket_writer_(socket_writer) { Start(); }
1211 void OnTestProgramStart(
const UnitTest & )
1213 SendLn(
"event=TestProgramStart");
1216 void OnTestProgramEnd(
const UnitTest & unit_test)
1220 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.
Passed()));
1223 socket_writer_->CloseConnection();
1226 void OnTestIterationStart(
const UnitTest & ,
int iteration)
1228 SendLn(
"event=TestIterationStart&iteration=" +
1232 void OnTestIterationEnd(
const UnitTest & unit_test,
int )
1234 SendLn(
"event=TestIterationEnd&passed=" +
1235 FormatBool(unit_test.
Passed()) +
"&elapsed_time=" +
1239 void OnTestCaseStart(
const TestCase & test_case)
1244 void OnTestCaseEnd(
const TestCase & test_case)
1246 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_case.
Passed())
1251 void OnTestStart(
const TestInfo & test_info)
1256 void OnTestEnd(
const TestInfo & test_info)
1258 SendLn(
"event=TestEnd&passed=" +
1259 FormatBool((test_info.
result())->Passed()) +
1266 const char * file_name = test_part_result.
file_name();
1268 if (file_name == NULL)
1271 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1273 "&message=" + UrlEncode(test_part_result.
message()));
1278 void SendLn(
const string &
message) { socket_writer_->SendLn(message); }
1282 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1284 string FormatBool(
bool value) {
return value ?
"1" :
"0"; }
1291 #endif // GTEST_CAN_STREAM_RESULTS_ 1296 #endif // GTEST_SRC_GTEST_INTERNAL_INL_H_
GTEST_API_ bool g_help_flag
UnitTestImpl * GetUnitTestImpl()
internal::SetUpTestCaseFunc SetUpTestCaseFunc
void RecordProperty(const std::string &xml_element, const TestProperty &test_property)
static const std::vector< testing::TestPartResult > & test_part_results(const TestResult &test_result)
int GetRandomSeedFromFlag(Int32 random_seed_flag)
TimeInMillis elapsed_time() const
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
const TestResult * ad_hoc_test_result() const
std::string death_test_style_
TestPropertyKeyIs(const std::string &key)
GTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms)
internal::Int32 random_seed_
E GetElementOr(const std::vector< E > &v, int i, E default_value)
const char kAlsoRunDisabledTestsFlag[]
const char kListTestsFlag[]
bool death_test_use_fork_
void set_current_test_info(TestInfo *a_current_test_info)
class testing::internal::GTestFlagSaver GTEST_ATTRIBUTE_UNUSED_
std::vector< Environment * > & environments()
const char kStreamResultToFlag[]
TypeWithSize< 4 >::Int Int32
const char * message() const
int GetNextRandomSeed(int seed)
const std::vector< TestPartResult > & test_part_results() const
GTEST_API_ bool ShouldUseColor(bool stdout_is_tty)
TypeWithSize< 4 >::UInt UInt32
GTEST_API_ bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id)
TimeInMillis start_timestamp() const
std::string internal_run_death_test_
void ClearAdHocTestResult()
GTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms)
void set_current_test_case(TestCase *a_current_test_case)
internal::TearDownTestCaseFunc TearDownTestCaseFunc
internal::Int32 stack_trace_depth_
static void ClearTestPartResults(TestResult *test_result)
GTEST_API_ bool ShouldShard(const char *total_shards_str, const char *shard_index_str, bool in_subprocess_for_death_test)
GTEST_API_ std::string CodePointToUtf8(UInt32 code_point)
bool catch_exceptions() const
const char kBreakOnFailureFlag[]
const char kRandomSeedFlag[]
std::string StreamableToString(const T &streamable)
const char kShuffleFlag[]
bool write(ros_opcua_srvs::Write::Request &req, ros_opcua_srvs::Write::Response &res)
GTEST_DECLARE_bool_(death_test_use_fork)
const char * name() const
const std::vector< TraceInfo > & gtest_trace_stack() const
void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc, Test::TearDownTestCaseFunc tear_down_tc, TestInfo *test_info)
UInt32 Generate(UInt32 range)
GTEST_API_ const TypeId kTestTypeIdInGoogleTest
static void RecordProperty(TestResult *test_result, const std::string &xml_element, const TestProperty &property)
const TestCase * GetTestCase(int i) const
const char * file_name() const
const TestCase * current_test_case() const
void ClearNonAdHocTestResult()
virtual ~OsStackTraceGetterInterface()
GTEST_API_ FilePath GetCurrentExecutableName()
GTEST_API_ TimeInMillis GetTimeInMillis()
const TestInfo * current_test_info() const
const char * test_case_name() const
GTEST_API_ std::string WideStringToUtf8(const wchar_t *str, int num_chars)
void ShuffleRange(internal::Random *random, int begin, int end, std::vector< E > *v)
static FilePath GetCurrentDir()
int CountIf(const Container &c, Predicate predicate)
internal::UnitTestImpl * impl()
const char kStackTraceDepthFlag[]
const char kCatchExceptionsFlag[]
bool operator()(const TestProperty &test_property) const
static void ClearTestCaseResult(TestCase *test_case)
TestEventListeners * listeners()
GTEST_API_ void ParseGoogleTestFlagsOnly(int *argc, char **argv)
void Shuffle(internal::Random *random, std::vector< E > *v)
internal::Random * random()
std::vector< TraceInfo > & gtest_trace_stack()
const char * name() const
TimeInMillis elapsed_time() const
const TestResult * result() const
bool also_run_disabled_tests_
static UnitTest * GetInstance()
void ClearTestPartResults()
void set_catch_exceptions(bool value)
OsStackTraceGetterInterface()
const char * type_param() const
TypeWithSize< 8 >::Int TimeInMillis
void ForEach(const Container &c, Functor functor)
#define GTEST_CHECK_(condition)
GTEST_API_ Int32 Int32FromEnvOrDie(const char *env_var, Int32 default_val)
TimeInMillis elapsed_time() const
void WriteToShardStatusFileIfNeeded()
std::string stream_result_to_
#define GTEST_LOG_(severity)
const char kPrintTimeFlag[]
TestInfo * current_test_info()
const char kThrowOnFailureFlag[]
GTEST_API_ bool ParseInt32Flag(const char *str, const char *flag, Int32 *value)
FilePath original_working_dir_
#define GTEST_LOCK_EXCLUDED_(locks)
TestCase * GetMutableTestCase(int i)