source: src/unittests/SingletonTest.cpp@ 13e3c3

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 13e3c3 was bf3817, checked in by Frederik Heber <heber@…>, 14 years ago

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

  • is now topmost in front of MemDebug.hpp (and any other).
  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * SingletonTest.cpp
3 *
4 * Created on: Mar 11, 2010
5 * Author: crueger
6 */
7
8// include config.h
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12
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
27CPPUNIT_TEST_SUITE_REGISTRATION( SingletonTest );
28
29// some necessary stubs
30class SingletonStub1 : public Singleton <SingletonStub1>{
31 friend class Singleton<SingletonStub1>;
32private:
33 SingletonStub1(){
34 count1++;
35 }
36 // explicit copy constructor to catch if this is ever called
37 SingletonStub1(const SingletonStub1&){
38 CPPUNIT_FAIL ( "Copy constructor of Singleton called" );
39 }
40 virtual ~SingletonStub1(){
41 count2++;
42 }
43public:
44 static int count1;
45 static int count2;
46};
47
48int SingletonStub1::count1 = 0;
49int SingletonStub1::count2 = 0;
50
51CONSTRUCT_SINGLETON(SingletonStub1);
52
53class SingletonStub2 : public Singleton<SingletonStub2>{
54 friend class Singleton<SingletonStub2>;
55private:
56 SingletonStub2(){
57 count1++;
58 }
59 // explicit copy constructor to catch if this is ever called
60 SingletonStub2(const SingletonStub2&){
61 CPPUNIT_FAIL ( "Copy constructor of Singleton called" );
62 }
63 virtual ~SingletonStub2(){
64 count2++;
65 }
66public:
67 static int count1;
68 static int count2;
69};
70
71int SingletonStub2::count1 = 0;
72int SingletonStub2::count2 = 0;
73
74CONSTRUCT_SINGLETON(SingletonStub2);
75
76void SingletonTest::setUp(){}
77void SingletonTest::tearDown(){}
78
79void 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);
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);
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}
Note: See TracBrowser for help on using the repository browser.