source: molecuilder/src/unittests/manipulateAtomsTest.cpp@ 4fc41a

Last change on this file since 4fc41a was 4c60ef, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added generic singleton Pattern that can be inherited to any class making that class a singleton.

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[cbc27f]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'
25CPPUNIT_TEST_SUITE_REGISTRATION( manipulateAtomsTest );
26
27// some stubs
28class AtomStub : public atom {
29public:
30 AtomStub(int _id) :
31 atom(),
[4c60ef]32 manipulated(false),
33 id(_id)
[cbc27f]34 {}
35
[f058ef]36 virtual atomId_t getId(){
[cbc27f]37 return id;
38 }
39
40 virtual void doSomething(){
41 manipulated = true;
42 }
43
44 bool manipulated;
45private:
[f058ef]46 atomId_t id;
[cbc27f]47};
48
[9ef76a]49class countObserver : public Observer{
50public:
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};
[cbc27f]65
66// set up and tear down
67void manipulateAtomsTest::setUp(){
[4c60ef]68 World::getInstance();
[cbc27f]69 for(int i=0;i<ATOM_COUNT;++i){
70 atoms[i]= new AtomStub(i);
[4c60ef]71 World::getInstance().registerAtom(atoms[i]);
[cbc27f]72 }
73}
74void manipulateAtomsTest::tearDown(){
[4c60ef]75 World::purgeInstance();
[cbc27f]76 ActionRegistry::purgeRegistry();
77}
78
[dc5413]79static void operation(atom* _atom){
[cbc27f]80 AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
81 assert(atom);
82 atom->doSomething();
83}
84
85
86void manipulateAtomsTest::testManipulateSimple(){
[4c60ef]87 ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
[cbc27f]88 proc->call();
[4c60ef]89 std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
[cbc27f]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
99void manipulateAtomsTest::testManipulateExcluded(){
[4c60ef]100 ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
[cbc27f]101 proc->call();
[4c60ef]102 std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
[cbc27f]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
[9ef76a]115void manipulateAtomsTest::testObserver(){
116 countObserver *obs = new countObserver();
[4c60ef]117 World::getInstance().signOn(obs);
118 ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
[9ef76a]119 proc->call();
120
121 CPPUNIT_ASSERT_EQUAL(1,obs->count);
[4c60ef]122 World::getInstance().signOff(obs);
[9ef76a]123 delete obs;
124}
Note: See TracBrowser for help on using the repository browser.