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