source: src/unittests/SingletonTest.cpp@ 88b400

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 88b400 was b8d4a3, checked in by Saskia Metzler <metzler@…>, 15 years ago

Tremolo format parser

  • Property mode set to 100644
File size: 4.5 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 // explicit copy constructor to catch if this is ever called
32 SingletonStub1(const SingletonStub1&){
33 CPPUNIT_FAIL ( "Copy constructor of Singleton called" );
34 }
35 virtual ~SingletonStub1(){
36 count2++;
37 }
38public:
39 static int count1;
40 static int count2;
41};
42
43int SingletonStub1::count1 = 0;
44int SingletonStub1::count2 = 0;
45
46CONSTRUCT_SINGLETON(SingletonStub1);
47
48class SingletonStub2 : public Singleton<SingletonStub2>{
49 friend class Singleton<SingletonStub2>;
50private:
51 SingletonStub2(){
52 count1++;
53 }
54 // explicit copy constructor to catch if this is ever called
55 SingletonStub2(const SingletonStub2&){
56 CPPUNIT_FAIL ( "Copy constructor of Singleton called" );
57 }
58 virtual ~SingletonStub2(){
59 count2++;
60 }
61public:
62 static int count1;
63 static int count2;
64};
65
66int SingletonStub2::count1 = 0;
67int SingletonStub2::count2 = 0;
68
69CONSTRUCT_SINGLETON(SingletonStub2);
70
71void SingletonTest::setUp(){}
72void SingletonTest::tearDown(){}
73
74void SingletonTest::ConstructionTest(){
75 void *ptr_1_1 = reinterpret_cast<void*>(SingletonStub1::getPointer());
76 void *ptr_1_2 = reinterpret_cast<void*>(SingletonStub1::getPointer());
77 void *ptr_1_3 = reinterpret_cast<void*>(&(SingletonStub1::getInstance()));
78
79 // test if we always get the same instance of our singleton
80 CPPUNIT_ASSERT_EQUAL(ptr_1_1,ptr_1_2);
81 CPPUNIT_ASSERT_EQUAL(ptr_1_1,ptr_1_3);
82
83 void *ptr_2_1 = reinterpret_cast<void*>(SingletonStub2::getPointer());
84 void *ptr_2_2 = reinterpret_cast<void*>(SingletonStub2::getPointer());
85 void *ptr_2_3 = reinterpret_cast<void*>(&(SingletonStub2::getInstance()));
86
87 // same as above but for a different singleton
88 CPPUNIT_ASSERT_EQUAL(ptr_2_1,ptr_2_2);
89 CPPUNIT_ASSERT_EQUAL(ptr_2_1,ptr_2_3);
90
91 // see if the two singletons actually differ
92 CPPUNIT_ASSERT(ptr_1_1!=ptr_2_1);
93
94 // see if each constructor was called exactly once
95 CPPUNIT_ASSERT_EQUAL(1,SingletonStub1::count1);
96 CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count1);
97 // no calls to the destructor should have occured so far
98 CPPUNIT_ASSERT_EQUAL(0,SingletonStub1::count2);
99 CPPUNIT_ASSERT_EQUAL(0,SingletonStub2::count2);
100
101 SingletonStub1::purgeInstance();
102
103 void *ptr_3_1 = reinterpret_cast<void*>(SingletonStub1::getPointer());
104 void *ptr_3_2 = reinterpret_cast<void*>(SingletonStub1::getPointer());
105
106 // now the constructor should have been called twice
107 CPPUNIT_ASSERT_EQUAL(2,SingletonStub1::count1);
108 // the destructor should have been called once
109 CPPUNIT_ASSERT_EQUAL(1,SingletonStub1::count2);
110 // see if the singleton Assumption holds
111 CPPUNIT_ASSERT_EQUAL(ptr_3_1,ptr_3_2);
112 // Some esoteric thing might cause our pointer to lay at the position of Singleton2 now
113 // See that those two objects still differ
114 CPPUNIT_ASSERT(ptr_3_1!=ptr_2_1);
115 // don't test for pointer difference between first and second singleton here,
116 // because they might be constructed in the same place
117
118
119 SingletonStub2::resetInstance();
120 // now the constructor should have been called twice
121 CPPUNIT_ASSERT_EQUAL(2,SingletonStub2::count1);
122 // the destructor should have been called once
123 CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count2);
124
125 void *ptr_4_1 = reinterpret_cast<void*>(SingletonStub2::getPointer());
126 void *ptr_4_2 = reinterpret_cast<void*>(SingletonStub2::getPointer());
127
128 // Still only two calls to the constructor, one call to destructor
129 CPPUNIT_ASSERT_EQUAL(2,SingletonStub2::count1);
130 CPPUNIT_ASSERT_EQUAL(1,SingletonStub2::count2);
131
132 // test if Singleton assumption can be broken by reset
133 CPPUNIT_ASSERT_EQUAL(ptr_4_1,ptr_4_2);
134
135 // and again test if we actually have a object seperate from anything else
136 CPPUNIT_ASSERT(ptr_4_1!=ptr_3_1);
137
138
139 // we don't purge our singletons here. Purging should be done automatically by the Singleton
140 // mechanism. Check with Valgrind to see if memory-leak occurs
141 std::cout << "Not purging Singleton!\n Check with Valgrind to see if automatic purgins is working!" << std::endl;
142
143}
Note: See TracBrowser for help on using the repository browser.