source: src/unittests/SingletonTest.cpp@ 4c1230

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 4c1230 was bcf653, checked in by Frederik Heber <heber@…>, 14 years ago

Added copyright note to each .cpp file and an extensive one to builder.cpp.

  • Property mode set to 100644
File size: 4.8 KB
Line 
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
34CPPUNIT_TEST_SUITE_REGISTRATION( SingletonTest );
35
36// some necessary stubs
37class SingletonStub1 : public Singleton <SingletonStub1>{
38 friend class Singleton<SingletonStub1>;
39private:
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 }
50public:
51 static int count1;
52 static int count2;
53};
54
55int SingletonStub1::count1 = 0;
56int SingletonStub1::count2 = 0;
57
58CONSTRUCT_SINGLETON(SingletonStub1);
59
60class SingletonStub2 : public Singleton<SingletonStub2>{
61 friend class Singleton<SingletonStub2>;
62private:
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 }
73public:
74 static int count1;
75 static int count2;
76};
77
78int SingletonStub2::count1 = 0;
79int SingletonStub2::count2 = 0;
80
81CONSTRUCT_SINGLETON(SingletonStub2);
82
83void SingletonTest::setUp(){}
84void SingletonTest::tearDown(){}
85
86void 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}
Note: See TracBrowser for help on using the repository browser.