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