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