1 | /*
|
---|
2 | * DescriptorUnittest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Feb 9, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "DescriptorUnittest.hpp"
|
---|
9 |
|
---|
10 | #include <cppunit/CompilerOutputter.h>
|
---|
11 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
12 | #include <cppunit/ui/text/TestRunner.h>
|
---|
13 | #include <iostream>
|
---|
14 |
|
---|
15 | #include <Descriptors/AtomDescriptor.hpp>
|
---|
16 | #include <Descriptors/AtomIdDescriptor.hpp>
|
---|
17 |
|
---|
18 | #include "World.hpp"
|
---|
19 | #include "atom.hpp"
|
---|
20 |
|
---|
21 | #ifdef HAVE_TESTRUNNER
|
---|
22 | #include "UnitTestMain.hpp"
|
---|
23 | #endif /*HAVE_TESTRUNNER*/
|
---|
24 |
|
---|
25 | /********************************************** Test classes **************************************/
|
---|
26 | // Registers the fixture into the 'registry'
|
---|
27 | CPPUNIT_TEST_SUITE_REGISTRATION( DescriptorUnittest );
|
---|
28 |
|
---|
29 | // some stubs
|
---|
30 | class AtomStub : public atom {
|
---|
31 | public:
|
---|
32 | AtomStub(int _id) :
|
---|
33 | atom(),
|
---|
34 | id(_id)
|
---|
35 | {}
|
---|
36 |
|
---|
37 | virtual int getId(){
|
---|
38 | return id;
|
---|
39 | }
|
---|
40 |
|
---|
41 | private:
|
---|
42 | int id;
|
---|
43 | };
|
---|
44 |
|
---|
45 |
|
---|
46 | // set up and tear down
|
---|
47 | void DescriptorUnittest::setUp(){
|
---|
48 | World::get();
|
---|
49 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
50 | atoms[i]= new AtomStub(i);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | void DescriptorUnittest::tearDown(){
|
---|
54 | World::destroy();
|
---|
55 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
56 | delete atoms[i];
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | // some helper functions
|
---|
61 | bool hasAll(std::vector<atom*> atoms,int min, int max, std::set<int> excluded = std::set<int>()){
|
---|
62 | for(int i=min;i<max;++i){
|
---|
63 | if(!excluded.count(i)){
|
---|
64 | std::vector<atom*>::iterator iter;
|
---|
65 | bool res=false;
|
---|
66 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
67 | res |= (*iter)->getId() == i;
|
---|
68 | }
|
---|
69 | if(!res) {
|
---|
70 | cout << "Atom " << i << " missing in returned list" << endl;
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | bool hasNoDuplicates(std::vector<atom*> atoms){
|
---|
79 | std::set<int> found;
|
---|
80 | std::vector<atom*>::iterator iter;
|
---|
81 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
82 | int id = (*iter)->getId();
|
---|
83 | if(found.count(id))
|
---|
84 | return false;
|
---|
85 | found.insert(id);
|
---|
86 | }
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | void DescriptorUnittest::AtomBaseSetsTest(){
|
---|
92 | std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
|
---|
93 | CPPUNIT_ASSERT_EQUAL( true , hasAll(allAtoms,0,ATOM_COUNT));
|
---|
94 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(allAtoms));
|
---|
95 |
|
---|
96 | std::vector<atom*> noAtoms = World::get()->getAllAtoms(NoAtoms());
|
---|
97 | CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
|
---|
98 | }
|
---|
99 | void DescriptorUnittest::AtomIdTest(){
|
---|
100 | // test Atoms from boundaries and middle of the set
|
---|
101 | atom* testAtom;
|
---|
102 | testAtom = World::get()->getAtom(AtomById(0));
|
---|
103 | CPPUNIT_ASSERT_EQUAL( 0, testAtom->getId());
|
---|
104 | testAtom = World::get()->getAtom(AtomById(ATOM_COUNT/2));
|
---|
105 | CPPUNIT_ASSERT_EQUAL( ATOM_COUNT/2, testAtom->getId());
|
---|
106 | testAtom = World::get()->getAtom(AtomById(ATOM_COUNT-1));
|
---|
107 | CPPUNIT_ASSERT_EQUAL( ATOM_COUNT-1, testAtom->getId());
|
---|
108 |
|
---|
109 | // test from outside of set
|
---|
110 | testAtom = World::get()->getAtom(AtomById(ATOM_COUNT));
|
---|
111 | CPPUNIT_ASSERT(!testAtom);
|
---|
112 | }
|
---|
113 | void DescriptorUnittest::AtomCalcTest(){
|
---|
114 | // test some elementary set operations
|
---|
115 | {
|
---|
116 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()||NoAtoms());
|
---|
117 | CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
|
---|
118 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
|
---|
119 | }
|
---|
120 |
|
---|
121 | {
|
---|
122 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||AllAtoms());
|
---|
123 | CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
|
---|
124 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
|
---|
125 | }
|
---|
126 |
|
---|
127 | {
|
---|
128 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()&&AllAtoms());
|
---|
129 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
130 | }
|
---|
131 |
|
---|
132 | {
|
---|
133 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&NoAtoms());
|
---|
134 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
135 | }
|
---|
136 |
|
---|
137 | {
|
---|
138 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(!AllAtoms());
|
---|
139 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
140 | }
|
---|
141 |
|
---|
142 | {
|
---|
143 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(!NoAtoms());
|
---|
144 | CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
|
---|
145 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
|
---|
146 | }
|
---|
147 |
|
---|
148 | // exclude and include some atoms
|
---|
149 | {
|
---|
150 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&(!AtomById(ATOM_COUNT/2)));
|
---|
151 | std::set<int> excluded;
|
---|
152 | excluded.insert(ATOM_COUNT/2);
|
---|
153 | CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT,excluded));
|
---|
154 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
|
---|
155 | CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
|
---|
156 | }
|
---|
157 |
|
---|
158 | {
|
---|
159 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||(AtomById(ATOM_COUNT/2)));
|
---|
160 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
|
---|
161 | CPPUNIT_ASSERT_EQUAL( ATOM_COUNT/2, testAtoms[0]->getId());
|
---|
162 | }
|
---|
163 | }
|
---|