| [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" | 
|---|
|  | 23 | #include "molecule.hpp" | 
|---|
|  | 24 | #include "periodentafel.hpp" | 
|---|
|  | 25 | #include "bondgraphunittest.hpp" | 
|---|
| [e6fdbe] | 26 | #include "World.hpp" | 
|---|
| [b70721] | 27 |  | 
|---|
| [9b6b2f] | 28 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 29 | #include "UnitTestMain.hpp" | 
|---|
|  | 30 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| [b70721] | 31 |  | 
|---|
|  | 32 | /********************************************** Test classes **************************************/ | 
|---|
|  | 33 |  | 
|---|
|  | 34 | // Registers the fixture into the 'registry' | 
|---|
|  | 35 | CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest ); | 
|---|
|  | 36 |  | 
|---|
|  | 37 |  | 
|---|
|  | 38 | void BondGraphTest::setUp() | 
|---|
|  | 39 | { | 
|---|
|  | 40 | atom *Walker = NULL; | 
|---|
|  | 41 |  | 
|---|
|  | 42 | // init private all pointers to zero | 
|---|
|  | 43 | TestMolecule = NULL; | 
|---|
|  | 44 | hydrogen = NULL; | 
|---|
|  | 45 | tafel = NULL; | 
|---|
|  | 46 |  | 
|---|
|  | 47 | // construct element | 
|---|
|  | 48 | hydrogen = new element; | 
|---|
|  | 49 | hydrogen->Z = 1; | 
|---|
|  | 50 | strcpy(hydrogen->name, "hydrogen"); | 
|---|
|  | 51 | strcpy(hydrogen->symbol, "H"); | 
|---|
|  | 52 | carbon = new element; | 
|---|
|  | 53 | carbon->Z = 1; | 
|---|
|  | 54 | strcpy(carbon->name, "carbon"); | 
|---|
|  | 55 | strcpy(carbon->symbol, "C"); | 
|---|
|  | 56 |  | 
|---|
|  | 57 |  | 
|---|
|  | 58 | // construct periodentafel | 
|---|
| [cbc5fb] | 59 | tafel = World::get()->getPeriode(); | 
|---|
| [b70721] | 60 | tafel->AddElement(hydrogen); | 
|---|
|  | 61 | tafel->AddElement(carbon); | 
|---|
|  | 62 |  | 
|---|
|  | 63 | // construct molecule (tetraeder of hydrogens) | 
|---|
| [cbc5fb] | 64 | TestMolecule = World::get()->createMolecule(); | 
|---|
| [46d958] | 65 | Walker = World::get()->createAtom(); | 
|---|
| [b70721] | 66 | Walker->type = hydrogen; | 
|---|
|  | 67 | Walker->node->Init(1., 0., 1. ); | 
|---|
|  | 68 | TestMolecule->AddAtom(Walker); | 
|---|
| [46d958] | 69 | Walker = World::get()->createAtom(); | 
|---|
| [b70721] | 70 | Walker->type = hydrogen; | 
|---|
|  | 71 | Walker->node->Init(0., 1., 1. ); | 
|---|
|  | 72 | TestMolecule->AddAtom(Walker); | 
|---|
| [46d958] | 73 | Walker = World::get()->createAtom(); | 
|---|
| [b70721] | 74 | Walker->type = hydrogen; | 
|---|
|  | 75 | Walker->node->Init(1., 1., 0. ); | 
|---|
|  | 76 | TestMolecule->AddAtom(Walker); | 
|---|
| [46d958] | 77 | Walker = World::get()->createAtom(); | 
|---|
| [b70721] | 78 | Walker->type = hydrogen; | 
|---|
|  | 79 | Walker->node->Init(0., 0., 0. ); | 
|---|
|  | 80 | TestMolecule->AddAtom(Walker); | 
|---|
|  | 81 |  | 
|---|
|  | 82 | // check that TestMolecule was correctly constructed | 
|---|
|  | 83 | CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 4 ); | 
|---|
|  | 84 |  | 
|---|
|  | 85 | // create a small file with table | 
|---|
|  | 86 | filename = new string("test.dat"); | 
|---|
|  | 87 | ofstream test(filename->c_str()); | 
|---|
| [b21a64] | 88 | test << ".\tH\tC\n"; | 
|---|
|  | 89 | test << "H\t1.\t1.2\n"; | 
|---|
|  | 90 | test << "C\t1.2\t1.5\n"; | 
|---|
| [9a7186] | 91 | test.close(); | 
|---|
| [b70721] | 92 | BG = new BondGraph(true); | 
|---|
|  | 93 | }; | 
|---|
|  | 94 |  | 
|---|
|  | 95 |  | 
|---|
|  | 96 | void BondGraphTest::tearDown() | 
|---|
|  | 97 | { | 
|---|
|  | 98 | // remove the file | 
|---|
|  | 99 | remove(filename->c_str()); | 
|---|
|  | 100 | delete(filename); | 
|---|
|  | 101 | delete(BG); | 
|---|
|  | 102 |  | 
|---|
|  | 103 | // remove molecule | 
|---|
| [cbc5fb] | 104 | World::get()->destroyMolecule(TestMolecule); | 
|---|
| [a1510d] | 105 | // note that all the atoms, molecules, the tafel and the elements | 
|---|
|  | 106 | // are all cleaned when the world is destroyed | 
|---|
| [cbc5fb] | 107 | World::destroy(); | 
|---|
| [e6fdbe] | 108 | MemoryUsageObserver::purgeInstance(); | 
|---|
|  | 109 | logger::purgeInstance(); | 
|---|
| [b70721] | 110 | }; | 
|---|
|  | 111 |  | 
|---|
| [9879f6] | 112 | /** Tests whether setup worked. | 
|---|
|  | 113 | */ | 
|---|
|  | 114 | void BondGraphTest::SetupTest() | 
|---|
|  | 115 | { | 
|---|
|  | 116 | CPPUNIT_ASSERT_EQUAL (false, TestMolecule->empty()); | 
|---|
|  | 117 | CPPUNIT_ASSERT_EQUAL ((size_t)4, TestMolecule->size()); | 
|---|
|  | 118 | }; | 
|---|
|  | 119 |  | 
|---|
| [b70721] | 120 | /** UnitTest for BondGraphTest::LoadBondLengthTable(). | 
|---|
|  | 121 | */ | 
|---|
|  | 122 | void BondGraphTest::LoadTableTest() | 
|---|
|  | 123 | { | 
|---|
| [e138de] | 124 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) ); | 
|---|
| [b70721] | 125 | CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) ); | 
|---|
|  | 126 | CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) ); | 
|---|
|  | 127 | CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) ); | 
|---|
|  | 128 | }; | 
|---|
|  | 129 |  | 
|---|
|  | 130 | /** UnitTest for BondGraphTest::ConstructBondGraph(). | 
|---|
|  | 131 | */ | 
|---|
|  | 132 | void BondGraphTest::ConstructGraphTest() | 
|---|
|  | 133 | { | 
|---|
| [9879f6] | 134 | molecule::iterator Walker = TestMolecule->begin(); | 
|---|
|  | 135 | molecule::iterator Runner = TestMolecule->begin(); | 
|---|
|  | 136 | Runner++; | 
|---|
| [e138de] | 137 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) ); | 
|---|
|  | 138 | CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) ); | 
|---|
| [9879f6] | 139 | CPPUNIT_ASSERT_EQUAL( true , (*Walker)->IsBondedTo((*Runner)) ); | 
|---|
| [b70721] | 140 | }; | 
|---|