| 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 |  * BondGraphUnitTest.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Oct 29, 2009
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | using namespace std;
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 38 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 39 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 40 | 
 | 
|---|
| 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 | 
 | 
|---|
| 45 | #include <iostream>
 | 
|---|
| 46 | #include <stdio.h>
 | 
|---|
| 47 | #include <cstring>
 | 
|---|
| 48 | 
 | 
|---|
| 49 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 50 | 
 | 
|---|
| 51 | #include "Atom/atom.hpp"
 | 
|---|
| 52 | #include "Bond/bond.hpp"
 | 
|---|
| 53 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 54 | #include "Element/element.hpp"
 | 
|---|
| 55 | #include "Graph/BondGraph.hpp"
 | 
|---|
| 56 | #include "molecule.hpp"
 | 
|---|
| 57 | #include "Element/periodentafel.hpp"
 | 
|---|
| 58 | #include "World.hpp"
 | 
|---|
| 59 | #include "WorldTime.hpp"
 | 
|---|
| 60 | 
 | 
|---|
| 61 | #include "BondGraphUnitTest.hpp"
 | 
|---|
| 62 | 
 | 
|---|
| 63 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 64 | #include "UnitTestMain.hpp"
 | 
|---|
| 65 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 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
 | 
|---|
| 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");
 | 
|---|
| 82 | 
 | 
|---|
| 83 |   // construct molecule (tetraeder of hydrogens)
 | 
|---|
| 84 |   TestMolecule = World::getInstance().createMolecule();
 | 
|---|
| 85 |   CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
 | 
|---|
| 86 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 87 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 88 |   Walker->setType(carbon);
 | 
|---|
| 89 |   Walker->setPosition(Vector(1., 0., 1. ));
 | 
|---|
| 90 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 93 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 94 |   Walker->setType(carbon);
 | 
|---|
| 95 |   Walker->setPosition(Vector(0., 1., 1. ));
 | 
|---|
| 96 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 99 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 100 |   Walker->setType(carbon);
 | 
|---|
| 101 |   Walker->setPosition(Vector(1., 1., 0. ));
 | 
|---|
| 102 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 105 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 106 |   Walker->setType(carbon);
 | 
|---|
| 107 |   Walker->setPosition(Vector(0., 0., 0. ));
 | 
|---|
| 108 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   // check that TestMolecule was correctly constructed
 | 
|---|
| 111 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 );
 | 
|---|
| 112 | 
 | 
|---|
| 113 |   // create stream with table
 | 
|---|
| 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";
 | 
|---|
| 121 |   // created bad stream (i.e. non-present file)
 | 
|---|
| 122 |   dummy.setstate(ios::eofbit);
 | 
|---|
| 123 |   CPPUNIT_ASSERT(dummy.eof());
 | 
|---|
| 124 |   BG = new BondGraph(true);
 | 
|---|
| 125 |   CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
 | 
|---|
| 126 | };
 | 
|---|
| 127 | 
 | 
|---|
| 128 | 
 | 
|---|
| 129 | void BondGraphTest::tearDown()
 | 
|---|
| 130 | {
 | 
|---|
| 131 |   // remove the file
 | 
|---|
| 132 |   delete(BG);
 | 
|---|
| 133 | 
 | 
|---|
| 134 |   // remove molecule
 | 
|---|
| 135 |   World::getInstance().destroyMolecule(TestMolecule);
 | 
|---|
| 136 |   // note that all the atoms, molecules, the tafel and the elements
 | 
|---|
| 137 |   // are all cleaned when the world is destroyed
 | 
|---|
| 138 |   World::purgeInstance();
 | 
|---|
| 139 |   logger::purgeInstance();
 | 
|---|
| 140 | };
 | 
|---|
| 141 | 
 | 
|---|
| 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 | 
 | 
|---|
| 150 | /** UnitTest for BondGraphTest::LoadBondLengthTable().
 | 
|---|
| 151 |  */
 | 
|---|
| 152 | void BondGraphTest::LoadTableTest()
 | 
|---|
| 153 | {
 | 
|---|
| 154 |   CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) );
 | 
|---|
| 155 |   CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
 | 
|---|
| 156 |   CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,5) );
 | 
|---|
| 157 |   CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(5,5) );
 | 
|---|
| 158 | };
 | 
|---|
| 159 | 
 | 
|---|
| 160 | /** UnitTest for BondGraphTest::CreateAdjacency().
 | 
|---|
| 161 |  */
 | 
|---|
| 162 | void BondGraphTest::ConstructGraphFromTableTest()
 | 
|---|
| 163 | {
 | 
|---|
| 164 |   molecule::iterator Walker = TestMolecule->begin();
 | 
|---|
| 165 |   molecule::iterator Runner = TestMolecule->begin();
 | 
|---|
| 166 |   Runner++;
 | 
|---|
| 167 |   CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) );
 | 
|---|
| 168 |   World::AtomComposite Set = TestMolecule->getAtomSet();
 | 
|---|
| 169 |   BG->CreateAdjacency(Set);
 | 
|---|
| 170 |   CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0);
 | 
|---|
| 171 |   CPPUNIT_ASSERT_EQUAL( true , (*Walker)->IsBondedTo(WorldTime::getTime(), (*Runner)) );
 | 
|---|
| 172 | };
 | 
|---|
| 173 | 
 | 
|---|
| 174 | /** UnitTest for BondGraphTest::CreateAdjacency().
 | 
|---|
| 175 |  */
 | 
|---|
| 176 | void BondGraphTest::ConstructGraphFromCovalentRadiiTest()
 | 
|---|
| 177 | {
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(dummy) );
 | 
|---|
| 180 |   World::AtomComposite Set = TestMolecule->getAtomSet();
 | 
|---|
| 181 |   BG->CreateAdjacency(Set);
 | 
|---|
| 182 |   CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0);
 | 
|---|
| 183 | 
 | 
|---|
| 184 |   // this cannot be assured using dynamic IDs
 | 
|---|
| 185 |   //CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
 | 
|---|
| 186 | };
 | 
|---|
| 187 | 
 | 
|---|
| 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 | 
 | 
|---|
| 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 | 
 | 
|---|