source: molecuilder/src/unittests/atomsCalculationTest.cpp@ 01d28a

Last change on this file since 01d28a was 01d28a, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added templates that allow arbitrary calculations on atoms to be mapped to sets of Atoms

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[01d28a]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'
27CPPUNIT_TEST_SUITE_REGISTRATION( atomsCalculationTest );
28
29// some stubs
30class AtomStub : public atom {
31public:
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;
47private:
48 int id;
49};
50
51// set up and tear down
52void atomsCalculationTest::setUp(){
53 World::get();
54 for(int i=0;i<ATOM_COUNT;++i){
55 atoms[i]= new AtomStub(i);
56 }
57}
58void atomsCalculationTest::tearDown(){
59 World::destroy();
60 for(int i=0;i<ATOM_COUNT;++i){
61 delete atoms[i];
62 }
63 ActionRegistry::purgeRegistry();
64}
65
66// some helper functions
67bool hasAll(std::vector<int> ids,int min, int max, std::set<int> excluded = std::set<int>()){
68 for(int i=min;i<max;++i){
69 if(!excluded.count(i)){
70 std::vector<int>::iterator iter;
71 bool res=false;
72 for(iter=ids.begin();iter!=ids.end();++iter){
73 res |= (*iter) == i;
74 }
75 if(!res) {
76 cout << "Atom " << i << " missing in returned list" << endl;
77 return false;
78 }
79 }
80 }
81 return true;
82}
83
84bool hasNoDuplicates(std::vector<int> ids){
85 std::set<int> found;
86 std::vector<int>::iterator iter;
87 for(iter=ids.begin();iter!=ids.end();++iter){
88 int id = (*iter);
89 if(found.count(id))
90 return false;
91 found.insert(id);
92 }
93 return true;
94}
95
96void operation(atom* _atom){
97 AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
98 assert(atom);
99 atom->doSomething();
100}
101
102
103void atomsCalculationTest::testCalculateSimple(){
104 AtomsCalculation<int> *calc = World::get()->calcOnAtoms<int>(boost::bind(&atom::getId,_1),"FOO",AllAtoms());
105 std::vector<int> allIds = (*calc)();
106 CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT));
107 CPPUNIT_ASSERT(hasNoDuplicates(allIds));
108}
109
110void atomsCalculationTest::testCalculateExcluded(){
111 int excluded = ATOM_COUNT/2;
112 AtomsCalculation<int> *calc = World::get()->calcOnAtoms<int>(boost::bind(&atom::getId,_1),"FOO",AllAtoms() && !AtomById(excluded));
113 std::vector<int> allIds = (*calc)();
114 std::set<int> excluded_set;
115 excluded_set.insert(excluded);
116 CPPUNIT_ASSERT(hasAll(allIds,0,ATOM_COUNT,excluded_set));
117 CPPUNIT_ASSERT(hasNoDuplicates(allIds));
118 CPPUNIT_ASSERT_EQUAL((size_t)(ATOM_COUNT-1),allIds.size());
119}
120
121/********************************************** Main routine **************************************/
122
123int main(int argc, char **argv)
124{
125 // Get the top level suite from the registry
126 CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
127
128 // Adds the test to the list of test to run
129 CppUnit::TextUi::TestRunner runner;
130 runner.addTest( suite );
131
132 // Change the default outputter to a compiler error format outputter
133 runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
134 std::cerr ) );
135 // Run the tests.
136 bool wasSucessful = runner.run();
137
138 // Return error code 1 if the one of test failed.
139 return wasSucessful ? 0 : 1;
140};
Note: See TracBrowser for help on using the repository browser.