[b54ac8] | 1 | /*
|
---|
| 2 | * atomsCalculationTest.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Feb 19, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[b54ac8] | 13 | #include "atomsCalculationTest.hpp"
|
---|
| 14 |
|
---|
| 15 | #include <cppunit/CompilerOutputter.h>
|
---|
| 16 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 17 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 18 | #include <iostream>
|
---|
| 19 | #include <boost/bind.hpp>
|
---|
| 20 |
|
---|
| 21 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
| 22 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
| 23 | #include "Actions/AtomsCalculation.hpp"
|
---|
| 24 | #include "Actions/AtomsCalculation_impl.hpp"
|
---|
| 25 | #include "Actions/ActionRegistry.hpp"
|
---|
| 26 |
|
---|
| 27 | #include "World.hpp"
|
---|
| 28 | #include "World_calculations.hpp"
|
---|
| 29 | #include "atom.hpp"
|
---|
| 30 |
|
---|
[7ca806] | 31 | #ifdef HAVE_TESTRUNNER
|
---|
| 32 | #include "UnitTestMain.hpp"
|
---|
| 33 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 34 |
|
---|
[b54ac8] | 35 | // Registers the fixture into the 'registry'
|
---|
| 36 | CPPUNIT_TEST_SUITE_REGISTRATION( atomsCalculationTest );
|
---|
| 37 |
|
---|
| 38 | // set up and tear down
|
---|
| 39 | void atomsCalculationTest::setUp(){
|
---|
[23b547] | 40 | World::getInstance();
|
---|
[b54ac8] | 41 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
[ead4e6] | 42 | atoms[i]= World::getInstance().createAtom();
|
---|
| 43 | atomIds[i]= atoms[i]->getId();
|
---|
[b54ac8] | 44 | }
|
---|
| 45 | }
|
---|
| 46 | void atomsCalculationTest::tearDown(){
|
---|
[23b547] | 47 | World::purgeInstance();
|
---|
[e73a8a2] | 48 | ActionRegistry::purgeInstance();
|
---|
[b54ac8] | 49 | }
|
---|
| 50 |
|
---|
| 51 | // some helper functions
|
---|
[ead4e6] | 52 | static bool hasAllIds(std::vector<atomId_t> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>()){
|
---|
| 53 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
| 54 | atomId_t id = ids[i];
|
---|
| 55 | if(!excluded.count(id)){
|
---|
| 56 | std::vector<atomId_t>::iterator iter;
|
---|
[b54ac8] | 57 | bool res=false;
|
---|
[ead4e6] | 58 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
| 59 | res |= (*iter) == id;
|
---|
[b54ac8] | 60 | }
|
---|
| 61 | if(!res) {
|
---|
[ead4e6] | 62 | cout << "Atom " << id << " missing in returned list" << endl;
|
---|
[b54ac8] | 63 | return false;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | return true;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[ead4e6] | 70 | static bool hasNoDuplicates(std::vector<atomId_t> ids){
|
---|
| 71 | std::set<atomId_t> found;
|
---|
| 72 | std::vector<atomId_t>::iterator iter;
|
---|
[b54ac8] | 73 | for(iter=ids.begin();iter!=ids.end();++iter){
|
---|
| 74 | int id = (*iter);
|
---|
| 75 | if(found.count(id))
|
---|
| 76 | return false;
|
---|
| 77 | found.insert(id);
|
---|
| 78 | }
|
---|
| 79 | return true;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | void atomsCalculationTest::testCalculateSimple(){
|
---|
[ead4e6] | 83 | AtomsCalculation<atomId_t> *calc = World::getInstance().calcOnAtoms<atomId_t>(boost::bind(&atom::getId,_1),"FOO",AllAtoms());
|
---|
| 84 | std::vector<atomId_t> allIds = (*calc)();
|
---|
| 85 | CPPUNIT_ASSERT(hasAllIds(allIds,atomIds));
|
---|
[b54ac8] | 86 | CPPUNIT_ASSERT(hasNoDuplicates(allIds));
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void atomsCalculationTest::testCalculateExcluded(){
|
---|
[ead4e6] | 90 | atomId_t excluded = atomIds[ATOM_COUNT/2];
|
---|
| 91 | AtomsCalculation<atomId_t> *calc = World::getInstance().calcOnAtoms<atomId_t>(boost::bind(&atom::getId,_1),"FOO",AllAtoms() && !AtomById(excluded));
|
---|
| 92 | std::vector<atomId_t> allIds = (*calc)();
|
---|
| 93 | std::set<atomId_t> excluded_set;
|
---|
[b54ac8] | 94 | excluded_set.insert(excluded);
|
---|
[ead4e6] | 95 | CPPUNIT_ASSERT(hasAllIds(allIds,atomIds,excluded_set));
|
---|
[b54ac8] | 96 | CPPUNIT_ASSERT(hasNoDuplicates(allIds));
|
---|
| 97 | CPPUNIT_ASSERT_EQUAL((size_t)(ATOM_COUNT-1),allIds.size());
|
---|
| 98 | }
|
---|