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 | // Registers the fixture into the 'registry'
|
---|
22 | CPPUNIT_TEST_SUITE_REGISTRATION( DescriptorUnittest );
|
---|
23 |
|
---|
24 | // some stubs
|
---|
25 | class AtomStub : public atom {
|
---|
26 | public:
|
---|
27 | AtomStub(int _id) :
|
---|
28 | atom(),
|
---|
29 | id(_id)
|
---|
30 | {}
|
---|
31 |
|
---|
32 | virtual int getId(){
|
---|
33 | return id;
|
---|
34 | }
|
---|
35 |
|
---|
36 | private:
|
---|
37 | int id;
|
---|
38 | };
|
---|
39 |
|
---|
40 |
|
---|
41 | // set up and tear down
|
---|
42 | void DescriptorUnittest::setUp(){
|
---|
43 | World::get();
|
---|
44 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
45 | atoms[i]= new AtomStub(i);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | void DescriptorUnittest::tearDown(){
|
---|
49 | World::destroy();
|
---|
50 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
51 | delete atoms[i];
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | // some helper functions
|
---|
56 | bool hasAll(std::vector<atom*> atoms,int min, int max, std::set<int> excluded = std::set<int>()){
|
---|
57 | for(int i=min;i<max;++i){
|
---|
58 | if(!excluded.count(i)){
|
---|
59 | std::vector<atom*>::iterator iter;
|
---|
60 | bool res=false;
|
---|
61 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
62 | res |= (*iter)->getId() == i;
|
---|
63 | }
|
---|
64 | if(!res) {
|
---|
65 | cout << "Atom " << i << " missing in returned list" << endl;
|
---|
66 | return false;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 |
|
---|
73 | bool hasNoDuplicates(std::vector<atom*> atoms){
|
---|
74 | std::set<int> found;
|
---|
75 | std::vector<atom*>::iterator iter;
|
---|
76 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
77 | int id = (*iter)->getId();
|
---|
78 | if(found.count(id))
|
---|
79 | return false;
|
---|
80 | found.insert(id);
|
---|
81 | }
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | void DescriptorUnittest::AtomBaseSetsTest(){
|
---|
87 | std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
|
---|
88 | CPPUNIT_ASSERT_EQUAL( true , hasAll(allAtoms,0,ATOM_COUNT));
|
---|
89 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(allAtoms));
|
---|
90 |
|
---|
91 | std::vector<atom*> noAtoms = World::get()->getAllAtoms(NoAtoms());
|
---|
92 | CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
|
---|
93 | }
|
---|
94 | void DescriptorUnittest::AtomIdTest(){
|
---|
95 | // test Atoms from boundaries and middle of the set
|
---|
96 | atom* testAtom;
|
---|
97 | testAtom = World::get()->getAtom(AtomById(0));
|
---|
98 | CPPUNIT_ASSERT_EQUAL( 0, testAtom->getId());
|
---|
99 | testAtom = World::get()->getAtom(AtomById(ATOM_COUNT/2));
|
---|
100 | CPPUNIT_ASSERT_EQUAL( ATOM_COUNT/2, testAtom->getId());
|
---|
101 | testAtom = World::get()->getAtom(AtomById(ATOM_COUNT-1));
|
---|
102 | CPPUNIT_ASSERT_EQUAL( ATOM_COUNT-1, testAtom->getId());
|
---|
103 |
|
---|
104 | // test from outside of set
|
---|
105 | testAtom = World::get()->getAtom(AtomById(ATOM_COUNT));
|
---|
106 | CPPUNIT_ASSERT(!testAtom);
|
---|
107 | }
|
---|
108 | void DescriptorUnittest::AtomCalcTest(){
|
---|
109 | // test some elementary set operations
|
---|
110 | {
|
---|
111 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()||NoAtoms());
|
---|
112 | CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
|
---|
113 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
|
---|
114 | }
|
---|
115 |
|
---|
116 | {
|
---|
117 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||AllAtoms());
|
---|
118 | CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
|
---|
119 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
|
---|
120 | }
|
---|
121 |
|
---|
122 | {
|
---|
123 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()&&AllAtoms());
|
---|
124 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
125 | }
|
---|
126 |
|
---|
127 | {
|
---|
128 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&NoAtoms());
|
---|
129 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
130 | }
|
---|
131 |
|
---|
132 | {
|
---|
133 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(!AllAtoms());
|
---|
134 | CPPUNIT_ASSERT_EQUAL( true , testAtoms.empty());
|
---|
135 | }
|
---|
136 |
|
---|
137 | {
|
---|
138 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(!NoAtoms());
|
---|
139 | CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT));
|
---|
140 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
|
---|
141 | }
|
---|
142 |
|
---|
143 | // exclude and include some atoms
|
---|
144 | {
|
---|
145 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(AllAtoms()&&(!AtomById(ATOM_COUNT/2)));
|
---|
146 | std::set<int> excluded;
|
---|
147 | excluded.insert(ATOM_COUNT/2);
|
---|
148 | CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,0,ATOM_COUNT,excluded));
|
---|
149 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(testAtoms));
|
---|
150 | CPPUNIT_ASSERT_EQUAL( (size_t)(ATOM_COUNT-1), testAtoms.size());
|
---|
151 | }
|
---|
152 |
|
---|
153 | {
|
---|
154 | std::vector<atom*> testAtoms = World::get()->getAllAtoms(NoAtoms()||(AtomById(ATOM_COUNT/2)));
|
---|
155 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
|
---|
156 | CPPUNIT_ASSERT_EQUAL( ATOM_COUNT/2, testAtoms[0]->getId());
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /********************************************** Main routine **************************************/
|
---|
161 |
|
---|
162 | int main(int argc, char **argv)
|
---|
163 | {
|
---|
164 | // Get the top level suite from the registry
|
---|
165 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
166 |
|
---|
167 | // Adds the test to the list of test to run
|
---|
168 | CppUnit::TextUi::TestRunner runner;
|
---|
169 | runner.addTest( suite );
|
---|
170 |
|
---|
171 | // Change the default outputter to a compiler error format outputter
|
---|
172 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
173 | std::cerr ) );
|
---|
174 | // Run the tests.
|
---|
175 | bool wasSucessful = runner.run();
|
---|
176 |
|
---|
177 | // Return error code 1 if the one of test failed.
|
---|
178 | return wasSucessful ? 0 : 1;
|
---|
179 | };
|
---|