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