| 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 |  | 
|---|
| 28 | #ifdef HAVE_TESTRUNNER | 
|---|
| 29 | #include "UnitTestMain.hpp" | 
|---|
| 30 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 31 |  | 
|---|
| 32 | /********************************************** Test classes **************************************/ | 
|---|
| 33 |  | 
|---|
| 34 | // Registers the fixture into the 'registry' | 
|---|
| 35 | CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisBondsTest ); | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | void 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; | 
|---|
| 50 | hydrogen->Valence = 1; | 
|---|
| 51 | hydrogen->NoValenceOrbitals = 1; | 
|---|
| 52 | strcpy(hydrogen->name, "hydrogen"); | 
|---|
| 53 | strcpy(hydrogen->symbol, "H"); | 
|---|
| 54 | carbon = new element; | 
|---|
| 55 | carbon->Z = 2; | 
|---|
| 56 | carbon->Valence = 4; | 
|---|
| 57 | carbon->NoValenceOrbitals = 4; | 
|---|
| 58 | strcpy(carbon->name, "carbon"); | 
|---|
| 59 | strcpy(carbon->symbol, "C"); | 
|---|
| 60 |  | 
|---|
| 61 |  | 
|---|
| 62 | // construct periodentafel | 
|---|
| 63 | tafel = World::getInstance().getPeriode(); | 
|---|
| 64 | tafel->AddElement(hydrogen); | 
|---|
| 65 | tafel->AddElement(carbon); | 
|---|
| 66 |  | 
|---|
| 67 | // construct molecule (tetraeder of hydrogens) | 
|---|
| 68 | TestMolecule = World::getInstance().createMolecule(); | 
|---|
| 69 | Walker = World::getInstance().createAtom(); | 
|---|
| 70 | Walker->type = hydrogen; | 
|---|
| 71 | *Walker->node = Vector(1.5, 0., 1.5 ); | 
|---|
| 72 | TestMolecule->AddAtom(Walker); | 
|---|
| 73 | Walker = World::getInstance().createAtom(); | 
|---|
| 74 | Walker->type = hydrogen; | 
|---|
| 75 | *Walker->node = Vector(0., 1.5, 1.5 ); | 
|---|
| 76 | TestMolecule->AddAtom(Walker); | 
|---|
| 77 | Walker = World::getInstance().createAtom(); | 
|---|
| 78 | Walker->type = hydrogen; | 
|---|
| 79 | *Walker->node = Vector(1.5, 1.5, 0. ); | 
|---|
| 80 | TestMolecule->AddAtom(Walker); | 
|---|
| 81 | Walker = World::getInstance().createAtom(); | 
|---|
| 82 | Walker->type = hydrogen; | 
|---|
| 83 | *Walker->node = Vector(0., 0., 0. ); | 
|---|
| 84 | TestMolecule->AddAtom(Walker); | 
|---|
| 85 | Walker = World::getInstance().createAtom(); | 
|---|
| 86 | Walker->type = carbon; | 
|---|
| 87 | *Walker->node = Vector(0.5, 0.5, 0.5 ); | 
|---|
| 88 | TestMolecule->AddAtom(Walker); | 
|---|
| 89 |  | 
|---|
| 90 | // check that TestMolecule was correctly constructed | 
|---|
| 91 | CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 5 ); | 
|---|
| 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"; | 
|---|
| 99 | test.close(); | 
|---|
| 100 | BG = new BondGraph(true); | 
|---|
| 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); | 
|---|
| 108 | }; | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 | void AnalysisBondsTest::tearDown() | 
|---|
| 112 | { | 
|---|
| 113 | // remove the file | 
|---|
| 114 | remove(filename->c_str()); | 
|---|
| 115 | delete(filename); | 
|---|
| 116 | delete(BG); | 
|---|
| 117 |  | 
|---|
| 118 | // remove molecule | 
|---|
| 119 | World::getInstance().destroyMolecule(TestMolecule); | 
|---|
| 120 | // note that all the atoms are cleaned by TestMolecule | 
|---|
| 121 | World::purgeInstance(); | 
|---|
| 122 | }; | 
|---|
| 123 |  | 
|---|
| 124 | /** UnitTest for GetMaxMinMeanBondCount(). | 
|---|
| 125 | */ | 
|---|
| 126 | void AnalysisBondsTest::GetMaxMinMeanBondCountTest() | 
|---|
| 127 | { | 
|---|
| 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 ); | 
|---|
| 135 |  | 
|---|
| 136 | }; | 
|---|
| 137 |  | 
|---|
| 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 ); | 
|---|
| 168 | }; | 
|---|