| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2011 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * manipulateAtomsTest.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Feb 18, 2010
 | 
|---|
| 12 |  *      Author: crueger
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 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"
 | 
|---|
| 31 | #include "Actions/ActionHistory.hpp"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include "World.hpp"
 | 
|---|
| 34 | #include "Atom/atom.hpp"
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "stubs/DummyUI.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include "ManipulateAtomsUnitTest.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | using namespace MoleCuilder;
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 43 | #include "UnitTestMain.hpp"
 | 
|---|
| 44 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 45 | 
 | 
|---|
| 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(),
 | 
|---|
| 54 |   manipulated(false),
 | 
|---|
| 55 |   id(_id)
 | 
|---|
| 56 |   {}
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   virtual atomId_t getId(){
 | 
|---|
| 59 |     return id;
 | 
|---|
| 60 |   }
 | 
|---|
| 61 | 
 | 
|---|
| 62 |   virtual void doSomething(){
 | 
|---|
| 63 |     manipulated = true;
 | 
|---|
| 64 |   }
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   bool manipulated;
 | 
|---|
| 67 | private:
 | 
|---|
| 68 |   atomId_t id;
 | 
|---|
| 69 | };
 | 
|---|
| 70 | 
 | 
|---|
| 71 | class countObserver : public Observer{
 | 
|---|
| 72 | public:
 | 
|---|
| 73 |   countObserver() :
 | 
|---|
| 74 |     Observer("countObserver"),
 | 
|---|
| 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 | };
 | 
|---|
| 88 | 
 | 
|---|
| 89 | // set up and tear down
 | 
|---|
| 90 | void manipulateAtomsTest::setUp(){
 | 
|---|
| 91 |   hasDescriptor = false;
 | 
|---|
| 92 |   ActionHistory::init();
 | 
|---|
| 93 |   World::getInstance();
 | 
|---|
| 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");
 | 
|---|
| 100 |   for(int i=0;i<ATOM_COUNT;++i){
 | 
|---|
| 101 |     atoms[i]= new AtomStub(i);
 | 
|---|
| 102 |     World::getInstance().registerAtom(atoms[i]);
 | 
|---|
| 103 |   }
 | 
|---|
| 104 | }
 | 
|---|
| 105 | void manipulateAtomsTest::tearDown(){
 | 
|---|
| 106 |   World::purgeInstance();
 | 
|---|
| 107 |   ActionRegistry::purgeInstance();
 | 
|---|
| 108 |   ActionHistory::purgeInstance();
 | 
|---|
| 109 |   {
 | 
|---|
| 110 |     UIFactory::purgeInstance();
 | 
|---|
| 111 |     hasDescriptor = false;
 | 
|---|
| 112 |   }
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | static void operation(atom* _atom){
 | 
|---|
| 116 |   AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
 | 
|---|
| 117 |   CPPUNIT_ASSERT(atom);
 | 
|---|
| 118 |   atom->doSomething();
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | 
 | 
|---|
| 122 | void manipulateAtomsTest::testManipulateSimple(){
 | 
|---|
| 123 |   ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
 | 
|---|
| 124 |   proc->call();
 | 
|---|
| 125 |   std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
 | 
|---|
| 126 |   std::vector<atom*>::iterator iter;
 | 
|---|
| 127 |   for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
 | 
|---|
| 128 |     AtomStub *atom;
 | 
|---|
| 129 |     atom = dynamic_cast<AtomStub*>(*iter);
 | 
|---|
| 130 |     CPPUNIT_ASSERT(atom);
 | 
|---|
| 131 |     CPPUNIT_ASSERT(atom->manipulated);
 | 
|---|
| 132 |   }
 | 
|---|
| 133 | }
 | 
|---|
| 134 | 
 | 
|---|
| 135 | void manipulateAtomsTest::testManipulateExcluded(){
 | 
|---|
| 136 | 
 | 
|---|
| 137 |   ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
 | 
|---|
| 138 |   proc->call();
 | 
|---|
| 139 |   std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
 | 
|---|
| 140 |   std::vector<atom*>::iterator iter;
 | 
|---|
| 141 |   for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
 | 
|---|
| 142 |     AtomStub *atom;
 | 
|---|
| 143 |     atom = dynamic_cast<AtomStub*>(*iter);
 | 
|---|
| 144 |     CPPUNIT_ASSERT(atom);
 | 
|---|
| 145 |     if(atom->getId()!=(int)ATOM_COUNT/2)
 | 
|---|
| 146 |       CPPUNIT_ASSERT(atom->manipulated);
 | 
|---|
| 147 |     else
 | 
|---|
| 148 |       CPPUNIT_ASSERT(!atom->manipulated);
 | 
|---|
| 149 |   }
 | 
|---|
| 150 | }
 | 
|---|
| 151 | 
 | 
|---|
| 152 | void manipulateAtomsTest::testObserver(){
 | 
|---|
| 153 |   countObserver *obs = new countObserver();
 | 
|---|
| 154 |   World::getInstance().signOn(obs);
 | 
|---|
| 155 |   ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
 | 
|---|
| 156 |   proc->call();
 | 
|---|
| 157 | 
 | 
|---|
| 158 |   CPPUNIT_ASSERT_EQUAL(1,obs->count);
 | 
|---|
| 159 |   World::getInstance().signOff(obs);
 | 
|---|
| 160 |   delete obs;
 | 
|---|
| 161 | }
 | 
|---|