| [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(){ | 
|---|
| [23b547] | 53 | World::getInstance(); | 
|---|
| [b54ac8] | 54 | for(int i=0;i<ATOM_COUNT;++i){ | 
|---|
|  | 55 | atoms[i]= new AtomStub(i); | 
|---|
| [23b547] | 56 | World::getInstance().registerAtom(atoms[i]); | 
|---|
| [b54ac8] | 57 | } | 
|---|
|  | 58 | } | 
|---|
|  | 59 | void atomsCalculationTest::tearDown(){ | 
|---|
| [23b547] | 60 | World::purgeInstance(); | 
|---|
| [b54ac8] | 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 |  | 
|---|
|  | 94 | void atomsCalculationTest::testCalculateSimple(){ | 
|---|
| [23b547] | 95 | AtomsCalculation<int> *calc = World::getInstance().calcOnAtoms<int>(boost::bind(&atom::getId,_1),"FOO",AllAtoms()); | 
|---|
| [b54ac8] | 96 | std::vector<int> allIds = (*calc)(); | 
|---|
|  | 97 | CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT)); | 
|---|
|  | 98 | CPPUNIT_ASSERT(hasNoDuplicates(allIds)); | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
|  | 101 | void atomsCalculationTest::testCalculateExcluded(){ | 
|---|
|  | 102 | int excluded = ATOM_COUNT/2; | 
|---|
| [23b547] | 103 | AtomsCalculation<int> *calc = World::getInstance().calcOnAtoms<int>(boost::bind(&atom::getId,_1),"FOO",AllAtoms() && !AtomById(excluded)); | 
|---|
| [b54ac8] | 104 | std::vector<int> allIds = (*calc)(); | 
|---|
|  | 105 | std::set<int> excluded_set; | 
|---|
|  | 106 | excluded_set.insert(excluded); | 
|---|
|  | 107 | CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT,excluded_set)); | 
|---|
|  | 108 | CPPUNIT_ASSERT(hasNoDuplicates(allIds)); | 
|---|
|  | 109 | CPPUNIT_ASSERT_EQUAL((size_t)(ATOM_COUNT-1),allIds.size()); | 
|---|
|  | 110 | } | 
|---|