| [bcf653] | 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 | 
 | 
|---|
| [57adc7] | 8 | /*
 | 
|---|
 | 9 |  * MoleculeDescriptorTest.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Mar 4, 2010
 | 
|---|
 | 12 |  *      Author: crueger
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [57adc7] | 20 | #include "MoleculeDescriptorTest.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/MoleculeDescriptor.hpp>
 | 
|---|
 | 28 | #include <Descriptors/MoleculeIdDescriptor.hpp>
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | #include "World.hpp"
 | 
|---|
 | 31 | #include "molecule.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( MoleculeDescriptorTest );
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | // set up and tear down
 | 
|---|
 | 42 | void MoleculeDescriptorTest::setUp(){
 | 
|---|
| [23b547] | 43 |   World::getInstance();
 | 
|---|
| [57adc7] | 44 |   for(int i=0;i<MOLECULE_COUNT;++i){
 | 
|---|
| [23b547] | 45 |     molecules[i]= World::getInstance().createMolecule();
 | 
|---|
| [57adc7] | 46 |     moleculeIds[i]= molecules[i]->getId();
 | 
|---|
 | 47 |   }
 | 
|---|
 | 48 | }
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | void MoleculeDescriptorTest::tearDown(){
 | 
|---|
| [23b547] | 51 |   World::purgeInstance();
 | 
|---|
| [57adc7] | 52 | }
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | // some helper functions
 | 
|---|
 | 55 | static bool hasAllMolecules(std::vector<molecule*> molecules,moleculeId_t ids[MOLECULE_COUNT], std::set<moleculeId_t> excluded = std::set<moleculeId_t>()){
 | 
|---|
 | 56 |   for(int i=0;i<MOLECULE_COUNT;++i){
 | 
|---|
 | 57 |     moleculeId_t id = ids[i];
 | 
|---|
 | 58 |     if(!excluded.count(id)){
 | 
|---|
 | 59 |       std::vector<molecule*>::iterator iter;
 | 
|---|
 | 60 |       bool res=false;
 | 
|---|
 | 61 |       for(iter=molecules.begin();iter!=molecules.end();++iter){
 | 
|---|
 | 62 |         res |= (*iter)->getId() == id;
 | 
|---|
 | 63 |       }
 | 
|---|
 | 64 |       if(!res) {
 | 
|---|
 | 65 |         cout << "Molecule " << id << " missing in returned list" << endl;
 | 
|---|
 | 66 |         return false;
 | 
|---|
 | 67 |       }
 | 
|---|
 | 68 |     }
 | 
|---|
 | 69 |   }
 | 
|---|
 | 70 |   return true;
 | 
|---|
 | 71 | }
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 | static bool hasNoDuplicateMolecules(std::vector<molecule*> molecules){
 | 
|---|
 | 74 |   std::set<moleculeId_t> found;
 | 
|---|
 | 75 |   std::vector<molecule*>::iterator iter;
 | 
|---|
 | 76 |   for(iter=molecules.begin();iter!=molecules.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 MoleculeDescriptorTest::MoleculeBaseSetsTest(){
 | 
|---|
| [23b547] | 87 |   std::vector<molecule*> allMolecules = World::getInstance().getAllMolecules(AllMolecules());
 | 
|---|
| [57adc7] | 88 |   CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(allMolecules,moleculeIds));
 | 
|---|
 | 89 |   CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(allMolecules));
 | 
|---|
 | 90 | 
 | 
|---|
| [23b547] | 91 |   std::vector<molecule*> noMolecules = World::getInstance().getAllMolecules(NoMolecules());
 | 
|---|
| [57adc7] | 92 |   CPPUNIT_ASSERT_EQUAL( true , noMolecules.empty());
 | 
|---|
 | 93 | }
 | 
|---|
 | 94 | void MoleculeDescriptorTest::MoleculeIdTest(){
 | 
|---|
 | 95 |   // test Molecules from boundaries and middle of the set
 | 
|---|
 | 96 |   molecule* testMolecule;
 | 
|---|
| [23b547] | 97 |   testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[0]));
 | 
|---|
| [57adc7] | 98 |   CPPUNIT_ASSERT(testMolecule);
 | 
|---|
 | 99 |   CPPUNIT_ASSERT_EQUAL( moleculeIds[0], testMolecule->getId());
 | 
|---|
| [23b547] | 100 |   testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT/2]));
 | 
|---|
| [57adc7] | 101 |   CPPUNIT_ASSERT(testMolecule);
 | 
|---|
 | 102 |   CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecule->getId());
 | 
|---|
| [23b547] | 103 |   testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT-1]));
 | 
|---|
| [57adc7] | 104 |   CPPUNIT_ASSERT(testMolecule);
 | 
|---|
 | 105 |   CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT-1], testMolecule->getId());
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 |   // find some ID that has not been created
 | 
|---|
 | 108 |   moleculeId_t outsideId=0;
 | 
|---|
 | 109 |   bool res = false;
 | 
|---|
 | 110 |   for(outsideId=0;!res;++outsideId) {
 | 
|---|
 | 111 |     res = true;
 | 
|---|
 | 112 |     for(int i = 0; i < MOLECULE_COUNT; ++i){
 | 
|---|
 | 113 |       res &= moleculeIds[i]!=outsideId;
 | 
|---|
 | 114 |     }
 | 
|---|
 | 115 |   }
 | 
|---|
 | 116 |   // test from outside of set
 | 
|---|
| [23b547] | 117 |   testMolecule = World::getInstance().getMolecule(MoleculeById(outsideId));
 | 
|---|
| [57adc7] | 118 |   CPPUNIT_ASSERT(!testMolecule);
 | 
|---|
 | 119 | }
 | 
|---|
 | 120 | void MoleculeDescriptorTest::MoleculeCalcTest(){
 | 
|---|
 | 121 |   // test some elementary set operations
 | 
|---|
 | 122 |   {
 | 
|---|
| [23b547] | 123 |     std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()||NoMolecules());
 | 
|---|
| [57adc7] | 124 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
 | 
|---|
 | 125 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
 | 126 |   }
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 |   {
 | 
|---|
| [23b547] | 129 |     std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()||AllMolecules());
 | 
|---|
| [57adc7] | 130 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
 | 
|---|
 | 131 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
 | 132 |   }
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 |   {
 | 
|---|
| [23b547] | 135 |     std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()&&AllMolecules());
 | 
|---|
| [57adc7] | 136 |     CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
 | 
|---|
 | 137 |   }
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 |   {
 | 
|---|
| [23b547] | 140 |     std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()&&NoMolecules());
 | 
|---|
| [57adc7] | 141 |     CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
 | 
|---|
 | 142 |   }
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 |   {
 | 
|---|
| [23b547] | 145 |     std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(!AllMolecules());
 | 
|---|
| [57adc7] | 146 |     CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty());
 | 
|---|
 | 147 |   }
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 |   {
 | 
|---|
| [23b547] | 150 |     std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(!NoMolecules());
 | 
|---|
| [57adc7] | 151 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds));
 | 
|---|
 | 152 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
 | 153 |   }
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 |   // exclude and include some molecules
 | 
|---|
 | 156 |   {
 | 
|---|
| [23b547] | 157 |     std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()&&(!MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
 | 
|---|
| [57adc7] | 158 |     std::set<moleculeId_t> excluded;
 | 
|---|
 | 159 |     excluded.insert(moleculeIds[MOLECULE_COUNT/2]);
 | 
|---|
 | 160 |     CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds,excluded));
 | 
|---|
 | 161 |     CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules));
 | 
|---|
 | 162 |     CPPUNIT_ASSERT_EQUAL( (size_t)(MOLECULE_COUNT-1), testMolecules.size());
 | 
|---|
 | 163 |   }
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 |   {
 | 
|---|
| [23b547] | 166 |     std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()||(MoleculeById(moleculeIds[MOLECULE_COUNT/2])));
 | 
|---|
| [57adc7] | 167 |     CPPUNIT_ASSERT_EQUAL( (size_t)1, testMolecules.size());
 | 
|---|
 | 168 |     CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecules[0]->getId());
 | 
|---|
 | 169 |   }
 | 
|---|
 | 170 | }
 | 
|---|