1 | /*
|
---|
2 | * manipulateAtomsTest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Feb 18, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "manipulateAtomsTest.hpp"
|
---|
9 |
|
---|
10 | #include <cppunit/CompilerOutputter.h>
|
---|
11 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
12 | #include <cppunit/ui/text/TestRunner.h>
|
---|
13 | #include <iostream>
|
---|
14 | #include <boost/bind.hpp>
|
---|
15 |
|
---|
16 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
17 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
18 | #include "Actions/ManipulateAtomsProcess.hpp"
|
---|
19 | #include "Actions/ActionRegistry.hpp"
|
---|
20 |
|
---|
21 | #include "World.hpp"
|
---|
22 | #include "atom.hpp"
|
---|
23 |
|
---|
24 | // Registers the fixture into the 'registry'
|
---|
25 | CPPUNIT_TEST_SUITE_REGISTRATION( manipulateAtomsTest );
|
---|
26 |
|
---|
27 | // some stubs
|
---|
28 | class AtomStub : public atom {
|
---|
29 | public:
|
---|
30 | AtomStub(int _id) :
|
---|
31 | atom(),
|
---|
32 | manipulated(false),
|
---|
33 | id(_id)
|
---|
34 | {}
|
---|
35 |
|
---|
36 | virtual atomId_t getId(){
|
---|
37 | return id;
|
---|
38 | }
|
---|
39 |
|
---|
40 | virtual void doSomething(){
|
---|
41 | manipulated = true;
|
---|
42 | }
|
---|
43 |
|
---|
44 | bool manipulated;
|
---|
45 | private:
|
---|
46 | atomId_t id;
|
---|
47 | };
|
---|
48 |
|
---|
49 | class countObserver : public Observer{
|
---|
50 | public:
|
---|
51 | countObserver() :
|
---|
52 | count(0)
|
---|
53 | {}
|
---|
54 | virtual ~countObserver(){}
|
---|
55 |
|
---|
56 | void update(Observable *){
|
---|
57 | count++;
|
---|
58 | }
|
---|
59 |
|
---|
60 | void subjectKilled(Observable *)
|
---|
61 | {}
|
---|
62 |
|
---|
63 | int count;
|
---|
64 | };
|
---|
65 |
|
---|
66 | // set up and tear down
|
---|
67 | void manipulateAtomsTest::setUp(){
|
---|
68 | World::getInstance();
|
---|
69 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
70 | atoms[i]= new AtomStub(i);
|
---|
71 | World::getInstance().registerAtom(atoms[i]);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | void manipulateAtomsTest::tearDown(){
|
---|
75 | World::purgeInstance();
|
---|
76 | ActionRegistry::purgeRegistry();
|
---|
77 | }
|
---|
78 |
|
---|
79 | static void operation(atom* _atom){
|
---|
80 | AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
|
---|
81 | assert(atom);
|
---|
82 | atom->doSomething();
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | void manipulateAtomsTest::testManipulateSimple(){
|
---|
87 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
|
---|
88 | proc->call();
|
---|
89 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
|
---|
90 | std::vector<atom*>::iterator iter;
|
---|
91 | for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
|
---|
92 | AtomStub *atom;
|
---|
93 | atom = dynamic_cast<AtomStub*>(*iter);
|
---|
94 | assert(atom);
|
---|
95 | CPPUNIT_ASSERT(atom->manipulated);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | void manipulateAtomsTest::testManipulateExcluded(){
|
---|
100 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
|
---|
101 | proc->call();
|
---|
102 | std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
|
---|
103 | std::vector<atom*>::iterator iter;
|
---|
104 | for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
|
---|
105 | AtomStub *atom;
|
---|
106 | atom = dynamic_cast<AtomStub*>(*iter);
|
---|
107 | assert(atom);
|
---|
108 | if(atom->getId()!=(int)ATOM_COUNT/2)
|
---|
109 | CPPUNIT_ASSERT(atom->manipulated);
|
---|
110 | else
|
---|
111 | CPPUNIT_ASSERT(!atom->manipulated);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | void manipulateAtomsTest::testObserver(){
|
---|
116 | countObserver *obs = new countObserver();
|
---|
117 | World::getInstance().signOn(obs);
|
---|
118 | ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
|
---|
119 | proc->call();
|
---|
120 |
|
---|
121 | CPPUNIT_ASSERT_EQUAL(1,obs->count);
|
---|
122 | World::getInstance().signOff(obs);
|
---|
123 | delete obs;
|
---|
124 | }
|
---|