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