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" 137 const unsigned int raw_seed = (random_seed_flag == 0) ?
139 static_cast<unsigned int>(random_seed_flag);
143 const int normalized_seed =
144 static_cast<int>((raw_seed - 1U) %
145 static_cast<unsigned int>(kMaxRandomSeed)) + 1;
146 return normalized_seed;
154 <<
"Invalid random seed " << seed <<
" - must be in [1, " 155 << kMaxRandomSeed <<
"].";
156 const int next_seed = seed + 1;
157 return (next_seed > kMaxRandomSeed) ? 1 : next_seed;
162 class GTestFlagSaver {
263 const char* shard_index_str,
264 bool in_subprocess_for_death_test);
276 int total_shards,
int shard_index,
int test_id);
282 template <
class Container,
typename Predicate>
283 inline int CountIf(
const Container&
c, Predicate predicate) {
287 for (
typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {
295 template <
class Container,
typename Functor>
296 void ForEach(
const Container& c, Functor functor) {
302 template <
typename E>
304 return (i < 0 || i >= static_cast<int>(v.size())) ? default_value : v[
i];
311 template <
typename E>
314 const int size =
static_cast<int>(v->size());
316 <<
"Invalid shuffle range start " << begin <<
": must be in range [0, " 319 <<
"Invalid shuffle range finish " << end <<
": must be in range [" 320 << begin <<
", " << size <<
"].";
324 for (
int range_width = end - begin; range_width >= 2; range_width--) {
325 const int last_in_range = begin + range_width - 1;
326 const int selected = begin + random->
Generate(range_width);
327 std::swap((*v)[selected], (*v)[last_in_range]);
332 template <
typename E>
334 ShuffleRange(random, 0, static_cast<int>(v->size()), v);
339 template <
typename T>
356 return test_property.
key() == key_;
392 static bool PatternMatchesString(
const char *pattern,
const char *str);
396 static bool FilterMatchesTest(
const std::string &test_case_name,
405 static int GTestShouldProcessSEH(DWORD exception_code);
406 #endif // GTEST_OS_WINDOWS 429 virtual string CurrentStackTrace(
int max_depth,
int skip_count) = 0;
434 virtual void UponLeavingGTest() = 0;
438 static const char*
const kElidedFramesMarker;
449 virtual string CurrentStackTrace(
int max_depth,
int skip_count);
450 virtual void UponLeavingGTest();
502 virtual ~UnitTestImpl();
515 void SetGlobalTestPartResultReporter(
522 void SetTestPartResultReporterForCurrentThread(
526 int successful_test_case_count()
const;
529 int failed_test_case_count()
const;
532 int total_test_case_count()
const;
536 int test_case_to_run_count()
const;
539 int successful_test_count()
const;
542 int failed_test_count()
const;
545 int reportable_disabled_test_count()
const;
548 int disabled_test_count()
const;
551 int reportable_test_count()
const;
554 int total_test_count()
const;
557 int test_to_run_count()
const;
567 bool Passed()
const {
return !Failed(); }
572 return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();
578 const int index =
GetElementOr(test_case_indices_, i, -1);
579 return index < 0 ? NULL : test_cases_[
i];
585 const int index =
GetElementOr(test_case_indices_, i, -1);
586 return index < 0 ? NULL : test_cases_[index];
633 TestCase* GetTestCase(
const char* test_case_name,
634 const char* type_param,
658 <<
"Failed to get the current working directory.";
664 tear_down_tc)->AddTestInfo(test_info);
667 #if GTEST_HAS_PARAM_TEST 670 internal::ParameterizedTestCaseRegistry& parameterized_test_registry() {
671 return parameterized_test_registry_;
673 #endif // GTEST_HAS_PARAM_TEST 677 current_test_case_ = a_current_test_case;
684 current_test_info_ = a_current_test_info;
693 void RegisterParameterizedTests();
708 ad_hoc_test_result_.Clear();
718 HONOR_SHARDING_PROTOCOL,
719 IGNORE_SHARDING_PROTOCOL
731 void ListTestsMatchingFilter();
743 return *(gtest_trace_stack_.pointer());
746 return gtest_trace_stack_.get();
749 #if GTEST_HAS_DEATH_TEST 750 void InitDeathTestSubprocessControlInfo() {
751 internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());
757 const InternalRunDeathTestFlag* internal_run_death_test_flag()
const {
758 return internal_run_death_test_flag_.get();
762 internal::DeathTestFactory* death_test_factory() {
763 return death_test_factory_.get();
766 void SuppressTestEventsIfInSubprocess();
768 friend class ReplaceDeathTestFactory;
769 #endif // GTEST_HAS_DEATH_TEST 773 void ConfigureXmlOutput();
775 #if GTEST_CAN_STREAM_RESULTS_ 778 void ConfigureStreamingOutput();
786 void PostFlagParsingInit();
799 void UnshuffleTests();
806 friend class ::testing::UnitTest;
822 default_per_thread_test_part_result_reporter_;
832 per_thread_test_part_result_reporter_;
836 std::vector<Environment*> environments_;
840 std::vector<TestCase*> test_cases_;
846 std::vector<int> test_case_indices_;
848 #if GTEST_HAS_PARAM_TEST 851 internal::ParameterizedTestCaseRegistry parameterized_test_registry_;
854 bool parameterized_tests_registered_;
855 #endif // GTEST_HAS_PARAM_TEST 858 int last_death_test_case_;
893 bool post_flag_parse_init_performed_;
908 #if GTEST_HAS_DEATH_TEST 913 #endif // GTEST_HAS_DEATH_TEST 931 #if GTEST_USES_SIMPLE_RE 942 GTEST_API_ bool AtomMatchesChar(
bool escaped,
char pattern,
char ch);
944 GTEST_API_ bool MatchRegexAtHead(
const char* regex,
const char* str);
945 GTEST_API_ bool MatchRepetitionAndRegexAtHead(
946 bool escaped,
char ch,
char repeat,
const char* regex,
const char* str);
947 GTEST_API_ bool MatchRegexAnywhere(
const char* regex,
const char* str);
949 #endif // GTEST_USES_SIMPLE_RE 956 #if GTEST_HAS_DEATH_TEST 966 template <
typename Integer>
971 if (str.empty() || !
IsDigit(str[0])) {
980 # if GTEST_OS_WINDOWS && !defined(__GNUC__) 983 typedef unsigned __int64 BiggestConvertible;
984 const BiggestConvertible parsed = _strtoui64(str.c_str(), &
end, 10);
988 typedef unsigned long long BiggestConvertible;
989 const BiggestConvertible parsed = strtoull(str.c_str(), &
end, 10);
991 # endif // GTEST_OS_WINDOWS && !defined(__GNUC__) 993 const bool parse_success = *end ==
'\0' && errno == 0;
1000 if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {
1006 #endif // GTEST_HAS_DEATH_TEST 1032 #if GTEST_CAN_STREAM_RESULTS_ 1038 class AbstractSocketWriter {
1040 virtual ~AbstractSocketWriter() {}
1043 virtual void Send(
const string&
message) = 0;
1046 virtual void CloseConnection() {}
1049 void SendLn(
const string& message) {
1050 Send(message +
"\n");
1055 class SocketWriter :
public AbstractSocketWriter {
1057 SocketWriter(
const string& host,
const string& port)
1058 : sockfd_(-1), host_name_(host), port_num_(port) {
1062 virtual ~SocketWriter() {
1068 virtual void Send(
const string&
message) {
1070 <<
"Send() can be called only when there is a connection.";
1072 const int len =
static_cast<int>(message.length());
1073 if (
write(sockfd_, message.c_str(), len) != len) {
1075 <<
"stream_result_to: failed to stream to " 1076 << host_name_ <<
":" << port_num_;
1082 void MakeConnection();
1085 void CloseConnection() {
1087 <<
"CloseConnection() can be called only when there is a connection.";
1094 const string host_name_;
1095 const string port_num_;
1101 static string UrlEncode(
const char* str);
1103 StreamingListener(
const string& host,
const string& port)
1104 : socket_writer_(
new SocketWriter(host, port)) { Start(); }
1106 explicit StreamingListener(AbstractSocketWriter* socket_writer)
1107 : socket_writer_(socket_writer) { Start(); }
1109 void OnTestProgramStart(
const UnitTest& ) {
1110 SendLn(
"event=TestProgramStart");
1113 void OnTestProgramEnd(
const UnitTest& unit_test) {
1116 SendLn(
"event=TestProgramEnd&passed=" + FormatBool(unit_test.
Passed()));
1119 socket_writer_->CloseConnection();
1122 void OnTestIterationStart(
const UnitTest& ,
int iteration) {
1123 SendLn(
"event=TestIterationStart&iteration=" +
1127 void OnTestIterationEnd(
const UnitTest& unit_test,
int ) {
1128 SendLn(
"event=TestIterationEnd&passed=" +
1129 FormatBool(unit_test.
Passed()) +
"&elapsed_time=" +
1133 void OnTestCaseStart(
const TestCase& test_case) {
1137 void OnTestCaseEnd(
const TestCase& test_case) {
1138 SendLn(
"event=TestCaseEnd&passed=" + FormatBool(test_case.
Passed())
1143 void OnTestStart(
const TestInfo& test_info) {
1147 void OnTestEnd(
const TestInfo& test_info) {
1148 SendLn(
"event=TestEnd&passed=" +
1149 FormatBool((test_info.
result())->Passed()) +
1155 const char* file_name = test_part_result.
file_name();
1156 if (file_name == NULL)
1158 SendLn(
"event=TestPartResult&file=" + UrlEncode(file_name) +
1160 "&message=" + UrlEncode(test_part_result.
message()));
1165 void SendLn(
const string&
message) { socket_writer_->SendLn(message); }
1169 void Start() { SendLn(
"gtest_streaming_protocol_version=1.0"); }
1171 string FormatBool(
bool value) {
return value ?
"1" :
"0"; }
1178 #endif // GTEST_CAN_STREAM_RESULTS_ 1183 #endif // GTEST_SRC_GTEST_INTERNAL_INL_H_ GTEST_API_ bool g_help_flag
FilePath original_working_dir_
class UnitTestImpl * GetUnitTestImpl()
internal::SetUpTestCaseFunc SetUpTestCaseFunc
std::vector< Environment * > & environments()
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)
Integral2 random(Integral1 low, Integral2 up)
TimeInMillis elapsed_time() const
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)
void write(const T &in, folly::io::Appender &appender)
const char kAlsoRunDisabledTestsFlag[]
PUSHMI_INLINE_VAR constexpr detail::filter_fn filter
const char kListTestsFlag[]
bool death_test_use_fork_
void set_current_test_info(TestInfo *a_current_test_info)
class GTEST_API_ testing::internal::ScopedTrace GTEST_ATTRIBUTE_UNUSED_
const char kStreamResultToFlag[]
TypeWithSize< 4 >::Int Int32
const char * message() const
int GetNextRandomSeed(int seed)
#define GTEST_LOG_(severity)
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_DECLARE_bool_(also_run_disabled_tests)
auto begin(TestAdlIterable &instance)
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[]
constexpr auto size(C const &c) -> decltype(c.size())
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)
#define GTEST_CHECK_(condition)
GTEST_API_ const TypeId kTestTypeIdInGoogleTest
auto end(TestAdlIterable &instance)
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
void repeat(F const &func)
const TestCase * current_test_case() const
void ClearNonAdHocTestResult()
virtual ~OsStackTraceGetterInterface()
GTEST_API_ FilePath GetCurrentExecutableName()
GTEST_API_ TimeInMillis GetTimeInMillis()
static const char *const value
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 swap(SwapTrackingAlloc< T > &, SwapTrackingAlloc< T > &)
void ClearTestPartResults()
void for_each(T const &range, Function< void(typename T::value_type const &) const > const &func)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
void set_catch_exceptions(bool value)
OsStackTraceGetterInterface()
const char * type_param() const
TypeWithSize< 8 >::Int TimeInMillis
UInt32 Generate(UInt32 range)
void ForEach(const Container &c, Functor functor)
GTEST_API_ Int32 Int32FromEnvOrDie(const char *env_var, Int32 default_val)
TimeInMillis elapsed_time() const
void WriteToShardStatusFileIfNeeded()
std::string stream_result_to_
int close(NetworkSocket s)
const char kPrintTimeFlag[]
TestInfo * current_test_info()
const char kThrowOnFailureFlag[]
folly::Function< void()> parent
GTEST_API_ bool ParseInt32Flag(const char *str, const char *flag, Int32 *value)
const char kFlagfileFlag[]
TestCase * GetMutableTestCase(int i)