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