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