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