[b54ac8] | 1 | /*
|
---|
| 2 | * atomsCalculationTest.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Feb 19, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include "atomsCalculationTest.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/AtomsCalculation.hpp"
|
---|
| 19 | #include "Actions/AtomsCalculation_impl.hpp"
|
---|
| 20 | #include "Actions/ActionRegistry.hpp"
|
---|
| 21 |
|
---|
| 22 | #include "World.hpp"
|
---|
| 23 | #include "World_calculations.hpp"
|
---|
| 24 | #include "atom.hpp"
|
---|
| 25 |
|
---|
| 26 | // Registers the fixture into the 'registry'
|
---|
| 27 | CPPUNIT_TEST_SUITE_REGISTRATION( atomsCalculationTest );
|
---|
| 28 |
|
---|
| 29 | // some stubs
|
---|
| 30 | class AtomStub : public atom {
|
---|
| 31 | public:
|
---|
[57adc7] | 32 | AtomStub(atomId_t _id) :
|
---|
[b54ac8] | 33 | atom(),
|
---|
| 34 | id(_id),
|
---|
| 35 | manipulated(false)
|
---|
| 36 | {}
|
---|
| 37 |
|
---|
[57adc7] | 38 | virtual atomId_t getId(){
|
---|
[b54ac8] | 39 | return id;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | virtual void doSomething(){
|
---|
| 43 | manipulated = true;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | bool manipulated;
|
---|
| 47 | private:
|
---|
[57adc7] | 48 | atomId_t id;
|
---|
[b54ac8] | 49 | };
|
---|
| 50 |
|
---|
| 51 | // set up and tear down
|
---|
| 52 | void atomsCalculationTest::setUp(){
|
---|
| 53 | World::get();
|
---|
| 54 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
| 55 | atoms[i]= new AtomStub(i);
|
---|
[46d958] | 56 | World::get()->registerAtom(atoms[i]);
|
---|
[b54ac8] | 57 | }
|
---|
| 58 | }
|
---|
| 59 | void atomsCalculationTest::tearDown(){
|
---|
| 60 | World::destroy();
|
---|
| 61 | ActionRegistry::purgeRegistry();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | // some helper functions
|
---|
[a1510d] | 65 | static bool hasAll(std::vector<int> ids,int min, int max, std::set<int> excluded = std::set<int>()){
|
---|
[b54ac8] | 66 | for(int i=min;i<max;++i){
|
---|
| 67 | if(!excluded.count(i)){
|
---|
| 68 | std::vector<int>::iterator iter;
|
---|
| 69 | bool res=false;
|
---|
| 70 | for(iter=ids.begin();iter!=ids.end();++iter){
|
---|
| 71 | res |= (*iter) == i;
|
---|
| 72 | }
|
---|
| 73 | if(!res) {
|
---|
| 74 | cout << "Atom " << i << " missing in returned list" << endl;
|
---|
| 75 | return false;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 | return true;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[a1510d] | 82 | static bool hasNoDuplicates(std::vector<int> ids){
|
---|
[b54ac8] | 83 | std::set<int> found;
|
---|
| 84 | std::vector<int>::iterator iter;
|
---|
| 85 | for(iter=ids.begin();iter!=ids.end();++iter){
|
---|
| 86 | int id = (*iter);
|
---|
| 87 | if(found.count(id))
|
---|
| 88 | return false;
|
---|
| 89 | found.insert(id);
|
---|
| 90 | }
|
---|
| 91 | return true;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[a1510d] | 94 | static void operation(atom* _atom){
|
---|
[b54ac8] | 95 | AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
|
---|
| 96 | assert(atom);
|
---|
| 97 | atom->doSomething();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | void atomsCalculationTest::testCalculateSimple(){
|
---|
| 102 | AtomsCalculation<int> *calc = World::get()->calcOnAtoms<int>(boost::bind(&atom::getId,_1),"FOO",AllAtoms());
|
---|
| 103 | std::vector<int> allIds = (*calc)();
|
---|
| 104 | CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT));
|
---|
| 105 | CPPUNIT_ASSERT(hasNoDuplicates(allIds));
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void atomsCalculationTest::testCalculateExcluded(){
|
---|
| 109 | int excluded = ATOM_COUNT/2;
|
---|
| 110 | AtomsCalculation<int> *calc = World::get()->calcOnAtoms<int>(boost::bind(&atom::getId,_1),"FOO",AllAtoms() && !AtomById(excluded));
|
---|
| 111 | std::vector<int> allIds = (*calc)();
|
---|
| 112 | std::set<int> excluded_set;
|
---|
| 113 | excluded_set.insert(excluded);
|
---|
| 114 | CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT,excluded_set));
|
---|
| 115 | CPPUNIT_ASSERT(hasNoDuplicates(allIds));
|
---|
| 116 | CPPUNIT_ASSERT_EQUAL((size_t)(ATOM_COUNT-1),allIds.size());
|
---|
| 117 | }
|
---|