| 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 |  * CheckAgainstAdjacencyFileUnitTest.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 "CheckAgainstAdjacencyFileUnitTest.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 38 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 39 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include <iostream>
 | 
|---|
| 42 | #include <string>
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 45 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 46 | 
 | 
|---|
| 47 | #include "Atom/atom.hpp"
 | 
|---|
| 48 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| 49 | #include "Element/element.hpp"
 | 
|---|
| 50 | #include "Element/periodentafel.hpp"
 | 
|---|
| 51 | #include "Graph/CheckAgainstAdjacencyFile.hpp"
 | 
|---|
| 52 | #include "molecule.hpp"
 | 
|---|
| 53 | #include "World.hpp"
 | 
|---|
| 54 | #include "WorldTime.hpp"
 | 
|---|
| 55 | 
 | 
|---|
| 56 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 57 | #include "UnitTestMain.hpp"
 | 
|---|
| 58 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 59 | 
 | 
|---|
| 60 | /********************************************** Test classes **************************************/
 | 
|---|
| 61 | // Registers the fixture into the 'registry'
 | 
|---|
| 62 | CPPUNIT_TEST_SUITE_REGISTRATION( CheckAgainstAdjacencyFileTest );
 | 
|---|
| 63 | 
 | 
|---|
| 64 | static std::string adjacencyfile ="\
 | 
|---|
| 65 | 1 2\n\
 | 
|---|
| 66 | 2 1 3\n\
 | 
|---|
| 67 | 3 2 4\n\
 | 
|---|
| 68 | 4 3 5\n\
 | 
|---|
| 69 | 5 4 6\n\
 | 
|---|
| 70 | 6 5 7\n\
 | 
|---|
| 71 | 7 6 8\n\
 | 
|---|
| 72 | 8 7 9\n\
 | 
|---|
| 73 | 9 8 10\n\
 | 
|---|
| 74 | 10 9\n";
 | 
|---|
| 75 | 
 | 
|---|
| 76 | static std::string wrongadjacencyfile1 ="\
 | 
|---|
| 77 | 1 2\n\
 | 
|---|
| 78 | 2 1 3\n\
 | 
|---|
| 79 | 4 3 5\n\
 | 
|---|
| 80 | 5 4 6\n\
 | 
|---|
| 81 | 6 5 7\n\
 | 
|---|
| 82 | 7 6 8\n\
 | 
|---|
| 83 | 8 7 9\n\
 | 
|---|
| 84 | 9 8 10\n\
 | 
|---|
| 85 | 10 9\n";
 | 
|---|
| 86 | 
 | 
|---|
| 87 | static std::string wrongadjacencyfile2 ="\
 | 
|---|
| 88 | 1 2\n\
 | 
|---|
| 89 | 2 1 3\n\
 | 
|---|
| 90 | 3 2 4\n\
 | 
|---|
| 91 | 4 3 5\n\
 | 
|---|
| 92 | 5 4 6\n\
 | 
|---|
| 93 | 6 5 7\n\
 | 
|---|
| 94 | 7 6 8\n\
 | 
|---|
| 95 | 8 7 9\n\
 | 
|---|
| 96 | 9 8 10\n\
 | 
|---|
| 97 | 10 9 11\n\
 | 
|---|
| 98 | 11 10";
 | 
|---|
| 99 | 
 | 
|---|
| 100 | // set up and tear down
 | 
|---|
| 101 | void CheckAgainstAdjacencyFileTest::setUp()
 | 
|---|
| 102 | {
 | 
|---|
| 103 |   const element *hydrogen = World::getInstance().getPeriode()->FindElement(1);
 | 
|---|
| 104 |   CPPUNIT_ASSERT(hydrogen != NULL);
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   // failing asserts should be thrown
 | 
|---|
| 107 |   ASSERT_DO(Assert::Throw);
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   TestMolecule = World::getInstance().createMolecule();
 | 
|---|
| 110 |   CPPUNIT_ASSERT(TestMolecule != NULL);
 | 
|---|
| 111 |   for(int i=0;i<ATOM_COUNT;++i){
 | 
|---|
| 112 |     atoms[i]= World::getInstance().createAtom();
 | 
|---|
| 113 |     CPPUNIT_ASSERT(atoms[i] != NULL);
 | 
|---|
| 114 |     atoms[i]->setType(hydrogen);
 | 
|---|
| 115 |     TestMolecule->AddAtom(atoms[i]);
 | 
|---|
| 116 |     atomIds[i]= atoms[i]->getId();
 | 
|---|
| 117 |   }
 | 
|---|
| 118 |   // create linear chain
 | 
|---|
| 119 |   for(int i=0;i<ATOM_COUNT-1;++i){
 | 
|---|
| 120 |     atoms[i]->addBond(WorldTime::getTime(), atoms[i+1]);
 | 
|---|
| 121 |   }
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   // create map as it should be
 | 
|---|
| 124 |   for(int i=0;i<ATOM_COUNT;++i) {
 | 
|---|
| 125 |     if (i != 0) // first has only one bond
 | 
|---|
| 126 |       comparisonMap.insert( std::make_pair(atomIds[i], atomIds[i-1]) );
 | 
|---|
| 127 |     if (i != ATOM_COUNT-1) // last has only one bond
 | 
|---|
| 128 |       comparisonMap.insert( std::make_pair(atomIds[i], atomIds[i+1]) );
 | 
|---|
| 129 |   }
 | 
|---|
| 130 | }
 | 
|---|
| 131 | 
 | 
|---|
| 132 | void CheckAgainstAdjacencyFileTest::tearDown()
 | 
|---|
| 133 | {
 | 
|---|
| 134 |   comparisonMap.clear();
 | 
|---|
| 135 | 
 | 
|---|
| 136 |   // destroy molecule and contained atoms
 | 
|---|
| 137 |   TestMolecule->removeAtomsinMolecule();
 | 
|---|
| 138 |   World::getInstance().destroyMolecule(TestMolecule);
 | 
|---|
| 139 |   // destroy World
 | 
|---|
| 140 |   World::purgeInstance();
 | 
|---|
| 141 | //  logger::purgeInstance();
 | 
|---|
| 142 | //  errorLogger::purgeInstance();
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | /** Unit tests for CheckAgainstAdjacencyFile::CreateInternalMap().
 | 
|---|
| 146 |  *
 | 
|---|
| 147 |  */
 | 
|---|
| 148 | void CheckAgainstAdjacencyFileTest::CreateInternalMapTest()
 | 
|---|
| 149 | {
 | 
|---|
| 150 |   World::getInstance().selectAllAtoms(AllAtoms());
 | 
|---|
| 151 |   CheckAgainstAdjacencyFile fileChecker(World::getInstance().beginAtomSelection(), World::getInstance().endAtomSelection());
 | 
|---|
| 152 | 
 | 
|---|
| 153 |   // check size (it's 8*2 + 2*1 = 18 keys)
 | 
|---|
| 154 |   CPPUNIT_ASSERT_EQUAL( (size_t)18, fileChecker.InternalAtomBondMap.size() );
 | 
|---|
| 155 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, fileChecker.ExternalAtomBondMap.size() );
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   // check equality
 | 
|---|
| 158 |   CPPUNIT_ASSERT( comparisonMap.size() == fileChecker.InternalAtomBondMap.size() );
 | 
|---|
| 159 | //  std::cout << "comparisonMap: " << comparisonMap << std::endl;
 | 
|---|
| 160 | //  std::cout << "fileChecker.InternalAtomBondMap: " << fileChecker.InternalAtomBondMap << std::endl;
 | 
|---|
| 161 |   CPPUNIT_ASSERT( comparisonMap == fileChecker.InternalAtomBondMap );
 | 
|---|
| 162 | 
 | 
|---|
| 163 |   // check non-equality: more
 | 
|---|
| 164 |   comparisonMap.insert( std::make_pair( (atomId_t)10, (atomId_t)8) );
 | 
|---|
| 165 |   CPPUNIT_ASSERT( comparisonMap != fileChecker.InternalAtomBondMap );
 | 
|---|
| 166 |   comparisonMap.erase((atomId_t)10);
 | 
|---|
| 167 | 
 | 
|---|
| 168 |   // check non-equality: less
 | 
|---|
| 169 |   comparisonMap.erase((atomId_t)9);
 | 
|---|
| 170 |   CPPUNIT_ASSERT( comparisonMap != fileChecker.InternalAtomBondMap );
 | 
|---|
| 171 | }
 | 
|---|
| 172 | 
 | 
|---|
| 173 | /** Unit tests for CheckAgainstAdjacencyFile::ParseInExternalMap().
 | 
|---|
| 174 |  *
 | 
|---|
| 175 |  */
 | 
|---|
| 176 | void CheckAgainstAdjacencyFileTest::ParseInExternalMapTest()
 | 
|---|
| 177 | {
 | 
|---|
| 178 |   std::stringstream input(adjacencyfile);
 | 
|---|
| 179 |   World::getInstance().selectAllAtoms(NoAtoms());
 | 
|---|
| 180 |   CheckAgainstAdjacencyFile fileChecker(World::getInstance().beginAtomSelection(), World::getInstance().endAtomSelection());
 | 
|---|
| 181 |   fileChecker.ParseInExternalMap(input);
 | 
|---|
| 182 | 
 | 
|---|
| 183 |   // check size (it's 8*2 + 2*1 = 18 keys)
 | 
|---|
| 184 |   CPPUNIT_ASSERT_EQUAL( (size_t)0, fileChecker.InternalAtomBondMap.size() );
 | 
|---|
| 185 |   CPPUNIT_ASSERT_EQUAL( (size_t)18, fileChecker.ExternalAtomBondMap.size() );
 | 
|---|
| 186 | 
 | 
|---|
| 187 |   // check equality
 | 
|---|
| 188 |   CPPUNIT_ASSERT( comparisonMap.size() == fileChecker.ExternalAtomBondMap.size() );
 | 
|---|
| 189 |   CPPUNIT_ASSERT( comparisonMap == fileChecker.ExternalAtomBondMap );
 | 
|---|
| 190 | 
 | 
|---|
| 191 |   // check non-equality: more
 | 
|---|
| 192 |   comparisonMap.insert( std::make_pair( (atomId_t)10, (atomId_t)8) );
 | 
|---|
| 193 |   CPPUNIT_ASSERT( comparisonMap != fileChecker.ExternalAtomBondMap );
 | 
|---|
| 194 |   comparisonMap.erase((atomId_t)10);
 | 
|---|
| 195 | 
 | 
|---|
| 196 |   // check non-equality: less
 | 
|---|
| 197 |   comparisonMap.erase((atomId_t)9);
 | 
|---|
| 198 |   CPPUNIT_ASSERT( comparisonMap != fileChecker.ExternalAtomBondMap );
 | 
|---|
| 199 | }
 | 
|---|
| 200 | 
 | 
|---|
| 201 | /** Unit tests for CheckAgainstAdjacencyFile::CompareInternalExternalMap().
 | 
|---|
| 202 |  *
 | 
|---|
| 203 |  */
 | 
|---|
| 204 | void CheckAgainstAdjacencyFileTest::CompareInternalExternalMapTest()
 | 
|---|
| 205 | {
 | 
|---|
| 206 |   World::getInstance().selectAllAtoms(AllAtoms());
 | 
|---|
| 207 |   CheckAgainstAdjacencyFile fileChecker(World::getInstance().beginAtomSelection(), World::getInstance().endAtomSelection());
 | 
|---|
| 208 | 
 | 
|---|
| 209 |   // assert non-equality before parsing
 | 
|---|
| 210 |   CPPUNIT_ASSERT( fileChecker.InternalAtomBondMap.size() != fileChecker.ExternalAtomBondMap.size() );
 | 
|---|
| 211 |   CPPUNIT_ASSERT( fileChecker.InternalAtomBondMap != fileChecker.ExternalAtomBondMap );
 | 
|---|
| 212 |   CPPUNIT_ASSERT( !fileChecker.CompareInternalExternalMap() );
 | 
|---|
| 213 | 
 | 
|---|
| 214 |   // parse
 | 
|---|
| 215 |   std::stringstream input(adjacencyfile);
 | 
|---|
| 216 |   fileChecker.ParseInExternalMap(input);
 | 
|---|
| 217 | 
 | 
|---|
| 218 |   // assert equality after parsing
 | 
|---|
| 219 |   CPPUNIT_ASSERT_EQUAL( fileChecker.InternalAtomBondMap.size(), fileChecker.ExternalAtomBondMap.size() );
 | 
|---|
| 220 |   CPPUNIT_ASSERT_EQUAL( fileChecker.InternalAtomBondMap, fileChecker.ExternalAtomBondMap );
 | 
|---|
| 221 |   CPPUNIT_ASSERT( fileChecker.CompareInternalExternalMap() );
 | 
|---|
| 222 | }
 | 
|---|
| 223 | 
 | 
|---|
| 224 | /** Unit tests for CheckAgainstAdjacencyFile::operator()().
 | 
|---|
| 225 |  *
 | 
|---|
| 226 |  */
 | 
|---|
| 227 | void CheckAgainstAdjacencyFileTest::operatorTest()
 | 
|---|
| 228 | {
 | 
|---|
| 229 |   World::getInstance().selectAllAtoms(AllAtoms());
 | 
|---|
| 230 |   CheckAgainstAdjacencyFile fileChecker(World::getInstance().beginAtomSelection(), World::getInstance().endAtomSelection());
 | 
|---|
| 231 |   {
 | 
|---|
| 232 |     // parse right
 | 
|---|
| 233 |     std::stringstream input(adjacencyfile);
 | 
|---|
| 234 |     CPPUNIT_ASSERT( fileChecker(input) );
 | 
|---|
| 235 |   }
 | 
|---|
| 236 |   {
 | 
|---|
| 237 |     // parse wrong1
 | 
|---|
| 238 |     std::stringstream input(wrongadjacencyfile1);
 | 
|---|
| 239 |     CPPUNIT_ASSERT( !fileChecker(input) );
 | 
|---|
| 240 |   }
 | 
|---|
| 241 |   {
 | 
|---|
| 242 |     // parse wrong2 (there is no atom 10)
 | 
|---|
| 243 |     std::stringstream input(wrongadjacencyfile2);
 | 
|---|
| 244 | #ifndef NDEBUG
 | 
|---|
| 245 |     std::cout << "The following Assert failures are intended and do not indicate a failure of the test." << std::endl;
 | 
|---|
| 246 |     CPPUNIT_ASSERT_THROW( fileChecker(input), Assert::AssertionFailure );
 | 
|---|
| 247 | #else
 | 
|---|
| 248 |     CPPUNIT_ASSERT( !fileChecker(input) );
 | 
|---|
| 249 | #endif
 | 
|---|
| 250 |   }
 | 
|---|
| 251 | }
 | 
|---|