[dfe8ef] | 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 |
|
---|
[4eb4fe] | 19 | #include "Helpers/Assert.hpp"
|
---|
| 20 |
|
---|
[388049] | 21 | #include "analysis_bonds.hpp"
|
---|
[dfe8ef] | 22 | #include "atom.hpp"
|
---|
| 23 | #include "bond.hpp"
|
---|
| 24 | #include "bondgraph.hpp"
|
---|
| 25 | #include "element.hpp"
|
---|
| 26 | #include "molecule.hpp"
|
---|
| 27 | #include "periodentafel.hpp"
|
---|
[5f612ee] | 28 | #include "World.hpp"
|
---|
[dfe8ef] | 29 | #include "CountBondsUnitTest.hpp"
|
---|
| 30 |
|
---|
[5f612ee] | 31 | #ifdef HAVE_TESTRUNNER
|
---|
| 32 | #include "UnitTestMain.hpp"
|
---|
| 33 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 34 |
|
---|
[dfe8ef] | 35 | /********************************************** Test classes **************************************/
|
---|
| 36 |
|
---|
| 37 | // Registers the fixture into the 'registry'
|
---|
| 38 | CPPUNIT_TEST_SUITE_REGISTRATION( CountBondsTest );
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | void CountBondsTest::setUp()
|
---|
| 42 | {
|
---|
| 43 | atom *Walker = NULL;
|
---|
| 44 |
|
---|
| 45 | // construct element
|
---|
[4eb4fe] | 46 | hydrogen = World::getInstance().getPeriode()->FindElement(1);
|
---|
| 47 | oxygen = World::getInstance().getPeriode()->FindElement(8);
|
---|
| 48 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
|
---|
| 49 | CPPUNIT_ASSERT(oxygen != NULL && "could not find element oxygen");
|
---|
[dfe8ef] | 50 |
|
---|
| 51 | // construct molecule (water molecule)
|
---|
[5f612ee] | 52 | TestMolecule1 = World::getInstance().createMolecule();
|
---|
[4eb4fe] | 53 | CPPUNIT_ASSERT(TestMolecule1 != NULL && "could not create first molecule");
|
---|
[5f612ee] | 54 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 55 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[dfe8ef] | 56 | Walker->type = hydrogen;
|
---|
[8cbb97] | 57 | *Walker->node = Vector(-0.2418, 0.9350, 0. );
|
---|
[dfe8ef] | 58 | TestMolecule1->AddAtom(Walker);
|
---|
[5f612ee] | 59 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 60 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[dfe8ef] | 61 | Walker->type = hydrogen;
|
---|
[8cbb97] | 62 | *Walker->node = Vector(0.9658, 0., 0. );
|
---|
[dfe8ef] | 63 | TestMolecule1->AddAtom(Walker);
|
---|
[5f612ee] | 64 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 65 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[dfe8ef] | 66 | Walker->type = oxygen;
|
---|
[8cbb97] | 67 | *Walker->node = Vector(0., 0., 0. );
|
---|
[dfe8ef] | 68 | TestMolecule1->AddAtom(Walker);
|
---|
| 69 |
|
---|
[5f612ee] | 70 | TestMolecule2 = World::getInstance().createMolecule();
|
---|
[4eb4fe] | 71 | CPPUNIT_ASSERT(TestMolecule2 != NULL && "could not create second molecule");
|
---|
[5f612ee] | 72 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 73 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[dfe8ef] | 74 | Walker->type = hydrogen;
|
---|
[8cbb97] | 75 | *Walker->node = Vector(-0.2418, 0.9350, 0. );
|
---|
[dfe8ef] | 76 | TestMolecule2->AddAtom(Walker);
|
---|
[5f612ee] | 77 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 78 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[dfe8ef] | 79 | Walker->type = hydrogen;
|
---|
[8cbb97] | 80 | *Walker->node = Vector(0.9658, 0., 0. );
|
---|
[dfe8ef] | 81 | TestMolecule2->AddAtom(Walker);
|
---|
[5f612ee] | 82 | Walker = World::getInstance().createAtom();
|
---|
[4eb4fe] | 83 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
[dfe8ef] | 84 | Walker->type = oxygen;
|
---|
[8cbb97] | 85 | *Walker->node = Vector(0., 0., 0. );
|
---|
[dfe8ef] | 86 | TestMolecule2->AddAtom(Walker);
|
---|
[5f612ee] | 87 |
|
---|
| 88 | molecules = World::getInstance().getMolecules();
|
---|
[4eb4fe] | 89 | CPPUNIT_ASSERT(molecules != NULL && "could not obtain list of molecules");
|
---|
[5f612ee] | 90 | molecules->insert(TestMolecule1);
|
---|
[dfe8ef] | 91 | molecules->insert(TestMolecule2);
|
---|
| 92 |
|
---|
| 93 | // check that TestMolecule was correctly constructed
|
---|
[a7b761b] | 94 | CPPUNIT_ASSERT_EQUAL( TestMolecule1->getAtomCount(), 3 );
|
---|
| 95 | CPPUNIT_ASSERT_EQUAL( TestMolecule2->getAtomCount(), 3 );
|
---|
[dfe8ef] | 96 |
|
---|
| 97 | // create a small file with table
|
---|
| 98 | BG = new BondGraph(true);
|
---|
[4eb4fe] | 99 | CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
|
---|
[dfe8ef] | 100 |
|
---|
| 101 | // construct bond graphs
|
---|
| 102 | CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule1) );
|
---|
| 103 | CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule2) );
|
---|
| 104 | // TestMolecule1->Output((ofstream *)&cout);
|
---|
| 105 | // TestMolecule1->OutputBondsList();
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | void CountBondsTest::tearDown()
|
---|
| 110 | {
|
---|
| 111 | // remove the file
|
---|
| 112 | delete(BG);
|
---|
| 113 |
|
---|
[5f612ee] | 114 | World::purgeInstance();
|
---|
[dfe8ef] | 115 | };
|
---|
| 116 |
|
---|
| 117 | /** UnitTest for CountBondsTest::BondsOfTwoTest().
|
---|
| 118 | */
|
---|
| 119 | void CountBondsTest::BondsOfTwoTest()
|
---|
| 120 | {
|
---|
| 121 | CPPUNIT_ASSERT_EQUAL( 4 , CountBondsOfTwo(molecules, hydrogen, oxygen) );
|
---|
| 122 | CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfTwo(molecules, hydrogen, hydrogen) );
|
---|
| 123 | CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfTwo(molecules, oxygen, oxygen) );
|
---|
| 124 | };
|
---|
| 125 |
|
---|
| 126 | /** UnitTest for CountBondsTest::BondsOfThreeTest().
|
---|
| 127 | */
|
---|
| 128 | void CountBondsTest::BondsOfThreeTest()
|
---|
| 129 | {
|
---|
| 130 | CPPUNIT_ASSERT_EQUAL( 2 , CountBondsOfThree(molecules, hydrogen, oxygen, hydrogen) );
|
---|
| 131 | CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfThree(molecules, oxygen, hydrogen, oxygen) );
|
---|
| 132 | };
|
---|
| 133 |
|
---|
[62c9c0] | 134 | void OutputTestMolecule(molecule *mol, const char *name)
|
---|
| 135 | {
|
---|
| 136 | ofstream output(name);
|
---|
| 137 | mol->OutputXYZ(&output);
|
---|
| 138 | output.close();
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[dfe8ef] | 141 | /** UnitTest for CountBondsTest::HydrogenBridgeBondsTest().
|
---|
| 142 | */
|
---|
| 143 | void CountBondsTest::HydrogenBridgeBondsTest()
|
---|
| 144 | {
|
---|
| 145 | double *mirror = new double[3];
|
---|
[4eb4fe] | 146 | CPPUNIT_ASSERT(mirror != NULL && "could not create array of doubles");
|
---|
[dfe8ef] | 147 | for (int i=0;i<3;i++)
|
---|
| 148 | mirror[i] = -1.;
|
---|
| 149 | Vector Translator;
|
---|
[62c9c0] | 150 |
|
---|
| 151 | //OutputTestMolecule(TestMolecule1, "testmolecule1.xyz");
|
---|
| 152 |
|
---|
[fe238c] | 153 | cout << "Case 1: offset of (3,0,0), hence angles are (104.5, 0, 75.5, 180) < 30." << endl;
|
---|
[8cbb97] | 154 | Translator = Vector(3,0,0);
|
---|
[dfe8ef] | 155 | TestMolecule2->Translate(&Translator);
|
---|
| 156 | CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
|
---|
| 157 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, oxygen) );
|
---|
[62c9c0] | 158 | //OutputTestMolecule(TestMolecule2, "testmolecule2-1.xyz");
|
---|
[8cbb97] | 159 | Translator = Vector(-3,0,0);
|
---|
[dfe8ef] | 160 | TestMolecule2->Translate(&Translator);
|
---|
| 161 |
|
---|
[fe238c] | 162 | 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;
|
---|
[8cbb97] | 163 | Translator = Vector(0,3,0);
|
---|
[dfe8ef] | 164 | TestMolecule2->Translate(&Translator);
|
---|
| 165 | CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
|
---|
[62c9c0] | 166 | //OutputTestMolecule(TestMolecule2, "testmolecule2-2.xyz");
|
---|
[8cbb97] | 167 | Translator = Vector(0,-3,0);
|
---|
[dfe8ef] | 168 | TestMolecule2->Translate(&Translator);
|
---|
| 169 |
|
---|
[fe238c] | 170 | cout << "Case 3: offset of (0,-3,0) and mirror, hence angle are (165.5, 90, 165.5, 90) > 30." << endl;
|
---|
[8cbb97] | 171 | Translator = Vector(0,-3,0);
|
---|
[dfe8ef] | 172 | TestMolecule2->Scale((const double ** const)&mirror);
|
---|
| 173 | TestMolecule2->Translate(&Translator);
|
---|
| 174 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
|
---|
[62c9c0] | 175 | //OutputTestMolecule(TestMolecule2, "testmolecule2-3.xyz");
|
---|
[8cbb97] | 176 | Translator = Vector(0,3,0);
|
---|
[dfe8ef] | 177 | TestMolecule2->Translate(&Translator);
|
---|
| 178 | TestMolecule2->Scale((const double ** const)&mirror);
|
---|
| 179 |
|
---|
[fe238c] | 180 | cout << "Case 4: offset of (2,1,0), hence angle are (78, 26.6, 102, 153.4) < 30." << endl;
|
---|
[8cbb97] | 181 | Translator = Vector(2,1,0);
|
---|
[dfe8ef] | 182 | TestMolecule2->Translate(&Translator);
|
---|
| 183 | CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
|
---|
[62c9c0] | 184 | //OutputTestMolecule(TestMolecule2, "testmolecule2-4.xyz");
|
---|
[8cbb97] | 185 | Translator = Vector(-2,-1,0);
|
---|
[dfe8ef] | 186 | TestMolecule2->Translate(&Translator);
|
---|
| 187 |
|
---|
[fe238c] | 188 | cout << "Case 5: offset of (0,0,3), hence angle are (90, 90, 90, 90) > 30." << endl;
|
---|
[8cbb97] | 189 | Translator = Vector(0,0,3);
|
---|
[dfe8ef] | 190 | TestMolecule2->Translate(&Translator);
|
---|
| 191 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
|
---|
[62c9c0] | 192 | //OutputTestMolecule(TestMolecule2, "testmolecule2-5.xyz");
|
---|
[8cbb97] | 193 | Translator = Vector(0,0,-3);
|
---|
[dfe8ef] | 194 | TestMolecule2->Translate(&Translator);
|
---|
| 195 |
|
---|
[fe238c] | 196 | cout << "Case 6: offset of (-3,0,0) and mirror, hence angle are (75.5, 180, 104.5, 180) > 30." << endl;
|
---|
[8cbb97] | 197 | Translator = Vector(-3,0,0);
|
---|
[dfe8ef] | 198 | TestMolecule2->Scale((const double ** const)&mirror);
|
---|
| 199 | TestMolecule2->Translate(&Translator);
|
---|
| 200 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
|
---|
[62c9c0] | 201 | //OutputTestMolecule(TestMolecule2, "testmolecule2-6.xyz");
|
---|
[8cbb97] | 202 | Translator = Vector(3,0,0);
|
---|
[dfe8ef] | 203 | TestMolecule2->Translate(&Translator);
|
---|
| 204 | TestMolecule2->Scale((const double ** const)&mirror);
|
---|
[fe238c] | 205 |
|
---|
| 206 | cout << "Case 7: offset of (3,0,0) and mirror, hence angles are (104.5, 0, 104.5, 0) < 30, but interfering hydrogens." << endl;
|
---|
[8cbb97] | 207 | Translator = Vector(3,0,0);
|
---|
[fe238c] | 208 | TestMolecule2->Scale((const double ** const)&mirror);
|
---|
| 209 | TestMolecule2->Translate(&Translator);
|
---|
| 210 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
|
---|
| 211 | //OutputTestMolecule(TestMolecule2, "testmolecule2-7.xyz");
|
---|
[8cbb97] | 212 | Translator = Vector(-3,0,0);
|
---|
[fe238c] | 213 | TestMolecule2->Translate(&Translator);
|
---|
| 214 | TestMolecule2->Scale((const double ** const)&mirror);
|
---|
| 215 |
|
---|
| 216 | cout << "Case 8: offset of (0,3,0), hence angle are (14.5, 90, 14.5, 90) < 30, but interfering hydrogens." << endl;
|
---|
[8cbb97] | 217 | Translator = Vector(0,3,0);
|
---|
[fe238c] | 218 | TestMolecule2->Scale((const double ** const)&mirror);
|
---|
| 219 | TestMolecule2->Translate(&Translator);
|
---|
| 220 | //OutputTestMolecule(TestMolecule2, "testmolecule2-8.xyz");
|
---|
| 221 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
|
---|
[8cbb97] | 222 | Translator = Vector(0,-3,0);
|
---|
[fe238c] | 223 | TestMolecule2->Translate(&Translator);
|
---|
| 224 | TestMolecule2->Scale((const double ** const)&mirror);
|
---|
| 225 |
|
---|
[5f612ee] | 226 | delete[](mirror);
|
---|
[dfe8ef] | 227 | };
|
---|