42 #ifdef GTEST_HAS_DEATH_TEST
44 #ifdef GTEST_OS_WINDOWS
51 #endif // GTEST_OS_WINDOWS
62 #endif // GTEST_OS_LINUX
67 namespace posix = ::testing::internal::posix;
69 using testing::ContainsRegex;
72 using testing::internal::DeathTest;
73 using testing::internal::DeathTestFactory;
74 using testing::internal::FilePath;
75 using testing::internal::GetLastErrnoDescription;
77 using testing::internal::InDeathTestChild;
78 using testing::internal::ParseNaturalNumber;
85 class ReplaceDeathTestFactory {
87 explicit ReplaceDeathTestFactory(DeathTestFactory* new_factory)
89 old_factory_ = unit_test_impl_->death_test_factory_.release();
90 unit_test_impl_->death_test_factory_.reset(new_factory);
93 ~ReplaceDeathTestFactory() {
94 unit_test_impl_->death_test_factory_.release();
95 unit_test_impl_->death_test_factory_.reset(old_factory_);
100 ReplaceDeathTestFactory(
const ReplaceDeathTestFactory&);
101 void operator=(
const ReplaceDeathTestFactory&);
103 UnitTestImpl* unit_test_impl_;
104 DeathTestFactory* old_factory_;
112 void DieWithMessage(const ::std::string& message) {
113 fprintf(stderr,
"%s", message.c_str());
127 void DieInside(const ::std::string&
function) {
128 DieWithMessage(
"death inside " +
function +
"().");
135 TestForDeathTest() : original_dir_(FilePath::GetCurrentDir()) {}
137 ~TestForDeathTest()
override {
posix::ChDir(original_dir_.c_str()); }
140 static void StaticMemberFunction() { DieInside(
"StaticMemberFunction"); }
143 void MemberFunction() {
144 if (should_die_) DieInside(
"MemberFunction");
149 const FilePath original_dir_;
155 explicit MayDie(
bool should_die) : should_die_(should_die) {}
158 void MemberFunction()
const {
159 if (should_die_) DieInside(
"MayDie::MemberFunction");
168 void GlobalFunction() { DieInside(
"GlobalFunction"); }
171 int NonVoidFunction() {
172 DieInside(
"NonVoidFunction");
177 void DieIf(
bool should_die) {
178 if (should_die) DieInside(
"DieIf");
182 bool DieIfLessThan(
int x,
int y) {
184 DieInside(
"DieIfLessThan");
190 void DeathTestSubroutine() {
191 EXPECT_DEATH(GlobalFunction(),
"death.*GlobalFunction");
192 ASSERT_DEATH(GlobalFunction(),
"death.*GlobalFunction");
196 int DieInDebugElse12(
int* sideeffect) {
197 if (sideeffect) *sideeffect = 12;
201 DieInside(
"DieInDebugElse12");
208 #ifdef GTEST_OS_WINDOWS
211 int DieInCRTDebugElse12(
int* sideeffect) {
212 if (sideeffect) *sideeffect = 12;
216 EXPECT_EQ(_pipe(fdpipe, 256, O_BINARY), 0);
226 #endif // GTEST_OS_WINDOWS
228 #if defined(GTEST_OS_WINDOWS) || defined(GTEST_OS_FUCHSIA)
231 TEST(ExitStatusPredicateTest, ExitedWithCode) {
246 static int NormalExitStatus(
int exit_code) {
247 pid_t child_pid = fork();
248 if (child_pid == 0) {
252 waitpid(child_pid, &status, 0);
261 static int KilledExitStatus(
int signum) {
262 pid_t child_pid = fork();
263 if (child_pid == 0) {
268 waitpid(child_pid, &status, 0);
273 TEST(ExitStatusPredicateTest, ExitedWithCode) {
274 const int status0 = NormalExitStatus(0);
275 const int status1 = NormalExitStatus(1);
276 const int status42 = NormalExitStatus(42);
277 const testing::ExitedWithCode pred0(0);
278 const testing::ExitedWithCode pred1(1);
279 const testing::ExitedWithCode pred42(42);
289 TEST(ExitStatusPredicateTest, KilledBySignal) {
290 const int status_segv = KilledExitStatus(SIGSEGV);
291 const int status_kill = KilledExitStatus(SIGKILL);
292 const testing::KilledBySignal pred_segv(SIGSEGV);
293 const testing::KilledBySignal pred_kill(SIGKILL);
294 #if !(defined(GTEST_OS_LINUX_ANDROID) && __ANDROID_API__ <= 21)
302 #endif // GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA
306 #pragma GCC diagnostic push
307 #pragma GCC diagnostic ignored "-Wdangling-else"
308 #pragma GCC diagnostic ignored "-Wempty-body"
309 #pragma GCC diagnostic ignored "-Wpragmas"
314 TEST_F(TestForDeathTest, SingleStatement) {
317 ASSERT_DEATH(
return,
"");
320 EXPECT_DEATH(_Exit(1),
"");
326 if (
AlwaysFalse()) ASSERT_DEATH(
return,
"") <<
"did not die";
331 EXPECT_DEATH(_Exit(1),
"") << 1 << 2 << 3;
334 #pragma GCC diagnostic pop
339 TEST_F(TestForDeathTest, SwitchStatement) {
346 ASSERT_DEATH(_Exit(1), "") << "exit in default
switch handler";
350 EXPECT_DEATH(_Exit(1), "") << "exit in
switch case";
357 TEST_F(TestForDeathTest, StaticMemberFunctionFastStyle) {
359 ASSERT_DEATH(StaticMemberFunction(),
"death.*StaticMember");
364 TEST_F(TestForDeathTest, MemberFunctionFastStyle) {
367 EXPECT_DEATH(MemberFunction(),
"inside.*MemberFunction");
374 TEST_F(TestForDeathTest, FastDeathTestInChangedDir) {
378 EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1),
"");
381 ASSERT_DEATH(_Exit(1),
"");
384 #ifdef GTEST_OS_LINUX
385 void SigprofAction(
int, siginfo_t*,
void*) {
389 void SetSigprofActionAndTimer() {
390 struct sigaction signal_action;
391 memset(&signal_action, 0,
sizeof(signal_action));
392 sigemptyset(&signal_action.sa_mask);
393 signal_action.sa_sigaction = SigprofAction;
394 signal_action.sa_flags = SA_RESTART | SA_SIGINFO;
395 ASSERT_EQ(0, sigaction(SIGPROF, &signal_action,
nullptr));
398 struct itimerval timer;
399 timer.it_interval.tv_sec = 0;
400 timer.it_interval.tv_usec = 1;
401 timer.it_value = timer.it_interval;
402 ASSERT_EQ(0, setitimer(ITIMER_PROF, &timer,
nullptr));
406 void DisableSigprofActionAndTimer(
struct sigaction* old_signal_action) {
407 struct itimerval timer;
408 timer.it_interval.tv_sec = 0;
409 timer.it_interval.tv_usec = 0;
410 timer.it_value = timer.it_interval;
411 ASSERT_EQ(0, setitimer(ITIMER_PROF, &timer,
nullptr));
412 struct sigaction signal_action;
413 memset(&signal_action, 0,
sizeof(signal_action));
414 sigemptyset(&signal_action.sa_mask);
415 signal_action.sa_handler = SIG_IGN;
416 ASSERT_EQ(0, sigaction(SIGPROF, &signal_action, old_signal_action));
420 TEST_F(TestForDeathTest, FastSigprofActionSet) {
422 SetSigprofActionAndTimer();
423 EXPECT_DEATH(_Exit(1),
"");
424 struct sigaction old_signal_action;
425 DisableSigprofActionAndTimer(&old_signal_action);
426 EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
429 TEST_F(TestForDeathTest, ThreadSafeSigprofActionSet) {
431 SetSigprofActionAndTimer();
432 EXPECT_DEATH(_Exit(1),
"");
433 struct sigaction old_signal_action;
434 DisableSigprofActionAndTimer(&old_signal_action);
435 EXPECT_TRUE(old_signal_action.sa_sigaction == SigprofAction);
437 #endif // GTEST_OS_LINUX
441 TEST_F(TestForDeathTest, StaticMemberFunctionThreadsafeStyle) {
443 ASSERT_DEATH(StaticMemberFunction(),
"death.*StaticMember");
446 TEST_F(TestForDeathTest, MemberFunctionThreadsafeStyle) {
449 EXPECT_DEATH(MemberFunction(),
"inside.*MemberFunction");
452 TEST_F(TestForDeathTest, ThreadsafeDeathTestInLoop) {
455 for (
int i = 0;
i < 3; ++
i)
456 EXPECT_EXIT(_Exit(
i), testing::ExitedWithCode(
i),
"") <<
": i = " <<
i;
459 TEST_F(TestForDeathTest, ThreadsafeDeathTestInChangedDir) {
463 EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1),
"");
466 ASSERT_DEATH(_Exit(1),
"");
469 TEST_F(TestForDeathTest, MixedStyles) {
471 EXPECT_DEATH(_Exit(1),
"");
473 EXPECT_DEATH(_Exit(1),
"");
476 #if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
480 void SetPthreadFlag() { pthread_flag =
true; }
482 TEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) {
485 pthread_flag =
false;
486 ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag,
nullptr,
nullptr));
487 ASSERT_DEATH(_Exit(1),
"");
492 #endif // GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
495 TEST_F(TestForDeathTest, MethodOfAnotherClass) {
496 const MayDie
x(
true);
497 ASSERT_DEATH(x.MemberFunction(),
"MayDie\\:\\:MemberFunction");
501 TEST_F(TestForDeathTest, GlobalFunction) {
502 EXPECT_DEATH(GlobalFunction(),
"GlobalFunction");
507 TEST_F(TestForDeathTest, AcceptsAnythingConvertibleToRE) {
508 static const char regex_c_str[] =
"GlobalFunction";
509 EXPECT_DEATH(GlobalFunction(), regex_c_str);
511 const testing::internal::RE regex(regex_c_str);
512 EXPECT_DEATH(GlobalFunction(), regex);
514 const ::std::string regex_std_str(regex_c_str);
515 EXPECT_DEATH(GlobalFunction(), regex_std_str);
519 EXPECT_DEATH(GlobalFunction(), ::std::string(regex_c_str).c_str());
523 TEST_F(TestForDeathTest, NonVoidFunction) {
524 ASSERT_DEATH(NonVoidFunction(),
"NonVoidFunction");
528 TEST_F(TestForDeathTest, FunctionWithParameter) {
529 EXPECT_DEATH(DieIf(
true),
"DieIf\\(\\)");
530 EXPECT_DEATH(DieIfLessThan(2, 3),
"DieIfLessThan");
534 TEST_F(TestForDeathTest, OutsideFixture) { DeathTestSubroutine(); }
537 TEST_F(TestForDeathTest, InsideLoop) {
538 for (
int i = 0;
i < 5;
i++) {
539 EXPECT_DEATH(DieIfLessThan(-1,
i),
"DieIfLessThan") <<
"where i == " <<
i;
544 TEST_F(TestForDeathTest, CompoundStatement) {
555 TEST_F(TestForDeathTest, DoesNotDie) {
560 TEST_F(TestForDeathTest, ErrorMessageMismatch) {
563 EXPECT_DEATH(DieIf(
true),
"DieIfLessThan")
564 <<
"End of death test message.";
566 "died but not with expected error");
571 void ExpectDeathTestHelper(
bool* aborted) {
573 EXPECT_DEATH(DieIf(
false),
"DieIf");
578 TEST_F(TestForDeathTest, EXPECT_DEATH) {
585 TEST_F(TestForDeathTest, ASSERT_DEATH) {
590 ASSERT_DEATH(DieIf(
false),
"DieIf");
598 TEST_F(TestForDeathTest, SingleEvaluation) {
600 EXPECT_DEATH(DieIf((++x) == 4),
"DieIf");
602 const char* regex =
"DieIf";
603 const char* regex_save = regex;
604 EXPECT_DEATH(DieIfLessThan(3, 4), regex++);
609 TEST_F(TestForDeathTest, RunawayIsFailure) {
616 TEST_F(TestForDeathTest, ReturnIsFailure) {
618 "illegal return in test statement.");
628 TEST_F(TestForDeathTest, TestExpectDebugDeath) {
633 const char* regex =
"death.*DieInDebugElse12";
635 EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), regex)
636 <<
"Must accept a streamed message";
651 #ifdef GTEST_OS_WINDOWS
661 TEST_F(TestForDeathTest, CRTDebugDeath) {
662 EXPECT_DEATH(DieInCRTDebugElse12(
nullptr),
"dup.* : Assertion failed")
663 <<
"Must accept a streamed message";
667 #endif // GTEST_OS_WINDOWS
676 TEST_F(TestForDeathTest, TestAssertDebugDeath) {
679 ASSERT_DEBUG_DEATH(DieInDebugElse12(&sideeffect),
"death.*DieInDebugElse12")
680 <<
"Must accept a streamed message";
697 void ExpectDebugDeathHelper(
bool* aborted) {
699 EXPECT_DEBUG_DEATH(
return,
"") <<
"This is expected to fail.";
703 #ifdef GTEST_OS_WINDOWS
704 TEST(PopUpDeathTest, DoesNotShowPopUpOnAbort) {
706 "This test should be considered failing if it shows "
707 "any pop-up dialogs.\n");
717 #endif // GTEST_OS_WINDOWS
721 TEST_F(TestForDeathTest, ExpectDebugDeathDoesNotAbort) {
727 void AssertDebugDeathHelper(
bool* aborted) {
729 GTEST_LOG_(INFO) <<
"Before ASSERT_DEBUG_DEATH";
730 ASSERT_DEBUG_DEATH(
GTEST_LOG_(INFO) <<
"In ASSERT_DEBUG_DEATH";
return,
"")
731 <<
"This is expected to fail.";
732 GTEST_LOG_(INFO) <<
"After ASSERT_DEBUG_DEATH";
738 TEST_F(TestForDeathTest, AssertDebugDeathAborts) {
745 TEST_F(TestForDeathTest, AssertDebugDeathAborts2) {
752 TEST_F(TestForDeathTest, AssertDebugDeathAborts3) {
759 TEST_F(TestForDeathTest, AssertDebugDeathAborts4) {
766 TEST_F(TestForDeathTest, AssertDebugDeathAborts5) {
773 TEST_F(TestForDeathTest, AssertDebugDeathAborts6) {
780 TEST_F(TestForDeathTest, AssertDebugDeathAborts7) {
787 TEST_F(TestForDeathTest, AssertDebugDeathAborts8) {
794 TEST_F(TestForDeathTest, AssertDebugDeathAborts9) {
801 TEST_F(TestForDeathTest, AssertDebugDeathAborts10) {
811 static void TestExitMacros() {
812 EXPECT_EXIT(_Exit(1), testing::ExitedWithCode(1),
"");
813 ASSERT_EXIT(_Exit(42), testing::ExitedWithCode(42),
"");
815 #ifdef GTEST_OS_WINDOWS
820 EXPECT_EXIT(
raise(SIGABRT), testing::ExitedWithCode(3),
"") <<
"b_ar";
822 #elif !defined(GTEST_OS_FUCHSIA)
825 EXPECT_EXIT(
raise(SIGKILL), testing::KilledBySignal(SIGKILL),
"") <<
"foo";
826 ASSERT_EXIT(
raise(SIGUSR2), testing::KilledBySignal(SIGUSR2),
"") <<
"bar";
830 ASSERT_EXIT(_Exit(0), testing::KilledBySignal(SIGSEGV),
"")
831 <<
"This failure is expected, too.";
833 "This failure is expected, too.");
835 #endif // GTEST_OS_WINDOWS
839 EXPECT_EXIT(
raise(SIGSEGV), testing::ExitedWithCode(0),
"")
840 <<
"This failure is expected.";
842 "This failure is expected.");
845 TEST_F(TestForDeathTest, ExitMacros) { TestExitMacros(); }
847 TEST_F(TestForDeathTest, ExitMacrosUsingFork) {
852 TEST_F(TestForDeathTest, InvalidStyle) {
856 EXPECT_DEATH(_Exit(0),
"") <<
"This failure is expected.";
858 "This failure is expected.");
861 TEST_F(TestForDeathTest, DeathTestFailedOutput) {
864 EXPECT_DEATH(DieWithMessage(
"death\n"),
"expected message"),
866 "[ DEATH ] death\n");
869 TEST_F(TestForDeathTest, DeathTestUnexpectedReturnOutput) {
873 fprintf(stderr,
"returning\n");
878 " Result: illegal return in test statement.\n"
880 "[ DEATH ] returning\n");
883 TEST_F(TestForDeathTest, DeathTestBadExitCodeOutput) {
886 EXPECT_EXIT(DieWithMessage(
"exiting with rc 1\n"),
887 testing::ExitedWithCode(3),
"expected message"),
888 " Result: died but not with expected exit code:\n"
889 " Exited with exit status 1\n"
891 "[ DEATH ] exiting with rc 1\n");
894 TEST_F(TestForDeathTest, DeathTestMultiLineMatchFail) {
897 EXPECT_DEATH(DieWithMessage(
"line 1\nline 2\nline 3\n"),
898 "line 1\nxyz\nline 3\n"),
902 "[ DEATH ] line 3\n");
905 TEST_F(TestForDeathTest, DeathTestMultiLineMatchPass) {
907 EXPECT_DEATH(DieWithMessage(
"line 1\nline 2\nline 3\n"),
908 "line 1\nline 2\nline 3\n");
912 class MockDeathTestFactory :
public DeathTestFactory {
914 MockDeathTestFactory();
915 bool Create(
const char* statement,
917 int line, DeathTest** test)
override;
920 void SetParameters(
bool create, DeathTest::TestRole role,
int status,
924 int AssumeRoleCalls()
const {
return assume_role_calls_; }
925 int WaitCalls()
const {
return wait_calls_; }
926 size_t PassedCalls()
const {
return passed_args_.size(); }
927 bool PassedArgument(
int n)
const {
928 return passed_args_[
static_cast<size_t>(n)];
930 size_t AbortCalls()
const {
return abort_args_.size(); }
931 DeathTest::AbortReason AbortArgument(
int n)
const {
932 return abort_args_[
static_cast<size_t>(n)];
934 bool TestDeleted()
const {
return test_deleted_; }
937 friend class MockDeathTest;
942 DeathTest::TestRole role_;
949 int assume_role_calls_;
954 std::vector<bool> passed_args_;
957 std::vector<DeathTest::AbortReason> abort_args_;
967 class MockDeathTest :
public DeathTest {
969 MockDeathTest(MockDeathTestFactory* parent, TestRole role,
int status,
971 : parent_(parent), role_(role), status_(status), passed_(passed) {}
972 ~MockDeathTest()
override { parent_->test_deleted_ =
true; }
973 TestRole AssumeRole()
override {
974 ++parent_->assume_role_calls_;
977 int Wait()
override {
978 ++parent_->wait_calls_;
981 bool Passed(
bool exit_status_ok)
override {
982 parent_->passed_args_.push_back(exit_status_ok);
985 void Abort(AbortReason reason)
override {
986 parent_->abort_args_.push_back(reason);
990 MockDeathTestFactory*
const parent_;
991 const TestRole role_;
997 MockDeathTestFactory::MockDeathTestFactory()
999 role_(DeathTest::OVERSEE_TEST),
1002 assume_role_calls_(0),
1008 void MockDeathTestFactory::SetParameters(
bool create, DeathTest::TestRole role,
1009 int status,
bool passed) {
1015 assume_role_calls_ = 0;
1017 passed_args_.clear();
1018 abort_args_.clear();
1024 bool MockDeathTestFactory::Create(
1026 const char* ,
int , DeathTest** test) {
1027 test_deleted_ =
false;
1029 *test =
new MockDeathTest(
this, role_, status_, passed_);
1041 static testing::internal::ReplaceDeathTestFactory* replacer_;
1042 static MockDeathTestFactory* factory_;
1045 factory_ =
new MockDeathTestFactory;
1046 replacer_ =
new testing::internal::ReplaceDeathTestFactory(factory_);
1051 replacer_ =
nullptr;
1059 static void RunReturningDeathTest(
bool* flag) {
1069 testing::internal::ReplaceDeathTestFactory* MacroLogicDeathTest::replacer_ =
1071 MockDeathTestFactory* MacroLogicDeathTest::factory_ =
nullptr;
1074 TEST_F(MacroLogicDeathTest, NothingHappens) {
1076 factory_->SetParameters(
false, DeathTest::OVERSEE_TEST, 0,
true);
1077 EXPECT_DEATH(flag =
true,
"");
1079 EXPECT_EQ(0, factory_->AssumeRoleCalls());
1089 TEST_F(MacroLogicDeathTest, ChildExitsSuccessfully) {
1091 factory_->SetParameters(
true, DeathTest::OVERSEE_TEST, 0,
true);
1092 EXPECT_DEATH(flag =
true,
"");
1094 EXPECT_EQ(1, factory_->AssumeRoleCalls());
1104 TEST_F(MacroLogicDeathTest, ChildExitsUnsuccessfully) {
1106 factory_->SetParameters(
true, DeathTest::OVERSEE_TEST, 1,
true);
1107 EXPECT_DEATH(flag =
true,
"");
1109 EXPECT_EQ(1, factory_->AssumeRoleCalls());
1120 TEST_F(MacroLogicDeathTest, ChildPerformsReturn) {
1122 factory_->SetParameters(
true, DeathTest::EXECUTE_TEST, 0,
true);
1123 RunReturningDeathTest(&flag);
1125 EXPECT_EQ(1, factory_->AssumeRoleCalls());
1129 EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT,
1130 factory_->AbortArgument(0));
1136 TEST_F(MacroLogicDeathTest, ChildDoesNotDie) {
1138 factory_->SetParameters(
true, DeathTest::EXECUTE_TEST, 0,
true);
1139 EXPECT_DEATH(flag =
true,
"");
1141 EXPECT_EQ(1, factory_->AssumeRoleCalls());
1150 EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE, factory_->AbortArgument(0));
1151 EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT,
1152 factory_->AbortArgument(1));
1158 TEST(SuccessRegistrationDeathTest, NoSuccessPart) {
1159 EXPECT_DEATH(_Exit(1),
"");
1163 TEST(StreamingAssertionsDeathTest, DeathTest) {
1164 EXPECT_DEATH(_Exit(1),
"") <<
"unexpected failure";
1165 ASSERT_DEATH(_Exit(1),
"") <<
"unexpected failure";
1168 EXPECT_DEATH(_Exit(0),
"") <<
"expected failure";
1170 "expected failure");
1173 ASSERT_DEATH(_Exit(0),
"") <<
"expected failure";
1175 "expected failure");
1180 TEST(GetLastErrnoDescription, GetLastErrnoDescriptionWorks) {
1187 #ifdef GTEST_OS_WINDOWS
1188 TEST(AutoHandleTest, AutoHandleWorks) {
1189 HANDLE handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
1190 ASSERT_NE(INVALID_HANDLE_VALUE, handle);
1193 testing::internal::AutoHandle auto_handle(handle);
1198 auto_handle.Reset();
1199 EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle.Get());
1203 handle = ::CreateEvent(NULL, FALSE, FALSE, NULL);
1204 ASSERT_NE(INVALID_HANDLE_VALUE, handle);
1205 auto_handle.Reset(handle);
1209 testing::internal::AutoHandle auto_handle2;
1210 EXPECT_EQ(INVALID_HANDLE_VALUE, auto_handle2.Get());
1212 #endif // GTEST_OS_WINDOWS
1214 #ifdef GTEST_OS_WINDOWS
1215 typedef unsigned __int64 BiggestParsable;
1216 typedef signed __int64 BiggestSignedParsable;
1218 typedef unsigned long long BiggestParsable;
1219 typedef signed long long BiggestSignedParsable;
1220 #endif // GTEST_OS_WINDOWS
1224 const BiggestParsable kBiggestParsableMax = ULLONG_MAX;
1225 const BiggestSignedParsable kBiggestSignedParsableMax = LLONG_MAX;
1227 TEST(ParseNaturalNumberTest, RejectsInvalidFormat) {
1228 BiggestParsable result = 0;
1231 EXPECT_FALSE(ParseNaturalNumber(
"non-number string", &result));
1244 TEST(ParseNaturalNumberTest, RejectsOverflownNumbers) {
1245 BiggestParsable result = 0;
1247 EXPECT_FALSE(ParseNaturalNumber(
"99999999999999999999999", &result));
1249 signed char char_result = 0;
1254 TEST(ParseNaturalNumberTest, AcceptsValidNumbers) {
1255 BiggestParsable result = 0;
1267 ASSERT_TRUE(ParseNaturalNumber(
"00000", &result));
1271 TEST(ParseNaturalNumberTest, AcceptsTypeLimits) {
1273 msg << kBiggestParsableMax;
1275 BiggestParsable result = 0;
1276 EXPECT_TRUE(ParseNaturalNumber(msg.GetString(), &result));
1280 msg2 << kBiggestSignedParsableMax;
1282 BiggestSignedParsable signed_result = 0;
1283 EXPECT_TRUE(ParseNaturalNumber(msg2.GetString(), &signed_result));
1284 EXPECT_EQ(kBiggestSignedParsableMax, signed_result);
1290 EXPECT_TRUE(ParseNaturalNumber(msg3.GetString(), &int_result));
1296 unsigned int uint_result = 0;
1297 EXPECT_TRUE(ParseNaturalNumber(msg4.GetString(), &uint_result));
1301 TEST(ParseNaturalNumberTest, WorksForShorterIntegers) {
1302 short short_result = 0;
1303 ASSERT_TRUE(ParseNaturalNumber(
"123", &short_result));
1306 signed char char_result = 0;
1307 ASSERT_TRUE(ParseNaturalNumber(
"123", &char_result));
1311 #ifdef GTEST_OS_WINDOWS
1312 TEST(EnvironmentTest, HandleFitsIntoSizeT) {
1315 #endif // GTEST_OS_WINDOWS
1319 TEST(ConditionalDeathMacrosDeathTest, ExpectsDeathWhenDeathTestsAvailable) {
1321 "death inside CondDeathTestExpectMacro");
1323 "death inside CondDeathTestAssertMacro");
1330 TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInFastStyle) {
1335 fprintf(stderr, InDeathTestChild() ?
"Inside" :
"Outside");
1342 TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) {
1347 fprintf(stderr, InDeathTestChild() ?
"Inside" :
"Outside");
1354 void DieWithMessage(
const char* message) {
1355 fputs(message, stderr);
1360 TEST(MatcherDeathTest, DoesNotBreakBareRegexMatching) {
1363 #ifdef GTEST_USES_POSIX_RE
1364 EXPECT_DEATH(DieWithMessage(
"O, I die, Horatio."),
"I d[aeiou]e");
1366 EXPECT_DEATH(DieWithMessage(
"O, I die, Horatio."),
"I di?e");
1370 TEST(MatcherDeathTest, MonomorphicMatcherMatches) {
1371 EXPECT_DEATH(DieWithMessage(
"Behind O, I am slain!"),
1372 Matcher<const std::string&>(ContainsRegex(
"I am slain")));
1375 TEST(MatcherDeathTest, MonomorphicMatcherDoesNotMatch) {
1378 DieWithMessage(
"Behind O, I am slain!"),
1379 Matcher<const std::string&>(ContainsRegex(
"Ow, I am slain"))),
1380 "Expected: contains regular expression \"Ow, I am slain\"");
1383 TEST(MatcherDeathTest, PolymorphicMatcherMatches) {
1384 EXPECT_DEATH(DieWithMessage(
"The rest is silence."),
1385 ContainsRegex(
"rest is silence"));
1388 TEST(MatcherDeathTest, PolymorphicMatcherDoesNotMatch) {
1390 EXPECT_DEATH(DieWithMessage(
"The rest is silence."),
1391 ContainsRegex(
"rest is science")),
1392 "Expected: contains regular expression \"rest is science\"");
1397 #else // !GTEST_HAS_DEATH_TEST follows
1407 TEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) {
1414 "Death tests are not supported on this platform"));
1421 ASSERT_TRUE(NULL == strstr(output.c_str(),
"streamed message"));
1427 "Death tests are not supported on this platform"));
1433 ASSERT_TRUE(NULL == strstr(output.c_str(),
"streamed message"));
1436 void FuncWithAssert(
int* n) {
1443 TEST(ConditionalDeathMacrosTest, AssertDeatDoesNotReturnhIfUnsupported) {
1451 #endif // !GTEST_HAS_DEATH_TEST
1457 #pragma GCC diagnostic push
1458 #pragma GCC diagnostic ignored "-Wdangling-else"
1459 #pragma GCC diagnostic ignored "-Wempty-body"
1460 #pragma GCC diagnostic ignored "-Wpragmas"
1467 TEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) {
1487 #pragma GCC diagnostic pop
1492 TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) {
class UnitTestImpl * GetUnitTestImpl()
#define ASSERT_DEATH_IF_SUPPORTED(statement, regex)
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
GTEST_API_ std::string GetCapturedStderr()
GTEST_DISABLE_MSC_WARNINGS_POP_() TEST(LinkTest
#define EXPECT_STRNE(s1, s2)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define ASSERT_NE(val1, val2)
#define GTEST_LOG_(severity)
#define TEST_F(test_fixture, test_name)
#define TEST(test_suite_name, test_name)
#define ASSERT_EQ(val1, val2)
int ChDir(const char *dir)
#define EXPECT_FATAL_FAILURE(statement, substr)
#define EXPECT_PRED1(pred, v1)
#define ASSERT_TRUE(condition)
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
static void TearDownTestSuite()
GTEST_API_ bool AlwaysTrue()
#define GTEST_FLAG_SET(name, value)
#define EXPECT_STREQ(s1, s2)
#define GTEST_FLAG_GET(name)
#define EXPECT_EQ(val1, val2)
GTEST_API_ void CaptureStderr()
#define EXPECT_TRUE(condition)
static void SetUpTestSuite()
#define EXPECT_FALSE(condition)
#define ASSERT_FALSE(condition)