| 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 | * 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 | #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" | 
|---|
| 32 | #include "Actions/ActionHistory.hpp" | 
|---|
| 33 |  | 
|---|
| 34 | #include "World.hpp" | 
|---|
| 35 | #include "atom.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include "DummyUI.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #ifdef HAVE_TESTRUNNER | 
|---|
| 40 | #include "UnitTestMain.hpp" | 
|---|
| 41 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 42 |  | 
|---|
| 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(), | 
|---|
| 51 | manipulated(false), | 
|---|
| 52 | id(_id) | 
|---|
| 53 | {} | 
|---|
| 54 |  | 
|---|
| 55 | virtual atomId_t getId(){ | 
|---|
| 56 | return id; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | virtual void doSomething(){ | 
|---|
| 60 | manipulated = true; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | bool manipulated; | 
|---|
| 64 | private: | 
|---|
| 65 | atomId_t id; | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 | class countObserver : public Observer{ | 
|---|
| 69 | public: | 
|---|
| 70 | countObserver() : | 
|---|
| 71 | Observer("countObserver"), | 
|---|
| 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 | }; | 
|---|
| 85 |  | 
|---|
| 86 | // set up and tear down | 
|---|
| 87 | void manipulateAtomsTest::setUp(){ | 
|---|
| 88 | hasDescriptor = false; | 
|---|
| 89 | ActionHistory::init(); | 
|---|
| 90 | World::getInstance(); | 
|---|
| 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"); | 
|---|
| 97 | for(int i=0;i<ATOM_COUNT;++i){ | 
|---|
| 98 | atoms[i]= new AtomStub(i); | 
|---|
| 99 | World::getInstance().registerAtom(atoms[i]); | 
|---|
| 100 | } | 
|---|
| 101 | } | 
|---|
| 102 | void manipulateAtomsTest::tearDown(){ | 
|---|
| 103 | World::purgeInstance(); | 
|---|
| 104 | ActionRegistry::purgeInstance(); | 
|---|
| 105 | ActionHistory::purgeInstance(); | 
|---|
| 106 | { | 
|---|
| 107 | UIFactory::purgeInstance(); | 
|---|
| 108 | hasDescriptor = false; | 
|---|
| 109 | } | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | static void operation(atom* _atom){ | 
|---|
| 113 | AtomStub *atom = dynamic_cast<AtomStub*>(_atom); | 
|---|
| 114 | CPPUNIT_ASSERT(atom); | 
|---|
| 115 | atom->doSomething(); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 |  | 
|---|
| 119 | void manipulateAtomsTest::testManipulateSimple(){ | 
|---|
| 120 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms()); | 
|---|
| 121 | proc->call(); | 
|---|
| 122 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms()); | 
|---|
| 123 | std::vector<atom*>::iterator iter; | 
|---|
| 124 | for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){ | 
|---|
| 125 | AtomStub *atom; | 
|---|
| 126 | atom = dynamic_cast<AtomStub*>(*iter); | 
|---|
| 127 | CPPUNIT_ASSERT(atom); | 
|---|
| 128 | CPPUNIT_ASSERT(atom->manipulated); | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | void manipulateAtomsTest::testManipulateExcluded(){ | 
|---|
| 133 |  | 
|---|
| 134 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2)); | 
|---|
| 135 | proc->call(); | 
|---|
| 136 | std::vector<atom*> allAtoms = World::getInstance().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 | CPPUNIT_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 |  | 
|---|
| 149 | void manipulateAtomsTest::testObserver(){ | 
|---|
| 150 | countObserver *obs = new countObserver(); | 
|---|
| 151 | World::getInstance().signOn(obs); | 
|---|
| 152 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms()); | 
|---|
| 153 | proc->call(); | 
|---|
| 154 |  | 
|---|
| 155 | CPPUNIT_ASSERT_EQUAL(1,obs->count); | 
|---|
| 156 | World::getInstance().signOff(obs); | 
|---|
| 157 | delete obs; | 
|---|
| 158 | } | 
|---|