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