source: molecuilder/src/unittests/SingletonTest.cpp@ ff1812

Last change on this file since ff1812 was ff1812, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added Unittest for Singleton pattern

  • Property mode set to 100644
File size: 4.0 KB
Line 
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
22CPPUNIT_TEST_SUITE_REGISTRATION( SingletonTest );
23
24// some necessary stubs
25class SingletonStub1 : public Singleton <SingletonStub1>{
26 friend class Singleton<SingletonStub1>;
27private:
28 SingletonStub1(){
29 count1++;
30 }
31 virtual ~SingletonStub1(){
32 count2++;
33 }
34public:
35 static int count1;
36 static int count2;
37};
38
39int SingletonStub1::count1 = 0;
40int SingletonStub1::count2 = 0;
41
42CONSTRUCT_SINGLETON(SingletonStub1);
43
44class SingletonStub2 : public Singleton<SingletonStub2>{
45 friend class Singleton<SingletonStub2>;
46private:
47 SingletonStub2(){
48 count1++;
49 }
50 virtual ~SingletonStub2(){
51 count2++;
52 }
53public:
54 static int count1;
55 static int count2;
56};
57
58int SingletonStub2::count1 = 0;
59int SingletonStub2::count2 = 0;
60
61CONSTRUCT_SINGLETON(SingletonStub2);
62
63void SingletonTest::setUp(){}
64void SingletonTest::tearDown(){}
65
66void 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);
89
90 SingletonStub1::purgeInstance();
91
92 void *ptr_3_1 = reinterpret_cast<void*>(SingletonStub1::getPointer());
93 void *ptr_3_2 = reinterpret_cast<void*>(SingletonStub1::getPointer());
94
95 // now the constructor should have been called twice
96 CPPUNIT_ASSERT_EQUAL(2,SingletonStub1::count1);
97 // the destructor should have been called once
98 CPPUNIT_ASSERT_EQUAL(1,SingletonStub1::count2);
99 // see if the singleton Assumption holds
100 CPPUNIT_ASSERT_EQUAL(ptr_3_1,ptr_3_2);
101 // Some esoteric thing might cause our pointer to lay at the position of Singleton2 now
102 // See that those two objects still differ
103 CPPUNIT_ASSERT(ptr_3_1!=ptr_2_1);
104 // don't test for pointer difference between first and second singleton here,
105 // because they might be constructed in the same place
106
107
108 SingletonStub2::resetInstance();
109 // now the constructor should have been called twice
110 CPPUNIT_ASSERT_EQUAL(2,SingletonStub2::count1);
111 // the destructor should have been called once
112 CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count2);
113
114 void *ptr_4_1 = reinterpret_cast<void*>(SingletonStub2::getPointer());
115 void *ptr_4_2 = reinterpret_cast<void*>(SingletonStub2::getPointer());
116
117 // Still only two calls to the constructor, one call to destructor
118 CPPUNIT_ASSERT_EQUAL(2,SingletonStub2::count1);
119 CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count2);
120
121 // test if Singleton assumption can be broken by reset
122 CPPUNIT_ASSERT_EQUAL(ptr_4_1,ptr_4_2);
123
124 // and again test if we actually have a object seperate from anything else
125 CPPUNIT_ASSERT(ptr_4_1!=ptr_3_1);
126
127
128 // we don't purge our singletons here. Purging should be done automatically by the Singleton
129 // mechanism. Check with Valgrind to see if memory-leak occurs
130 std::cout << "Not purging Singleton!\n Check with Valgrind to see if automatic purgins is working!" << std::endl;
131
132}
Note: See TracBrowser for help on using the repository browser.