[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 | }
|
---|
| 31 | virtual ~SingletonStub1(){
|
---|
| 32 | count2++;
|
---|
| 33 | }
|
---|
| 34 | public:
|
---|
| 35 | static int count1;
|
---|
| 36 | static int count2;
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | int SingletonStub1::count1 = 0;
|
---|
| 40 | int SingletonStub1::count2 = 0;
|
---|
| 41 |
|
---|
| 42 | CONSTRUCT_SINGLETON(SingletonStub1);
|
---|
| 43 |
|
---|
| 44 | class SingletonStub2 : public Singleton<SingletonStub2>{
|
---|
| 45 | friend class Singleton<SingletonStub2>;
|
---|
| 46 | private:
|
---|
| 47 | SingletonStub2(){
|
---|
| 48 | count1++;
|
---|
| 49 | }
|
---|
| 50 | virtual ~SingletonStub2(){
|
---|
| 51 | count2++;
|
---|
| 52 | }
|
---|
| 53 | public:
|
---|
| 54 | static int count1;
|
---|
| 55 | static int count2;
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | int SingletonStub2::count1 = 0;
|
---|
| 59 | int SingletonStub2::count2 = 0;
|
---|
| 60 |
|
---|
| 61 | CONSTRUCT_SINGLETON(SingletonStub2);
|
---|
| 62 |
|
---|
| 63 | void SingletonTest::setUp(){}
|
---|
| 64 | void SingletonTest::tearDown(){}
|
---|
| 65 |
|
---|
| 66 | void SingletonTest::ConstructionTest(){
|
---|
| 67 | void *ptr_1_1 = reinterpret_cast<void*>(SingletonStub1::getPointer());
|
---|
| 68 | void *ptr_1_2 = reinterpret_cast<void*>(SingletonStub1::getPointer());
|
---|
| 69 | void *ptr_1_3 = reinterpret_cast<void*>(&(SingletonStub1::getInstance()));
|
---|
| 70 |
|
---|
| 71 | // test if we always get the same instance of our singleton
|
---|
| 72 | CPPUNIT_ASSERT_EQUAL(ptr_1_1,ptr_1_2);
|
---|
| 73 | CPPUNIT_ASSERT_EQUAL(ptr_1_1,ptr_1_3);
|
---|
| 74 |
|
---|
| 75 | void *ptr_2_1 = reinterpret_cast<void*>(SingletonStub2::getPointer());
|
---|
| 76 | void *ptr_2_2 = reinterpret_cast<void*>(SingletonStub2::getPointer());
|
---|
| 77 | void *ptr_2_3 = reinterpret_cast<void*>(&(SingletonStub2::getInstance()));
|
---|
| 78 |
|
---|
| 79 | // same as above but for a different singleton
|
---|
| 80 | CPPUNIT_ASSERT_EQUAL(ptr_2_1,ptr_2_2);
|
---|
| 81 | CPPUNIT_ASSERT_EQUAL(ptr_2_1,ptr_2_3);
|
---|
| 82 |
|
---|
| 83 | // see if the two singletons actually differ
|
---|
| 84 | CPPUNIT_ASSERT(ptr_1_1!=ptr_2_1);
|
---|
| 85 |
|
---|
| 86 | // see if each constructor was called exactly once
|
---|
| 87 | CPPUNIT_ASSERT_EQUAL(1,SingletonStub1::count1);
|
---|
| 88 | CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count1);
|
---|
[e73a8a2] | 89 | // no calls to the destructor should have occured so far
|
---|
| 90 | CPPUNIT_ASSERT_EQUAL(0,SingletonStub1::count2);
|
---|
| 91 | CPPUNIT_ASSERT_EQUAL(0,SingletonStub2::count2);
|
---|
[d25863] | 92 |
|
---|
| 93 | SingletonStub1::purgeInstance();
|
---|
| 94 |
|
---|
| 95 | void *ptr_3_1 = reinterpret_cast<void*>(SingletonStub1::getPointer());
|
---|
| 96 | void *ptr_3_2 = reinterpret_cast<void*>(SingletonStub1::getPointer());
|
---|
| 97 |
|
---|
| 98 | // now the constructor should have been called twice
|
---|
| 99 | CPPUNIT_ASSERT_EQUAL(2,SingletonStub1::count1);
|
---|
| 100 | // the destructor should have been called once
|
---|
| 101 | CPPUNIT_ASSERT_EQUAL(1,SingletonStub1::count2);
|
---|
| 102 | // see if the singleton Assumption holds
|
---|
| 103 | CPPUNIT_ASSERT_EQUAL(ptr_3_1,ptr_3_2);
|
---|
| 104 | // Some esoteric thing might cause our pointer to lay at the position of Singleton2 now
|
---|
| 105 | // See that those two objects still differ
|
---|
| 106 | CPPUNIT_ASSERT(ptr_3_1!=ptr_2_1);
|
---|
| 107 | // don't test for pointer difference between first and second singleton here,
|
---|
| 108 | // because they might be constructed in the same place
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | SingletonStub2::resetInstance();
|
---|
| 112 | // now the constructor should have been called twice
|
---|
| 113 | CPPUNIT_ASSERT_EQUAL(2,SingletonStub2::count1);
|
---|
| 114 | // the destructor should have been called once
|
---|
| 115 | CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count2);
|
---|
| 116 |
|
---|
| 117 | void *ptr_4_1 = reinterpret_cast<void*>(SingletonStub2::getPointer());
|
---|
| 118 | void *ptr_4_2 = reinterpret_cast<void*>(SingletonStub2::getPointer());
|
---|
| 119 |
|
---|
| 120 | // Still only two calls to the constructor, one call to destructor
|
---|
| 121 | CPPUNIT_ASSERT_EQUAL(2,SingletonStub2::count1);
|
---|
| 122 | CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count2);
|
---|
| 123 |
|
---|
| 124 | // test if Singleton assumption can be broken by reset
|
---|
| 125 | CPPUNIT_ASSERT_EQUAL(ptr_4_1,ptr_4_2);
|
---|
| 126 |
|
---|
| 127 | // and again test if we actually have a object seperate from anything else
|
---|
| 128 | CPPUNIT_ASSERT(ptr_4_1!=ptr_3_1);
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | // we don't purge our singletons here. Purging should be done automatically by the Singleton
|
---|
| 132 | // mechanism. Check with Valgrind to see if memory-leak occurs
|
---|
| 133 | std::cout << "Not purging Singleton!\n Check with Valgrind to see if automatic purgins is working!" << std::endl;
|
---|
| 134 |
|
---|
| 135 | }
|
---|