source: src/unittests/manipulateAtomsTest.cpp@ afb47f

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 Candidate_v1.7.0 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 afb47f was afb47f, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added a mechanism that allows the world to track changes done by the manipulators

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * manipulateAtomsTest.cpp
3 *
4 * Created on: Feb 18, 2010
5 * Author: crueger
6 */
7
8#include "manipulateAtomsTest.hpp"
9
10#include "DescriptorUnittest.hpp"
11
12#include <cppunit/CompilerOutputter.h>
13#include <cppunit/extensions/TestFactoryRegistry.h>
14#include <cppunit/ui/text/TestRunner.h>
15#include <iostream>
16#include <boost/bind.hpp>
17
18#include "Descriptors/AtomDescriptor.hpp"
19#include "Descriptors/AtomIdDescriptor.hpp"
20#include "Actions/ManipulateAtomsProcess.hpp"
21#include "Actions/ActionRegistry.hpp"
22
23#include "World.hpp"
24#include "atom.hpp"
25
26// Registers the fixture into the 'registry'
27CPPUNIT_TEST_SUITE_REGISTRATION( manipulateAtomsTest );
28
29// some stubs
30class AtomStub : public atom {
31public:
32 AtomStub(int _id) :
33 atom(),
34 id(_id),
35 manipulated(false)
36 {}
37
38 virtual int getId(){
39 return id;
40 }
41
42 virtual void doSomething(){
43 manipulated = true;
44 }
45
46 bool manipulated;
47private:
48 int id;
49};
50
51class countObserver : public Observer{
52public:
53 countObserver() :
54 count(0)
55 {}
56 virtual ~countObserver(){}
57
58 void update(Observable *){
59 count++;
60 }
61
62 void subjectKilled(Observable *)
63 {}
64
65 int count;
66};
67
68// set up and tear down
69void manipulateAtomsTest::setUp(){
70 World::get();
71 for(int i=0;i<ATOM_COUNT;++i){
72 atoms[i]= new AtomStub(i);
73 }
74}
75void manipulateAtomsTest::tearDown(){
76 World::destroy();
77 for(int i=0;i<ATOM_COUNT;++i){
78 delete atoms[i];
79 }
80 ActionRegistry::purgeRegistry();
81}
82
83// some helper functions
84bool hasAll(std::vector<atom*> atoms,int min, int max, std::set<int> excluded = std::set<int>()){
85 for(int i=min;i<max;++i){
86 if(!excluded.count(i)){
87 std::vector<atom*>::iterator iter;
88 bool res=false;
89 for(iter=atoms.begin();iter!=atoms.end();++iter){
90 res |= (*iter)->getId() == i;
91 }
92 if(!res) {
93 cout << "Atom " << i << " missing in returned list" << endl;
94 return false;
95 }
96 }
97 }
98 return true;
99}
100
101bool hasNoDuplicates(std::vector<atom*> atoms){
102 std::set<int> found;
103 std::vector<atom*>::iterator iter;
104 for(iter=atoms.begin();iter!=atoms.end();++iter){
105 int id = (*iter)->getId();
106 if(found.count(id))
107 return false;
108 found.insert(id);
109 }
110 return true;
111}
112
113void operation(atom* _atom){
114 AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
115 assert(atom);
116 atom->doSomething();
117}
118
119
120void manipulateAtomsTest::testManipulateSimple(){
121 ManipulateAtomsProcess *proc = World::get()->manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
122 proc->call();
123 std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
124 std::vector<atom*>::iterator iter;
125 for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
126 AtomStub *atom;
127 atom = dynamic_cast<AtomStub*>(*iter);
128 assert(atom);
129 CPPUNIT_ASSERT(atom->manipulated);
130 }
131}
132
133void manipulateAtomsTest::testManipulateExcluded(){
134 ManipulateAtomsProcess *proc = World::get()->manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
135 proc->call();
136 std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
137 std::vector<atom*>::iterator iter;
138 for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
139 AtomStub *atom;
140 atom = dynamic_cast<AtomStub*>(*iter);
141 assert(atom);
142 if(atom->getId()!=(int)ATOM_COUNT/2)
143 CPPUNIT_ASSERT(atom->manipulated);
144 else
145 CPPUNIT_ASSERT(!atom->manipulated);
146 }
147}
148
149void manipulateAtomsTest::testObserver(){
150 countObserver *obs = new countObserver();
151 World::get()->signOn(obs);
152 ManipulateAtomsProcess *proc = World::get()->manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
153 proc->call();
154
155 CPPUNIT_ASSERT_EQUAL(1,obs->count);
156 World::get()->signOff(obs);
157 delete obs;
158}
159
160/********************************************** Main routine **************************************/
161
162int main(int argc, char **argv)
163{
164 // Get the top level suite from the registry
165 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
166
167 // Adds the test to the list of test to run
168 CppUnit::TextUi::TestRunner runner;
169 runner.addTest( suite );
170
171 // Change the default outputter to a compiler error format outputter
172 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
173 std::cerr ) );
174 // Run the tests.
175 bool wasSucessful = runner.run();
176
177 // Return error code 1 if the one of test failed.
178 return wasSucessful ? 0 : 1;
179};
Note: See TracBrowser for help on using the repository browser.