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;
83 FAIL() <<
"This should never be reached.";
86 TEST(PassingTest, PassingTest1) {
89 TEST(PassingTest, PassingTest2) {
108 TEST(NonfatalFailureTest, EscapesStringOperands) {
116 TEST(NonfatalFailureTest, DiffForLongStrings) {
122 TEST(FatalFailureTest, FatalFailureInSubroutine) {
123 printf(
"(expecting a failure that x should be 1)\n");
129 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
130 printf(
"(expecting a failure that x should be 1)\n");
139 if (HasFatalFailure())
return;
142 FAIL() <<
"This should never be reached.";
146 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
147 printf(
"(expecting a failure on false)\n");
153 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
154 static const int a[4] = {
158 printf(
"(expecting 2 failures on (3) >= (a[i]))\n");
159 for (
int i = 0; i < static_cast<int>(
sizeof(a)/
sizeof(*a));
i++) {
181 TEST(SCOPED_TRACETest, ObeysScopes) {
182 printf(
"(expected to fail)\n");
185 ADD_FAILURE() <<
"This failure is expected, and shouldn't have a trace.";
191 ADD_FAILURE() <<
"This failure is expected, and should have a trace.";
196 ADD_FAILURE() <<
"This failure is expected, and shouldn't have a trace.";
200 TEST(SCOPED_TRACETest, WorksInLoop) {
201 printf(
"(expected to fail)\n");
203 for (
int i = 1;
i <= 2;
i++) {
211 TEST(SCOPED_TRACETest, WorksInSubroutine) {
212 printf(
"(expected to fail)\n");
219 TEST(SCOPED_TRACETest, CanBeNested) {
220 printf(
"(expected to fail)\n");
228 TEST(SCOPED_TRACETest, CanBeRepeated) {
229 printf(
"(expected to fail)\n");
233 <<
"This failure is expected, and should contain trace point A.";
237 <<
"This failure is expected, and should contain trace point A and B.";
241 ADD_FAILURE() <<
"This failure is expected, and should " 242 <<
"contain trace point A, B, and C.";
246 ADD_FAILURE() <<
"This failure is expected, and should " 247 <<
"contain trace point A, B, and D.";
250 #if GTEST_IS_THREADSAFE 286 static void ThreadWithScopedTrace(CheckPoints* check_points) {
290 <<
"Expected failure #1 (in thread B, only trace B alive).";
291 check_points->n1.Notify();
292 check_points->n2.WaitForNotification();
295 <<
"Expected failure #3 (in thread B, trace A & B both alive).";
298 <<
"Expected failure #4 (in thread B, only trace A alive).";
299 check_points->n3.Notify();
302 TEST(SCOPED_TRACETest, WorksConcurrently) {
303 printf(
"(expecting 6 failures)\n");
305 CheckPoints check_points;
306 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
309 check_points.n1.WaitForNotification();
314 <<
"Expected failure #2 (in thread A, trace A & B both alive).";
315 check_points.n2.Notify();
316 check_points.n3.WaitForNotification();
319 <<
"Expected failure #5 (in thread A, only trace A alive).";
322 <<
"Expected failure #6 (in thread A, no trace alive).";
325 #endif // GTEST_IS_THREADSAFE 328 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
339 printf(
"The non-test part of the code is expected to have 2 failures.\n\n");
354 printf(
"(expecting 5 failures)\n");
355 ADD_FAILURE() <<
"Expected failure #1, in the test fixture c'tor.";
359 ADD_FAILURE() <<
"Expected failure #5, in the test fixture d'tor.";
363 ADD_FAILURE() <<
"Expected failure #2, in SetUp().";
367 ADD_FAILURE() <<
"Expected failure #4, in TearDown.";
372 ADD_FAILURE() <<
"Expected failure #3, in the test body.";
379 printf(
"(expecting 2 failures)\n");
384 ADD_FAILURE() <<
"Expected failure #2, in the test fixture d'tor.";
389 <<
"We should never get here, as the test fixture c'tor " 390 <<
"had a fatal failure.";
394 ADD_FAILURE() <<
"UNEXPECTED failure in TearDown(). " 395 <<
"We should never get here, as the test fixture c'tor " 396 <<
"had a fatal failure.";
401 FAIL() <<
"Expected failure #1, in the test fixture c'tor.";
406 ADD_FAILURE() <<
"UNEXPECTED failure in the test body. " 407 <<
"We should never get here, as the test fixture c'tor " 408 <<
"had a fatal failure.";
419 printf(
"(expecting 4 failures)\n");
420 ADD_FAILURE() <<
"Expected failure #1, in SetUp().";
424 FAIL() <<
"Expected failure #3, in TearDown().";
428 FAIL() <<
"Expected failure #4, in the test fixture d'tor.";
433 FAIL() <<
"Expected failure #2, in the test function.";
444 printf(
"(expecting 3 failures)\n");
445 FAIL() <<
"Expected failure #1, in SetUp().";
449 FAIL() <<
"Expected failure #2, in TearDown().";
453 FAIL() <<
"Expected failure #3, in the test fixture d'tor.";
458 FAIL() <<
"UNEXPECTED failure in the test function. " 459 <<
"We should never get here, as SetUp() failed.";
462 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
466 #if GTEST_IS_THREADSAFE 469 void DieIf(
bool should_die) {
470 GTEST_CHECK_(!should_die) <<
" - death inside DieIf().";
476 struct SpawnThreadNotifications {
477 SpawnThreadNotifications() {}
488 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
490 notifications->spawn_thread_started.Notify();
493 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
502 virtual void SetUp() {
503 thread_.reset(
new ThreadWithParam<SpawnThreadNotifications*>(
504 &ThreadRoutine, ¬ifications_, NULL));
505 notifications_.spawn_thread_started.WaitForNotification();
512 virtual void TearDown() {
513 notifications_.spawn_thread_ok_to_terminate.Notify();
517 SpawnThreadNotifications notifications_;
521 #endif // GTEST_IS_THREADSAFE 545 TheSecondTestWithThisNameShouldFail) {}
565 TheSecondTestWithThisNameShouldFail) {}
597 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
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) {
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 759 #if GTEST_HAS_TYPED_TEST 761 template <
typename T>
772 EXPECT_EQ(1, TypeParam()) <<
"Expected failure";
775 #endif // GTEST_HAS_TYPED_TEST 778 #if GTEST_HAS_TYPED_TEST_P 780 template <
typename T>
791 EXPECT_EQ(1U, TypeParam()) <<
"Expected failure";
796 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
799 #endif // GTEST_HAS_TYPED_TEST_P 801 #if GTEST_HAS_DEATH_TEST 806 TEST(ADeathTest, ShouldRunFirst) {
809 # if GTEST_HAS_TYPED_TEST 814 template <
typename T>
818 typedef testing::Types<int, double> NumericTypes;
824 # endif // GTEST_HAS_TYPED_TEST 826 # if GTEST_HAS_TYPED_TEST_P 832 template <
typename T>
845 # endif // GTEST_HAS_TYPED_TEST_P 847 #endif // GTEST_HAS_DEATH_TEST 858 if (failure == FATAL_FAILURE) {
859 FAIL() <<
"Expected fatal failure.";
868 printf(
"(expecting 1 failure)\n");
871 printf(
"(expecting 1 failure)\n");
875 printf(
"(expecting 1 failure)\n");
882 printf(
"(expecting 1 failure)\n");
885 printf(
"(expecting 1 failure)\n");
888 printf(
"(expecting 1 failure)\n");
893 #if GTEST_IS_THREADSAFE 897 static void AddFailureInOtherThread(FailureMode failure) {
898 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
903 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
905 printf(
"(expecting 2 failures)\n");
907 "Expected fatal failure.");
910 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
912 printf(
"(expecting 2 failures)\n");
914 "Expected non-fatal failure.");
917 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
921 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
922 printf(
"(expecting 2 failures)\n");
923 TestPartResultArray results;
925 ScopedFakeTestPartResultReporter reporter(
926 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
928 AddFailureInOtherThread(FATAL_FAILURE);
929 AddFailureInOtherThread(NONFATAL_FAILURE);
932 EXPECT_EQ(0, results.size()) <<
"This shouldn't fail.";
935 #endif // GTEST_IS_THREADSAFE 939 printf(
"(expecting 1 failure)\n");
942 printf(
"(expecting 1 failure)\n");
944 "Expected non-fatal failure.");
946 printf(
"(expecting 1 failure)\n");
948 "Some other fatal failure expected.");
953 printf(
"(expecting 1 failure)\n");
957 printf(
"(expecting 1 failure)\n");
959 "Expected fatal failure.");
961 printf(
"(expecting 1 failure)\n");
963 "Some other non-fatal failure.");
972 printf(
"%s",
"FooEnvironment::SetUp() called.\n");
976 printf(
"%s",
"FooEnvironment::TearDown() called.\n");
977 FAIL() <<
"Expected fatal failure.";
984 printf(
"%s",
"BarEnvironment::SetUp() called.\n");
988 printf(
"%s",
"BarEnvironment::TearDown() called.\n");
993 bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) =
false;
1013 "--gtest_internal_skip_environment_and_ad_hoc_tests"))
1014 GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) =
true;
1016 #if GTEST_HAS_DEATH_TEST 1020 # if GTEST_OS_WINDOWS 1024 # endif // GTEST_OS_WINDOWS 1027 #endif // GTEST_HAS_DEATH_TEST 1029 if (
GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests))
FILE * FReopen(const char *path, const char *mode, FILE *stream)
Environment * AddGlobalTestEnvironment(Environment *env)
static bool HasFatalFailure()
~NonFatalFailureInFixtureConstructorTest()
#define EXPECT_NONFATAL_FAILURE(statement, substr)
NonFatalFailureInFixtureConstructorTest()
#define EXPECT_GE(val1, val2)
#define ADD_FAILURE_AT(file, line)
TEST(PassingTest, PassingTest1)
#define ASSERT_EQ(val1, val2)
#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr)
static const char kGoldenString[]
#define EXPECT_FATAL_FAILURE(statement, substr)
int main(int argc, char **argv)
REGISTER_TYPED_TEST_CASE_P(TypeParamTest, TestA, TestB)
virtual ~FatalFailureInSetUpTest()
TYPED_TEST(TypedTest, TestA)
~FatalFailureInFixtureConstructorTest()
INSTANTIATE_TEST_CASE_P(PrintingFailingParams, FailingParamTest, testing::Values(2))
TYPED_TEST_CASE(TypedTest, MyTypes)
bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests)
#define GTEST_CHECK_(condition)
virtual ~NonFatalFailureInSetUpTest()
void SubWithoutTrace(int n)
TYPED_TEST_CASE_P(TypeParamTest)
static void AddFailure(FailureMode failure)
#define EXPECT_EQ(expected, actual)
#define SCOPED_TRACE(message)
TYPED_TEST_P(TypeParamTest, TestA)
FatalFailureInFixtureConstructorTest()
INSTANTIATE_TYPED_TEST_CASE_P(My, TypeParamTest, MyTypes)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor)
#define EXPECT_TRUE(condition)
TEST_P(FailingParamTest, Fails)
#define ASSERT_FALSE(condition)