| 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 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 27 | #include "UnitTestMain.hpp"
 | 
|---|
| 28 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // Registers the fixture into the 'registry'
 | 
|---|
| 31 | CPPUNIT_TEST_SUITE_REGISTRATION( atomsCalculationTest );
 | 
|---|
| 32 | 
 | 
|---|
| 33 | // set up and tear down
 | 
|---|
| 34 | void atomsCalculationTest::setUp(){
 | 
|---|
| 35 |   World::getInstance();
 | 
|---|
| 36 |   for(int i=0;i<ATOM_COUNT;++i){
 | 
|---|
| 37 |     atoms[i]= World::getInstance().createAtom();
 | 
|---|
| 38 |     atomIds[i]= atoms[i]->getId();
 | 
|---|
| 39 |   }
 | 
|---|
| 40 | }
 | 
|---|
| 41 | void atomsCalculationTest::tearDown(){
 | 
|---|
| 42 |   World::purgeInstance();
 | 
|---|
| 43 |   ActionRegistry::purgeInstance();
 | 
|---|
| 44 | }
 | 
|---|
| 45 | 
 | 
|---|
| 46 | // some helper functions
 | 
|---|
| 47 | static bool hasAllIds(std::vector<atomId_t> atoms,atomId_t ids[ATOM_COUNT], std::set<atomId_t> excluded = std::set<atomId_t>()){
 | 
|---|
| 48 |   for(int i=0;i<ATOM_COUNT;++i){
 | 
|---|
| 49 |     atomId_t id = ids[i];
 | 
|---|
| 50 |     if(!excluded.count(id)){
 | 
|---|
| 51 |       std::vector<atomId_t>::iterator iter;
 | 
|---|
| 52 |       bool res=false;
 | 
|---|
| 53 |       for(iter=atoms.begin();iter!=atoms.end();++iter){
 | 
|---|
| 54 |         res |= (*iter) == id;
 | 
|---|
| 55 |       }
 | 
|---|
| 56 |       if(!res) {
 | 
|---|
| 57 |         cout << "Atom " << id << " missing in returned list" << endl;
 | 
|---|
| 58 |         return false;
 | 
|---|
| 59 |       }
 | 
|---|
| 60 |     }
 | 
|---|
| 61 |   }
 | 
|---|
| 62 |   return true;
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | static bool hasNoDuplicates(std::vector<atomId_t> ids){
 | 
|---|
| 66 |   std::set<atomId_t> found;
 | 
|---|
| 67 |   std::vector<atomId_t>::iterator iter;
 | 
|---|
| 68 |   for(iter=ids.begin();iter!=ids.end();++iter){
 | 
|---|
| 69 |     int id = (*iter);
 | 
|---|
| 70 |     if(found.count(id))
 | 
|---|
| 71 |       return false;
 | 
|---|
| 72 |     found.insert(id);
 | 
|---|
| 73 |   }
 | 
|---|
| 74 |   return true;
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | void atomsCalculationTest::testCalculateSimple(){
 | 
|---|
| 78 |   AtomsCalculation<atomId_t> *calc = World::getInstance().calcOnAtoms<atomId_t>(boost::bind(&atom::getId,_1),"FOO",AllAtoms());
 | 
|---|
| 79 |   std::vector<atomId_t> allIds = (*calc)();
 | 
|---|
| 80 |   CPPUNIT_ASSERT(hasAllIds(allIds,atomIds));
 | 
|---|
| 81 |   CPPUNIT_ASSERT(hasNoDuplicates(allIds));
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | void atomsCalculationTest::testCalculateExcluded(){
 | 
|---|
| 85 |   atomId_t excluded = atomIds[ATOM_COUNT/2];
 | 
|---|
| 86 |   AtomsCalculation<atomId_t> *calc = World::getInstance().calcOnAtoms<atomId_t>(boost::bind(&atom::getId,_1),"FOO",AllAtoms() && !AtomById(excluded));
 | 
|---|
| 87 |   std::vector<atomId_t> allIds = (*calc)();
 | 
|---|
| 88 |   std::set<atomId_t> excluded_set;
 | 
|---|
| 89 |   excluded_set.insert(excluded);
 | 
|---|
| 90 |   CPPUNIT_ASSERT(hasAllIds(allIds,atomIds,excluded_set));
 | 
|---|
| 91 |   CPPUNIT_ASSERT(hasNoDuplicates(allIds));
 | 
|---|
| 92 |   CPPUNIT_ASSERT_EQUAL((size_t)(ATOM_COUNT-1),allIds.size());
 | 
|---|
| 93 | }
 | 
|---|