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