| [bcf653] | 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 | 
 | 
|---|
| [b70721] | 8 | /*
 | 
|---|
 | 9 |  * bondgraphunittest.cpp
 | 
|---|
 | 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 | 
 | 
|---|
 | 26 | #include <iostream>
 | 
|---|
 | 27 | #include <stdio.h>
 | 
|---|
| [49e1ae] | 28 | #include <cstring>
 | 
|---|
| [b70721] | 29 | 
 | 
|---|
| [4eb4fe] | 30 | #include "Helpers/Assert.hpp"
 | 
|---|
 | 31 | 
 | 
|---|
| [46d958] | 32 | #include "World.hpp"
 | 
|---|
| [b70721] | 33 | #include "atom.hpp"
 | 
|---|
 | 34 | #include "bond.hpp"
 | 
|---|
 | 35 | #include "bondgraph.hpp"
 | 
|---|
 | 36 | #include "element.hpp"
 | 
|---|
| [952f38] | 37 | #include "Helpers/Log.hpp"
 | 
|---|
| [b70721] | 38 | #include "molecule.hpp"
 | 
|---|
 | 39 | #include "periodentafel.hpp"
 | 
|---|
 | 40 | #include "bondgraphunittest.hpp"
 | 
|---|
| [e6fdbe] | 41 | #include "World.hpp"
 | 
|---|
| [b70721] | 42 | 
 | 
|---|
| [9b6b2f] | 43 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 44 | #include "UnitTestMain.hpp"
 | 
|---|
 | 45 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| [b70721] | 46 | 
 | 
|---|
 | 47 | /********************************************** Test classes **************************************/
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | // Registers the fixture into the 'registry'
 | 
|---|
 | 50 | CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest );
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | void BondGraphTest::setUp()
 | 
|---|
 | 54 | {
 | 
|---|
 | 55 |   atom *Walker = NULL;
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 |   // construct element
 | 
|---|
| [4eb4fe] | 58 |   hydrogen = World::getInstance().getPeriode()->FindElement(1);
 | 
|---|
 | 59 |   carbon = World::getInstance().getPeriode()->FindElement(6);
 | 
|---|
 | 60 |   CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
 | 
|---|
 | 61 |   CPPUNIT_ASSERT(carbon != NULL && "could not find element carbon");
 | 
|---|
| [b70721] | 62 | 
 | 
|---|
 | 63 |   // construct molecule (tetraeder of hydrogens)
 | 
|---|
| [23b547] | 64 |   TestMolecule = World::getInstance().createMolecule();
 | 
|---|
| [4eb4fe] | 65 |   CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
 | 
|---|
| [23b547] | 66 |   Walker = World::getInstance().createAtom();
 | 
|---|
| [4eb4fe] | 67 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| [d74077] | 68 |   Walker->setType(carbon);
 | 
|---|
 | 69 |   Walker->setPosition(Vector(1., 0., 1. ));
 | 
|---|
| [b70721] | 70 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| [8cbb97] | 71 | 
 | 
|---|
| [23b547] | 72 |   Walker = World::getInstance().createAtom();
 | 
|---|
| [4eb4fe] | 73 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| [d74077] | 74 |   Walker->setType(carbon);
 | 
|---|
 | 75 |   Walker->setPosition(Vector(0., 1., 1. ));
 | 
|---|
| [b70721] | 76 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| [8cbb97] | 77 | 
 | 
|---|
| [23b547] | 78 |   Walker = World::getInstance().createAtom();
 | 
|---|
| [4eb4fe] | 79 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| [d74077] | 80 |   Walker->setType(carbon);
 | 
|---|
 | 81 |   Walker->setPosition(Vector(1., 1., 0. ));
 | 
|---|
| [b70721] | 82 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| [8cbb97] | 83 | 
 | 
|---|
| [23b547] | 84 |   Walker = World::getInstance().createAtom();
 | 
|---|
| [4eb4fe] | 85 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| [d74077] | 86 |   Walker->setType(carbon);
 | 
|---|
 | 87 |   Walker->setPosition(Vector(0., 0., 0. ));
 | 
|---|
| [b70721] | 88 |   TestMolecule->AddAtom(Walker);
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 |   // check that TestMolecule was correctly constructed
 | 
|---|
| [ea7176] | 91 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 );
 | 
|---|
| [b70721] | 92 | 
 | 
|---|
 | 93 |   // create a small file with table
 | 
|---|
| [e5ad5c] | 94 |   dummyname = new string("dummy.dat");
 | 
|---|
| [4eb4fe] | 95 |   CPPUNIT_ASSERT(dummyname != NULL && "could not create string");
 | 
|---|
| [b70721] | 96 |   filename = new string("test.dat");
 | 
|---|
| [4eb4fe] | 97 |   CPPUNIT_ASSERT(filename != NULL && "could not create string");
 | 
|---|
| [b70721] | 98 |   ofstream test(filename->c_str());
 | 
|---|
| [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";
 | 
|---|
| [9a7186] | 106 |   test.close();
 | 
|---|
| [b70721] | 107 |   BG = new BondGraph(true);
 | 
|---|
| [4eb4fe] | 108 |   CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
 | 
|---|
| [b70721] | 109 | };
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 | void BondGraphTest::tearDown()
 | 
|---|
 | 113 | {
 | 
|---|
 | 114 |   // remove the file
 | 
|---|
 | 115 |   remove(filename->c_str());
 | 
|---|
 | 116 |   delete(filename);
 | 
|---|
| [e5ad5c] | 117 |   delete(dummyname);
 | 
|---|
| [b70721] | 118 |   delete(BG);
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 |   // remove molecule
 | 
|---|
| [23b547] | 121 |   World::getInstance().destroyMolecule(TestMolecule);
 | 
|---|
| [a1510d] | 122 |   // note that all the atoms, molecules, the tafel and the elements
 | 
|---|
 | 123 |   // are all cleaned when the world is destroyed
 | 
|---|
| [23b547] | 124 |   World::purgeInstance();
 | 
|---|
| [e6fdbe] | 125 |   logger::purgeInstance();
 | 
|---|
| [b70721] | 126 | };
 | 
|---|
 | 127 | 
 | 
|---|
| [9879f6] | 128 | /** Tests whether setup worked.
 | 
|---|
 | 129 |  */
 | 
|---|
 | 130 | void BondGraphTest::SetupTest()
 | 
|---|
 | 131 | {
 | 
|---|
 | 132 |   CPPUNIT_ASSERT_EQUAL (false, TestMolecule->empty());
 | 
|---|
 | 133 |   CPPUNIT_ASSERT_EQUAL ((size_t)4, TestMolecule->size());
 | 
|---|
 | 134 | };
 | 
|---|
 | 135 | 
 | 
|---|
| [b70721] | 136 | /** UnitTest for BondGraphTest::LoadBondLengthTable().
 | 
|---|
 | 137 |  */
 | 
|---|
 | 138 | void BondGraphTest::LoadTableTest()
 | 
|---|
 | 139 | {
 | 
|---|
| [e138de] | 140 |   CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
 | 
|---|
| [b70721] | 141 |   CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
 | 
|---|
| [4eb4fe] | 142 |   CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,5) );
 | 
|---|
 | 143 |   CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(5,5) );
 | 
|---|
| [b70721] | 144 | };
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | /** UnitTest for BondGraphTest::ConstructBondGraph().
 | 
|---|
 | 147 |  */
 | 
|---|
| [e5ad5c] | 148 | void BondGraphTest::ConstructGraphFromTableTest()
 | 
|---|
| [b70721] | 149 | {
 | 
|---|
| [9879f6] | 150 |   molecule::iterator Walker = TestMolecule->begin();
 | 
|---|
 | 151 |   molecule::iterator Runner = TestMolecule->begin();
 | 
|---|
 | 152 |   Runner++;
 | 
|---|
| [e138de] | 153 |   CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
 | 
|---|
 | 154 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
 | 
|---|
| [9879f6] | 155 |   CPPUNIT_ASSERT_EQUAL( true , (*Walker)->IsBondedTo((*Runner)) );
 | 
|---|
| [b70721] | 156 | };
 | 
|---|
 | 157 | 
 | 
|---|
| [e5ad5c] | 158 | /** UnitTest for BondGraphTest::ConstructBondGraph().
 | 
|---|
 | 159 |  */
 | 
|---|
 | 160 | void BondGraphTest::ConstructGraphFromCovalentRadiiTest()
 | 
|---|
 | 161 | {
 | 
|---|
| [a7b761b] | 162 | 
 | 
|---|
 | 163 |   //atom *Walker = TestMolecule->start->next;
 | 
|---|
 | 164 |   //atom *Runner = TestMolecule->end->previous;
 | 
|---|
 | 165 |   //CPPUNIT_ASSERT( TestMolecule->end != Walker );
 | 
|---|
| [e5ad5c] | 166 |   CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(*dummyname) );
 | 
|---|
 | 167 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
 | 
|---|
| [a7b761b] | 168 | 
 | 
|---|
 | 169 |   // this cannot be assured using dynamic IDs
 | 
|---|
 | 170 |   //CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
 | 
|---|
| [e5ad5c] | 171 | };
 | 
|---|
 | 172 | 
 | 
|---|