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