| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * MoleculeDescriptorTest.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Mar 4, 2010 | 
|---|
| 12 | *      Author: crueger | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 21 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 22 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 23 | #include <iostream> | 
|---|
| 24 |  | 
|---|
| 25 | #include <Descriptors/MoleculeDescriptor.hpp> | 
|---|
| 26 | #include <Descriptors/MoleculeIdDescriptor.hpp> | 
|---|
| 27 | #include <Descriptors/MoleculeNameDescriptor.hpp> | 
|---|
| 28 | #include <Descriptors/MoleculeOrderDescriptor.hpp> | 
|---|
| 29 |  | 
|---|
| 30 | #include "World.hpp" | 
|---|
| 31 | #include "molecule.hpp" | 
|---|
| 32 |  | 
|---|
| 33 | #include "MoleculeDescriptorUnitTest.hpp" | 
|---|
| 34 |  | 
|---|
| 35 | #ifdef HAVE_TESTRUNNER | 
|---|
| 36 | #include "UnitTestMain.hpp" | 
|---|
| 37 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 38 |  | 
|---|
| 39 | /********************************************** Test classes **************************************/ | 
|---|
| 40 | // Registers the fixture into the 'registry' | 
|---|
| 41 | CPPUNIT_TEST_SUITE_REGISTRATION( MoleculeDescriptorTest ); | 
|---|
| 42 |  | 
|---|
| 43 | // set up and tear down | 
|---|
| 44 | void MoleculeDescriptorTest::setUp(){ | 
|---|
| 45 | World::getInstance(); | 
|---|
| 46 | for(int i=0;i<MOLECULE_COUNT;++i){ | 
|---|
| 47 | molecules[i]= World::getInstance().createMolecule(); | 
|---|
| 48 | moleculeIds[i]= molecules[i]->getId(); | 
|---|
| 49 | } | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | void MoleculeDescriptorTest::tearDown(){ | 
|---|
| 53 | World::purgeInstance(); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | // some helper functions | 
|---|
| 57 | static bool hasAllMolecules(std::vector<molecule*> molecules,moleculeId_t ids[MOLECULE_COUNT], std::set<moleculeId_t> excluded = std::set<moleculeId_t>()){ | 
|---|
| 58 | for(int i=0;i<MOLECULE_COUNT;++i){ | 
|---|
| 59 | moleculeId_t id = ids[i]; | 
|---|
| 60 | if(!excluded.count(id)){ | 
|---|
| 61 | std::vector<molecule*>::iterator iter; | 
|---|
| 62 | bool res=false; | 
|---|
| 63 | for(iter=molecules.begin();iter!=molecules.end();++iter){ | 
|---|
| 64 | res |= (*iter)->getId() == id; | 
|---|
| 65 | } | 
|---|
| 66 | if(!res) { | 
|---|
| 67 | cout << "Molecule " << id << " missing in returned list" << endl; | 
|---|
| 68 | return false; | 
|---|
| 69 | } | 
|---|
| 70 | } | 
|---|
| 71 | } | 
|---|
| 72 | return true; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | static bool hasNoDuplicateMolecules(std::vector<molecule*> molecules){ | 
|---|
| 76 | std::set<moleculeId_t> found; | 
|---|
| 77 | std::vector<molecule*>::iterator iter; | 
|---|
| 78 | for(iter=molecules.begin();iter!=molecules.end();++iter){ | 
|---|
| 79 | int id = (*iter)->getId(); | 
|---|
| 80 | if(found.count(id)) | 
|---|
| 81 | return false; | 
|---|
| 82 | found.insert(id); | 
|---|
| 83 | } | 
|---|
| 84 | return true; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 |  | 
|---|
| 88 | void MoleculeDescriptorTest::MoleculeBaseSetsTest(){ | 
|---|
| 89 | std::vector<molecule*> allMolecules = World::getInstance().getAllMolecules(AllMolecules()); | 
|---|
| 90 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(allMolecules,moleculeIds)); | 
|---|
| 91 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(allMolecules)); | 
|---|
| 92 |  | 
|---|
| 93 | std::vector<molecule*> noMolecules = World::getInstance().getAllMolecules(NoMolecules()); | 
|---|
| 94 | CPPUNIT_ASSERT_EQUAL( true , noMolecules.empty()); | 
|---|
| 95 | } | 
|---|
| 96 | void MoleculeDescriptorTest::MoleculeIdTest(){ | 
|---|
| 97 | // test Molecules from boundaries and middle of the set | 
|---|
| 98 | molecule* testMolecule; | 
|---|
| 99 | testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[0])); | 
|---|
| 100 | CPPUNIT_ASSERT(testMolecule); | 
|---|
| 101 | CPPUNIT_ASSERT_EQUAL( moleculeIds[0], testMolecule->getId()); | 
|---|
| 102 | testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT/2])); | 
|---|
| 103 | CPPUNIT_ASSERT(testMolecule); | 
|---|
| 104 | CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecule->getId()); | 
|---|
| 105 | testMolecule = World::getInstance().getMolecule(MoleculeById(moleculeIds[MOLECULE_COUNT-1])); | 
|---|
| 106 | CPPUNIT_ASSERT(testMolecule); | 
|---|
| 107 | CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT-1], testMolecule->getId()); | 
|---|
| 108 |  | 
|---|
| 109 | // find some ID that has not been created | 
|---|
| 110 | moleculeId_t outsideId=0; | 
|---|
| 111 | bool res = false; | 
|---|
| 112 | for(outsideId=0;!res;++outsideId) { | 
|---|
| 113 | res = true; | 
|---|
| 114 | for(int i = 0; i < MOLECULE_COUNT; ++i){ | 
|---|
| 115 | res &= moleculeIds[i]!=outsideId; | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 | // test from outside of set | 
|---|
| 119 | testMolecule = World::getInstance().getMolecule(MoleculeById(outsideId)); | 
|---|
| 120 | CPPUNIT_ASSERT(!testMolecule); | 
|---|
| 121 | } | 
|---|
| 122 | void MoleculeDescriptorTest::MoleculeCalcTest(){ | 
|---|
| 123 | // test some elementary set operations | 
|---|
| 124 | { | 
|---|
| 125 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()||NoMolecules()); | 
|---|
| 126 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds)); | 
|---|
| 127 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | { | 
|---|
| 131 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()||AllMolecules()); | 
|---|
| 132 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds)); | 
|---|
| 133 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | { | 
|---|
| 137 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()&&AllMolecules()); | 
|---|
| 138 | CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty()); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | { | 
|---|
| 142 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()&&NoMolecules()); | 
|---|
| 143 | CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty()); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | { | 
|---|
| 147 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(!AllMolecules()); | 
|---|
| 148 | CPPUNIT_ASSERT_EQUAL( true , testMolecules.empty()); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | { | 
|---|
| 152 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(!NoMolecules()); | 
|---|
| 153 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds)); | 
|---|
| 154 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | // exclude and include some molecules | 
|---|
| 158 | { | 
|---|
| 159 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(AllMolecules()&&(!MoleculeById(moleculeIds[MOLECULE_COUNT/2]))); | 
|---|
| 160 | std::set<moleculeId_t> excluded; | 
|---|
| 161 | excluded.insert(moleculeIds[MOLECULE_COUNT/2]); | 
|---|
| 162 | CPPUNIT_ASSERT_EQUAL( true , hasAllMolecules(testMolecules,moleculeIds,excluded)); | 
|---|
| 163 | CPPUNIT_ASSERT_EQUAL( true , hasNoDuplicateMolecules(testMolecules)); | 
|---|
| 164 | CPPUNIT_ASSERT_EQUAL( (size_t)(MOLECULE_COUNT-1), testMolecules.size()); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | { | 
|---|
| 168 | std::vector<molecule*> testMolecules = World::getInstance().getAllMolecules(NoMolecules()||(MoleculeById(moleculeIds[MOLECULE_COUNT/2]))); | 
|---|
| 169 | CPPUNIT_ASSERT_EQUAL( (size_t)1, testMolecules.size()); | 
|---|
| 170 | CPPUNIT_ASSERT_EQUAL( moleculeIds[MOLECULE_COUNT/2], testMolecules[0]->getId()); | 
|---|
| 171 | } | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | void MoleculeDescriptorTest::MoleculeNameTest() | 
|---|
| 175 | { | 
|---|
| 176 | molecule* testMolecule; | 
|---|
| 177 |  | 
|---|
| 178 | // name each molecule | 
|---|
| 179 | for(int i=1;i<=MOLECULE_COUNT;++i) | 
|---|
| 180 | molecules[i-1]->setName(toString(i)); | 
|---|
| 181 |  | 
|---|
| 182 | // retrieve each | 
|---|
| 183 | for(int i=1;i<=MOLECULE_COUNT;++i) { | 
|---|
| 184 | testMolecule = World::getInstance().getMolecule(MoleculeByName(toString(i))); | 
|---|
| 185 | CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId()); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | // check for non-present name | 
|---|
| 189 | testMolecule = World::getInstance().getMolecule(MoleculeByName("not present")); | 
|---|
| 190 | CPPUNIT_ASSERT(!testMolecule); | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 |  | 
|---|
| 194 | void MoleculeDescriptorTest::MoleculeOrderTest() | 
|---|
| 195 | { | 
|---|
| 196 | molecule* testMolecule; | 
|---|
| 197 |  | 
|---|
| 198 | // test in normal order: 1, 2, ... | 
|---|
| 199 | for(int i=1;i<=MOLECULE_COUNT;++i){ | 
|---|
| 200 | testMolecule = World::getInstance().getMolecule(MoleculeByOrder(i)); | 
|---|
| 201 | CPPUNIT_ASSERT_EQUAL( moleculeIds[i-1], testMolecule->getId()); | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | // test in reverse order: -1, -2, ... | 
|---|
| 205 | for(int i=1; i<= MOLECULE_COUNT;++i){ | 
|---|
| 206 | testMolecule = World::getInstance().getMolecule(MoleculeByOrder(-i)); | 
|---|
| 207 | CPPUNIT_ASSERT_EQUAL( moleculeIds[(int)MOLECULE_COUNT-i], testMolecule->getId()); | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | // test from outside of set | 
|---|
| 211 | testMolecule = World::getInstance().getMolecule(MoleculeByOrder(MOLECULE_COUNT+1)); | 
|---|
| 212 | CPPUNIT_ASSERT(!testMolecule); | 
|---|
| 213 | testMolecule = World::getInstance().getMolecule(MoleculeByOrder(-MOLECULE_COUNT-1)); | 
|---|
| 214 | CPPUNIT_ASSERT(!testMolecule); | 
|---|
| 215 | } | 
|---|