46 #define GTEST_IMPLEMENTATION_ 1
48 #undef GTEST_IMPLEMENTATION_
52 #if GTEST_IS_THREADSAFE
56 using testing::internal::Notification;
57 using testing::internal::ThreadWithParam;
60 namespace posix = ::testing::internal::posix;
82 FAIL() <<
"This should never be reached.";
85 TEST(PassingTest, PassingTest1) {
88 TEST(PassingTest, PassingTest2) {
107 TEST(NonfatalFailureTest, EscapesStringOperands) {
115 TEST(NonfatalFailureTest, DiffForLongStrings) {
116 std::string golden_str(kGoldenString,
sizeof(kGoldenString) - 1);
121 TEST(FatalFailureTest, FatalFailureInSubroutine) {
122 printf(
"(expecting a failure that x should be 1)\n");
128 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
129 printf(
"(expecting a failure that x should be 1)\n");
138 if (HasFatalFailure())
return;
141 FAIL() <<
"This should never be reached.";
145 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
146 printf(
"(expecting a failure on false)\n");
152 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
153 static const int a[4] = {
157 printf(
"(expecting 2 failures on (3) >= (a[i]))\n");
158 for (
int i = 0; i < static_cast<int>(
sizeof(
a)/
sizeof(*a));
i++) {
180 TEST(SCOPED_TRACETest, ObeysScopes) {
181 printf(
"(expected to fail)\n");
184 ADD_FAILURE() <<
"This failure is expected, and shouldn't have a trace.";
190 ADD_FAILURE() <<
"This failure is expected, and should have a trace.";
195 ADD_FAILURE() <<
"This failure is expected, and shouldn't have a trace.";
199 TEST(SCOPED_TRACETest, WorksInLoop) {
200 printf(
"(expected to fail)\n");
202 for (
int i = 1;
i <= 2;
i++) {
210 TEST(SCOPED_TRACETest, WorksInSubroutine) {
211 printf(
"(expected to fail)\n");
218 TEST(SCOPED_TRACETest, CanBeNested) {
219 printf(
"(expected to fail)\n");
227 TEST(SCOPED_TRACETest, CanBeRepeated) {
228 printf(
"(expected to fail)\n");
232 <<
"This failure is expected, and should contain trace point A.";
236 <<
"This failure is expected, and should contain trace point A and B.";
240 ADD_FAILURE() <<
"This failure is expected, and should "
241 <<
"contain trace point A, B, and C.";
245 ADD_FAILURE() <<
"This failure is expected, and should "
246 <<
"contain trace point A, B, and D.";
249 #if GTEST_IS_THREADSAFE
285 static void ThreadWithScopedTrace(CheckPoints* check_points) {
289 <<
"Expected failure #1 (in thread B, only trace B alive).";
290 check_points->n1.Notify();
291 check_points->n2.WaitForNotification();
294 <<
"Expected failure #3 (in thread B, trace A & B both alive).";
297 <<
"Expected failure #4 (in thread B, only trace A alive).";
298 check_points->n3.Notify();
301 TEST(SCOPED_TRACETest, WorksConcurrently) {
302 printf(
"(expecting 6 failures)\n");
304 CheckPoints check_points;
305 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
308 check_points.n1.WaitForNotification();
313 <<
"Expected failure #2 (in thread A, trace A & B both alive).";
314 check_points.n2.Notify();
315 check_points.n3.WaitForNotification();
318 <<
"Expected failure #5 (in thread A, only trace A alive).";
321 <<
"Expected failure #6 (in thread A, no trace alive).";
324 #endif // GTEST_IS_THREADSAFE
327 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
338 printf(
"The non-test part of the code is expected to have 2 failures.\n\n");
353 printf(
"(expecting 5 failures)\n");
354 ADD_FAILURE() <<
"Expected failure #1, in the test fixture c'tor.";
358 ADD_FAILURE() <<
"Expected failure #5, in the test fixture d'tor.";
362 ADD_FAILURE() <<
"Expected failure #2, in SetUp().";
366 ADD_FAILURE() <<
"Expected failure #4, in TearDown.";
371 ADD_FAILURE() <<
"Expected failure #3, in the test body.";
378 printf(
"(expecting 2 failures)\n");
383 ADD_FAILURE() <<
"Expected failure #2, in the test fixture d'tor.";
388 <<
"We should never get here, as the test fixture c'tor "
389 <<
"had a fatal failure.";
393 ADD_FAILURE() <<
"UNEXPECTED failure in TearDown(). "
394 <<
"We should never get here, as the test fixture c'tor "
395 <<
"had a fatal failure.";
400 FAIL() <<
"Expected failure #1, in the test fixture c'tor.";
405 ADD_FAILURE() <<
"UNEXPECTED failure in the test body. "
406 <<
"We should never get here, as the test fixture c'tor "
407 <<
"had a fatal failure.";
418 printf(
"(expecting 4 failures)\n");
419 ADD_FAILURE() <<
"Expected failure #1, in SetUp().";
423 FAIL() <<
"Expected failure #3, in TearDown().";
427 FAIL() <<
"Expected failure #4, in the test fixture d'tor.";
432 FAIL() <<
"Expected failure #2, in the test function.";
443 printf(
"(expecting 3 failures)\n");
444 FAIL() <<
"Expected failure #1, in SetUp().";
448 FAIL() <<
"Expected failure #2, in TearDown().";
452 FAIL() <<
"Expected failure #3, in the test fixture d'tor.";
457 FAIL() <<
"UNEXPECTED failure in the test function. "
458 <<
"We should never get here, as SetUp() failed.";
461 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
465 #if GTEST_IS_THREADSAFE
468 void DieIf(
bool should_die) {
469 GTEST_CHECK_(!should_die) <<
" - death inside DieIf().";
475 struct SpawnThreadNotifications {
476 SpawnThreadNotifications() {}
478 Notification spawn_thread_started;
479 Notification spawn_thread_ok_to_terminate;
487 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
489 notifications->spawn_thread_started.Notify();
492 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
501 virtual void SetUp() {
502 thread_.reset(
new ThreadWithParam<SpawnThreadNotifications*>(
503 &ThreadRoutine, ¬ifications_, NULL));
504 notifications_.spawn_thread_started.WaitForNotification();
512 notifications_.spawn_thread_ok_to_terminate.Notify();
516 SpawnThreadNotifications notifications_;
521 #endif // GTEST_IS_THREADSAFE
545 TheSecondTestWithThisNameShouldFail) {}
565 TheSecondTestWithThisNameShouldFail) {}
597 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
600 EXPECT_EQ(1, global_integer) <<
"Expected non-fatal failure.";
601 },
"Expected non-fatal failure.");
606 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
611 EXPECT_EQ(m, n) <<
"Expected non-fatal failure.";
612 },
"Expected non-fatal failure.");
617 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
620 },
"Expected non-fatal failure.");
625 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
626 printf(
"(expecting a failure)\n");
633 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
634 printf(
"(expecting a failure)\n");
643 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
644 printf(
"(expecting a failure)\n");
646 FAIL() <<
"Expected fatal failure.";
652 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
653 printf(
"(expecting a failure)\n");
659 #if GTEST_HAS_EXCEPTIONS
663 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
664 printf(
"(expecting a failure)\n");
673 #endif // GTEST_HAS_EXCEPTIONS
676 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
679 ASSERT_EQ(1, global_integer) <<
"Expected fatal failure.";
680 },
"Expected fatal failure.");
685 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
689 ASSERT_EQ(0, n) <<
"Expected fatal failure.";
690 },
"Expected fatal failure.");
695 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
697 FAIL() <<
"Expected fatal failure.";
698 },
"Expected fatal failure.");
703 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
704 printf(
"(expecting a failure)\n");
711 FAIL() <<
"Expected fatal failure.";
716 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
717 printf(
"(expecting a failure)\n");
726 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
727 printf(
"(expecting a failure)\n");
735 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
736 printf(
"(expecting a failure)\n");
742 #if GTEST_HAS_EXCEPTIONS
746 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
747 printf(
"(expecting a failure)\n");
756 #endif // GTEST_HAS_EXCEPTIONS
760 #if GTEST_HAS_PARAM_TEST
762 std::string ParamNameFunc(
const testing::TestParamInfo<std::string>& info) {
766 class ParamTest :
public testing::TestWithParam<std::string> {
769 TEST_P(ParamTest, Success) {
773 TEST_P(ParamTest, Failure) {
774 EXPECT_EQ(
"b", GetParam()) <<
"Expected failure";
782 #endif // GTEST_HAS_PARAM_TEST
785 #if GTEST_HAS_TYPED_TEST
787 template <
typename T>
798 EXPECT_EQ(1, TypeParam()) <<
"Expected failure";
801 #endif // GTEST_HAS_TYPED_TEST
804 #if GTEST_HAS_TYPED_TEST_P
806 template <
typename T>
817 EXPECT_EQ(1U, TypeParam()) <<
"Expected failure";
822 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
825 #endif // GTEST_HAS_TYPED_TEST_P
827 #if GTEST_HAS_DEATH_TEST
832 TEST(ADeathTest, ShouldRunFirst) {
835 # if GTEST_HAS_TYPED_TEST
840 template <
typename T>
844 typedef testing::Types<int, double> NumericTypes;
850 # endif // GTEST_HAS_TYPED_TEST
852 # if GTEST_HAS_TYPED_TEST_P
858 template <
typename T>
871 # endif // GTEST_HAS_TYPED_TEST_P
873 #endif // GTEST_HAS_DEATH_TEST
885 FAIL() <<
"Expected fatal failure.";
894 printf(
"(expecting 1 failure)\n");
897 printf(
"(expecting 1 failure)\n");
901 printf(
"(expecting 1 failure)\n");
908 printf(
"(expecting 1 failure)\n");
911 printf(
"(expecting 1 failure)\n");
914 printf(
"(expecting 1 failure)\n");
919 #if GTEST_IS_THREADSAFE
923 static void AddFailureInOtherThread(FailureMode failure) {
924 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
929 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
931 printf(
"(expecting 2 failures)\n");
933 "Expected fatal failure.");
936 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
938 printf(
"(expecting 2 failures)\n");
940 "Expected non-fatal failure.");
943 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
947 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
948 printf(
"(expecting 2 failures)\n");
951 ScopedFakeTestPartResultReporter reporter(
952 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
954 AddFailureInOtherThread(FATAL_FAILURE);
955 AddFailureInOtherThread(NONFATAL_FAILURE);
958 EXPECT_EQ(0, results.size()) <<
"This shouldn't fail.";
961 #endif // GTEST_IS_THREADSAFE
965 printf(
"(expecting 1 failure)\n");
968 printf(
"(expecting 1 failure)\n");
970 "Expected non-fatal failure.");
972 printf(
"(expecting 1 failure)\n");
974 "Some other fatal failure expected.");
979 printf(
"(expecting 1 failure)\n");
983 printf(
"(expecting 1 failure)\n");
985 "Expected fatal failure.");
987 printf(
"(expecting 1 failure)\n");
989 "Some other non-fatal failure.");
998 printf(
"%s",
"FooEnvironment::SetUp() called.\n");
1002 printf(
"%s",
"FooEnvironment::TearDown() called.\n");
1003 FAIL() <<
"Expected fatal failure.";
1010 printf(
"%s",
"BarEnvironment::SetUp() called.\n");
1014 printf(
"%s",
"BarEnvironment::TearDown() called.\n");
1035 bool internal_skip_environment_and_ad_hoc_tests =
1037 std::string(
"internal_skip_environment_and_ad_hoc_tests")) > 0;
1039 #if GTEST_HAS_DEATH_TEST
1043 # if GTEST_OS_WINDOWS
1047 # endif // GTEST_OS_WINDOWS
1050 #endif // GTEST_HAS_DEATH_TEST
1052 if (internal_skip_environment_and_ad_hoc_tests)