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