[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[b70721] | 8 | /*
|
---|
[f844ef] | 9 | * BondGraphUnitTest.cpp
|
---|
[b70721] | 10 | *
|
---|
| 11 | * Created on: Oct 29, 2009
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[b70721] | 20 | using namespace std;
|
---|
| 21 |
|
---|
| 22 | #include <cppunit/CompilerOutputter.h>
|
---|
| 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 24 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 25 |
|
---|
| 26 | #include <iostream>
|
---|
| 27 | #include <stdio.h>
|
---|
[49e1ae] | 28 | #include <cstring>
|
---|
[b70721] | 29 |
|
---|
[ad011c] | 30 | #include "CodePatterns/Assert.hpp"
|
---|
[4eb4fe] | 31 |
|
---|
[46d958] | 32 | #include "World.hpp"
|
---|
[b70721] | 33 | #include "atom.hpp"
|
---|
| 34 | #include "bond.hpp"
|
---|
| 35 | #include "bondgraph.hpp"
|
---|
| 36 | #include "element.hpp"
|
---|
[ad011c] | 37 | #include "CodePatterns/Log.hpp"
|
---|
[b70721] | 38 | #include "molecule.hpp"
|
---|
| 39 | #include "periodentafel.hpp"
|
---|
[e6fdbe] | 40 | #include "World.hpp"
|
---|
[b70721] | 41 |
|
---|
[f844ef] | 42 | #include "BondGraphUnitTest.hpp"
|
---|
| 43 |
|
---|
[9b6b2f] | 44 | #ifdef HAVE_TESTRUNNER
|
---|
| 45 | #include "UnitTestMain.hpp"
|
---|
| 46 | #endif /*HAVE_TESTRUNNER*/
|
---|
[b70721] | 47 |
|
---|
| 48 | /********************************************** Test classes **************************************/
|
---|
| 49 |
|
---|
| 50 | // Registers the fixture into the 'registry'
|
---|
| 51 | CPPUNIT_TEST_SUITE_REGISTRATION( BondGraphTest );
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | void BondGraphTest::setUp()
|
---|
| 55 | {
|
---|
| 56 | atom *Walker = NULL;
|
---|
| 57 |
|
---|
| 58 | // construct element
|
---|
[4eb4fe] | 59 | hydrogen = World::getInstance().getPeriode()->FindElement(1);
|
---|
| 60 | carbon = World::getInstance().getPeriode()->FindElement(6);
|
---|
| 61 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
|
---|
| 62 | CPPUNIT_ASSERT(carbon != NULL && "could not find element carbon");
|
---|
[b70721] | 63 |
|
---|
| 64 | // construct molecule (tetraeder of hydrogens)
|
---|
[23b547] | 65 | TestMolecule = World::getInstance().createMolecule();
|
---|
[4eb4fe] | 66 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
|
---|
[23b547] | 67 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 68 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[d74077] | 69 | Walker->setType(carbon);
|
---|
| 70 | Walker->setPosition(Vector(1., 0., 1. ));
|
---|
[b70721] | 71 | TestMolecule->AddAtom(Walker);
|
---|
[8cbb97] | 72 |
|
---|
[23b547] | 73 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 74 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[d74077] | 75 | Walker->setType(carbon);
|
---|
| 76 | Walker->setPosition(Vector(0., 1., 1. ));
|
---|
[b70721] | 77 | TestMolecule->AddAtom(Walker);
|
---|
[8cbb97] | 78 |
|
---|
[23b547] | 79 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 80 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[d74077] | 81 | Walker->setType(carbon);
|
---|
| 82 | Walker->setPosition(Vector(1., 1., 0. ));
|
---|
[b70721] | 83 | TestMolecule->AddAtom(Walker);
|
---|
[8cbb97] | 84 |
|
---|
[23b547] | 85 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 86 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[d74077] | 87 | Walker->setType(carbon);
|
---|
| 88 | Walker->setPosition(Vector(0., 0., 0. ));
|
---|
[b70721] | 89 | TestMolecule->AddAtom(Walker);
|
---|
| 90 |
|
---|
| 91 | // check that TestMolecule was correctly constructed
|
---|
[ea7176] | 92 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 );
|
---|
[b70721] | 93 |
|
---|
| 94 | // create a small file with table
|
---|
[e5ad5c] | 95 | dummyname = new string("dummy.dat");
|
---|
[4eb4fe] | 96 | CPPUNIT_ASSERT(dummyname != NULL && "could not create string");
|
---|
[b70721] | 97 | filename = new string("test.dat");
|
---|
[4eb4fe] | 98 | CPPUNIT_ASSERT(filename != NULL && "could not create string");
|
---|
[b70721] | 99 | ofstream test(filename->c_str());
|
---|
[4eb4fe] | 100 | test << ".\tH\tHe\tLi\tBe\tB\tC\n";
|
---|
| 101 | test << "H\t1.\t1.\t1.\t1.\t1.\t1.2\n";
|
---|
| 102 | test << "He\t1.\t1.\t1.\t1.\t1.\t1.\n";
|
---|
| 103 | test << "Li\t1.\t1.\t1.\t1.\t1.\t1.\n";
|
---|
| 104 | test << "Be\t1.\t1.\t1.\t1.\t1.\t1.\n";
|
---|
| 105 | test << "B\t1.\t1.\t1.\t1.\t1.\t1.\n";
|
---|
| 106 | test << "C\t1.2\t1.\t1.\t1.\t1.\t1.5\n";
|
---|
[9a7186] | 107 | test.close();
|
---|
[b70721] | 108 | BG = new BondGraph(true);
|
---|
[4eb4fe] | 109 | CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
|
---|
[b70721] | 110 | };
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | void BondGraphTest::tearDown()
|
---|
| 114 | {
|
---|
| 115 | // remove the file
|
---|
| 116 | remove(filename->c_str());
|
---|
| 117 | delete(filename);
|
---|
[e5ad5c] | 118 | delete(dummyname);
|
---|
[b70721] | 119 | delete(BG);
|
---|
| 120 |
|
---|
| 121 | // remove molecule
|
---|
[23b547] | 122 | World::getInstance().destroyMolecule(TestMolecule);
|
---|
[a1510d] | 123 | // note that all the atoms, molecules, the tafel and the elements
|
---|
| 124 | // are all cleaned when the world is destroyed
|
---|
[23b547] | 125 | World::purgeInstance();
|
---|
[e6fdbe] | 126 | logger::purgeInstance();
|
---|
[b70721] | 127 | };
|
---|
| 128 |
|
---|
[9879f6] | 129 | /** Tests whether setup worked.
|
---|
| 130 | */
|
---|
| 131 | void BondGraphTest::SetupTest()
|
---|
| 132 | {
|
---|
| 133 | CPPUNIT_ASSERT_EQUAL (false, TestMolecule->empty());
|
---|
| 134 | CPPUNIT_ASSERT_EQUAL ((size_t)4, TestMolecule->size());
|
---|
| 135 | };
|
---|
| 136 |
|
---|
[b70721] | 137 | /** UnitTest for BondGraphTest::LoadBondLengthTable().
|
---|
| 138 | */
|
---|
| 139 | void BondGraphTest::LoadTableTest()
|
---|
| 140 | {
|
---|
[e138de] | 141 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
|
---|
[b70721] | 142 | CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
|
---|
[4eb4fe] | 143 | CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,5) );
|
---|
| 144 | CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(5,5) );
|
---|
[b70721] | 145 | };
|
---|
| 146 |
|
---|
| 147 | /** UnitTest for BondGraphTest::ConstructBondGraph().
|
---|
| 148 | */
|
---|
[e5ad5c] | 149 | void BondGraphTest::ConstructGraphFromTableTest()
|
---|
[b70721] | 150 | {
|
---|
[9879f6] | 151 | molecule::iterator Walker = TestMolecule->begin();
|
---|
| 152 | molecule::iterator Runner = TestMolecule->begin();
|
---|
| 153 | Runner++;
|
---|
[e138de] | 154 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
|
---|
| 155 | CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
|
---|
[9879f6] | 156 | CPPUNIT_ASSERT_EQUAL( true , (*Walker)->IsBondedTo((*Runner)) );
|
---|
[b70721] | 157 | };
|
---|
| 158 |
|
---|
[e5ad5c] | 159 | /** UnitTest for BondGraphTest::ConstructBondGraph().
|
---|
| 160 | */
|
---|
| 161 | void BondGraphTest::ConstructGraphFromCovalentRadiiTest()
|
---|
| 162 | {
|
---|
[a7b761b] | 163 |
|
---|
| 164 | //atom *Walker = TestMolecule->start->next;
|
---|
| 165 | //atom *Runner = TestMolecule->end->previous;
|
---|
| 166 | //CPPUNIT_ASSERT( TestMolecule->end != Walker );
|
---|
[e5ad5c] | 167 | CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(*dummyname) );
|
---|
| 168 | CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule) );
|
---|
[a7b761b] | 169 |
|
---|
| 170 | // this cannot be assured using dynamic IDs
|
---|
| 171 | //CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
|
---|
[e5ad5c] | 172 | };
|
---|
| 173 |
|
---|