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