| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [d103d3] | 4 |  * Copyright (C)  2010-2011 University of Bonn. All rights reserved.
 | 
|---|
| [bcf653] | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [b2cfdb] | 8 | /*
 | 
|---|
 | 9 |  * manipulateAtomsTest.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Feb 18, 2010
 | 
|---|
 | 12 |  *      Author: crueger
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [b2cfdb] | 20 | 
 | 
|---|
 | 21 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 22 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 23 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
 | 24 | #include <iostream>
 | 
|---|
 | 25 | #include <boost/bind.hpp>
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
 | 28 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
 | 29 | #include "Actions/ManipulateAtomsProcess.hpp"
 | 
|---|
 | 30 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| [975a11] | 31 | #include "Actions/ActionHistory.hpp"
 | 
|---|
| [b2cfdb] | 32 | 
 | 
|---|
 | 33 | #include "World.hpp"
 | 
|---|
| [6f0841] | 34 | #include "Atom/atom.hpp"
 | 
|---|
| [b2cfdb] | 35 | 
 | 
|---|
| [fdcd1b] | 36 | #include "stubs/DummyUI.hpp"
 | 
|---|
| [efd61b] | 37 | 
 | 
|---|
 | 38 | #include "ManipulateAtomsUnitTest.hpp"
 | 
|---|
| [112f90] | 39 | 
 | 
|---|
| [ce7fdc] | 40 | using namespace MoleCuilder;
 | 
|---|
 | 41 | 
 | 
|---|
| [7ca806] | 42 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 43 | #include "UnitTestMain.hpp"
 | 
|---|
 | 44 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
 | 45 | 
 | 
|---|
| [b2cfdb] | 46 | // Registers the fixture into the 'registry'
 | 
|---|
 | 47 | CPPUNIT_TEST_SUITE_REGISTRATION( manipulateAtomsTest );
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | // some stubs
 | 
|---|
 | 50 | class AtomStub : public atom {
 | 
|---|
 | 51 | public:
 | 
|---|
 | 52 |   AtomStub(int _id) :
 | 
|---|
 | 53 |   atom(),
 | 
|---|
| [23b547] | 54 |   manipulated(false),
 | 
|---|
 | 55 |   id(_id)
 | 
|---|
| [b2cfdb] | 56 |   {}
 | 
|---|
 | 57 | 
 | 
|---|
| [57adc7] | 58 |   virtual atomId_t getId(){
 | 
|---|
| [b2cfdb] | 59 |     return id;
 | 
|---|
 | 60 |   }
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |   virtual void doSomething(){
 | 
|---|
 | 63 |     manipulated = true;
 | 
|---|
 | 64 |   }
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |   bool manipulated;
 | 
|---|
 | 67 | private:
 | 
|---|
| [57adc7] | 68 |   atomId_t id;
 | 
|---|
| [b2cfdb] | 69 | };
 | 
|---|
 | 70 | 
 | 
|---|
| [afb47f] | 71 | class countObserver : public Observer{
 | 
|---|
 | 72 | public:
 | 
|---|
 | 73 |   countObserver() :
 | 
|---|
| [cd5047] | 74 |     Observer("countObserver"),
 | 
|---|
| [afb47f] | 75 |     count(0)
 | 
|---|
 | 76 |     {}
 | 
|---|
 | 77 |   virtual ~countObserver(){}
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 |   void update(Observable *){
 | 
|---|
 | 80 |     count++;
 | 
|---|
 | 81 |   }
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 |   void subjectKilled(Observable *)
 | 
|---|
 | 84 |   {}
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 |   int count;
 | 
|---|
 | 87 | };
 | 
|---|
| [b2cfdb] | 88 | 
 | 
|---|
 | 89 | // set up and tear down
 | 
|---|
 | 90 | void manipulateAtomsTest::setUp(){
 | 
|---|
| [694d84] | 91 |   hasDescriptor = false;
 | 
|---|
| [975a11] | 92 |   ActionHistory::init();
 | 
|---|
| [23b547] | 93 |   World::getInstance();
 | 
|---|
| [112f90] | 94 |   // TODO: find a way to really reset the factory to a clean state in tear-down
 | 
|---|
 | 95 |   if(!hasDescriptor){
 | 
|---|
 | 96 |     UIFactory::registerFactory(new DummyUIFactory::description());
 | 
|---|
 | 97 |     hasDescriptor = true;
 | 
|---|
 | 98 |   }
 | 
|---|
 | 99 |   UIFactory::makeUserInterface("Dummy");
 | 
|---|
| [b2cfdb] | 100 |   for(int i=0;i<ATOM_COUNT;++i){
 | 
|---|
 | 101 |     atoms[i]= new AtomStub(i);
 | 
|---|
| [23b547] | 102 |     World::getInstance().registerAtom(atoms[i]);
 | 
|---|
| [b2cfdb] | 103 |   }
 | 
|---|
 | 104 | }
 | 
|---|
 | 105 | void manipulateAtomsTest::tearDown(){
 | 
|---|
| [23b547] | 106 |   World::purgeInstance();
 | 
|---|
| [e73a8a2] | 107 |   ActionRegistry::purgeInstance();
 | 
|---|
| [975a11] | 108 |   ActionHistory::purgeInstance();
 | 
|---|
| [694d84] | 109 |   {
 | 
|---|
 | 110 |     UIFactory::purgeInstance();
 | 
|---|
 | 111 |     hasDescriptor = false;
 | 
|---|
 | 112 |   }
 | 
|---|
| [b2cfdb] | 113 | }
 | 
|---|
 | 114 | 
 | 
|---|
| [a1510d] | 115 | static void operation(atom* _atom){
 | 
|---|
| [b2cfdb] | 116 |   AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
 | 
|---|
| [6d574a] | 117 |   CPPUNIT_ASSERT(atom);
 | 
|---|
| [b2cfdb] | 118 |   atom->doSomething();
 | 
|---|
 | 119 | }
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 | void manipulateAtomsTest::testManipulateSimple(){
 | 
|---|
| [23b547] | 123 |   ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
 | 
|---|
| [b2cfdb] | 124 |   proc->call();
 | 
|---|
| [23b547] | 125 |   std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
 | 
|---|
| [b2cfdb] | 126 |   std::vector<atom*>::iterator iter;
 | 
|---|
 | 127 |   for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
 | 
|---|
 | 128 |     AtomStub *atom;
 | 
|---|
 | 129 |     atom = dynamic_cast<AtomStub*>(*iter);
 | 
|---|
| [6d574a] | 130 |     CPPUNIT_ASSERT(atom);
 | 
|---|
| [b2cfdb] | 131 |     CPPUNIT_ASSERT(atom->manipulated);
 | 
|---|
 | 132 |   }
 | 
|---|
 | 133 | }
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 | void manipulateAtomsTest::testManipulateExcluded(){
 | 
|---|
| [229e3c] | 136 | 
 | 
|---|
| [23b547] | 137 |   ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
 | 
|---|
| [b2cfdb] | 138 |   proc->call();
 | 
|---|
| [23b547] | 139 |   std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
 | 
|---|
| [b2cfdb] | 140 |   std::vector<atom*>::iterator iter;
 | 
|---|
 | 141 |   for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
 | 
|---|
 | 142 |     AtomStub *atom;
 | 
|---|
 | 143 |     atom = dynamic_cast<AtomStub*>(*iter);
 | 
|---|
| [6d574a] | 144 |     CPPUNIT_ASSERT(atom);
 | 
|---|
| [b2cfdb] | 145 |     if(atom->getId()!=(int)ATOM_COUNT/2)
 | 
|---|
 | 146 |       CPPUNIT_ASSERT(atom->manipulated);
 | 
|---|
 | 147 |     else
 | 
|---|
 | 148 |       CPPUNIT_ASSERT(!atom->manipulated);
 | 
|---|
 | 149 |   }
 | 
|---|
 | 150 | }
 | 
|---|
 | 151 | 
 | 
|---|
| [afb47f] | 152 | void manipulateAtomsTest::testObserver(){
 | 
|---|
 | 153 |   countObserver *obs = new countObserver();
 | 
|---|
| [23b547] | 154 |   World::getInstance().signOn(obs);
 | 
|---|
| [6e97e5] | 155 |   ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
 | 
|---|
| [afb47f] | 156 |   proc->call();
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 |   CPPUNIT_ASSERT_EQUAL(1,obs->count);
 | 
|---|
| [23b547] | 159 |   World::getInstance().signOff(obs);
 | 
|---|
| [afb47f] | 160 |   delete obs;
 | 
|---|
 | 161 | }
 | 
|---|