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