File: | util/SingletonTest.cc |
Location: | line 34, column 9 |
Description: | Potential memory leak |
1 | #include <gtest/gtest.h> | ||||
2 | #include "util/Singleton.h" | ||||
3 | |||||
4 | namespace orxonox | ||||
5 | { | ||||
6 | namespace | ||||
7 | { | ||||
8 | class TestSingleton : public Singleton<TestSingleton> | ||||
9 | { | ||||
10 | friend class Singleton<TestSingleton>; | ||||
11 | |||||
12 | public: | ||||
13 | TestSingleton() : value_(MAGIC_VALUE) {} | ||||
14 | virtual ~TestSingleton() {} | ||||
15 | |||||
16 | size_t getValue() const { return this->value_; } | ||||
17 | |||||
18 | static const size_t MAGIC_VALUE; | ||||
19 | |||||
20 | private: | ||||
21 | size_t value_; | ||||
22 | static TestSingleton* singletonPtr_s; | ||||
23 | }; | ||||
24 | |||||
25 | TestSingleton* TestSingleton::singletonPtr_s = nullptr; | ||||
26 | const size_t TestSingleton::MAGIC_VALUE = 0xCAFEBABE; | ||||
27 | } | ||||
28 | |||||
29 | TEST(DISABLED_SingletonDeathTest, MustBeUnique)class DISABLED_SingletonDeathTest_MustBeUnique_Test : public :: testing::Test { public: DISABLED_SingletonDeathTest_MustBeUnique_Test () {} private: virtual void TestBody(); static ::testing::TestInfo * const test_info_ __attribute__ ((unused)); DISABLED_SingletonDeathTest_MustBeUnique_Test (DISABLED_SingletonDeathTest_MustBeUnique_Test const &); void operator=(DISABLED_SingletonDeathTest_MustBeUnique_Test const &);};::testing::TestInfo* const DISABLED_SingletonDeathTest_MustBeUnique_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "DISABLED_SingletonDeathTest", "MustBeUnique", __null, __null , (::testing::internal::GetTestTypeId()), ::testing::Test::SetUpTestCase , ::testing::Test::TearDownTestCase, new ::testing::internal:: TestFactoryImpl< DISABLED_SingletonDeathTest_MustBeUnique_Test >);void DISABLED_SingletonDeathTest_MustBeUnique_Test::TestBody () | ||||
30 | { | ||||
31 | // create first instance | ||||
32 | TestSingleton* instance = new TestSingleton(); | ||||
33 | // create second instance, must abort | ||||
34 | EXPECT_DEATH(new TestSingleton(), ".*")switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { const ::testing::internal::RE& gtest_regex = (".*") ; ::testing::internal::DeathTest* gtest_dt; if (!::testing::internal ::DeathTest::Create("new TestSingleton()", >est_regex, "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 34, >est_dt)) { goto gtest_label_34; } if (gtest_dt != __null) { ::testing::internal::scoped_ptr< ::testing::internal ::DeathTest> gtest_dt_ptr(gtest_dt); switch (gtest_dt-> AssumeRole()) { case ::testing::internal::DeathTest::OVERSEE_TEST : if (!gtest_dt->Passed(::testing::internal::ExitedUnsuccessfully (gtest_dt->Wait()))) { goto gtest_label_34; } break; case :: testing::internal::DeathTest::EXECUTE_TEST: { ::testing::internal ::DeathTest::ReturnSentinel gtest_sentinel(gtest_dt); try { if (::testing::internal::AlwaysTrue()) { new TestSingleton(); } ; } catch (const ::std::exception& gtest_exception) { fprintf ( stderr, "\n%s: Caught std::exception-derived exception escaping the " "death test statement. Exception message: %s\n", ::testing:: internal::FormatFileLocation("/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 34).c_str(), gtest_exception.what()); fflush(stderr); gtest_dt ->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION ); } catch (...) { gtest_dt->Abort(::testing::internal::DeathTest ::TEST_THREW_EXCEPTION); }; gtest_dt->Abort(::testing::internal ::DeathTest::TEST_DID_NOT_DIE); break; } default: break; } } } else gtest_label_34: ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 34, ::testing::internal::DeathTest::LastMessage()) = ::testing ::Message(); | ||||
Within the expansion of the macro 'EXPECT_DEATH':
| |||||
35 | |||||
36 | delete instance; | ||||
37 | } | ||||
38 | |||||
39 | TEST(DISABLED_SingletonDeathTest, MustBeCreated)class DISABLED_SingletonDeathTest_MustBeCreated_Test : public ::testing::Test { public: DISABLED_SingletonDeathTest_MustBeCreated_Test () {} private: virtual void TestBody(); static ::testing::TestInfo * const test_info_ __attribute__ ((unused)); DISABLED_SingletonDeathTest_MustBeCreated_Test (DISABLED_SingletonDeathTest_MustBeCreated_Test const &); void operator=(DISABLED_SingletonDeathTest_MustBeCreated_Test const &);};::testing::TestInfo* const DISABLED_SingletonDeathTest_MustBeCreated_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "DISABLED_SingletonDeathTest", "MustBeCreated", __null, __null , (::testing::internal::GetTestTypeId()), ::testing::Test::SetUpTestCase , ::testing::Test::TearDownTestCase, new ::testing::internal:: TestFactoryImpl< DISABLED_SingletonDeathTest_MustBeCreated_Test >);void DISABLED_SingletonDeathTest_MustBeCreated_Test::TestBody () | ||||
40 | { | ||||
41 | // no instance created, must abort | ||||
42 | EXPECT_DEATH(TestSingleton::getInstance(), ".*")switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { const ::testing::internal::RE& gtest_regex = (".*") ; ::testing::internal::DeathTest* gtest_dt; if (!::testing::internal ::DeathTest::Create("TestSingleton::getInstance()", >est_regex , "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 42, >est_dt)) { goto gtest_label_42; } if (gtest_dt != __null) { ::testing::internal::scoped_ptr< ::testing::internal ::DeathTest> gtest_dt_ptr(gtest_dt); switch (gtest_dt-> AssumeRole()) { case ::testing::internal::DeathTest::OVERSEE_TEST : if (!gtest_dt->Passed(::testing::internal::ExitedUnsuccessfully (gtest_dt->Wait()))) { goto gtest_label_42; } break; case :: testing::internal::DeathTest::EXECUTE_TEST: { ::testing::internal ::DeathTest::ReturnSentinel gtest_sentinel(gtest_dt); try { if (::testing::internal::AlwaysTrue()) { TestSingleton::getInstance (); }; } catch (const ::std::exception& gtest_exception) { fprintf( stderr, "\n%s: Caught std::exception-derived exception escaping the " "death test statement. Exception message: %s\n", ::testing:: internal::FormatFileLocation("/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 42).c_str(), gtest_exception.what()); fflush(stderr); gtest_dt ->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION ); } catch (...) { gtest_dt->Abort(::testing::internal::DeathTest ::TEST_THREW_EXCEPTION); }; gtest_dt->Abort(::testing::internal ::DeathTest::TEST_DID_NOT_DIE); break; } default: break; } } } else gtest_label_42: ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 42, ::testing::internal::DeathTest::LastMessage()) = ::testing ::Message(); | ||||
43 | } | ||||
44 | |||||
45 | TEST(DISABLED_SingletonDeathTest, MustBeReset)class DISABLED_SingletonDeathTest_MustBeReset_Test : public :: testing::Test { public: DISABLED_SingletonDeathTest_MustBeReset_Test () {} private: virtual void TestBody(); static ::testing::TestInfo * const test_info_ __attribute__ ((unused)); DISABLED_SingletonDeathTest_MustBeReset_Test (DISABLED_SingletonDeathTest_MustBeReset_Test const &); void operator=(DISABLED_SingletonDeathTest_MustBeReset_Test const &);};::testing::TestInfo* const DISABLED_SingletonDeathTest_MustBeReset_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "DISABLED_SingletonDeathTest", "MustBeReset", __null, __null , (::testing::internal::GetTestTypeId()), ::testing::Test::SetUpTestCase , ::testing::Test::TearDownTestCase, new ::testing::internal:: TestFactoryImpl< DISABLED_SingletonDeathTest_MustBeReset_Test >);void DISABLED_SingletonDeathTest_MustBeReset_Test::TestBody () | ||||
46 | { | ||||
47 | // create instance | ||||
48 | TestSingleton* instance = new TestSingleton(); | ||||
49 | // getInstance() must return newly created instance | ||||
50 | EXPECT_EQ(instance, &TestSingleton::getInstance())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal:: EqHelper<(sizeof(::testing ::internal::IsNullLiteralHelper(instance)) == 1)>::Compare ("instance", "&TestSingleton::getInstance()", instance, & TestSingleton::getInstance()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 50, gtest_ar.failure_message()) = ::testing::Message(); | ||||
51 | |||||
52 | // delete instance | ||||
53 | delete instance; | ||||
54 | // must abort | ||||
55 | EXPECT_DEATH(TestSingleton::getInstance(), ".*")switch (0) case 0: default: if (::testing::internal::AlwaysTrue ()) { const ::testing::internal::RE& gtest_regex = (".*") ; ::testing::internal::DeathTest* gtest_dt; if (!::testing::internal ::DeathTest::Create("TestSingleton::getInstance()", >est_regex , "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 55, >est_dt)) { goto gtest_label_55; } if (gtest_dt != __null) { ::testing::internal::scoped_ptr< ::testing::internal ::DeathTest> gtest_dt_ptr(gtest_dt); switch (gtest_dt-> AssumeRole()) { case ::testing::internal::DeathTest::OVERSEE_TEST : if (!gtest_dt->Passed(::testing::internal::ExitedUnsuccessfully (gtest_dt->Wait()))) { goto gtest_label_55; } break; case :: testing::internal::DeathTest::EXECUTE_TEST: { ::testing::internal ::DeathTest::ReturnSentinel gtest_sentinel(gtest_dt); try { if (::testing::internal::AlwaysTrue()) { TestSingleton::getInstance (); }; } catch (const ::std::exception& gtest_exception) { fprintf( stderr, "\n%s: Caught std::exception-derived exception escaping the " "death test statement. Exception message: %s\n", ::testing:: internal::FormatFileLocation("/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 55).c_str(), gtest_exception.what()); fflush(stderr); gtest_dt ->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION ); } catch (...) { gtest_dt->Abort(::testing::internal::DeathTest ::TEST_THREW_EXCEPTION); }; gtest_dt->Abort(::testing::internal ::DeathTest::TEST_DID_NOT_DIE); break; } default: break; } } } else gtest_label_55: ::testing::internal::AssertHelper(::testing ::TestPartResult::kNonFatalFailure, "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 55, ::testing::internal::DeathTest::LastMessage()) = ::testing ::Message(); | ||||
56 | } | ||||
57 | |||||
58 | TEST(Singleton, MustBeTheSame)class Singleton_MustBeTheSame_Test : public ::testing::Test { public: Singleton_MustBeTheSame_Test() {} private: virtual void TestBody(); static ::testing::TestInfo* const test_info_ __attribute__ ((unused)); Singleton_MustBeTheSame_Test(Singleton_MustBeTheSame_Test const &); void operator=(Singleton_MustBeTheSame_Test const &);};::testing::TestInfo* const Singleton_MustBeTheSame_Test ::test_info_ = ::testing::internal::MakeAndRegisterTestInfo( "Singleton", "MustBeTheSame", __null, __null, (::testing::internal ::GetTestTypeId()), ::testing::Test::SetUpTestCase, ::testing ::Test::TearDownTestCase, new ::testing::internal::TestFactoryImpl < Singleton_MustBeTheSame_Test>);void Singleton_MustBeTheSame_Test ::TestBody() | ||||
59 | { | ||||
60 | // create instance | ||||
61 | TestSingleton* instance = new TestSingleton(); | ||||
62 | |||||
63 | EXPECT_EQ(instance, &TestSingleton::getInstance())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal:: EqHelper<(sizeof(::testing ::internal::IsNullLiteralHelper(instance)) == 1)>::Compare ("instance", "&TestSingleton::getInstance()", instance, & TestSingleton::getInstance()))) ; else ::testing::internal::AssertHelper (::testing::TestPartResult::kNonFatalFailure, "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 63, gtest_ar.failure_message()) = ::testing::Message(); | ||||
64 | EXPECT_EQ(TestSingleton::MAGIC_VALUE, TestSingleton::getInstance().getValue())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar = (::testing::internal:: EqHelper<(sizeof(::testing ::internal::IsNullLiteralHelper(TestSingleton::MAGIC_VALUE)) == 1)>::Compare("TestSingleton::MAGIC_VALUE", "TestSingleton::getInstance().getValue()" , TestSingleton::MAGIC_VALUE, TestSingleton::getInstance().getValue ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 64, gtest_ar.failure_message()) = ::testing::Message(); | ||||
65 | |||||
66 | // delete instance | ||||
67 | delete instance; | ||||
68 | } | ||||
69 | |||||
70 | TEST(Singleton, Exists)class Singleton_Exists_Test : public ::testing::Test { public : Singleton_Exists_Test() {} private: virtual void TestBody() ; static ::testing::TestInfo* const test_info_ __attribute__ ( (unused)); Singleton_Exists_Test(Singleton_Exists_Test const & ); void operator=(Singleton_Exists_Test const &);};::testing ::TestInfo* const Singleton_Exists_Test ::test_info_ = ::testing ::internal::MakeAndRegisterTestInfo( "Singleton", "Exists", __null , __null, (::testing::internal::GetTestTypeId()), ::testing:: Test::SetUpTestCase, ::testing::Test::TearDownTestCase, new :: testing::internal::TestFactoryImpl< Singleton_Exists_Test> );void Singleton_Exists_Test::TestBody() | ||||
71 | { | ||||
72 | EXPECT_FALSE(TestSingleton::exists())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(TestSingleton::exists ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 72, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "TestSingleton::exists()", "true", "false").c_str()) = ::testing ::Message(); | ||||
73 | |||||
74 | // create instance | ||||
75 | TestSingleton* instance = new TestSingleton(); | ||||
76 | |||||
77 | EXPECT_TRUE(TestSingleton::exists())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(TestSingleton::exists ())) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 77, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "TestSingleton::exists()", "false", "true").c_str()) = ::testing ::Message(); | ||||
78 | |||||
79 | // delete instance | ||||
80 | delete instance; | ||||
81 | |||||
82 | EXPECT_FALSE(TestSingleton::exists())switch (0) case 0: default: if (const ::testing::AssertionResult gtest_ar_ = ::testing::AssertionResult(!(TestSingleton::exists ()))) ; else ::testing::internal::AssertHelper(::testing::TestPartResult ::kNonFatalFailure, "/home/jenkins/workspace/orxonox_qc_trunk_checks/test/util/SingletonTest.cc" , 82, ::testing::internal::GetBoolAssertionFailureMessage( gtest_ar_ , "TestSingleton::exists()", "true", "false").c_str()) = ::testing ::Message(); | ||||
83 | } | ||||
84 | } |