| 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 "Helpers/Assert.hpp"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "World.hpp"
 | 
|---|
| 21 | #include "atom.hpp"
 | 
|---|
| 22 | #include "bond.hpp"
 | 
|---|
| 23 | #include "bondgraph.hpp"
 | 
|---|
| 24 | #include "element.hpp"
 | 
|---|
| 25 | #include "Helpers/Log.hpp"
 | 
|---|
| 26 | #include "molecule.hpp"
 | 
|---|
| 27 | #include "periodentafel.hpp"
 | 
|---|
| 28 | #include "bondgraphunittest.hpp"
 | 
|---|
| 29 | #include "World.hpp"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 32 | #include "UnitTestMain.hpp"
 | 
|---|
| 33 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 34 | 
 | 
|---|
| 35 | /********************************************** Test classes **************************************/
 | 
|---|
| 36 | 
 | 
|---|
| 37 | // Registers the fixture into the 'registry'
 | 
|---|
| 38 | CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest );
 | 
|---|
| 39 | 
 | 
|---|
| 40 | 
 | 
|---|
| 41 | void BondGraphTest::setUp()
 | 
|---|
| 42 | {
 | 
|---|
| 43 |   atom *Walker = NULL;
 | 
|---|
| 44 | 
 | 
|---|
| 45 |   // construct element
 | 
|---|
| 46 |   hydrogen = World::getInstance().getPeriode()->FindElement(1);
 | 
|---|
| 47 |   carbon = World::getInstance().getPeriode()->FindElement(6);
 | 
|---|
| 48 |   CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
 | 
|---|
| 49 |   CPPUNIT_ASSERT(carbon != NULL && "could not find element carbon");
 | 
|---|
| 50 | 
 | 
|---|
| 51 |   // construct molecule (tetraeder of hydrogens)
 | 
|---|
| 52 |   TestMolecule = World::getInstance().createMolecule();
 | 
|---|
| 53 |   CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
 | 
|---|
| 54 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 55 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 56 |   Walker->setType(carbon);
 | 
|---|
| 57 |   Walker->setPosition(Vector(1., 0., 1. ));
 | 
|---|
| 58 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 59 | 
 | 
|---|
| 60 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 61 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 62 |   Walker->setType(carbon);
 | 
|---|
| 63 |   Walker->setPosition(Vector(0., 1., 1. ));
 | 
|---|
| 64 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 67 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 68 |   Walker->setType(carbon);
 | 
|---|
| 69 |   Walker->setPosition(Vector(1., 1., 0. ));
 | 
|---|
| 70 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 71 | 
 | 
|---|
| 72 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 73 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 74 |   Walker->setType(carbon);
 | 
|---|
| 75 |   Walker->setPosition(Vector(0., 0., 0. ));
 | 
|---|
| 76 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| 77 | 
 | 
|---|
| 78 |   // check that TestMolecule was correctly constructed
 | 
|---|
| 79 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 );
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   // create a small file with table
 | 
|---|
| 82 |   dummyname = new string("dummy.dat");
 | 
|---|
| 83 |   CPPUNIT_ASSERT(dummyname != NULL && "could not create string");
 | 
|---|
| 84 |   filename = new string("test.dat");
 | 
|---|
| 85 |   CPPUNIT_ASSERT(filename != NULL && "could not create string");
 | 
|---|
| 86 |   ofstream test(filename->c_str());
 | 
|---|
| 87 |   test << ".\tH\tHe\tLi\tBe\tB\tC\n";
 | 
|---|
| 88 |   test << "H\t1.\t1.\t1.\t1.\t1.\t1.2\n";
 | 
|---|
| 89 |   test << "He\t1.\t1.\t1.\t1.\t1.\t1.\n";
 | 
|---|
| 90 |   test << "Li\t1.\t1.\t1.\t1.\t1.\t1.\n";
 | 
|---|
| 91 |   test << "Be\t1.\t1.\t1.\t1.\t1.\t1.\n";
 | 
|---|
| 92 |   test << "B\t1.\t1.\t1.\t1.\t1.\t1.\n";
 | 
|---|
| 93 |   test << "C\t1.2\t1.\t1.\t1.\t1.\t1.5\n";
 | 
|---|
| 94 |   test.close();
 | 
|---|
| 95 |   BG = new BondGraph(true);
 | 
|---|
| 96 |   CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
 | 
|---|
| 97 | };
 | 
|---|
| 98 | 
 | 
|---|
| 99 | 
 | 
|---|
| 100 | void BondGraphTest::tearDown()
 | 
|---|
| 101 | {
 | 
|---|
| 102 |   // remove the file
 | 
|---|
| 103 |   remove(filename->c_str());
 | 
|---|
| 104 |   delete(filename);
 | 
|---|
| 105 |   delete(dummyname);
 | 
|---|
| 106 |   delete(BG);
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   // remove molecule
 | 
|---|
| 109 |   World::getInstance().destroyMolecule(TestMolecule);
 | 
|---|
| 110 |   // note that all the atoms, molecules, the tafel and the elements
 | 
|---|
| 111 |   // are all cleaned when the world is destroyed
 | 
|---|
| 112 |   World::purgeInstance();
 | 
|---|
| 113 |   logger::purgeInstance();
 | 
|---|
| 114 | };
 | 
|---|
| 115 | 
 | 
|---|
| 116 | /** Tests whether setup worked.
 | 
|---|
| 117 |  */
 | 
|---|
| 118 | void BondGraphTest::SetupTest()
 | 
|---|
| 119 | {
 | 
|---|
| 120 |   CPPUNIT_ASSERT_EQUAL (false, TestMolecule->empty());
 | 
|---|
| 121 |   CPPUNIT_ASSERT_EQUAL ((size_t)4, TestMolecule->size());
 | 
|---|
| 122 | };
 | 
|---|
| 123 | 
 | 
|---|
| 124 | /** UnitTest for BondGraphTest::LoadBondLengthTable().
 | 
|---|
| 125 |  */
 | 
|---|
| 126 | void BondGraphTest::LoadTableTest()
 | 
|---|
| 127 | {
 | 
|---|
| 128 |   CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
 | 
|---|
| 129 |   CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
 | 
|---|
| 130 |   CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,5) );
 | 
|---|
| 131 |   CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(5,5) );
 | 
|---|
| 132 | };
 | 
|---|
| 133 | 
 | 
|---|
| 134 | /** UnitTest for BondGraphTest::ConstructBondGraph().
 | 
|---|
| 135 |  */
 | 
|---|
| 136 | void BondGraphTest::ConstructGraphFromTableTest()
 | 
|---|
| 137 | {
 | 
|---|
| 138 |   molecule::iterator Walker = TestMolecule->begin();
 | 
|---|
| 139 |   molecule::iterator Runner = TestMolecule->begin();
 | 
|---|
| 140 |   Runner++;
 | 
|---|
| 141 |   CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
 | 
|---|
| 142 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
 | 
|---|
| 143 |   CPPUNIT_ASSERT_EQUAL( true , (*Walker)->IsBondedTo((*Runner)) );
 | 
|---|
| 144 | };
 | 
|---|
| 145 | 
 | 
|---|
| 146 | /** UnitTest for BondGraphTest::ConstructBondGraph().
 | 
|---|
| 147 |  */
 | 
|---|
| 148 | void BondGraphTest::ConstructGraphFromCovalentRadiiTest()
 | 
|---|
| 149 | {
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   //atom *Walker = TestMolecule->start->next;
 | 
|---|
| 152 |   //atom *Runner = TestMolecule->end->previous;
 | 
|---|
| 153 |   //CPPUNIT_ASSERT( TestMolecule->end != Walker );
 | 
|---|
| 154 |   CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(*dummyname) );
 | 
|---|
| 155 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   // this cannot be assured using dynamic IDs
 | 
|---|
| 158 |   //CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
 | 
|---|
| 159 | };
 | 
|---|
| 160 | 
 | 
|---|