| 1 | /*
|
|---|
| 2 | * manipulateAtomsTest.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Feb 18, 2010
|
|---|
| 5 | * Author: crueger
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include "manipulateAtomsTest.hpp"
|
|---|
| 9 |
|
|---|
| 10 | #include "DescriptorUnittest.hpp"
|
|---|
| 11 |
|
|---|
| 12 | #include <cppunit/CompilerOutputter.h>
|
|---|
| 13 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
|---|
| 14 | #include <cppunit/ui/text/TestRunner.h>
|
|---|
| 15 | #include <iostream>
|
|---|
| 16 | #include <boost/bind.hpp>
|
|---|
| 17 |
|
|---|
| 18 | #include "Descriptors/AtomDescriptor.hpp"
|
|---|
| 19 | #include "Descriptors/AtomIdDescriptor.hpp"
|
|---|
| 20 | #include "Actions/ManipulateAtomsProcess.hpp"
|
|---|
| 21 | #include "Actions/ActionRegistry.hpp"
|
|---|
| 22 |
|
|---|
| 23 | #include "World.hpp"
|
|---|
| 24 | #include "atom.hpp"
|
|---|
| 25 |
|
|---|
| 26 | // Registers the fixture into the 'registry'
|
|---|
| 27 | CPPUNIT_TEST_SUITE_REGISTRATION( manipulateAtomsTest );
|
|---|
| 28 |
|
|---|
| 29 | // some stubs
|
|---|
| 30 | class AtomStub : public atom {
|
|---|
| 31 | public:
|
|---|
| 32 | AtomStub(int _id) :
|
|---|
| 33 | atom(),
|
|---|
| 34 | id(_id),
|
|---|
| 35 | manipulated(false)
|
|---|
| 36 | {}
|
|---|
| 37 |
|
|---|
| 38 | virtual int getId(){
|
|---|
| 39 | return id;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | virtual void doSomething(){
|
|---|
| 43 | manipulated = true;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | bool manipulated;
|
|---|
| 47 | private:
|
|---|
| 48 | int id;
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | // set up and tear down
|
|---|
| 53 | void manipulateAtomsTest::setUp(){
|
|---|
| 54 | World::get();
|
|---|
| 55 | for(int i=0;i<ATOM_COUNT;++i){
|
|---|
| 56 | atoms[i]= new AtomStub(i);
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | void manipulateAtomsTest::tearDown(){
|
|---|
| 60 | World::destroy();
|
|---|
| 61 | for(int i=0;i<ATOM_COUNT;++i){
|
|---|
| 62 | delete atoms[i];
|
|---|
| 63 | }
|
|---|
| 64 | ActionRegistry::purgeRegistry();
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | // some helper functions
|
|---|
| 68 | bool hasAll(std::vector<atom*> atoms,int min, int max, std::set<int> excluded = std::set<int>()){
|
|---|
| 69 | for(int i=min;i<max;++i){
|
|---|
| 70 | if(!excluded.count(i)){
|
|---|
| 71 | std::vector<atom*>::iterator iter;
|
|---|
| 72 | bool res=false;
|
|---|
| 73 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
|---|
| 74 | res |= (*iter)->getId() == i;
|
|---|
| 75 | }
|
|---|
| 76 | if(!res) {
|
|---|
| 77 | cout << "Atom " << i << " missing in returned list" << endl;
|
|---|
| 78 | return false;
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | }
|
|---|
| 82 | return true;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | bool hasNoDuplicates(std::vector<atom*> atoms){
|
|---|
| 86 | std::set<int> found;
|
|---|
| 87 | std::vector<atom*>::iterator iter;
|
|---|
| 88 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
|---|
| 89 | int id = (*iter)->getId();
|
|---|
| 90 | if(found.count(id))
|
|---|
| 91 | return false;
|
|---|
| 92 | found.insert(id);
|
|---|
| 93 | }
|
|---|
| 94 | return true;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | void operation(atom* _atom){
|
|---|
| 98 | AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
|
|---|
| 99 | assert(atom);
|
|---|
| 100 | atom->doSomething();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | void manipulateAtomsTest::testManipulateSimple(){
|
|---|
| 105 | ManipulateAtomsProcess *proc = World::get()->manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
|
|---|
| 106 | proc->call();
|
|---|
| 107 | std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
|
|---|
| 108 | std::vector<atom*>::iterator iter;
|
|---|
| 109 | for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
|
|---|
| 110 | AtomStub *atom;
|
|---|
| 111 | atom = dynamic_cast<AtomStub*>(*iter);
|
|---|
| 112 | assert(atom);
|
|---|
| 113 | CPPUNIT_ASSERT(atom->manipulated);
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | void manipulateAtomsTest::testManipulateExcluded(){
|
|---|
| 118 | ManipulateAtomsProcess *proc = World::get()->manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
|
|---|
| 119 | proc->call();
|
|---|
| 120 | std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
|
|---|
| 121 | std::vector<atom*>::iterator iter;
|
|---|
| 122 | for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
|
|---|
| 123 | AtomStub *atom;
|
|---|
| 124 | atom = dynamic_cast<AtomStub*>(*iter);
|
|---|
| 125 | assert(atom);
|
|---|
| 126 | if(atom->getId()!=(int)ATOM_COUNT/2)
|
|---|
| 127 | CPPUNIT_ASSERT(atom->manipulated);
|
|---|
| 128 | else
|
|---|
| 129 | CPPUNIT_ASSERT(!atom->manipulated);
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /********************************************** Main routine **************************************/
|
|---|
| 134 |
|
|---|
| 135 | int main(int argc, char **argv)
|
|---|
| 136 | {
|
|---|
| 137 | // Get the top level suite from the registry
|
|---|
| 138 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
|---|
| 139 |
|
|---|
| 140 | // Adds the test to the list of test to run
|
|---|
| 141 | CppUnit::TextUi::TestRunner runner;
|
|---|
| 142 | runner.addTest( suite );
|
|---|
| 143 |
|
|---|
| 144 | // Change the default outputter to a compiler error format outputter
|
|---|
| 145 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
|---|
| 146 | std::cerr ) );
|
|---|
| 147 | // Run the tests.
|
|---|
| 148 | bool wasSucessful = runner.run();
|
|---|
| 149 |
|
|---|
| 150 | // Return error code 1 if the one of test failed.
|
|---|
| 151 | return wasSucessful ? 0 : 1;
|
|---|
| 152 | };
|
|---|