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