| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [bcf653] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [b70721] | 8 | /* | 
|---|
| [f844ef] | 9 | * BondGraphUnitTest.cpp | 
|---|
| [b70721] | 10 | * | 
|---|
|  | 11 | *  Created on: Oct 29, 2009 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [b70721] | 20 | using namespace std; | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 23 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 24 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 25 |  | 
|---|
| [1ac24b] | 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 |  | 
|---|
| [b70721] | 30 | #include <iostream> | 
|---|
|  | 31 | #include <stdio.h> | 
|---|
| [49e1ae] | 32 | #include <cstring> | 
|---|
| [b70721] | 33 |  | 
|---|
| [ad011c] | 34 | #include "CodePatterns/Assert.hpp" | 
|---|
| [4eb4fe] | 35 |  | 
|---|
| [6f0841] | 36 | #include "Atom/atom.hpp" | 
|---|
| [129204] | 37 | #include "Bond/bond.hpp" | 
|---|
| [ad011c] | 38 | #include "CodePatterns/Log.hpp" | 
|---|
| [3bdb6d] | 39 | #include "Element/element.hpp" | 
|---|
| [129204] | 40 | #include "Graph/BondGraph.hpp" | 
|---|
| [b70721] | 41 | #include "molecule.hpp" | 
|---|
| [3bdb6d] | 42 | #include "Element/periodentafel.hpp" | 
|---|
| [e6fdbe] | 43 | #include "World.hpp" | 
|---|
| [073a9e4] | 44 | #include "WorldTime.hpp" | 
|---|
| [b70721] | 45 |  | 
|---|
| [f844ef] | 46 | #include "BondGraphUnitTest.hpp" | 
|---|
|  | 47 |  | 
|---|
| [9b6b2f] | 48 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 49 | #include "UnitTestMain.hpp" | 
|---|
|  | 50 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| [b70721] | 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 | 
|---|
| [4eb4fe] | 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"); | 
|---|
| [b70721] | 67 |  | 
|---|
|  | 68 | // construct molecule (tetraeder of hydrogens) | 
|---|
| [23b547] | 69 | TestMolecule = World::getInstance().createMolecule(); | 
|---|
| [4eb4fe] | 70 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule"); | 
|---|
| [23b547] | 71 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 72 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 73 | Walker->setType(carbon); | 
|---|
|  | 74 | Walker->setPosition(Vector(1., 0., 1. )); | 
|---|
| [b70721] | 75 | TestMolecule->AddAtom(Walker); | 
|---|
| [8cbb97] | 76 |  | 
|---|
| [23b547] | 77 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 78 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 79 | Walker->setType(carbon); | 
|---|
|  | 80 | Walker->setPosition(Vector(0., 1., 1. )); | 
|---|
| [b70721] | 81 | TestMolecule->AddAtom(Walker); | 
|---|
| [8cbb97] | 82 |  | 
|---|
| [23b547] | 83 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 84 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 85 | Walker->setType(carbon); | 
|---|
|  | 86 | Walker->setPosition(Vector(1., 1., 0. )); | 
|---|
| [b70721] | 87 | TestMolecule->AddAtom(Walker); | 
|---|
| [8cbb97] | 88 |  | 
|---|
| [23b547] | 89 | Walker = World::getInstance().createAtom(); | 
|---|
| [4eb4fe] | 90 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom"); | 
|---|
| [d74077] | 91 | Walker->setType(carbon); | 
|---|
|  | 92 | Walker->setPosition(Vector(0., 0., 0. )); | 
|---|
| [b70721] | 93 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | // check that TestMolecule was correctly constructed | 
|---|
| [ea7176] | 96 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 ); | 
|---|
| [b70721] | 97 |  | 
|---|
| [4e855e] | 98 | // create stream with table | 
|---|
| [4eb4fe] | 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"; | 
|---|
| [4e855e] | 106 | // created bad stream (i.e. non-present file) | 
|---|
|  | 107 | dummy.setstate(ios::eofbit); | 
|---|
|  | 108 | CPPUNIT_ASSERT(dummy.eof()); | 
|---|
| [b70721] | 109 | BG = new BondGraph(true); | 
|---|
| [4eb4fe] | 110 | CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph"); | 
|---|
| [b70721] | 111 | }; | 
|---|
|  | 112 |  | 
|---|
|  | 113 |  | 
|---|
|  | 114 | void BondGraphTest::tearDown() | 
|---|
|  | 115 | { | 
|---|
|  | 116 | // remove the file | 
|---|
|  | 117 | delete(BG); | 
|---|
|  | 118 |  | 
|---|
|  | 119 | // remove molecule | 
|---|
| [23b547] | 120 | World::getInstance().destroyMolecule(TestMolecule); | 
|---|
| [a1510d] | 121 | // note that all the atoms, molecules, the tafel and the elements | 
|---|
|  | 122 | // are all cleaned when the world is destroyed | 
|---|
| [23b547] | 123 | World::purgeInstance(); | 
|---|
| [e6fdbe] | 124 | logger::purgeInstance(); | 
|---|
| [b70721] | 125 | }; | 
|---|
|  | 126 |  | 
|---|
| [9879f6] | 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 |  | 
|---|
| [b70721] | 135 | /** UnitTest for BondGraphTest::LoadBondLengthTable(). | 
|---|
|  | 136 | */ | 
|---|
|  | 137 | void BondGraphTest::LoadTableTest() | 
|---|
|  | 138 | { | 
|---|
| [4e855e] | 139 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) ); | 
|---|
| [b70721] | 140 | CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) ); | 
|---|
| [4eb4fe] | 141 | CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,5) ); | 
|---|
|  | 142 | CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(5,5) ); | 
|---|
| [b70721] | 143 | }; | 
|---|
|  | 144 |  | 
|---|
| [3738f0] | 145 | /** UnitTest for BondGraphTest::CreateAdjacency(). | 
|---|
| [b70721] | 146 | */ | 
|---|
| [e5ad5c] | 147 | void BondGraphTest::ConstructGraphFromTableTest() | 
|---|
| [b70721] | 148 | { | 
|---|
| [9879f6] | 149 | molecule::iterator Walker = TestMolecule->begin(); | 
|---|
|  | 150 | molecule::iterator Runner = TestMolecule->begin(); | 
|---|
|  | 151 | Runner++; | 
|---|
| [4e855e] | 152 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) ); | 
|---|
| [9317be] | 153 | World::AtomComposite Set = TestMolecule->getAtomSet(); | 
|---|
| [3738f0] | 154 | BG->CreateAdjacency(Set); | 
|---|
|  | 155 | CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0); | 
|---|
| [073a9e4] | 156 | CPPUNIT_ASSERT_EQUAL( true , (*Walker)->IsBondedTo(WorldTime::getTime(), (*Runner)) ); | 
|---|
| [b70721] | 157 | }; | 
|---|
|  | 158 |  | 
|---|
| [3738f0] | 159 | /** UnitTest for BondGraphTest::CreateAdjacency(). | 
|---|
| [e5ad5c] | 160 | */ | 
|---|
|  | 161 | void BondGraphTest::ConstructGraphFromCovalentRadiiTest() | 
|---|
|  | 162 | { | 
|---|
| [a7b761b] | 163 |  | 
|---|
| [4e855e] | 164 | CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(dummy) ); | 
|---|
| [9317be] | 165 | World::AtomComposite Set = TestMolecule->getAtomSet(); | 
|---|
| [3738f0] | 166 | BG->CreateAdjacency(Set); | 
|---|
|  | 167 | CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0); | 
|---|
| [a7b761b] | 168 |  | 
|---|
|  | 169 | // this cannot be assured using dynamic IDs | 
|---|
|  | 170 | //CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) ); | 
|---|
| [e5ad5c] | 171 | }; | 
|---|
|  | 172 |  | 
|---|
| [9b6663] | 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 |  | 
|---|
| [1ac24b] | 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 |  | 
|---|