[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 |
|
---|
[46d958] | 18 | #include "World.hpp"
|
---|
[b70721] | 19 | #include "atom.hpp"
|
---|
| 20 | #include "bond.hpp"
|
---|
| 21 | #include "bondgraph.hpp"
|
---|
| 22 | #include "element.hpp"
|
---|
[e5ad5c] | 23 | #include "log.hpp"
|
---|
[b70721] | 24 | #include "molecule.hpp"
|
---|
| 25 | #include "periodentafel.hpp"
|
---|
| 26 | #include "bondgraphunittest.hpp"
|
---|
[e6fdbe] | 27 | #include "World.hpp"
|
---|
[b70721] | 28 |
|
---|
[9b6b2f] | 29 | #ifdef HAVE_TESTRUNNER
|
---|
| 30 | #include "UnitTestMain.hpp"
|
---|
| 31 | #endif /*HAVE_TESTRUNNER*/
|
---|
[b70721] | 32 |
|
---|
| 33 | /********************************************** Test classes **************************************/
|
---|
| 34 |
|
---|
| 35 | // Registers the fixture into the 'registry'
|
---|
| 36 | CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest );
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | void BondGraphTest::setUp()
|
---|
| 40 | {
|
---|
| 41 | atom *Walker = NULL;
|
---|
| 42 |
|
---|
| 43 | // init private all pointers to zero
|
---|
| 44 | TestMolecule = NULL;
|
---|
| 45 | hydrogen = NULL;
|
---|
| 46 | tafel = NULL;
|
---|
| 47 |
|
---|
| 48 | // construct element
|
---|
| 49 | hydrogen = new element;
|
---|
| 50 | hydrogen->Z = 1;
|
---|
[e5ad5c] | 51 | hydrogen->CovalentRadius = 0.23;
|
---|
| 52 | hydrogen->VanDerWaalsRadius = 1.09;
|
---|
[b70721] | 53 | strcpy(hydrogen->name, "hydrogen");
|
---|
| 54 | strcpy(hydrogen->symbol, "H");
|
---|
| 55 | carbon = new element;
|
---|
[ead4e6] | 56 | carbon->Z = 2;
|
---|
[e5ad5c] | 57 | carbon->CovalentRadius = 0.68;
|
---|
| 58 | carbon->VanDerWaalsRadius = 1.7;
|
---|
[b70721] | 59 | strcpy(carbon->name, "carbon");
|
---|
| 60 | strcpy(carbon->symbol, "C");
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | // construct periodentafel
|
---|
[23b547] | 64 | tafel = World::getInstance().getPeriode();
|
---|
[b70721] | 65 | tafel->AddElement(hydrogen);
|
---|
| 66 | tafel->AddElement(carbon);
|
---|
| 67 |
|
---|
| 68 | // construct molecule (tetraeder of hydrogens)
|
---|
[23b547] | 69 | TestMolecule = World::getInstance().createMolecule();
|
---|
| 70 | Walker = World::getInstance().createAtom();
|
---|
[e5ad5c] | 71 | Walker->type = carbon;
|
---|
[1bd79e] | 72 | *Walker->node = Vector(1., 0., 1. );
|
---|
[b70721] | 73 | TestMolecule->AddAtom(Walker);
|
---|
[8cbb97] | 74 |
|
---|
[23b547] | 75 | Walker = World::getInstance().createAtom();
|
---|
[e5ad5c] | 76 | Walker->type = carbon;
|
---|
[1bd79e] | 77 | *Walker->node = Vector(0., 1., 1. );
|
---|
[b70721] | 78 | TestMolecule->AddAtom(Walker);
|
---|
[8cbb97] | 79 |
|
---|
[23b547] | 80 | Walker = World::getInstance().createAtom();
|
---|
[e5ad5c] | 81 | Walker->type = carbon;
|
---|
[1bd79e] | 82 | *Walker->node = Vector(1., 1., 0. );
|
---|
[b70721] | 83 | TestMolecule->AddAtom(Walker);
|
---|
[8cbb97] | 84 |
|
---|
[23b547] | 85 | Walker = World::getInstance().createAtom();
|
---|
[e5ad5c] | 86 | Walker->type = carbon;
|
---|
[1bd79e] | 87 | *Walker->node = Vector(0., 0., 0. );
|
---|
[b70721] | 88 | TestMolecule->AddAtom(Walker);
|
---|
| 89 |
|
---|
| 90 | // check that TestMolecule was correctly constructed
|
---|
| 91 | CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 4 );
|
---|
| 92 |
|
---|
| 93 | // create a small file with table
|
---|
[e5ad5c] | 94 | dummyname = new string("dummy.dat");
|
---|
[b70721] | 95 | filename = new string("test.dat");
|
---|
| 96 | ofstream test(filename->c_str());
|
---|
[b21a64] | 97 | test << ".\tH\tC\n";
|
---|
| 98 | test << "H\t1.\t1.2\n";
|
---|
| 99 | test << "C\t1.2\t1.5\n";
|
---|
[9a7186] | 100 | test.close();
|
---|
[b70721] | 101 | BG = new BondGraph(true);
|
---|
| 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 | MemoryUsageObserver::purgeInstance();
|
---|
| 119 | logger::purgeInstance();
|
---|
[b70721] | 120 | };
|
---|
| 121 |
|
---|
| 122 | /** UnitTest for BondGraphTest::LoadBondLengthTable().
|
---|
| 123 | */
|
---|
| 124 | void BondGraphTest::LoadTableTest()
|
---|
| 125 | {
|
---|
[e138de] | 126 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
|
---|
[b70721] | 127 | CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
|
---|
| 128 | CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) );
|
---|
| 129 | CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) );
|
---|
| 130 | };
|
---|
| 131 |
|
---|
| 132 | /** UnitTest for BondGraphTest::ConstructBondGraph().
|
---|
| 133 | */
|
---|
[e5ad5c] | 134 | void BondGraphTest::ConstructGraphFromTableTest()
|
---|
[b70721] | 135 | {
|
---|
| 136 | atom *Walker = TestMolecule->start->next;
|
---|
| 137 | atom *Runner = TestMolecule->end->previous;
|
---|
| 138 | CPPUNIT_ASSERT( TestMolecule->end != Walker );
|
---|
[e138de] | 139 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
|
---|
| 140 | CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
|
---|
[b70721] | 141 | CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
|
---|
| 142 | };
|
---|
| 143 |
|
---|
[e5ad5c] | 144 | /** UnitTest for BondGraphTest::ConstructBondGraph().
|
---|
| 145 | */
|
---|
| 146 | void BondGraphTest::ConstructGraphFromCovalentRadiiTest()
|
---|
| 147 | {
|
---|
| 148 | atom *Walker = TestMolecule->start->next;
|
---|
| 149 | atom *Runner = TestMolecule->end->previous;
|
---|
| 150 | CPPUNIT_ASSERT( TestMolecule->end != Walker );
|
---|
| 151 | CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(*dummyname) );
|
---|
| 152 | CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
|
---|
| 153 | CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
|
---|
| 154 | };
|
---|
| 155 |
|
---|