| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| 6 | * | 
|---|
| 7 | * | 
|---|
| 8 | *   This file is part of MoleCuilder. | 
|---|
| 9 | * | 
|---|
| 10 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 11 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 12 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 13 | *    (at your option) any later version. | 
|---|
| 14 | * | 
|---|
| 15 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 16 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 17 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 18 | *    GNU General Public License for more details. | 
|---|
| 19 | * | 
|---|
| 20 | *    You should have received a copy of the GNU General Public License | 
|---|
| 21 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | /* | 
|---|
| 25 | * AdjacencyListUnitTest.cpp | 
|---|
| 26 | * | 
|---|
| 27 | *  Created on: Oct 17, 2011 | 
|---|
| 28 | *      Author: heber | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | // include config.h | 
|---|
| 32 | #ifdef HAVE_CONFIG_H | 
|---|
| 33 | #include <config.h> | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | #include "AdjacencyListUnitTest.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 39 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 40 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 41 |  | 
|---|
| 42 | #include <boost/lambda/lambda.hpp> | 
|---|
| 43 | #include <iostream> | 
|---|
| 44 | #include <iterator> | 
|---|
| 45 | #include <iostream> | 
|---|
| 46 | #include <vector> | 
|---|
| 47 |  | 
|---|
| 48 | #include "CodePatterns/Assert.hpp" | 
|---|
| 49 | #include "CodePatterns/Log.hpp" | 
|---|
| 50 |  | 
|---|
| 51 | #include "Atom/atom.hpp" | 
|---|
| 52 | #include "Descriptors/AtomDescriptor.hpp" | 
|---|
| 53 | #include "Element/element.hpp" | 
|---|
| 54 | #include "Element/periodentafel.hpp" | 
|---|
| 55 | #include "Graph/AdjacencyList.hpp" | 
|---|
| 56 | #include "molecule.hpp" | 
|---|
| 57 | #include "World.hpp" | 
|---|
| 58 | #include "WorldTime.hpp" | 
|---|
| 59 |  | 
|---|
| 60 | #ifdef HAVE_TESTRUNNER | 
|---|
| 61 | #include "UnitTestMain.hpp" | 
|---|
| 62 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 63 |  | 
|---|
| 64 | /********************************************** Test classes **************************************/ | 
|---|
| 65 | // Registers the fixture into the 'registry' | 
|---|
| 66 | CPPUNIT_TEST_SUITE_REGISTRATION( AdjacencyListTest ); | 
|---|
| 67 |  | 
|---|
| 68 | const std::string adjacencyfile ="\ | 
|---|
| 69 | 1 2\n\ | 
|---|
| 70 | 2 1 3\n\ | 
|---|
| 71 | 3 2 4\n\ | 
|---|
| 72 | 4 3 5\n\ | 
|---|
| 73 | 5 4 6\n\ | 
|---|
| 74 | 6 5 7\n\ | 
|---|
| 75 | 7 6 8\n\ | 
|---|
| 76 | 8 7 9\n\ | 
|---|
| 77 | 9 8 10\n\ | 
|---|
| 78 | 10 9\n"; | 
|---|
| 79 |  | 
|---|
| 80 | const std::string wrongadjacencyfile1 ="\ | 
|---|
| 81 | 1 2\n\ | 
|---|
| 82 | 2 1\n\ | 
|---|
| 83 | 4 5\n\ | 
|---|
| 84 | 5 4 6\n\ | 
|---|
| 85 | 6 5 7\n\ | 
|---|
| 86 | 7 6 8\n\ | 
|---|
| 87 | 8 7 9\n\ | 
|---|
| 88 | 9 8 10\n\ | 
|---|
| 89 | 10 9\n"; | 
|---|
| 90 |  | 
|---|
| 91 | const std::string wrongadjacencyfile2 ="\ | 
|---|
| 92 | 1 2\n\ | 
|---|
| 93 | 2 1 3\n\ | 
|---|
| 94 | 3 2 4\n\ | 
|---|
| 95 | 4 3 5\n\ | 
|---|
| 96 | 5 4 6\n\ | 
|---|
| 97 | 6 5 7\n\ | 
|---|
| 98 | 7 6 8\n\ | 
|---|
| 99 | 8 7 9\n\ | 
|---|
| 100 | 9 8 10\n\ | 
|---|
| 101 | 10 9 11\n\ | 
|---|
| 102 | 11 10\n"; | 
|---|
| 103 |  | 
|---|
| 104 | const std::string wrongadjacencyfile3 ="\ | 
|---|
| 105 | 1 2\n\ | 
|---|
| 106 | 2 1 3\n\ | 
|---|
| 107 | 3 2 4\n\ | 
|---|
| 108 | 4 3 5\n\ | 
|---|
| 109 | 5 4 7\n\ | 
|---|
| 110 | 6\n\ | 
|---|
| 111 | 7 5 8\n\ | 
|---|
| 112 | 8 7 9\n\ | 
|---|
| 113 | 9 8 10\n\ | 
|---|
| 114 | 10 9\n"; | 
|---|
| 115 |  | 
|---|
| 116 | // set up and tear down | 
|---|
| 117 | void AdjacencyListTest::setUp() | 
|---|
| 118 | { | 
|---|
| 119 | // failing asserts should be thrown | 
|---|
| 120 | ASSERT_DO(Assert::Throw); | 
|---|
| 121 |  | 
|---|
| 122 | const element *hydrogen = World::getInstance().getPeriode()->FindElement(1); | 
|---|
| 123 | CPPUNIT_ASSERT(hydrogen != NULL); | 
|---|
| 124 |  | 
|---|
| 125 | TestMolecule = World::getInstance().createMolecule(); | 
|---|
| 126 | CPPUNIT_ASSERT(TestMolecule != NULL); | 
|---|
| 127 | for(int i=0;i<ATOM_COUNT;++i){ | 
|---|
| 128 | atom *_atom = World::getInstance().createAtom(); | 
|---|
| 129 | CPPUNIT_ASSERT(_atom != NULL); | 
|---|
| 130 | _atom->setType(hydrogen); | 
|---|
| 131 | TestMolecule->AddAtom(_atom); | 
|---|
| 132 | atoms.push_back(_atom); | 
|---|
| 133 | atomIds.push_back(_atom->getId()); | 
|---|
| 134 | } | 
|---|
| 135 | CPPUNIT_ASSERT_EQUAL( (size_t)ATOM_COUNT, atoms.size()); | 
|---|
| 136 | CPPUNIT_ASSERT_EQUAL( (size_t)ATOM_COUNT, atomIds.size()); | 
|---|
| 137 | // create linear chain | 
|---|
| 138 | for(int i=0;i<ATOM_COUNT-1;++i) | 
|---|
| 139 | atoms[i]->addBond(WorldTime::getTime(), atoms[i+1]); | 
|---|
| 140 |  | 
|---|
| 141 | // create map as it should be | 
|---|
| 142 | for(int i=0;i<ATOM_COUNT;++i) { | 
|---|
| 143 | if (i != 0) // first has only one bond | 
|---|
| 144 | comparisonMap.insert( std::make_pair(atomIds[i], atomIds[i-1]) ); | 
|---|
| 145 | if (i != ATOM_COUNT-1) // last has only one bond | 
|---|
| 146 | comparisonMap.insert( std::make_pair(atomIds[i], atomIds[i+1]) ); | 
|---|
| 147 | } | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | void AdjacencyListTest::tearDown() | 
|---|
| 151 | { | 
|---|
| 152 | comparisonMap.clear(); | 
|---|
| 153 |  | 
|---|
| 154 | // destroy molecule and contained atoms | 
|---|
| 155 | TestMolecule->removeAtomsinMolecule(); | 
|---|
| 156 | World::getInstance().destroyMolecule(TestMolecule); | 
|---|
| 157 | // destroy World | 
|---|
| 158 | World::purgeInstance(); | 
|---|
| 159 | //  logger::purgeInstance(); | 
|---|
| 160 | //  errorLogger::purgeInstance(); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | /** Unit tests for AdjacencyList::CreateMap(). | 
|---|
| 164 | * | 
|---|
| 165 | */ | 
|---|
| 166 | void AdjacencyListTest::CreateMapTest() | 
|---|
| 167 | { | 
|---|
| 168 | std::stringstream input; | 
|---|
| 169 | AdjacencyList FileAdjacency(input); | 
|---|
| 170 | AdjacencyList WorldAdjacency(atomIds); | 
|---|
| 171 |  | 
|---|
| 172 | // check size (it's 8*2 + 2*1 = 18 keys) | 
|---|
| 173 | CPPUNIT_ASSERT_EQUAL( (size_t)18, WorldAdjacency.atombondmap.size() ); | 
|---|
| 174 | CPPUNIT_ASSERT_EQUAL( (size_t)0, FileAdjacency.atombondmap.size() ); | 
|---|
| 175 |  | 
|---|
| 176 | // check equality | 
|---|
| 177 | CPPUNIT_ASSERT( comparisonMap.size() == WorldAdjacency.atombondmap.size() ); | 
|---|
| 178 | //  std::cout << "comparisonMap: " << comparisonMap << std::endl; | 
|---|
| 179 | //  std::cout << "WorldAdjacency.atombondmap: " << WorldAdjacency.atombondmap << std::endl; | 
|---|
| 180 | CPPUNIT_ASSERT( comparisonMap == WorldAdjacency.atombondmap ); | 
|---|
| 181 |  | 
|---|
| 182 | // check non-equality: more | 
|---|
| 183 | comparisonMap.insert( std::make_pair( (atomId_t)10, (atomId_t)8) ); | 
|---|
| 184 | CPPUNIT_ASSERT( comparisonMap != WorldAdjacency.atombondmap ); | 
|---|
| 185 | comparisonMap.erase((atomId_t)10); | 
|---|
| 186 |  | 
|---|
| 187 | // check non-equality: less | 
|---|
| 188 | comparisonMap.erase((atomId_t)9); | 
|---|
| 189 | CPPUNIT_ASSERT( comparisonMap != WorldAdjacency.atombondmap ); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | /** Unit tests for AdjacencyList::ParseIntoMap(). | 
|---|
| 193 | * | 
|---|
| 194 | */ | 
|---|
| 195 | void AdjacencyListTest::ParseIntoMapTest() | 
|---|
| 196 | { | 
|---|
| 197 | std::stringstream input(adjacencyfile); | 
|---|
| 198 | AdjacencyList FileAdjacency(input); | 
|---|
| 199 | AdjacencyList WorldAdjacency; | 
|---|
| 200 |  | 
|---|
| 201 | // check size (it's 8*2 + 2*1 = 18 keys) | 
|---|
| 202 | CPPUNIT_ASSERT_EQUAL( (size_t)0, WorldAdjacency.atombondmap.size() ); | 
|---|
| 203 | CPPUNIT_ASSERT_EQUAL( (size_t)18, FileAdjacency.atombondmap.size() ); | 
|---|
| 204 |  | 
|---|
| 205 | // check equality | 
|---|
| 206 | CPPUNIT_ASSERT( comparisonMap.size() == FileAdjacency.atombondmap.size() ); | 
|---|
| 207 | CPPUNIT_ASSERT( comparisonMap == FileAdjacency.atombondmap ); | 
|---|
| 208 |  | 
|---|
| 209 | // check non-equality: more | 
|---|
| 210 | comparisonMap.insert( std::make_pair( (atomId_t)10, (atomId_t)8) ); | 
|---|
| 211 | CPPUNIT_ASSERT( comparisonMap != FileAdjacency.atombondmap ); | 
|---|
| 212 | comparisonMap.erase((atomId_t)10); | 
|---|
| 213 |  | 
|---|
| 214 | // check non-equality: less | 
|---|
| 215 | comparisonMap.erase((atomId_t)9); | 
|---|
| 216 | CPPUNIT_ASSERT( comparisonMap != FileAdjacency.atombondmap ); | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 | /** Unit tests for AdjacencyList::StoreToFile(). | 
|---|
| 220 | * | 
|---|
| 221 | */ | 
|---|
| 222 | void AdjacencyListTest::StoreToFileTest() | 
|---|
| 223 | { | 
|---|
| 224 | std::stringstream input(adjacencyfile); | 
|---|
| 225 | AdjacencyList FileAdjacency(input); | 
|---|
| 226 |  | 
|---|
| 227 | // check size (it's 8*2 + 2*1 = 18 keys) | 
|---|
| 228 | CPPUNIT_ASSERT_EQUAL( (size_t)18, FileAdjacency.atombondmap.size() ); | 
|---|
| 229 |  | 
|---|
| 230 | // store to file | 
|---|
| 231 | std::stringstream output; | 
|---|
| 232 | FileAdjacency.StoreToFile(output); | 
|---|
| 233 | CPPUNIT_ASSERT_EQUAL( input.str(), output.str() ); | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | /** Unit tests for AdjacencyList::operator<(). | 
|---|
| 237 | * | 
|---|
| 238 | */ | 
|---|
| 239 | void AdjacencyListTest::operatorGreaterLessTest() | 
|---|
| 240 | { | 
|---|
| 241 | std::stringstream input(adjacencyfile); | 
|---|
| 242 | AdjacencyList FileAdjacency(input); | 
|---|
| 243 | AdjacencyList WorldAdjacency; | 
|---|
| 244 |  | 
|---|
| 245 | // assert empty set is subset of some filled set (empty sets should always return true) | 
|---|
| 246 | CPPUNIT_ASSERT( FileAdjacency.atombondmap.size() != WorldAdjacency.atombondmap.size() ); | 
|---|
| 247 | CPPUNIT_ASSERT( FileAdjacency.atombondmap != WorldAdjacency.atombondmap ); | 
|---|
| 248 | CPPUNIT_ASSERT( WorldAdjacency < FileAdjacency ); | 
|---|
| 249 | CPPUNIT_ASSERT( !(WorldAdjacency > FileAdjacency) ); | 
|---|
| 250 |  | 
|---|
| 251 | // parse in external map | 
|---|
| 252 | WorldAdjacency.CreateMap(atomIds); | 
|---|
| 253 |  | 
|---|
| 254 | // assert equality after parsing | 
|---|
| 255 | CPPUNIT_ASSERT_EQUAL( FileAdjacency.atombondmap.size(), WorldAdjacency.atombondmap.size() ); | 
|---|
| 256 | CPPUNIT_ASSERT( FileAdjacency.atombondmap == WorldAdjacency.atombondmap ); | 
|---|
| 257 | CPPUNIT_ASSERT( WorldAdjacency < FileAdjacency ); | 
|---|
| 258 | CPPUNIT_ASSERT( WorldAdjacency > FileAdjacency ); | 
|---|
| 259 |  | 
|---|
| 260 | // remove one entry from the world | 
|---|
| 261 | WorldAdjacency.atombondmap.erase((atomId_t)9); | 
|---|
| 262 | CPPUNIT_ASSERT( WorldAdjacency < FileAdjacency ); | 
|---|
| 263 | CPPUNIT_ASSERT( !(WorldAdjacency > FileAdjacency) ); | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | /** Unit tests for AdjacencyList::operator==(). | 
|---|
| 267 | * | 
|---|
| 268 | */ | 
|---|
| 269 | void AdjacencyListTest::operatorEqualTest() | 
|---|
| 270 | { | 
|---|
| 271 | std::stringstream input(adjacencyfile); | 
|---|
| 272 | AdjacencyList FileAdjacency(input); | 
|---|
| 273 | AdjacencyList WorldAdjacency; | 
|---|
| 274 |  | 
|---|
| 275 | // assert equality before parsing (empty sets should always return true) | 
|---|
| 276 | CPPUNIT_ASSERT( FileAdjacency.atombondmap.size() != WorldAdjacency.atombondmap.size() ); | 
|---|
| 277 | CPPUNIT_ASSERT( FileAdjacency.atombondmap != WorldAdjacency.atombondmap ); | 
|---|
| 278 | CPPUNIT_ASSERT( WorldAdjacency != FileAdjacency ); | 
|---|
| 279 |  | 
|---|
| 280 | // parse in external map | 
|---|
| 281 | WorldAdjacency.CreateMap(atomIds); | 
|---|
| 282 |  | 
|---|
| 283 | // assert equality after parsing | 
|---|
| 284 | CPPUNIT_ASSERT_EQUAL( FileAdjacency.atombondmap.size(), WorldAdjacency.atombondmap.size() ); | 
|---|
| 285 | CPPUNIT_ASSERT( FileAdjacency.atombondmap == WorldAdjacency.atombondmap ); | 
|---|
| 286 | CPPUNIT_ASSERT( WorldAdjacency == FileAdjacency ); | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | /** Unit tests for AdjacencyList::operator()(). | 
|---|
| 290 | * | 
|---|
| 291 | */ | 
|---|
| 292 | void AdjacencyListTest::CheckAgainstSubsetTest() | 
|---|
| 293 | { | 
|---|
| 294 | AdjacencyList WorldAdjacency(atomIds); | 
|---|
| 295 | { | 
|---|
| 296 | // parse right | 
|---|
| 297 | std::stringstream input(adjacencyfile); | 
|---|
| 298 | AdjacencyList FileAdjacency(input); | 
|---|
| 299 | CPPUNIT_ASSERT( (WorldAdjacency > FileAdjacency) && (WorldAdjacency < FileAdjacency) ); | 
|---|
| 300 | } | 
|---|
| 301 | { | 
|---|
| 302 | // parse wrong1 (more atoms in the world than in file, hence wrong) | 
|---|
| 303 | std::stringstream input(wrongadjacencyfile1); | 
|---|
| 304 | AdjacencyList FileAdjacency(input); | 
|---|
| 305 | CPPUNIT_ASSERT( !(WorldAdjacency < FileAdjacency) && (WorldAdjacency > FileAdjacency) ); | 
|---|
| 306 | } | 
|---|
| 307 | { | 
|---|
| 308 | // remove third atom (index starts at 0) and test for equality then | 
|---|
| 309 | std::vector<atomId_t> validids; | 
|---|
| 310 | std::remove_copy_if(atomIds.begin(), atomIds.end(), std::back_inserter(validids), boost::lambda::_1 == (atomId_t)2); | 
|---|
| 311 | CPPUNIT_ASSERT_EQUAL( (size_t)ATOM_COUNT-1, validids.size() ); | 
|---|
| 312 | AdjacencyList RestrictedWorldAdjacency(validids); | 
|---|
| 313 | // parse wrong1 (more atoms in the world than in file, hence wrong) | 
|---|
| 314 | std::stringstream input(wrongadjacencyfile1); | 
|---|
| 315 | AdjacencyList FileAdjacency(input); | 
|---|
| 316 | CPPUNIT_ASSERT( RestrictedWorldAdjacency == FileAdjacency ); | 
|---|
| 317 | } | 
|---|
| 318 | { | 
|---|
| 319 | // parse wrong2 (there is no atom 10, but present in file. hence true) | 
|---|
| 320 | std::stringstream input(wrongadjacencyfile2); | 
|---|
| 321 | AdjacencyList FileAdjacency(input); | 
|---|
| 322 | CPPUNIT_ASSERT( !(WorldAdjacency > FileAdjacency) && (WorldAdjacency < FileAdjacency) ); | 
|---|
| 323 | } | 
|---|
| 324 | { | 
|---|
| 325 | // parse wrong3 (6 is skipped in connection, hence neither is subset) | 
|---|
| 326 | std::stringstream input(wrongadjacencyfile3); | 
|---|
| 327 | AdjacencyList FileAdjacency(input); | 
|---|
| 328 | //    WorldAdjacency.StoreToFile((std::ostream &)std::cout); | 
|---|
| 329 | //    FileAdjacency.StoreToFile((std::ostream &)std::cout); | 
|---|
| 330 | CPPUNIT_ASSERT( WorldAdjacency != FileAdjacency ); | 
|---|
| 331 | } | 
|---|
| 332 | } | 
|---|