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