[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 |
|
---|
[46d958] | 18 | #include "World.hpp"
|
---|
[220cf37] | 19 | #include "analysis_bonds.hpp"
|
---|
| 20 | #include "analysisbondsunittest.hpp"
|
---|
[96c961] | 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 |
|
---|
| 28 | /********************************************** Test classes **************************************/
|
---|
| 29 |
|
---|
| 30 | // Registers the fixture into the 'registry'
|
---|
| 31 | CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisBondsTest );
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | void AnalysisBondsTest::setUp()
|
---|
| 35 | {
|
---|
| 36 | atom *Walker = NULL;
|
---|
| 37 |
|
---|
| 38 | // init private all pointers to zero
|
---|
| 39 | TestMolecule = NULL;
|
---|
| 40 | hydrogen = NULL;
|
---|
| 41 | tafel = NULL;
|
---|
| 42 |
|
---|
| 43 | // construct element
|
---|
| 44 | hydrogen = new element;
|
---|
| 45 | hydrogen->Z = 1;
|
---|
[220cf37] | 46 | hydrogen->Valence = 1;
|
---|
| 47 | hydrogen->NoValenceOrbitals = 1;
|
---|
[96c961] | 48 | strcpy(hydrogen->name, "hydrogen");
|
---|
| 49 | strcpy(hydrogen->symbol, "H");
|
---|
| 50 | carbon = new element;
|
---|
| 51 | carbon->Z = 1;
|
---|
[220cf37] | 52 | carbon->Valence = 4;
|
---|
| 53 | carbon->NoValenceOrbitals = 4;
|
---|
[96c961] | 54 | strcpy(carbon->name, "carbon");
|
---|
| 55 | strcpy(carbon->symbol, "C");
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | // construct periodentafel
|
---|
| 59 | tafel = new periodentafel;
|
---|
| 60 | tafel->AddElement(hydrogen);
|
---|
| 61 | tafel->AddElement(carbon);
|
---|
| 62 |
|
---|
| 63 | // construct molecule (tetraeder of hydrogens)
|
---|
| 64 | TestMolecule = new molecule(tafel);
|
---|
[46d958] | 65 | Walker = World::get()->createAtom();
|
---|
[96c961] | 66 | Walker->type = hydrogen;
|
---|
[220cf37] | 67 | Walker->node->Init(1.5, 0., 1.5 );
|
---|
[96c961] | 68 | TestMolecule->AddAtom(Walker);
|
---|
[46d958] | 69 | Walker = World::get()->createAtom();
|
---|
[96c961] | 70 | Walker->type = hydrogen;
|
---|
[220cf37] | 71 | Walker->node->Init(0., 1.5, 1.5 );
|
---|
[96c961] | 72 | TestMolecule->AddAtom(Walker);
|
---|
[46d958] | 73 | Walker = World::get()->createAtom();
|
---|
[96c961] | 74 | Walker->type = hydrogen;
|
---|
[220cf37] | 75 | Walker->node->Init(1.5, 1.5, 0. );
|
---|
[96c961] | 76 | TestMolecule->AddAtom(Walker);
|
---|
[46d958] | 77 | Walker = World::get()->createAtom();
|
---|
[96c961] | 78 | Walker->type = hydrogen;
|
---|
| 79 | Walker->node->Init(0., 0., 0. );
|
---|
| 80 | TestMolecule->AddAtom(Walker);
|
---|
[46d958] | 81 | Walker = World::get()->createAtom();
|
---|
[220cf37] | 82 | Walker->type = carbon;
|
---|
| 83 | Walker->node->Init(0.5, 0.5, 0.5 );
|
---|
| 84 | TestMolecule->AddAtom(Walker);
|
---|
[96c961] | 85 |
|
---|
| 86 | // check that TestMolecule was correctly constructed
|
---|
[220cf37] | 87 | CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 5 );
|
---|
[96c961] | 88 |
|
---|
| 89 | // create a small file with table
|
---|
| 90 | filename = new string("test.dat");
|
---|
| 91 | ofstream test(filename->c_str());
|
---|
| 92 | test << ".\tH\tC\n";
|
---|
| 93 | test << "H\t1.\t1.2\n";
|
---|
| 94 | test << "C\t1.2\t1.5\n";
|
---|
[220cf37] | 95 | test.close();
|
---|
[96c961] | 96 | BG = new BondGraph(true);
|
---|
[220cf37] | 97 |
|
---|
| 98 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
|
---|
| 99 | CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
|
---|
| 100 | CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) );
|
---|
| 101 | CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) );
|
---|
| 102 |
|
---|
| 103 | BG->ConstructBondGraph(TestMolecule);
|
---|
[96c961] | 104 | };
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | void AnalysisBondsTest::tearDown()
|
---|
| 108 | {
|
---|
| 109 | // remove the file
|
---|
| 110 | remove(filename->c_str());
|
---|
| 111 | delete(filename);
|
---|
| 112 | delete(BG);
|
---|
| 113 |
|
---|
| 114 | // remove molecule
|
---|
| 115 | delete(TestMolecule);
|
---|
| 116 | // note that all the atoms are cleaned by TestMolecule
|
---|
| 117 | delete(tafel);
|
---|
| 118 | // note that element is cleaned by periodentafel
|
---|
| 119 | };
|
---|
| 120 |
|
---|
[220cf37] | 121 | /** UnitTest for GetMaxMinMeanBondCount().
|
---|
[96c961] | 122 | */
|
---|
[220cf37] | 123 | void AnalysisBondsTest::GetMaxMinMeanBondCountTest()
|
---|
[96c961] | 124 | {
|
---|
[220cf37] | 125 | double Min = 20.; // check that initialization resets these arbitrary values
|
---|
| 126 | double Mean = 200.;
|
---|
| 127 | double Max = 1e-6;
|
---|
| 128 | GetMaxMinMeanBondCount(TestMolecule, Min, Mean, Max);
|
---|
| 129 | CPPUNIT_ASSERT_EQUAL( 1., Min );
|
---|
| 130 | CPPUNIT_ASSERT_EQUAL( 1.6, Mean );
|
---|
| 131 | CPPUNIT_ASSERT_EQUAL( 4., Max );
|
---|
[96c961] | 132 |
|
---|
[220cf37] | 133 | };
|
---|
[96c961] | 134 |
|
---|
[220cf37] | 135 | /** UnitTest for MinMaxBondDistanceBetweenElements().
|
---|
| 136 | */
|
---|
| 137 | void AnalysisBondsTest::MinMeanMaxBondDistanceBetweenElementsTest()
|
---|
| 138 | {
|
---|
| 139 | double Min = 20.; // check that initialization resets these arbitrary values
|
---|
| 140 | double Mean = 2e+6;
|
---|
| 141 | double Max = 1e-6;
|
---|
| 142 | double Min2 = 20.;
|
---|
| 143 | double Mean2 = 2e+6;
|
---|
| 144 | double Max2 = 1e-6;
|
---|
| 145 | const double maxbondlength = sqrt(1.*1. + 1.*1. + .5*.5);
|
---|
| 146 | const double minbondlength = sqrt(.5*.5 + .5*.5 + .5*.5);
|
---|
| 147 | const double meanbondlength = (minbondlength+3.*maxbondlength)/4.;
|
---|
| 148 | // check bond lengths C-H
|
---|
| 149 | MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, carbon, Min, Mean, Max);
|
---|
| 150 | CPPUNIT_ASSERT_EQUAL( minbondlength , Min );
|
---|
| 151 | CPPUNIT_ASSERT_EQUAL( meanbondlength , Mean );
|
---|
| 152 | CPPUNIT_ASSERT_EQUAL( maxbondlength , Max );
|
---|
| 153 |
|
---|
| 154 | // check that elements are symmetric, i.e. C-H == H-C
|
---|
| 155 | MinMeanMaxBondDistanceBetweenElements(TestMolecule, carbon, hydrogen, Min2, Mean2, Max2);
|
---|
| 156 | CPPUNIT_ASSERT_EQUAL( Min , Min2 );
|
---|
| 157 | CPPUNIT_ASSERT_EQUAL( Mean , Mean2 );
|
---|
| 158 | CPPUNIT_ASSERT_EQUAL( Max , Max2 );
|
---|
| 159 |
|
---|
| 160 | // check no bond case (no bonds H-H in system!)
|
---|
| 161 | MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, hydrogen, Min, Mean, Max);
|
---|
| 162 | CPPUNIT_ASSERT_EQUAL( 0. , Min );
|
---|
| 163 | CPPUNIT_ASSERT_EQUAL( 0. , Mean );
|
---|
| 164 | CPPUNIT_ASSERT_EQUAL( 0. , Max );
|
---|
[96c961] | 165 | };
|
---|
| 166 |
|
---|
[220cf37] | 167 |
|
---|
[96c961] | 168 | /********************************************** Main routine **************************************/
|
---|
| 169 |
|
---|
| 170 | int main(int argc, char **argv)
|
---|
| 171 | {
|
---|
| 172 | // Get the top level suite from the registry
|
---|
| 173 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
| 174 |
|
---|
| 175 | // Adds the test to the list of test to run
|
---|
| 176 | CppUnit::TextUi::TestRunner runner;
|
---|
| 177 | runner.addTest( suite );
|
---|
| 178 |
|
---|
| 179 | // Change the default outputter to a compiler error format outputter
|
---|
| 180 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
| 181 | std::cerr ) );
|
---|
| 182 | // Run the tests.
|
---|
| 183 | bool wasSucessful = runner.run();
|
---|
| 184 |
|
---|
| 185 | // Return error code 1 if the one of test failed.
|
---|
| 186 | return wasSucessful ? 0 : 1;
|
---|
| 187 | };
|
---|