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 |
|
---|
8 | /*
|
---|
9 | * BondGraphUnitTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Oct 29, 2009
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
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>
|
---|
28 | #include <cstring>
|
---|
29 |
|
---|
30 | #include "CodePatterns/Assert.hpp"
|
---|
31 |
|
---|
32 | #include "atom.hpp"
|
---|
33 | #include "Bond/bond.hpp"
|
---|
34 | #include "CodePatterns/Log.hpp"
|
---|
35 | #include "Element/element.hpp"
|
---|
36 | #include "Graph/BondGraph.hpp"
|
---|
37 | #include "molecule.hpp"
|
---|
38 | #include "Element/periodentafel.hpp"
|
---|
39 | #include "World.hpp"
|
---|
40 | #include "WorldTime.hpp"
|
---|
41 |
|
---|
42 | #include "BondGraphUnitTest.hpp"
|
---|
43 |
|
---|
44 | #ifdef HAVE_TESTRUNNER
|
---|
45 | #include "UnitTestMain.hpp"
|
---|
46 | #endif /*HAVE_TESTRUNNER*/
|
---|
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
|
---|
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");
|
---|
63 |
|
---|
64 | // construct molecule (tetraeder of hydrogens)
|
---|
65 | TestMolecule = World::getInstance().createMolecule();
|
---|
66 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
|
---|
67 | Walker = World::getInstance().createAtom();
|
---|
68 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
69 | Walker->setType(carbon);
|
---|
70 | Walker->setPosition(Vector(1., 0., 1. ));
|
---|
71 | TestMolecule->AddAtom(Walker);
|
---|
72 |
|
---|
73 | Walker = World::getInstance().createAtom();
|
---|
74 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
75 | Walker->setType(carbon);
|
---|
76 | Walker->setPosition(Vector(0., 1., 1. ));
|
---|
77 | TestMolecule->AddAtom(Walker);
|
---|
78 |
|
---|
79 | Walker = World::getInstance().createAtom();
|
---|
80 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
81 | Walker->setType(carbon);
|
---|
82 | Walker->setPosition(Vector(1., 1., 0. ));
|
---|
83 | TestMolecule->AddAtom(Walker);
|
---|
84 |
|
---|
85 | Walker = World::getInstance().createAtom();
|
---|
86 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
87 | Walker->setType(carbon);
|
---|
88 | Walker->setPosition(Vector(0., 0., 0. ));
|
---|
89 | TestMolecule->AddAtom(Walker);
|
---|
90 |
|
---|
91 | // check that TestMolecule was correctly constructed
|
---|
92 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 4 );
|
---|
93 |
|
---|
94 | // create stream with table
|
---|
95 | test << ".\tH\tHe\tLi\tBe\tB\tC\n";
|
---|
96 | test << "H\t1.\t1.\t1.\t1.\t1.\t1.2\n";
|
---|
97 | test << "He\t1.\t1.\t1.\t1.\t1.\t1.\n";
|
---|
98 | test << "Li\t1.\t1.\t1.\t1.\t1.\t1.\n";
|
---|
99 | test << "Be\t1.\t1.\t1.\t1.\t1.\t1.\n";
|
---|
100 | test << "B\t1.\t1.\t1.\t1.\t1.\t1.\n";
|
---|
101 | test << "C\t1.2\t1.\t1.\t1.\t1.\t1.5\n";
|
---|
102 | // created bad stream (i.e. non-present file)
|
---|
103 | dummy.setstate(ios::eofbit);
|
---|
104 | CPPUNIT_ASSERT(dummy.eof());
|
---|
105 | BG = new BondGraph(true);
|
---|
106 | CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
|
---|
107 | };
|
---|
108 |
|
---|
109 |
|
---|
110 | void BondGraphTest::tearDown()
|
---|
111 | {
|
---|
112 | // remove the file
|
---|
113 | delete(BG);
|
---|
114 |
|
---|
115 | // remove molecule
|
---|
116 | World::getInstance().destroyMolecule(TestMolecule);
|
---|
117 | // note that all the atoms, molecules, the tafel and the elements
|
---|
118 | // are all cleaned when the world is destroyed
|
---|
119 | World::purgeInstance();
|
---|
120 | logger::purgeInstance();
|
---|
121 | };
|
---|
122 |
|
---|
123 | /** Tests whether setup worked.
|
---|
124 | */
|
---|
125 | void BondGraphTest::SetupTest()
|
---|
126 | {
|
---|
127 | CPPUNIT_ASSERT_EQUAL (false, TestMolecule->empty());
|
---|
128 | CPPUNIT_ASSERT_EQUAL ((size_t)4, TestMolecule->size());
|
---|
129 | };
|
---|
130 |
|
---|
131 | /** UnitTest for BondGraphTest::LoadBondLengthTable().
|
---|
132 | */
|
---|
133 | void BondGraphTest::LoadTableTest()
|
---|
134 | {
|
---|
135 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) );
|
---|
136 | CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
|
---|
137 | CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,5) );
|
---|
138 | CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(5,5) );
|
---|
139 | };
|
---|
140 |
|
---|
141 | /** UnitTest for BondGraphTest::CreateAdjacency().
|
---|
142 | */
|
---|
143 | void BondGraphTest::ConstructGraphFromTableTest()
|
---|
144 | {
|
---|
145 | molecule::iterator Walker = TestMolecule->begin();
|
---|
146 | molecule::iterator Runner = TestMolecule->begin();
|
---|
147 | Runner++;
|
---|
148 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(test) );
|
---|
149 | molecule::atomVector Set = TestMolecule->getAtomSet();
|
---|
150 | BG->CreateAdjacency(Set);
|
---|
151 | CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0);
|
---|
152 | CPPUNIT_ASSERT_EQUAL( true , (*Walker)->IsBondedTo(WorldTime::getTime(), (*Runner)) );
|
---|
153 | };
|
---|
154 |
|
---|
155 | /** UnitTest for BondGraphTest::CreateAdjacency().
|
---|
156 | */
|
---|
157 | void BondGraphTest::ConstructGraphFromCovalentRadiiTest()
|
---|
158 | {
|
---|
159 |
|
---|
160 | CPPUNIT_ASSERT_EQUAL( false , BG->LoadBondLengthTable(dummy) );
|
---|
161 | molecule::atomVector Set = TestMolecule->getAtomSet();
|
---|
162 | BG->CreateAdjacency(Set);
|
---|
163 | CPPUNIT_ASSERT( TestMolecule->getBondCount() != 0);
|
---|
164 |
|
---|
165 | // this cannot be assured using dynamic IDs
|
---|
166 | //CPPUNIT_ASSERT_EQUAL( true , Walker->IsBondedTo(Runner) );
|
---|
167 | };
|
---|
168 |
|
---|