[96c961] | 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>
|
---|
[49e1ae] | 16 | #include <cstring>
|
---|
[96c961] | 17 |
|
---|
[220cf37] | 18 | #include "analysis_bonds.hpp"
|
---|
| 19 | #include "analysisbondsunittest.hpp"
|
---|
[96c961] | 20 | #include "atom.hpp"
|
---|
| 21 | #include "bond.hpp"
|
---|
| 22 | #include "bondgraph.hpp"
|
---|
| 23 | #include "element.hpp"
|
---|
| 24 | #include "molecule.hpp"
|
---|
| 25 | #include "periodentafel.hpp"
|
---|
| 26 |
|
---|
[9b6b2f] | 27 | #ifdef HAVE_TESTRUNNER
|
---|
| 28 | #include "UnitTestMain.hpp"
|
---|
| 29 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 30 |
|
---|
[96c961] | 31 | /********************************************** Test classes **************************************/
|
---|
| 32 |
|
---|
| 33 | // Registers the fixture into the 'registry'
|
---|
| 34 | CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisBondsTest );
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | void AnalysisBondsTest::setUp()
|
---|
| 38 | {
|
---|
| 39 | atom *Walker = NULL;
|
---|
| 40 |
|
---|
| 41 | // init private all pointers to zero
|
---|
| 42 | TestMolecule = NULL;
|
---|
| 43 | hydrogen = NULL;
|
---|
| 44 | tafel = NULL;
|
---|
| 45 |
|
---|
| 46 | // construct element
|
---|
| 47 | hydrogen = new element;
|
---|
| 48 | hydrogen->Z = 1;
|
---|
[220cf37] | 49 | hydrogen->Valence = 1;
|
---|
| 50 | hydrogen->NoValenceOrbitals = 1;
|
---|
[96c961] | 51 | strcpy(hydrogen->name, "hydrogen");
|
---|
| 52 | strcpy(hydrogen->symbol, "H");
|
---|
| 53 | carbon = new element;
|
---|
| 54 | carbon->Z = 1;
|
---|
[220cf37] | 55 | carbon->Valence = 4;
|
---|
| 56 | carbon->NoValenceOrbitals = 4;
|
---|
[96c961] | 57 | strcpy(carbon->name, "carbon");
|
---|
| 58 | strcpy(carbon->symbol, "C");
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | // construct periodentafel
|
---|
| 62 | tafel = new periodentafel;
|
---|
| 63 | tafel->AddElement(hydrogen);
|
---|
| 64 | tafel->AddElement(carbon);
|
---|
| 65 |
|
---|
| 66 | // construct molecule (tetraeder of hydrogens)
|
---|
| 67 | TestMolecule = new molecule(tafel);
|
---|
| 68 | Walker = new atom();
|
---|
| 69 | Walker->type = hydrogen;
|
---|
[220cf37] | 70 | Walker->node->Init(1.5, 0., 1.5 );
|
---|
[96c961] | 71 | TestMolecule->AddAtom(Walker);
|
---|
| 72 | Walker = new atom();
|
---|
| 73 | Walker->type = hydrogen;
|
---|
[220cf37] | 74 | Walker->node->Init(0., 1.5, 1.5 );
|
---|
[96c961] | 75 | TestMolecule->AddAtom(Walker);
|
---|
| 76 | Walker = new atom();
|
---|
| 77 | Walker->type = hydrogen;
|
---|
[220cf37] | 78 | Walker->node->Init(1.5, 1.5, 0. );
|
---|
[96c961] | 79 | TestMolecule->AddAtom(Walker);
|
---|
| 80 | Walker = new atom();
|
---|
| 81 | Walker->type = hydrogen;
|
---|
| 82 | Walker->node->Init(0., 0., 0. );
|
---|
| 83 | TestMolecule->AddAtom(Walker);
|
---|
[220cf37] | 84 | Walker = new atom();
|
---|
| 85 | Walker->type = carbon;
|
---|
| 86 | Walker->node->Init(0.5, 0.5, 0.5 );
|
---|
| 87 | TestMolecule->AddAtom(Walker);
|
---|
[96c961] | 88 |
|
---|
| 89 | // check that TestMolecule was correctly constructed
|
---|
[220cf37] | 90 | CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 5 );
|
---|
[96c961] | 91 |
|
---|
| 92 | // create a small file with table
|
---|
| 93 | filename = new string("test.dat");
|
---|
| 94 | ofstream test(filename->c_str());
|
---|
| 95 | test << ".\tH\tC\n";
|
---|
| 96 | test << "H\t1.\t1.2\n";
|
---|
| 97 | test << "C\t1.2\t1.5\n";
|
---|
[220cf37] | 98 | test.close();
|
---|
[96c961] | 99 | BG = new BondGraph(true);
|
---|
[220cf37] | 100 |
|
---|
| 101 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
|
---|
| 102 | CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
|
---|
| 103 | CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) );
|
---|
| 104 | CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) );
|
---|
| 105 |
|
---|
| 106 | BG->ConstructBondGraph(TestMolecule);
|
---|
[96c961] | 107 | };
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | void AnalysisBondsTest::tearDown()
|
---|
| 111 | {
|
---|
| 112 | // remove the file
|
---|
| 113 | remove(filename->c_str());
|
---|
| 114 | delete(filename);
|
---|
| 115 | delete(BG);
|
---|
| 116 |
|
---|
| 117 | // remove molecule
|
---|
| 118 | delete(TestMolecule);
|
---|
| 119 | // note that all the atoms are cleaned by TestMolecule
|
---|
| 120 | delete(tafel);
|
---|
| 121 | // note that element is cleaned by periodentafel
|
---|
| 122 | };
|
---|
| 123 |
|
---|
[220cf37] | 124 | /** UnitTest for GetMaxMinMeanBondCount().
|
---|
[96c961] | 125 | */
|
---|
[220cf37] | 126 | void AnalysisBondsTest::GetMaxMinMeanBondCountTest()
|
---|
[96c961] | 127 | {
|
---|
[220cf37] | 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 );
|
---|
[96c961] | 135 |
|
---|
[220cf37] | 136 | };
|
---|
[96c961] | 137 |
|
---|
[220cf37] | 138 | /** UnitTest for MinMaxBondDistanceBetweenElements().
|
---|
| 139 | */
|
---|
| 140 | void 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 );
|
---|
[96c961] | 168 | };
|
---|