| 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 | /********************************************** Test classes **************************************/
 | 
|---|
| 27 | 
 | 
|---|
| 28 | // Registers the fixture into the 'registry'
 | 
|---|
| 29 | CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest );
 | 
|---|
| 30 | 
 | 
|---|
| 31 | 
 | 
|---|
| 32 | void BondGraphTest::setUp()
 | 
|---|
| 33 | {
 | 
|---|
| 34 |   atom *Walker = NULL;
 | 
|---|
| 35 | 
 | 
|---|
| 36 |   // init private all pointers to zero
 | 
|---|
| 37 |   TestMolecule = NULL;
 | 
|---|
| 38 |   hydrogen = NULL;
 | 
|---|
| 39 |   tafel = NULL;
 | 
|---|
| 40 | 
 | 
|---|
| 41 |   // construct element
 | 
|---|
| 42 |   hydrogen = new element;
 | 
|---|
| 43 |   hydrogen->Z = 1;
 | 
|---|
| 44 |   strcpy(hydrogen->name, "hydrogen");
 | 
|---|
| 45 |   strcpy(hydrogen->symbol, "H");
 | 
|---|
| 46 |   carbon = new element;
 | 
|---|
| 47 |   carbon->Z = 1;
 | 
|---|
| 48 |   strcpy(carbon->name, "carbon");
 | 
|---|
| 49 |   strcpy(carbon->symbol, "C");
 | 
|---|
| 50 | 
 | 
|---|
| 51 | 
 | 
|---|
| 52 |   // construct periodentafel
 | 
|---|
| 53 |   tafel = new periodentafel;
 | 
|---|
| 54 |   tafel->AddElement(hydrogen);
 | 
|---|
| 55 |   tafel->AddElement(carbon);
 | 
|---|
| 56 | 
 | 
|---|
| 57 |   // construct molecule (tetraeder of hydrogens)
 | 
|---|
| 58 |   TestMolecule = new molecule(tafel);
 | 
|---|
| 59 |   Walker = new atom();
 | 
|---|
| 60 |   Walker->type = hydrogen;
 | 
|---|
| 61 |   Walker->node->Init(1., 0., 1. );
 | 
|---|
| 62 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 63 |   Walker = new atom();
 | 
|---|
| 64 |   Walker->type = hydrogen;
 | 
|---|
| 65 |   Walker->node->Init(0., 1., 1. );
 | 
|---|
| 66 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 67 |   Walker = new atom();
 | 
|---|
| 68 |   Walker->type = hydrogen;
 | 
|---|
| 69 |   Walker->node->Init(1., 1., 0. );
 | 
|---|
| 70 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 71 |   Walker = new atom();
 | 
|---|
| 72 |   Walker->type = hydrogen;
 | 
|---|
| 73 |   Walker->node->Init(0., 0., 0. );
 | 
|---|
| 74 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   // check that TestMolecule was correctly constructed
 | 
|---|
| 77 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 4 );
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   // create a small file with table
 | 
|---|
| 80 |   filename = new string("test.dat");
 | 
|---|
| 81 |   ofstream test(filename->c_str());
 | 
|---|
| 82 |   test << ".\tH\tC\n";
 | 
|---|
| 83 |   test << "H\t1.\t1.2\n";
 | 
|---|
| 84 |   test << "C\t1.2\t1.5\n";
 | 
|---|
| 85 |   test.close();
 | 
|---|
| 86 |   BG = new BondGraph(true);
 | 
|---|
| 87 | };
 | 
|---|
| 88 | 
 | 
|---|
| 89 | 
 | 
|---|
| 90 | void BondGraphTest::tearDown()
 | 
|---|
| 91 | {
 | 
|---|
| 92 |   // remove the file
 | 
|---|
| 93 |   remove(filename->c_str());
 | 
|---|
| 94 |   delete(filename);
 | 
|---|
| 95 |   delete(BG);
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   // remove molecule
 | 
|---|
| 98 |   delete(TestMolecule);
 | 
|---|
| 99 |   // note that all the atoms are cleaned by TestMolecule
 | 
|---|
| 100 |   delete(tafel);
 | 
|---|
| 101 |   // note that element is cleaned by periodentafel
 | 
|---|
| 102 | };
 | 
|---|
| 103 | 
 | 
|---|
| 104 | /** UnitTest for BondGraphTest::LoadBondLengthTable().
 | 
|---|
| 105 |  */
 | 
|---|
| 106 | void BondGraphTest::LoadTableTest()
 | 
|---|
| 107 | {
 | 
|---|
| 108 |   CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
 | 
|---|
| 109 |   CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
 | 
|---|
| 110 |   CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) );
 | 
|---|
| 111 |   CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) );
 | 
|---|
| 112 | };
 | 
|---|
| 113 | 
 | 
|---|
| 114 | /** UnitTest for BondGraphTest::ConstructBondGraph().
 | 
|---|
| 115 |  */
 | 
|---|
| 116 | void BondGraphTest::ConstructGraphTest()
 | 
|---|
| 117 | {
 | 
|---|
| 118 |   atom *Walker = TestMolecule->start->next;
 | 
|---|
| 119 |   atom *Runner = TestMolecule->end->previous;
 | 
|---|
| 120 |   CPPUNIT_ASSERT( TestMolecule->end != Walker );
 | 
|---|
| 121 |   CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
 | 
|---|
| 122 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
 | 
|---|
| 123 |   CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
 | 
|---|
| 124 | };
 | 
|---|
| 125 | 
 | 
|---|
| 126 | 
 | 
|---|
| 127 | /********************************************** Main routine **************************************/
 | 
|---|
| 128 | 
 | 
|---|
| 129 | int main(int argc, char **argv)
 | 
|---|
| 130 | {
 | 
|---|
| 131 |   // Get the top level suite from the registry
 | 
|---|
| 132 |   CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
 | 
|---|
| 133 | 
 | 
|---|
| 134 |   // Adds the test to the list of test to run
 | 
|---|
| 135 |   CppUnit::TextUi::TestRunner runner;
 | 
|---|
| 136 |   runner.addTest( suite );
 | 
|---|
| 137 | 
 | 
|---|
| 138 |   // Change the default outputter to a compiler error format outputter
 | 
|---|
| 139 |   runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
 | 
|---|
| 140 |                                                        std::cerr ) );
 | 
|---|
| 141 |   // Run the tests.
 | 
|---|
| 142 |   bool wasSucessful = runner.run();
 | 
|---|
| 143 | 
 | 
|---|
| 144 |   // Return error code 1 if the one of test failed.
 | 
|---|
| 145 |   return wasSucessful ? 0 : 1;
 | 
|---|
| 146 | };
 | 
|---|