| 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 = NULL; |
|---|
| 26 | const size_t TestSingleton::MAGIC_VALUE = 0xCAFEBABE; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | TEST(DISABLED_SingletonDeathTest, MustBeUnique) |
|---|
| 30 | { |
|---|
| 31 | // create first instance |
|---|
| 32 | TestSingleton* instance = new TestSingleton(); |
|---|
| 33 | // create second instance, must abort |
|---|
| 34 | EXPECT_DEATH(new TestSingleton(), ".*"); |
|---|
| 35 | |
|---|
| 36 | delete instance; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | TEST(DISABLED_SingletonDeathTest, MustBeCreated) |
|---|
| 40 | { |
|---|
| 41 | // no instance created, must abort |
|---|
| 42 | EXPECT_DEATH(TestSingleton::getInstance(), ".*"); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | TEST(DISABLED_SingletonDeathTest, MustBeReset) |
|---|
| 46 | { |
|---|
| 47 | // create instance |
|---|
| 48 | TestSingleton* instance = new TestSingleton(); |
|---|
| 49 | // getInstance() must return newly created instance |
|---|
| 50 | EXPECT_EQ(instance, &TestSingleton::getInstance()); |
|---|
| 51 | |
|---|
| 52 | // delete instance |
|---|
| 53 | delete instance; |
|---|
| 54 | // must abort |
|---|
| 55 | EXPECT_DEATH(TestSingleton::getInstance(), ".*"); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | TEST(Singleton, MustBeTheSame) |
|---|
| 59 | { |
|---|
| 60 | // create instance |
|---|
| 61 | TestSingleton* instance = new TestSingleton(); |
|---|
| 62 | |
|---|
| 63 | EXPECT_EQ(instance, &TestSingleton::getInstance()); |
|---|
| 64 | EXPECT_EQ(TestSingleton::MAGIC_VALUE, TestSingleton::getInstance().getValue()); |
|---|
| 65 | |
|---|
| 66 | // delete instance |
|---|
| 67 | delete instance; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | TEST(Singleton, Exists) |
|---|
| 71 | { |
|---|
| 72 | EXPECT_FALSE(TestSingleton::exists()); |
|---|
| 73 | |
|---|
| 74 | // create instance |
|---|
| 75 | TestSingleton* instance = new TestSingleton(); |
|---|
| 76 | |
|---|
| 77 | EXPECT_TRUE(TestSingleton::exists()); |
|---|
| 78 | |
|---|
| 79 | // delete instance |
|---|
| 80 | delete instance; |
|---|
| 81 | |
|---|
| 82 | EXPECT_FALSE(TestSingleton::exists()); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|