| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [94d5ac6] | 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/>. | 
|---|
| [bcf653] | 21 | */ | 
|---|
|  | 22 |  | 
|---|
| [b70721] | 23 | /* | 
|---|
| [f844ef] | 24 | * BondGraphUnitTest.cpp | 
|---|
| [b70721] | 25 | * | 
|---|
|  | 26 | *  Created on: Oct 29, 2009 | 
|---|
|  | 27 | *      Author: heber | 
|---|
|  | 28 | */ | 
|---|
|  | 29 |  | 
|---|
| [bf3817] | 30 | // include config.h | 
|---|
|  | 31 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 32 | #include <config.h> | 
|---|
|  | 33 | #endif | 
|---|
|  | 34 |  | 
|---|
| [b70721] | 35 | using namespace std; | 
|---|
|  | 36 |  | 
|---|
|  | 37 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 38 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 39 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 40 |  | 
|---|
| [1ac24b] | 41 | // include headers that implement a archive in simple text format | 
|---|
|  | 42 | #include <boost/archive/text_oarchive.hpp> | 
|---|
|  | 43 | #include <boost/archive/text_iarchive.hpp> | 
|---|
|  | 44 |  | 
|---|
| [b70721] | 45 | #include <iostream> | 
|---|
|  | 46 | #include <stdio.h> | 
|---|
| [49e1ae] | 47 | #include <cstring> | 
|---|
| [b70721] | 48 |  | 
|---|
| [ad011c] | 49 | #include "CodePatterns/Assert.hpp" | 
|---|
| [4eb4fe] | 50 |  | 
|---|
| [6f0841] | 51 | #include "Atom/atom.hpp" | 
|---|
| [129204] | 52 | #include "Bond/bond.hpp" | 
|---|
| [ad011c] | 53 | #include "CodePatterns/Log.hpp" | 
|---|
| [3bdb6d] | 54 | #include "Element/element.hpp" | 
|---|
| [129204] | 55 | #include "Graph/BondGraph.hpp" | 
|---|
| [b70721] | 56 | #include "molecule.hpp" | 
|---|
| [3bdb6d] | 57 | #include "Element/periodentafel.hpp" | 
|---|
| [e6fdbe] | 58 | #include "World.hpp" | 
|---|
| [073a9e4] | 59 | #include "WorldTime.hpp" | 
|---|
| [b70721] | 60 |  | 
|---|
| [f844ef] | 61 | #include "BondGraphUnitTest.hpp" | 
|---|
|  | 62 |  | 
|---|
| [9b6b2f] | 63 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 64 | #include "UnitTestMain.hpp" | 
|---|
|  | 65 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| [b70721] | 66 |  | 
|---|
|  | 67 | /********************************************** Test classes **************************************/ | 
|---|
|  | 68 |  | 
|---|
|  | 69 | // Registers the fixture into the 'registry' | 
|---|
|  | 70 | CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest ); | 
|---|
|  | 71 |  | 
|---|
|  | 72 |  | 
|---|
|  | 73 | void BondGraphTest::setUp() | 
|---|
|  | 74 | { | 
|---|
|  | 75 | atom *Walker = NULL; | 
|---|
|  | 76 |  | 
|---|
|  | 77 | // construct element | 
|---|
| [4eb4fe] | 78 | hydrogen = World::getInstance().getPeriode()->FindElement(1); | 
|---|
|  | 79 | carbon = World::getInstance().getPeriode()->FindElement(6); | 
|---|
|  | 80 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen"); | 
|---|
|  | 81 | CPPUNIT_ASSERT(carbon != NULL && "could not find element carbon"); | 
|---|
| [b70721] | 82 |  | 
|---|
|  | 83 | // construct molecule (tetraeder of hydrogens) | 
|---|
| [23b547] | 84 | TestMolecule = World::getInstance().createMolecule(); | 
|---|
| [4eb4fe] | 85 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule"); | 
|---|
| [23b547] | 86 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 87 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 88 | Walker->setType(carbon); | 
|---|
|  | 89 | Walker->setPosition(Vector(1., 0., 1. )); | 
|---|
| [b70721] | 90 | TestMolecule->AddAtom(Walker); | 
|---|
| [8cbb97] | 91 |  | 
|---|
| [23b547] | 92 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 93 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 94 | Walker->setType(carbon); | 
|---|
|  | 95 | Walker->setPosition(Vector(0., 1., 1. )); | 
|---|
| [b70721] | 96 | TestMolecule->AddAtom(Walker); | 
|---|
| [8cbb97] | 97 |  | 
|---|
| [23b547] | 98 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 99 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 100 | Walker->setType(carbon); | 
|---|
|  | 101 | Walker->setPosition(Vector(1., 1., 0. )); | 
|---|
| [b70721] | 102 | TestMolecule->AddAtom(Walker); | 
|---|
| [8cbb97] | 103 |  | 
|---|
| [23b547] | 104 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 105 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 106 | Walker->setType(carbon); | 
|---|
|  | 107 | Walker->setPosition(Vector(0., 0., 0. )); | 
|---|
| [b70721] | 108 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 109 |  | 
|---|
|  | 110 | // check that TestMolecule was correctly constructed | 
|---|
| [ea7176] | 111 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 ); | 
|---|
| [b70721] | 112 |  | 
|---|
| [4e855e] | 113 | // create stream with table | 
|---|
| [4eb4fe] | 114 | test << ".\tH\tHe\tLi\tBe\tB\tC\n"; | 
|---|
|  | 115 | test << "H\t1.\t1.\t1.\t1.\t1.\t1.2\n"; | 
|---|
|  | 116 | test << "He\t1.\t1.\t1.\t1.\t1.\t1.\n"; | 
|---|
|  | 117 | test << "Li\t1.\t1.\t1.\t1.\t1.\t1.\n"; | 
|---|
|  | 118 | test << "Be\t1.\t1.\t1.\t1.\t1.\t1.\n"; | 
|---|
|  | 119 | test << "B\t1.\t1.\t1.\t1.\t1.\t1.\n"; | 
|---|
|  | 120 | test << "C\t1.2\t1.\t1.\t1.\t1.\t1.5\n"; | 
|---|
| [4e855e] | 121 | // created bad stream (i.e. non-present file) | 
|---|
|  | 122 | dummy.setstate(ios::eofbit); | 
|---|
|  | 123 | CPPUNIT_ASSERT(dummy.eof()); | 
|---|
| [b70721] | 124 | BG = new BondGraph(true); | 
|---|
| [4eb4fe] | 125 | CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph"); | 
|---|
| [b70721] | 126 | }; | 
|---|
|  | 127 |  | 
|---|
|  | 128 |  | 
|---|
|  | 129 | void BondGraphTest::tearDown() | 
|---|
|  | 130 | { | 
|---|
|  | 131 | // remove the file | 
|---|
|  | 132 | delete(BG); | 
|---|
|  | 133 |  | 
|---|
|  | 134 | // remove molecule | 
|---|
| [23b547] | 135 | World::getInstance().destroyMolecule(TestMolecule); | 
|---|
| [a1510d] | 136 | // note that all the atoms, molecules, the tafel and the elements | 
|---|
|  | 137 | // are all cleaned when the world is destroyed | 
|---|
| [23b547] | 138 | World::purgeInstance(); | 
|---|
| [e6fdbe] | 139 | logger::purgeInstance(); | 
|---|
| [b70721] | 140 | }; | 
|---|
|  | 141 |  | 
|---|
| [9879f6] | 142 | /** Tests whether setup worked. | 
|---|
|  | 143 | */ | 
|---|
|  | 144 | void BondGraphTest::SetupTest() | 
|---|
|  | 145 | { | 
|---|
|  | 146 | CPPUNIT_ASSERT_EQUAL (false, TestMolecule->empty()); | 
|---|
|  | 147 | CPPUNIT_ASSERT_EQUAL ((size_t)4, TestMolecule->size()); | 
|---|
|  | 148 | }; | 
|---|
|  | 149 |  | 
|---|
| [b70721] | 150 | /** UnitTest for BondGraphTest::LoadBondLengthTable(). | 
|---|
|  | 151 | */ | 
|---|
|  | 152 | void BondGraphTest::LoadTableTest() | 
|---|
|  | 153 | { | 
|---|
| [4e855e] | 154 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) ); | 
|---|
| [b70721] | 155 | CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) ); | 
|---|
| [4eb4fe] | 156 | CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,5) ); | 
|---|
|  | 157 | CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(5,5) ); | 
|---|
| [b70721] | 158 | }; | 
|---|
|  | 159 |  | 
|---|
| [3738f0] | 160 | /** UnitTest for BondGraphTest::CreateAdjacency(). | 
|---|
| [b70721] | 161 | */ | 
|---|
| [e5ad5c] | 162 | void BondGraphTest::ConstructGraphFromTableTest() | 
|---|
| [b70721] | 163 | { | 
|---|
| [9879f6] | 164 | molecule::iterator Walker = TestMolecule->begin(); | 
|---|
|  | 165 | molecule::iterator Runner = TestMolecule->begin(); | 
|---|
|  | 166 | Runner++; | 
|---|
| [4e855e] | 167 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) ); | 
|---|
| [9317be] | 168 | World::AtomComposite Set = TestMolecule->getAtomSet(); | 
|---|
| [3738f0] | 169 | BG->CreateAdjacency(Set); | 
|---|
|  | 170 | CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0); | 
|---|
| [073a9e4] | 171 | CPPUNIT_ASSERT_EQUAL( true , (*Walker)->IsBondedTo(WorldTime::getTime(), (*Runner)) ); | 
|---|
| [b70721] | 172 | }; | 
|---|
|  | 173 |  | 
|---|
| [3738f0] | 174 | /** UnitTest for BondGraphTest::CreateAdjacency(). | 
|---|
| [e5ad5c] | 175 | */ | 
|---|
|  | 176 | void BondGraphTest::ConstructGraphFromCovalentRadiiTest() | 
|---|
|  | 177 | { | 
|---|
| [a7b761b] | 178 |  | 
|---|
| [4e855e] | 179 | CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(dummy) ); | 
|---|
| [9317be] | 180 | World::AtomComposite Set = TestMolecule->getAtomSet(); | 
|---|
| [3738f0] | 181 | BG->CreateAdjacency(Set); | 
|---|
|  | 182 | CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0); | 
|---|
| [a7b761b] | 183 |  | 
|---|
|  | 184 | // this cannot be assured using dynamic IDs | 
|---|
|  | 185 | //CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) ); | 
|---|
| [e5ad5c] | 186 | }; | 
|---|
|  | 187 |  | 
|---|
| [9b6663] | 188 | /** UnitTest for operator==() | 
|---|
|  | 189 | */ | 
|---|
|  | 190 | void BondGraphTest::EqualityTest() | 
|---|
|  | 191 | { | 
|---|
|  | 192 | // compare to self | 
|---|
|  | 193 | CPPUNIT_ASSERT( *BG == *BG ); | 
|---|
|  | 194 |  | 
|---|
|  | 195 | // create other instance | 
|---|
|  | 196 | BondGraph *BG2 = new BondGraph(true); | 
|---|
|  | 197 | CPPUNIT_ASSERT( *BG == *BG2 ); | 
|---|
|  | 198 | BG2->IsAngstroem = false; | 
|---|
|  | 199 | CPPUNIT_ASSERT( *BG != *BG2 ); | 
|---|
|  | 200 | delete BG2; | 
|---|
|  | 201 | }; | 
|---|
|  | 202 |  | 
|---|
| [1ac24b] | 203 | /** UnitTest for serialization | 
|---|
|  | 204 | */ | 
|---|
|  | 205 | void BondGraphTest::SerializationTest() | 
|---|
|  | 206 | { | 
|---|
|  | 207 | // write element to stream | 
|---|
|  | 208 | std::stringstream stream; | 
|---|
|  | 209 | boost::archive::text_oarchive oa(stream); | 
|---|
|  | 210 | oa << BG; | 
|---|
|  | 211 |  | 
|---|
|  | 212 | std::cout << "Contents of archive is " << stream.str() << std::endl; | 
|---|
|  | 213 |  | 
|---|
|  | 214 | // create and open an archive for input | 
|---|
|  | 215 | boost::archive::text_iarchive ia(stream); | 
|---|
|  | 216 | // read class state from archive | 
|---|
|  | 217 | BondGraph *BG2; | 
|---|
|  | 218 |  | 
|---|
|  | 219 | ia >> BG2; | 
|---|
|  | 220 |  | 
|---|
|  | 221 | CPPUNIT_ASSERT (*BG == *BG2); | 
|---|
|  | 222 |  | 
|---|
|  | 223 | delete BG2; | 
|---|
|  | 224 | }; | 
|---|
|  | 225 |  | 
|---|