source: molecuilder/src/unittests/analysisbondsunittest.cpp@ 5dba7a

Last change on this file since 5dba7a was 5dba7a, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Made the periodentafel use STL-containers instead of custom llists

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[341850]1/*
2 * analysisbondsunittest.cpp
3 *
4 * Created on: Nov 7, 2009
5 * Author: heber
6 */
7
8using 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>
[e6fe8a]16#include <cstring>
[341850]17
[7bfc19]18#include "World.hpp"
[e462b3]19#include "analysis_bonds.hpp"
20#include "analysisbondsunittest.hpp"
[341850]21#include "atom.hpp"
22#include "bond.hpp"
23#include "bondgraph.hpp"
24#include "element.hpp"
25#include "molecule.hpp"
26#include "periodentafel.hpp"
27
[44becc]28#ifdef HAVE_TESTRUNNER
29#include "UnitTestMain.hpp"
30#endif /*HAVE_TESTRUNNER*/
31
[341850]32/********************************************** Test classes **************************************/
33
34// Registers the fixture into the 'registry'
35CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisBondsTest );
36
37
38void AnalysisBondsTest::setUp()
39{
40 atom *Walker = NULL;
41
42 // init private all pointers to zero
43 TestMolecule = NULL;
44 hydrogen = NULL;
45 tafel = NULL;
46
47 // construct element
48 hydrogen = new element;
49 hydrogen->Z = 1;
[e462b3]50 hydrogen->Valence = 1;
51 hydrogen->NoValenceOrbitals = 1;
[341850]52 strcpy(hydrogen->name, "hydrogen");
53 strcpy(hydrogen->symbol, "H");
54 carbon = new element;
[5dba7a]55 carbon->Z = 2;
[e462b3]56 carbon->Valence = 4;
57 carbon->NoValenceOrbitals = 4;
[341850]58 strcpy(carbon->name, "carbon");
59 strcpy(carbon->symbol, "C");
60
61
62 // construct periodentafel
[4c60ef]63 tafel = World::getInstance().getPeriode();
[341850]64 tafel->AddElement(hydrogen);
65 tafel->AddElement(carbon);
66
67 // construct molecule (tetraeder of hydrogens)
[4c60ef]68 TestMolecule = World::getInstance().createMolecule();
69 Walker = World::getInstance().createAtom();
[341850]70 Walker->type = hydrogen;
[e462b3]71 Walker->node->Init(1.5, 0., 1.5 );
[341850]72 TestMolecule->AddAtom(Walker);
[4c60ef]73 Walker = World::getInstance().createAtom();
[341850]74 Walker->type = hydrogen;
[e462b3]75 Walker->node->Init(0., 1.5, 1.5 );
[341850]76 TestMolecule->AddAtom(Walker);
[4c60ef]77 Walker = World::getInstance().createAtom();
[341850]78 Walker->type = hydrogen;
[e462b3]79 Walker->node->Init(1.5, 1.5, 0. );
[341850]80 TestMolecule->AddAtom(Walker);
[4c60ef]81 Walker = World::getInstance().createAtom();
[341850]82 Walker->type = hydrogen;
83 Walker->node->Init(0., 0., 0. );
84 TestMolecule->AddAtom(Walker);
[4c60ef]85 Walker = World::getInstance().createAtom();
[e462b3]86 Walker->type = carbon;
87 Walker->node->Init(0.5, 0.5, 0.5 );
88 TestMolecule->AddAtom(Walker);
[341850]89
90 // check that TestMolecule was correctly constructed
[e462b3]91 CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 5 );
[341850]92
93 // create a small file with table
94 filename = new string("test.dat");
95 ofstream test(filename->c_str());
96 test << ".\tH\tC\n";
97 test << "H\t1.\t1.2\n";
98 test << "C\t1.2\t1.5\n";
[e462b3]99 test.close();
[341850]100 BG = new BondGraph(true);
[e462b3]101
102 CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
103 CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
104 CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) );
105 CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) );
106
107 BG->ConstructBondGraph(TestMolecule);
[341850]108};
109
110
111void AnalysisBondsTest::tearDown()
112{
113 // remove the file
114 remove(filename->c_str());
115 delete(filename);
116 delete(BG);
117
118 // remove molecule
[4c60ef]119 World::getInstance().destroyMolecule(TestMolecule);
[341850]120 // note that all the atoms are cleaned by TestMolecule
[4c60ef]121 World::purgeInstance();
[341850]122};
123
[e462b3]124/** UnitTest for GetMaxMinMeanBondCount().
[341850]125 */
[e462b3]126void AnalysisBondsTest::GetMaxMinMeanBondCountTest()
[341850]127{
[e462b3]128 double Min = 20.; // check that initialization resets these arbitrary values
129 double Mean = 200.;
130 double Max = 1e-6;
131 GetMaxMinMeanBondCount(TestMolecule, Min, Mean, Max);
132 CPPUNIT_ASSERT_EQUAL( 1., Min );
133 CPPUNIT_ASSERT_EQUAL( 1.6, Mean );
134 CPPUNIT_ASSERT_EQUAL( 4., Max );
[341850]135
[e462b3]136};
[341850]137
[e462b3]138/** UnitTest for MinMaxBondDistanceBetweenElements().
139 */
140void AnalysisBondsTest::MinMeanMaxBondDistanceBetweenElementsTest()
141{
142 double Min = 20.; // check that initialization resets these arbitrary values
143 double Mean = 2e+6;
144 double Max = 1e-6;
145 double Min2 = 20.;
146 double Mean2 = 2e+6;
147 double Max2 = 1e-6;
148 const double maxbondlength = sqrt(1.*1. + 1.*1. + .5*.5);
149 const double minbondlength = sqrt(.5*.5 + .5*.5 + .5*.5);
150 const double meanbondlength = (minbondlength+3.*maxbondlength)/4.;
151 // check bond lengths C-H
152 MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, carbon, Min, Mean, Max);
153 CPPUNIT_ASSERT_EQUAL( minbondlength , Min );
154 CPPUNIT_ASSERT_EQUAL( meanbondlength , Mean );
155 CPPUNIT_ASSERT_EQUAL( maxbondlength , Max );
156
157 // check that elements are symmetric, i.e. C-H == H-C
158 MinMeanMaxBondDistanceBetweenElements(TestMolecule, carbon, hydrogen, Min2, Mean2, Max2);
159 CPPUNIT_ASSERT_EQUAL( Min , Min2 );
160 CPPUNIT_ASSERT_EQUAL( Mean , Mean2 );
161 CPPUNIT_ASSERT_EQUAL( Max , Max2 );
162
163 // check no bond case (no bonds H-H in system!)
164 MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, hydrogen, Min, Mean, Max);
165 CPPUNIT_ASSERT_EQUAL( 0. , Min );
166 CPPUNIT_ASSERT_EQUAL( 0. , Mean );
167 CPPUNIT_ASSERT_EQUAL( 0. , Max );
[341850]168};
Note: See TracBrowser for help on using the repository browser.