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 | // set up and tear down
|
---|
30 | void DescriptorUnittest::setUp(){
|
---|
31 | World::get();
|
---|
32 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
33 | atoms[i]= World::get()->createAtom();
|
---|
34 | atomIds[i] = atoms[i]->getId();
|
---|
35 | }
|
---|
36 | }
|
---|
37 | void DescriptorUnittest::tearDown(){
|
---|
38 | World::destroy();
|
---|
39 | }
|
---|
40 |
|
---|
41 | // some helper functions
|
---|
42 | bool hasAll(std::vector<atom*> atoms,int ids[ATOM_COUNT], std::set<int> excluded = std::set<int>()){
|
---|
43 | for(int i=0;i<ATOM_COUNT;++i){
|
---|
44 | int id = ids[i];
|
---|
45 | if(!excluded.count(id)){
|
---|
46 | std::vector<atom*>::iterator iter;
|
---|
47 | bool res=false;
|
---|
48 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
49 | res |= (*iter)->getId() == id;
|
---|
50 | }
|
---|
51 | if(!res) {
|
---|
52 | cout << "Atom " << id << " missing in returned list" << endl;
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 |
|
---|
60 | bool hasNoDuplicates(std::vector<atom*> atoms){
|
---|
61 | std::set<int> found;
|
---|
62 | std::vector<atom*>::iterator iter;
|
---|
63 | for(iter=atoms.begin();iter!=atoms.end();++iter){
|
---|
64 | int id = (*iter)->getId();
|
---|
65 | if(found.count(id))
|
---|
66 | return false;
|
---|
67 | found.insert(id);
|
---|
68 | }
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | void DescriptorUnittest::AtomBaseSetsTest(){
|
---|
74 | std::vector<atom*> allAtoms = World::get()->getAllAtoms(AllAtoms());
|
---|
75 | CPPUNIT_ASSERT_EQUAL( true , hasAll(allAtoms,atomIds));
|
---|
76 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicates(allAtoms));
|
---|
77 |
|
---|
78 | std::vector<atom*> noAtoms = World::get()->getAllAtoms(NoAtoms());
|
---|
79 | CPPUNIT_ASSERT_EQUAL( true , noAtoms.empty());
|
---|
80 | }
|
---|
81 | void DescriptorUnittest::AtomIdTest(){
|
---|
82 | // test Atoms from boundaries and middle of the set
|
---|
83 | atom* testAtom;
|
---|
84 | testAtom = World::get()->getAtom(AtomById(atomIds[0]));
|
---|
85 | CPPUNIT_ASSERT(testAtom);
|
---|
86 | CPPUNIT_ASSERT_EQUAL( atomIds[0], testAtom->getId());
|
---|
87 | testAtom = World::get()->getAtom(AtomById(atomIds[ATOM_COUNT/2]));
|
---|
88 | CPPUNIT_ASSERT(testAtom);
|
---|
89 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtom->getId());
|
---|
90 | testAtom = World::get()->getAtom(AtomById(atomIds[ATOM_COUNT-1]));
|
---|
91 | CPPUNIT_ASSERT(testAtom);
|
---|
92 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT-1], testAtom->getId());
|
---|
93 |
|
---|
94 | // find some ID that has not been created
|
---|
95 | int outsideId =-1;
|
---|
96 | bool res = false;
|
---|
97 | while(!res) {
|
---|
98 | ++outsideId;
|
---|
99 | res = true;
|
---|
100 | for(int i = 0; i < ATOM_COUNT; ++i){
|
---|
101 | res &= atomIds[i]!=outsideId;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | // test from outside of set
|
---|
105 | testAtom = World::get()->getAtom(AtomById(outsideId));
|
---|
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,atomIds));
|
---|
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,atomIds));
|
---|
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,atomIds));
|
---|
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(atomIds[ATOM_COUNT/2])));
|
---|
146 | std::set<int> excluded;
|
---|
147 | excluded.insert(atomIds[ATOM_COUNT/2]);
|
---|
148 | CPPUNIT_ASSERT_EQUAL( true , hasAll(testAtoms,atomIds,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(atomIds[ATOM_COUNT/2])));
|
---|
155 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testAtoms.size());
|
---|
156 | CPPUNIT_ASSERT_EQUAL( atomIds[ATOM_COUNT/2], testAtoms[0]->getId());
|
---|
157 | }
|
---|
158 | }
|
---|