TEST(logging, CHECK) { ASSERT_DEATH(CHECK(false), "Check failed: false "); CHECK(true); ASSERT_DEATH(CHECK_EQ(0, 1), "Check failed: 0 == 1 "); CHECK_EQ(0, 0); ASSERT_DEATH(CHECK_STREQ("foo", "bar"), R"(Check failed: "foo" == "bar")"); CHECK_STREQ("foo", "foo"); }
/** * This should implicitly lead to an error most of the time, * but make sure it also errors explicitly. Fail Early! */ TEST(TrackedPointer, NullPointerAreInvalidForDeref) { MyClass* bar = NULL; tracked_ptr<MyClass> bar_p(bar); ASSERT_DEATH((*bar_p).foo(), ""); ASSERT_DEATH(bar_p->foo(), ""); delete bar; }
TEST_F(test_dvector, is_valid_index) { dvector dv(1, 4); ASSERT_EQ(false, dv.is_valid_index(0)); ASSERT_EQ(true, dv.is_valid_index(1)); ASSERT_EQ(true, dv.is_valid_index(2)); ASSERT_EQ(true, dv.is_valid_index(3)); ASSERT_EQ(true, dv.is_valid_index(4)); ASSERT_EQ(false, dv.is_valid_index(5)); ASSERT_EQ(false, false || dv.is_valid_index(0)); ASSERT_EQ(true, false || dv.is_valid_index(1)); ASSERT_EQ(true, false || dv.is_valid_index(4)); ASSERT_EQ(false, false || dv.is_valid_index(5)); ASSERT_DEATH(dv(0), "Assertion"); ASSERT_DEATH(dv(5), "Assertion"); }
GTEST_TEST(ExpectedTest, expected_was_not_checked_before_destruction_failure) { auto action = []() { auto expected = ExpectedSuccess<TestError>{Success()}; }; #ifndef NDEBUG ASSERT_DEATH(action(), "Expected was not checked before destruction"); #else boost::ignore_unused(action); #endif }
TEST(MutexTest,RecursiveLockFails) { Mutex *m = Mutex_Alloc(); ASSERT_NE(m,(void*)NULL); Mutex_Lock(m); ASSERT_DEATH(Mutex_Lock(m),"Detected an attempt to recursively acquire a mutex.*"); Mutex_Unlock(m); Mutex_Free(m); }
GTEST_TEST(ExpectedTest, take_error_from_expected_with_value) { auto action = []() { auto expected = Expected<int, TestError>(228); return expected.takeError(); }; #ifndef NDEBUG ASSERT_DEATH(action(), "Do not try to get error from Expected with value"); #else boost::ignore_unused(action); #endif }
TEST(ExampleTests, test_death){ auto a = [] () { int *f = new int; f[5000] = 543; delete f; delete f; delete [] f; }; ASSERT_DEATH(a(), ""); }
GTEST_TEST(ExpectedTest, get_value_from_expected_with_error) { auto action = []() { auto expected = Expected<int, TestError>(TestError::Logical, "Test error message @#$0k+Qh"); auto value = expected.get(); boost::ignore_unused(value); }; #ifndef NDEBUG ASSERT_DEATH(action(), "Do not try to get value from Expected with error"); #else boost::ignore_unused(action); #endif }
GTEST_TEST(ExpectedTest, take_value_from_expected_with_error) { auto action = []() { auto expected = Expected<int, TestError>(TestError::Semantic, "Test error message !&^?<>."); auto value = expected.take(); boost::ignore_unused(value); }; #ifndef NDEBUG ASSERT_DEATH(action(), "Do not try to get value from Expected with error"); #else boost::ignore_unused(action); #endif }
TEST(NAME, GettingNonExistingComponentsCausesDeath) { MockEntityManager em; Entity entity("entity", &em); // uninteresting calls EXPECT_CALL(em, informRemoveComponentHelper(testing::_, testing::_)).Times(testing::AtLeast(0)); EXPECT_CALL(em, informAddComponentHelper(testing::_, testing::_)).Times(testing::AtLeast(0)); entity.addComponent<TestComponent>(2, 3); ASSERT_EQ(TestComponent(2, 3), entity.getComponent<TestComponent>()); ASSERT_DEATH(entity.getComponent<NonExistingComponent>(), ""); }
/** * Make sure the use of an invalid pointer triggers an error. */ TEST(TrackedPointer, InvalidUseTriggersErrorStar) { MyClass* bar = new MyClass(); { // open new scope for the test tracked_ptr<MyClass> bar_p(bar); ASSERT_EQ(42, bar_p->foo()); tracked_ptr<MyClass>::invalidate_all_pointer(); ASSERT_DEATH((*bar_p).foo(), ""); } delete bar; }
TEST(TrackedPointer, ReferenceOperator) { // compiler check int i = 0; int* i_p = &i; // compiler check done MyClass* bar = new MyClass(); tracked_ptr<MyClass> bar_p = (tracked_ptr<MyClass>)bar; MyClass* const * bar_pp; ASSERT_DEATH(bar_pp = &bar_p, ""); //We want that to fail for the moment! // REM: if we don't want it to fail, then the following should hold: // ASSERT_EQ(*bar_pp, bar); // ASSERT_NE((MyClass*)NULL, bar); }
TEST(NAME, AddingTwoComponentsOfTheSameTypeCausesDeath) { MockEntityManager em; Entity entity("entity", &em); // uninteresting calls EXPECT_CALL(em, informRemoveComponentHelper(testing::_, testing::_)).Times(testing::AtLeast(0)); // duplicate component should not be added, which means an addComponent event should not // be triggered EXPECT_CALL(em, informAddComponentHelper(testing::_, testing::Pointee(TestComponent(2, 3)))) .Times(1); entity.addComponent<TestComponent>(2, 3); ASSERT_DEATH(entity.addComponent<TestComponent>(3, 4), ""); }
TEST(GTestSimpleTest, GTestDeathTest) { ASSERT_DEATH(crashMe(), "Crash me!"); }
TEST(GeoLib, InsertZeroPointsInGrid) { std::vector<GeoLib::Point*> pnts; ASSERT_DEATH(GeoLib::Grid<GeoLib::Point> grid(pnts.begin(), pnts.end()), ""); }
TEST(logging, LOG) { ASSERT_DEATH(LOG(FATAL) << "foobar", "foobar"); // We can't usefully check the output of any of these on Windows because we // don't have std::regex, but we can at least make sure we printed at least as // many characters are in the log message. { CapturedStderr cap; LOG(WARNING) << "foobar"; ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); std::string output; android::base::ReadFdToString(cap.fd(), &output); ASSERT_GT(output.length(), strlen("foobar")); #if !defined(_WIN32) std::regex message_regex( make_log_pattern(android::base::WARNING, "foobar")); ASSERT_TRUE(std::regex_search(output, message_regex)); #endif } { CapturedStderr cap; LOG(INFO) << "foobar"; ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); std::string output; android::base::ReadFdToString(cap.fd(), &output); ASSERT_GT(output.length(), strlen("foobar")); #if !defined(_WIN32) std::regex message_regex( make_log_pattern(android::base::INFO, "foobar")); ASSERT_TRUE(std::regex_search(output, message_regex)); #endif } { CapturedStderr cap; LOG(DEBUG) << "foobar"; ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); std::string output; android::base::ReadFdToString(cap.fd(), &output); ASSERT_TRUE(output.empty()); } { android::base::ScopedLogSeverity severity(android::base::DEBUG); CapturedStderr cap; LOG(DEBUG) << "foobar"; ASSERT_EQ(0, lseek(cap.fd(), SEEK_SET, 0)); std::string output; android::base::ReadFdToString(cap.fd(), &output); ASSERT_GT(output.length(), strlen("foobar")); #if !defined(_WIN32) std::regex message_regex( make_log_pattern(android::base::DEBUG, "foobar")); ASSERT_TRUE(std::regex_search(output, message_regex)); #endif } }
TEST(InvariantTest, invFail) { InvariantExampleClass c; ASSERT_DEATH(c.invFailMethod(), "Invariant check failed. Required at: "); }
TEST(StringUtil, to) { ASSERT_NEAR(paddle::str::to<double>("12.45"), 12.45, 1e-5); ASSERT_DEATH(paddle::str::to<double>("12.45x23"), ".*"); ASSERT_DEATH(paddle::str::to<int>(""), ".*"); }
TEST(MutexTest,OrphanUnlockFails) { Mutex *m = Mutex_Alloc(); ASSERT_NE(m,(void*)NULL); ASSERT_DEATH(Mutex_Unlock(m),"Detected an attempt to unlock a mutex that hasn't been locked.*"); Mutex_Free(m); }
TEST(TypeGuard, WrongCastFromOtherTypeGuard) { A a; ASSERT_DEATH(B::Cast(a.ToPointer()), "Given pointer has wrong type: .*"); }
TEST(TypeGuard, WrongCastFromBasicType) { int i = 0; ASSERT_DEATH(A::Cast(&i), "Given pointer has wrong type: .*"); }
TEST_F(test_dvector, save_dvector_derivatives_not_matching) { dvar_vector_position pos; dvector dv(1, 4); ASSERT_DEATH(dv.save_dvector_derivatives(pos), "Assertion"); }