[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"
|
---|
[e5ad5c] | 22 | #include "log.hpp"
|
---|
[b70721] | 23 | #include "molecule.hpp"
|
---|
| 24 | #include "periodentafel.hpp"
|
---|
| 25 | #include "bondgraphunittest.hpp"
|
---|
| 26 |
|
---|
| 27 | /********************************************** Test classes **************************************/
|
---|
| 28 |
|
---|
| 29 | // Registers the fixture into the 'registry'
|
---|
| 30 | CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest );
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | void BondGraphTest::setUp()
|
---|
| 34 | {
|
---|
| 35 | atom *Walker = NULL;
|
---|
| 36 |
|
---|
| 37 | // init private all pointers to zero
|
---|
| 38 | TestMolecule = NULL;
|
---|
| 39 | hydrogen = NULL;
|
---|
| 40 | tafel = NULL;
|
---|
| 41 |
|
---|
| 42 | // construct element
|
---|
| 43 | hydrogen = new element;
|
---|
| 44 | hydrogen->Z = 1;
|
---|
[e5ad5c] | 45 | hydrogen->CovalentRadius = 0.23;
|
---|
| 46 | hydrogen->VanDerWaalsRadius = 1.09;
|
---|
[b70721] | 47 | strcpy(hydrogen->name, "hydrogen");
|
---|
| 48 | strcpy(hydrogen->symbol, "H");
|
---|
| 49 | carbon = new element;
|
---|
[e5ad5c] | 50 | carbon->Z = 2;
|
---|
| 51 | carbon->CovalentRadius = 0.68;
|
---|
| 52 | carbon->VanDerWaalsRadius = 1.7;
|
---|
[b70721] | 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();
|
---|
[e5ad5c] | 65 | Walker->type = carbon;
|
---|
[b70721] | 66 | Walker->node->Init(1., 0., 1. );
|
---|
| 67 | TestMolecule->AddAtom(Walker);
|
---|
| 68 | Walker = new atom();
|
---|
[e5ad5c] | 69 | Walker->type = carbon;
|
---|
[b70721] | 70 | Walker->node->Init(0., 1., 1. );
|
---|
| 71 | TestMolecule->AddAtom(Walker);
|
---|
| 72 | Walker = new atom();
|
---|
[e5ad5c] | 73 | Walker->type = carbon;
|
---|
[b70721] | 74 | Walker->node->Init(1., 1., 0. );
|
---|
| 75 | TestMolecule->AddAtom(Walker);
|
---|
| 76 | Walker = new atom();
|
---|
[e5ad5c] | 77 | Walker->type = carbon;
|
---|
[b70721] | 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
|
---|
[e5ad5c] | 85 | dummyname = new string("dummy.dat");
|
---|
[b70721] | 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);
|
---|
[e5ad5c] | 101 | delete(dummyname);
|
---|
[b70721] | 102 | delete(BG);
|
---|
| 103 |
|
---|
| 104 | // remove molecule
|
---|
| 105 | delete(TestMolecule);
|
---|
| 106 | // note that all the atoms are cleaned by TestMolecule
|
---|
| 107 | delete(tafel);
|
---|
| 108 | // note that element is cleaned by periodentafel
|
---|
| 109 | };
|
---|
| 110 |
|
---|
| 111 | /** UnitTest for BondGraphTest::LoadBondLengthTable().
|
---|
| 112 | */
|
---|
| 113 | void BondGraphTest::LoadTableTest()
|
---|
| 114 | {
|
---|
[e138de] | 115 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
|
---|
[b70721] | 116 | CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
|
---|
| 117 | CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) );
|
---|
| 118 | CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) );
|
---|
| 119 | };
|
---|
| 120 |
|
---|
| 121 | /** UnitTest for BondGraphTest::ConstructBondGraph().
|
---|
| 122 | */
|
---|
[e5ad5c] | 123 | void BondGraphTest::ConstructGraphFromTableTest()
|
---|
[b70721] | 124 | {
|
---|
| 125 | atom *Walker = TestMolecule->start->next;
|
---|
| 126 | atom *Runner = TestMolecule->end->previous;
|
---|
| 127 | CPPUNIT_ASSERT( TestMolecule->end != Walker );
|
---|
[e138de] | 128 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
|
---|
| 129 | CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
|
---|
[b70721] | 130 | CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
|
---|
| 131 | };
|
---|
| 132 |
|
---|
[e5ad5c] | 133 | /** UnitTest for BondGraphTest::ConstructBondGraph().
|
---|
| 134 | */
|
---|
| 135 | void BondGraphTest::ConstructGraphFromCovalentRadiiTest()
|
---|
| 136 | {
|
---|
| 137 | atom *Walker = TestMolecule->start->next;
|
---|
| 138 | atom *Runner = TestMolecule->end->previous;
|
---|
| 139 | CPPUNIT_ASSERT( TestMolecule->end != Walker );
|
---|
| 140 | CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(*dummyname) );
|
---|
| 141 | CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
|
---|
| 142 | CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
|
---|
| 143 | };
|
---|
| 144 |
|
---|
[b70721] | 145 |
|
---|
| 146 | /********************************************** Main routine **************************************/
|
---|
| 147 |
|
---|
| 148 | int main(int argc, char **argv)
|
---|
| 149 | {
|
---|
| 150 | // Get the top level suite from the registry
|
---|
| 151 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
| 152 |
|
---|
| 153 | // Adds the test to the list of test to run
|
---|
| 154 | CppUnit::TextUi::TestRunner runner;
|
---|
| 155 | runner.addTest( suite );
|
---|
| 156 |
|
---|
| 157 | // Change the default outputter to a compiler error format outputter
|
---|
| 158 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
| 159 | std::cerr ) );
|
---|
| 160 | // Run the tests.
|
---|
| 161 | bool wasSucessful = runner.run();
|
---|
| 162 |
|
---|
| 163 | // Return error code 1 if the one of test failed.
|
---|
| 164 | return wasSucessful ? 0 : 1;
|
---|
| 165 | };
|
---|