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