source: src/unittests/SingletonTest.cpp@ cd032d

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 cd032d was e73a8a2, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Changed ActionRegistry to use the new Singleton Mechanism

  • Property mode set to 100644
File size: 4.2 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 // no calls to the destructor should have occured so far
90 CPPUNIT_ASSERT_EQUAL(0,SingletonStub1::count2);
91 CPPUNIT_ASSERT_EQUAL(0,SingletonStub2::count2);
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}
Note: See TracBrowser for help on using the repository browser.