[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(){
|
---|
[112f90] | 88 | static bool 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();
|
---|
[112f90] | 106 | UIFactory::purgeInstance();
|
---|
[b2cfdb] | 107 | }
|
---|
| 108 |
|
---|
[a1510d] | 109 | static void operation(atom* _atom){
|
---|
[b2cfdb] | 110 | AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
|
---|
[6d574a] | 111 | CPPUNIT_ASSERT(atom);
|
---|
[b2cfdb] | 112 | atom->doSomething();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 |
|
---|
| 116 | void manipulateAtomsTest::testManipulateSimple(){
|
---|
[23b547] | 117 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
|
---|
[b2cfdb] | 118 | proc->call();
|
---|
[23b547] | 119 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
|
---|
[b2cfdb] | 120 | std::vector<atom*>::iterator iter;
|
---|
| 121 | for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
|
---|
| 122 | AtomStub *atom;
|
---|
| 123 | atom = dynamic_cast<AtomStub*>(*iter);
|
---|
[6d574a] | 124 | CPPUNIT_ASSERT(atom);
|
---|
[b2cfdb] | 125 | CPPUNIT_ASSERT(atom->manipulated);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | void manipulateAtomsTest::testManipulateExcluded(){
|
---|
[229e3c] | 130 |
|
---|
[23b547] | 131 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
|
---|
[b2cfdb] | 132 | proc->call();
|
---|
[23b547] | 133 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
|
---|
[b2cfdb] | 134 | std::vector<atom*>::iterator iter;
|
---|
| 135 | for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
|
---|
| 136 | AtomStub *atom;
|
---|
| 137 | atom = dynamic_cast<AtomStub*>(*iter);
|
---|
[6d574a] | 138 | CPPUNIT_ASSERT(atom);
|
---|
[b2cfdb] | 139 | if(atom->getId()!=(int)ATOM_COUNT/2)
|
---|
| 140 | CPPUNIT_ASSERT(atom->manipulated);
|
---|
| 141 | else
|
---|
| 142 | CPPUNIT_ASSERT(!atom->manipulated);
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[afb47f] | 146 | void manipulateAtomsTest::testObserver(){
|
---|
| 147 | countObserver *obs = new countObserver();
|
---|
[23b547] | 148 | World::getInstance().signOn(obs);
|
---|
[6e97e5] | 149 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
|
---|
[afb47f] | 150 | proc->call();
|
---|
| 151 |
|
---|
| 152 | CPPUNIT_ASSERT_EQUAL(1,obs->count);
|
---|
[23b547] | 153 | World::getInstance().signOff(obs);
|
---|
[afb47f] | 154 | delete obs;
|
---|
| 155 | }
|
---|