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