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