| 1 | /*
 | 
|---|
| 2 |  * CountBondsUnitTest.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Mar 30, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | 
 | 
|---|
| 9 | using namespace std;
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 12 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 13 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include <iostream>
 | 
|---|
| 16 | #include <stdio.h>
 | 
|---|
| 17 | #include <cstring>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "analysis_bonds.hpp"
 | 
|---|
| 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 | #include "CountBondsUnitTest.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | /********************************************** Test classes **************************************/
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // Registers the fixture into the 'registry'
 | 
|---|
| 31 | CPPUNIT_TEST_SUITE_REGISTRATION( CountBondsTest );
 | 
|---|
| 32 | 
 | 
|---|
| 33 | 
 | 
|---|
| 34 | void CountBondsTest::setUp()
 | 
|---|
| 35 | {
 | 
|---|
| 36 |   atom *Walker = NULL;
 | 
|---|
| 37 | 
 | 
|---|
| 38 |   // init private all pointers to zero
 | 
|---|
| 39 |   molecules = NULL;
 | 
|---|
| 40 |   TestMolecule1 = NULL;
 | 
|---|
| 41 |   TestMolecule2 = NULL;
 | 
|---|
| 42 |   hydrogen = NULL;
 | 
|---|
| 43 |   oxygen = NULL;
 | 
|---|
| 44 |   tafel = NULL;
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   // construct element
 | 
|---|
| 47 |   hydrogen = new element;
 | 
|---|
| 48 |   hydrogen->Z = 1;
 | 
|---|
| 49 |   hydrogen->CovalentRadius = 0.23;
 | 
|---|
| 50 |   strcpy(hydrogen->name, "hydrogen");
 | 
|---|
| 51 |   strcpy(hydrogen->symbol, "H");
 | 
|---|
| 52 |   oxygen = new element;
 | 
|---|
| 53 |   oxygen->Z = 8;
 | 
|---|
| 54 |   oxygen->CovalentRadius = 0.68;
 | 
|---|
| 55 |   strcpy(oxygen->name, "oxygen");
 | 
|---|
| 56 |   strcpy(oxygen->symbol, "O");
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   // construct periodentafel
 | 
|---|
| 59 |   tafel = new periodentafel;
 | 
|---|
| 60 |   tafel->AddElement(hydrogen);
 | 
|---|
| 61 |   tafel->AddElement(oxygen);
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   // construct molecule (water molecule)
 | 
|---|
| 64 |   molecules = new MoleculeListClass;
 | 
|---|
| 65 |   TestMolecule1 = new molecule(tafel);
 | 
|---|
| 66 |   Walker = new atom();
 | 
|---|
| 67 |   Walker->type = hydrogen;
 | 
|---|
| 68 |   Walker->node->Init(-0.2418, 0.9350, 0. );
 | 
|---|
| 69 |   TestMolecule1->AddAtom(Walker);
 | 
|---|
| 70 |   Walker = new atom();
 | 
|---|
| 71 |   Walker->type = hydrogen;
 | 
|---|
| 72 |   Walker->node->Init(0.9658, 0., 0. );
 | 
|---|
| 73 |   TestMolecule1->AddAtom(Walker);
 | 
|---|
| 74 |   Walker = new atom();
 | 
|---|
| 75 |   Walker->type = oxygen;
 | 
|---|
| 76 |   Walker->node->Init(0., 0., 0. );
 | 
|---|
| 77 |   TestMolecule1->AddAtom(Walker);
 | 
|---|
| 78 |   molecules->insert(TestMolecule1);
 | 
|---|
| 79 | 
 | 
|---|
| 80 |   TestMolecule2 = new molecule(tafel);
 | 
|---|
| 81 |   Walker = new atom();
 | 
|---|
| 82 |   Walker->type = hydrogen;
 | 
|---|
| 83 |   Walker->node->Init(-0.2418, 0.9350, 0. );
 | 
|---|
| 84 |   TestMolecule2->AddAtom(Walker);
 | 
|---|
| 85 |   Walker = new atom();
 | 
|---|
| 86 |   Walker->type = hydrogen;
 | 
|---|
| 87 |   Walker->node->Init(0.9658, 0., 0. );
 | 
|---|
| 88 |   TestMolecule2->AddAtom(Walker);
 | 
|---|
| 89 |   Walker = new atom();
 | 
|---|
| 90 |   Walker->type = oxygen;
 | 
|---|
| 91 |   Walker->node->Init(0., 0., 0. );
 | 
|---|
| 92 |   TestMolecule2->AddAtom(Walker);
 | 
|---|
| 93 |   molecules->insert(TestMolecule2);
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   // check that TestMolecule was correctly constructed
 | 
|---|
| 96 |   CPPUNIT_ASSERT_EQUAL( TestMolecule1->AtomCount, 3 );
 | 
|---|
| 97 |   Walker = TestMolecule1->start->next;
 | 
|---|
| 98 |   CPPUNIT_ASSERT( TestMolecule1->end != Walker );
 | 
|---|
| 99 |   CPPUNIT_ASSERT_EQUAL( TestMolecule2->AtomCount, 3 );
 | 
|---|
| 100 |   Walker = TestMolecule2->start->next;
 | 
|---|
| 101 |   CPPUNIT_ASSERT( TestMolecule2->end != Walker );
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   // create a small file with table
 | 
|---|
| 104 |   BG = new BondGraph(true);
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   // construct bond graphs
 | 
|---|
| 107 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule1) );
 | 
|---|
| 108 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule2) );
 | 
|---|
| 109 | //  TestMolecule1->Output((ofstream *)&cout);
 | 
|---|
| 110 | //  TestMolecule1->OutputBondsList();
 | 
|---|
| 111 | };
 | 
|---|
| 112 | 
 | 
|---|
| 113 | 
 | 
|---|
| 114 | void CountBondsTest::tearDown()
 | 
|---|
| 115 | {
 | 
|---|
| 116 |   // remove the file
 | 
|---|
| 117 |   delete(BG);
 | 
|---|
| 118 | 
 | 
|---|
| 119 |   // remove molecule
 | 
|---|
| 120 |   delete(molecules);
 | 
|---|
| 121 |   // note that all the atoms are cleaned by TestMolecule
 | 
|---|
| 122 |   delete(tafel);
 | 
|---|
| 123 |   // note that element is cleaned by periodentafel
 | 
|---|
| 124 | };
 | 
|---|
| 125 | 
 | 
|---|
| 126 | /** UnitTest for CountBondsTest::BondsOfTwoTest().
 | 
|---|
| 127 |  */
 | 
|---|
| 128 | void CountBondsTest::BondsOfTwoTest()
 | 
|---|
| 129 | {
 | 
|---|
| 130 |   CPPUNIT_ASSERT_EQUAL( 4 , CountBondsOfTwo(molecules, hydrogen, oxygen) );
 | 
|---|
| 131 |   CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfTwo(molecules, hydrogen, hydrogen) );
 | 
|---|
| 132 |   CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfTwo(molecules, oxygen, oxygen) );
 | 
|---|
| 133 | };
 | 
|---|
| 134 | 
 | 
|---|
| 135 | /** UnitTest for CountBondsTest::BondsOfThreeTest().
 | 
|---|
| 136 |  */
 | 
|---|
| 137 | void CountBondsTest::BondsOfThreeTest()
 | 
|---|
| 138 | {
 | 
|---|
| 139 |   CPPUNIT_ASSERT_EQUAL( 2 , CountBondsOfThree(molecules, hydrogen, oxygen, hydrogen) );
 | 
|---|
| 140 |   CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfThree(molecules, oxygen, hydrogen, oxygen) );
 | 
|---|
| 141 | };
 | 
|---|
| 142 | 
 | 
|---|
| 143 | void OutputTestMolecule(molecule *mol, const char *name)
 | 
|---|
| 144 | {
 | 
|---|
| 145 |   ofstream output(name);
 | 
|---|
| 146 |   mol->OutputXYZ(&output);
 | 
|---|
| 147 |   output.close();
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | /** UnitTest for CountBondsTest::HydrogenBridgeBondsTest().
 | 
|---|
| 151 |  */
 | 
|---|
| 152 | void CountBondsTest::HydrogenBridgeBondsTest()
 | 
|---|
| 153 | {
 | 
|---|
| 154 |   double *mirror = new double[3];
 | 
|---|
| 155 |   for (int i=0;i<3;i++)
 | 
|---|
| 156 |     mirror[i] = -1.;
 | 
|---|
| 157 |   Vector Translator;
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   //OutputTestMolecule(TestMolecule1, "testmolecule1.xyz");
 | 
|---|
| 160 | 
 | 
|---|
| 161 |   cout << "Case 1: offset of (3,0,0), hence angles are (104.5, 0, 75.5, 180) < 30." << endl;
 | 
|---|
| 162 |   Translator.Init(3,0,0);
 | 
|---|
| 163 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 164 |   CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 165 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, oxygen) );
 | 
|---|
| 166 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-1.xyz");
 | 
|---|
| 167 |   Translator.Init(-3,0,0);
 | 
|---|
| 168 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   cout << "Case 2: offset of (0,3,0), hence angle are (14.5, 165.5, 90) < 30 (only three, because other 90 is missing due to first H01 only fulfilling H-bond criteria)." << endl;
 | 
|---|
| 171 |   Translator.Init(0,3,0);
 | 
|---|
| 172 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 173 |   CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 174 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-2.xyz");
 | 
|---|
| 175 |   Translator.Init(0,-3,0);
 | 
|---|
| 176 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 177 | 
 | 
|---|
| 178 |   cout << "Case 3: offset of (0,-3,0) and mirror, hence angle are (165.5, 90, 165.5, 90) > 30." << endl;
 | 
|---|
| 179 |   Translator.Init(0,-3,0);
 | 
|---|
| 180 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 181 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 182 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 183 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-3.xyz");
 | 
|---|
| 184 |   Translator.Init(0,3,0);
 | 
|---|
| 185 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 186 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 187 | 
 | 
|---|
| 188 |   cout << "Case 4: offset of (2,1,0), hence angle are (78, 26.6, 102, 153.4) < 30." << endl;
 | 
|---|
| 189 |   Translator.Init(2,1,0);
 | 
|---|
| 190 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 191 |   CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 192 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-4.xyz");
 | 
|---|
| 193 |   Translator.Init(-2,-1,0);
 | 
|---|
| 194 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 195 | 
 | 
|---|
| 196 |   cout << "Case 5: offset of (0,0,3), hence angle are (90, 90, 90, 90) > 30." << endl;
 | 
|---|
| 197 |   Translator.Init(0,0,3);
 | 
|---|
| 198 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 199 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 200 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-5.xyz");
 | 
|---|
| 201 |   Translator.Init(0,0,-3);
 | 
|---|
| 202 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 203 | 
 | 
|---|
| 204 |   cout << "Case 6: offset of (-3,0,0) and mirror, hence angle are (75.5, 180, 104.5, 180) > 30." << endl;
 | 
|---|
| 205 |   Translator.Init(-3,0,0);
 | 
|---|
| 206 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 207 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 208 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 209 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-6.xyz");
 | 
|---|
| 210 |   Translator.Init(3,0,0);
 | 
|---|
| 211 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 212 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 213 | 
 | 
|---|
| 214 |   cout << "Case 7: offset of (3,0,0) and mirror, hence angles are (104.5, 0, 104.5, 0) < 30, but interfering hydrogens." << endl;
 | 
|---|
| 215 |   Translator.Init(3,0,0);
 | 
|---|
| 216 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 217 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 218 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 219 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-7.xyz");
 | 
|---|
| 220 |   Translator.Init(-3,0,0);
 | 
|---|
| 221 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 222 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 223 | 
 | 
|---|
| 224 |   cout << "Case 8: offset of (0,3,0), hence angle are (14.5, 90, 14.5, 90) < 30, but interfering hydrogens." << endl;
 | 
|---|
| 225 |   Translator.Init(0,3,0);
 | 
|---|
| 226 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 227 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 228 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-8.xyz");
 | 
|---|
| 229 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 230 |   Translator.Init(0,-3,0);
 | 
|---|
| 231 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 232 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 233 | 
 | 
|---|
| 234 |   delete(mirror);
 | 
|---|
| 235 | };
 | 
|---|
| 236 | 
 | 
|---|
| 237 | 
 | 
|---|
| 238 | /********************************************** Main routine **************************************/
 | 
|---|
| 239 | 
 | 
|---|
| 240 | int main(int argc, char **argv)
 | 
|---|
| 241 | {
 | 
|---|
| 242 |   // Get the top level suite from the registry
 | 
|---|
| 243 |   CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
 | 
|---|
| 244 | 
 | 
|---|
| 245 |   // Adds the test to the list of test to run
 | 
|---|
| 246 |   CppUnit::TextUi::TestRunner runner;
 | 
|---|
| 247 |   runner.addTest( suite );
 | 
|---|
| 248 | 
 | 
|---|
| 249 |   // Change the default outputter to a compiler error format outputter
 | 
|---|
| 250 |   runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
 | 
|---|
| 251 |                                                        std::cerr ) );
 | 
|---|
| 252 |   // Run the tests.
 | 
|---|
| 253 |   bool wasSucessful = runner.run();
 | 
|---|
| 254 | 
 | 
|---|
| 255 |   // Return error code 1 if the one of test failed.
 | 
|---|
| 256 |   return wasSucessful ? 0 : 1;
 | 
|---|
| 257 | };
 | 
|---|