[d25863] | 1 | /*
|
---|
| 2 | * SingletonTest.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Mar 11, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include "SingletonTest.hpp"
|
---|
| 9 |
|
---|
| 10 | #include <cppunit/CompilerOutputter.h>
|
---|
| 11 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 12 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 13 | #include <iostream>
|
---|
| 14 |
|
---|
| 15 | #include "Patterns/Singleton.hpp"
|
---|
| 16 | #include "Patterns/Singleton_impl.hpp"
|
---|
| 17 |
|
---|
| 18 | #ifdef HAVE_TESTRUNNER
|
---|
| 19 | #include "UnitTestMain.hpp"
|
---|
| 20 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 21 |
|
---|
| 22 | CPPUNIT_TEST_SUITE_REGISTRATION( SingletonTest );
|
---|
| 23 |
|
---|
| 24 | // some necessary stubs
|
---|
| 25 | class SingletonStub1 : public Singleton <SingletonStub1>{
|
---|
| 26 | friend class Singleton<SingletonStub1>;
|
---|
| 27 | private:
|
---|
| 28 | SingletonStub1(){
|
---|
| 29 | count1++;
|
---|
| 30 | }
|
---|
[0f6f3a] | 31 | // explicit copy constructor to catch if this is ever called
|
---|
| 32 | SingletonStub1(const SingletonStub1&){
|
---|
| 33 | CPPUNIT_FAIL ( "Copy constructor of Singleton called" );
|
---|
| 34 | }
|
---|
[d25863] | 35 | virtual ~SingletonStub1(){
|
---|
| 36 | count2++;
|
---|
| 37 | }
|
---|
| 38 | public:
|
---|
| 39 | static int count1;
|
---|
| 40 | static int count2;
|
---|
| 41 | };
|
---|
| 42 |
|
---|
| 43 | int SingletonStub1::count1 = 0;
|
---|
| 44 | int SingletonStub1::count2 = 0;
|
---|
| 45 |
|
---|
| 46 | CONSTRUCT_SINGLETON(SingletonStub1);
|
---|
| 47 |
|
---|
| 48 | class SingletonStub2 : public Singleton<SingletonStub2>{
|
---|
| 49 | friend class Singleton<SingletonStub2>;
|
---|
| 50 | private:
|
---|
| 51 | SingletonStub2(){
|
---|
| 52 | count1++;
|
---|
| 53 | }
|
---|
[b8d4a3] | 54 | // explicit copy constructor to catch if this is ever called
|
---|
[0f6f3a] | 55 | SingletonStub2(const SingletonStub2&){
|
---|
| 56 | CPPUNIT_FAIL ( "Copy constructor of Singleton called" );
|
---|
| 57 | }
|
---|
[d25863] | 58 | virtual ~SingletonStub2(){
|
---|
| 59 | count2++;
|
---|
| 60 | }
|
---|
| 61 | public:
|
---|
| 62 | static int count1;
|
---|
| 63 | static int count2;
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 | int SingletonStub2::count1 = 0;
|
---|
| 67 | int SingletonStub2::count2 = 0;
|
---|
| 68 |
|
---|
| 69 | CONSTRUCT_SINGLETON(SingletonStub2);
|
---|
| 70 |
|
---|
| 71 | void SingletonTest::setUp(){}
|
---|
| 72 | void SingletonTest::tearDown(){}
|
---|
| 73 |
|
---|
| 74 | void SingletonTest::ConstructionTest(){
|
---|
| 75 | void *ptr_1_1 = reinterpret_cast<void*>(SingletonStub1::getPointer());
|
---|
| 76 | void *ptr_1_2 = reinterpret_cast<void*>(SingletonStub1::getPointer());
|
---|
| 77 | void *ptr_1_3 = reinterpret_cast<void*>(&(SingletonStub1::getInstance()));
|
---|
| 78 |
|
---|
| 79 | // test if we always get the same instance of our singleton
|
---|
| 80 | CPPUNIT_ASSERT_EQUAL(ptr_1_1,ptr_1_2);
|
---|
| 81 | CPPUNIT_ASSERT_EQUAL(ptr_1_1,ptr_1_3);
|
---|
| 82 |
|
---|
| 83 | void *ptr_2_1 = reinterpret_cast<void*>(SingletonStub2::getPointer());
|
---|
| 84 | void *ptr_2_2 = reinterpret_cast<void*>(SingletonStub2::getPointer());
|
---|
| 85 | void *ptr_2_3 = reinterpret_cast<void*>(&(SingletonStub2::getInstance()));
|
---|
| 86 |
|
---|
| 87 | // same as above but for a different singleton
|
---|
| 88 | CPPUNIT_ASSERT_EQUAL(ptr_2_1,ptr_2_2);
|
---|
| 89 | CPPUNIT_ASSERT_EQUAL(ptr_2_1,ptr_2_3);
|
---|
| 90 |
|
---|
| 91 | // see if the two singletons actually differ
|
---|
| 92 | CPPUNIT_ASSERT(ptr_1_1!=ptr_2_1);
|
---|
| 93 |
|
---|
| 94 | // see if each constructor was called exactly once
|
---|
| 95 | CPPUNIT_ASSERT_EQUAL(1,SingletonStub1::count1);
|
---|
| 96 | CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count1);
|
---|
[e73a8a2] | 97 | // no calls to the destructor should have occured so far
|
---|
| 98 | CPPUNIT_ASSERT_EQUAL(0,SingletonStub1::count2);
|
---|
| 99 | CPPUNIT_ASSERT_EQUAL(0,SingletonStub2::count2);
|
---|
[d25863] | 100 |
|
---|
| 101 | SingletonStub1::purgeInstance();
|
---|
| 102 |
|
---|
| 103 | void *ptr_3_1 = reinterpret_cast<void*>(SingletonStub1::getPointer());
|
---|
| 104 | void *ptr_3_2 = reinterpret_cast<void*>(SingletonStub1::getPointer());
|
---|
| 105 |
|
---|
| 106 | // now the constructor should have been called twice
|
---|
| 107 | CPPUNIT_ASSERT_EQUAL(2,SingletonStub1::count1);
|
---|
| 108 | // the destructor should have been called once
|
---|
| 109 | CPPUNIT_ASSERT_EQUAL(1,SingletonStub1::count2);
|
---|
| 110 | // see if the singleton Assumption holds
|
---|
| 111 | CPPUNIT_ASSERT_EQUAL(ptr_3_1,ptr_3_2);
|
---|
| 112 | // Some esoteric thing might cause our pointer to lay at the position of Singleton2 now
|
---|
| 113 | // See that those two objects still differ
|
---|
| 114 | CPPUNIT_ASSERT(ptr_3_1!=ptr_2_1);
|
---|
| 115 | // don't test for pointer difference between first and second singleton here,
|
---|
| 116 | // because they might be constructed in the same place
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | SingletonStub2::resetInstance();
|
---|
| 120 | // now the constructor should have been called twice
|
---|
| 121 | CPPUNIT_ASSERT_EQUAL(2,SingletonStub2::count1);
|
---|
| 122 | // the destructor should have been called once
|
---|
| 123 | CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count2);
|
---|
| 124 |
|
---|
| 125 | void *ptr_4_1 = reinterpret_cast<void*>(SingletonStub2::getPointer());
|
---|
| 126 | void *ptr_4_2 = reinterpret_cast<void*>(SingletonStub2::getPointer());
|
---|
| 127 |
|
---|
| 128 | // Still only two calls to the constructor, one call to destructor
|
---|
| 129 | CPPUNIT_ASSERT_EQUAL(2,SingletonStub2::count1);
|
---|
| 130 | CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count2);
|
---|
| 131 |
|
---|
| 132 | // test if Singleton assumption can be broken by reset
|
---|
| 133 | CPPUNIT_ASSERT_EQUAL(ptr_4_1,ptr_4_2);
|
---|
| 134 |
|
---|
| 135 | // and again test if we actually have a object seperate from anything else
|
---|
| 136 | CPPUNIT_ASSERT(ptr_4_1!=ptr_3_1);
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | // we don't purge our singletons here. Purging should be done automatically by the Singleton
|
---|
| 140 | // mechanism. Check with Valgrind to see if memory-leak occurs
|
---|
| 141 | std::cout << "Not purging Singleton!\n Check with Valgrind to see if automatic purgins is working!" << std::endl;
|
---|
| 142 |
|
---|
| 143 | }
|
---|