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