[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:
|
---|
| 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 | // 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
|
---|
| 65 | bool hasAll(std::vector<int> ids,int min, int max, std::set<int> excluded = std::set<int>()){
|
---|
| 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 |
|
---|
| 82 | bool hasNoDuplicates(std::vector<int> ids){
|
---|
| 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 operation(atom* _atom){
|
---|
| 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 | }
|
---|
| 118 |
|
---|
| 119 | /********************************************** Main routine **************************************/
|
---|
| 120 |
|
---|
| 121 | int main(int argc, char **argv)
|
---|
| 122 | {
|
---|
| 123 | // Get the top level suite from the registry
|
---|
| 124 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
| 125 |
|
---|
| 126 | // Adds the test to the list of test to run
|
---|
| 127 | CppUnit::TextUi::TestRunner runner;
|
---|
| 128 | runner.addTest( suite );
|
---|
| 129 |
|
---|
| 130 | // Change the default outputter to a compiler error format outputter
|
---|
| 131 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
| 132 | std::cerr ) );
|
---|
| 133 | // Run the tests.
|
---|
| 134 | bool wasSucessful = runner.run();
|
---|
| 135 |
|
---|
| 136 | // Return error code 1 if the one of test failed.
|
---|
| 137 | return wasSucessful ? 0 : 1;
|
---|
| 138 | };
|
---|