1 | /*
|
---|
2 | * LinkedCellUnitTest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Apr 9, 2010
|
---|
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 | #include <cstring>
|
---|
17 |
|
---|
18 | #include "atom.hpp"
|
---|
19 | #include "element.hpp"
|
---|
20 | #include "linkedcell.hpp"
|
---|
21 | #include "molecule.hpp"
|
---|
22 | #include "periodentafel.hpp"
|
---|
23 | #include "LinkedCellUnitTest.hpp"
|
---|
24 |
|
---|
25 | /********************************************** Test classes **************************************/
|
---|
26 |
|
---|
27 | // Registers the fixture into the 'registry'
|
---|
28 | CPPUNIT_TEST_SUITE_REGISTRATION( LinkedCellTest );
|
---|
29 |
|
---|
30 |
|
---|
31 | void LinkedCellTest::setUp()
|
---|
32 | {
|
---|
33 | atom *Walker = NULL;
|
---|
34 |
|
---|
35 | // init private all pointers to zero
|
---|
36 | TestMolecule = NULL;
|
---|
37 | hydrogen = NULL;
|
---|
38 | tafel = NULL;
|
---|
39 |
|
---|
40 | // construct element
|
---|
41 | hydrogen = new element;
|
---|
42 | hydrogen->Z = 1;
|
---|
43 | hydrogen->CovalentRadius = 0.23;
|
---|
44 | strcpy(hydrogen->name, "hydrogen");
|
---|
45 | strcpy(hydrogen->symbol, "H");
|
---|
46 |
|
---|
47 | // construct periodentafel
|
---|
48 | tafel = new periodentafel;
|
---|
49 | tafel->AddElement(hydrogen);
|
---|
50 |
|
---|
51 | // construct molecule (water molecule)
|
---|
52 | TestMolecule = new molecule(tafel);
|
---|
53 | for (double x=0.5;x<3;x+=1.)
|
---|
54 | for (double y=0.5;y<3;y+=1.)
|
---|
55 | for (double z=0.5;z<3;z+=1.) {
|
---|
56 | Walker = new atom();
|
---|
57 | Walker->type = hydrogen;
|
---|
58 | Walker->node->Init(x, y, z );
|
---|
59 | TestMolecule->AddAtom(Walker);
|
---|
60 | }
|
---|
61 |
|
---|
62 | // construct linked cell
|
---|
63 | LC = new LinkedCell (TestMolecule, 1.);
|
---|
64 |
|
---|
65 | // check that TestMolecule was correctly constructed
|
---|
66 | CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 3*3*3 );
|
---|
67 | Walker = TestMolecule->start->next;
|
---|
68 | CPPUNIT_ASSERT( TestMolecule->end != Walker );
|
---|
69 | };
|
---|
70 |
|
---|
71 |
|
---|
72 | void LinkedCellTest::tearDown()
|
---|
73 | {
|
---|
74 | delete(LC);
|
---|
75 | delete(TestMolecule);
|
---|
76 | // note that all the atoms are cleaned by TestMolecule
|
---|
77 | delete(tafel);
|
---|
78 | // note that element is cleaned by periodentafel
|
---|
79 | };
|
---|
80 |
|
---|
81 |
|
---|
82 | /** UnitTest for LinkedCell::CheckBounds().
|
---|
83 | */
|
---|
84 | void LinkedCellTest::CheckBoundsTest()
|
---|
85 | {
|
---|
86 | // check for within bounds
|
---|
87 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
88 | CPPUNIT_ASSERT_EQUAL( true, LC->CheckBounds() );
|
---|
89 | LC->n[0] = LC->n[1] = LC->n[2] = 1;
|
---|
90 | CPPUNIT_ASSERT_EQUAL( true, LC->CheckBounds() );
|
---|
91 | LC->n[0] = LC->n[1] = LC->n[2] = 2;
|
---|
92 | CPPUNIT_ASSERT_EQUAL( true, LC->CheckBounds() );
|
---|
93 |
|
---|
94 | // check for out of bounds
|
---|
95 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
96 | LC->n[0] = 404040;
|
---|
97 | CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
|
---|
98 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
99 | LC->n[0] = 0;
|
---|
100 | LC->n[1] = 5000;
|
---|
101 | CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
|
---|
102 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
103 | LC->n[1] = 0;
|
---|
104 | LC->n[2] = -70;
|
---|
105 | CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
|
---|
106 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
107 | LC->n[0] = LC->n[1] = LC->n[2] = 3;
|
---|
108 | CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
|
---|
109 | };
|
---|
110 |
|
---|
111 |
|
---|
112 | /** UnitTest for LinkedCell::GetCurrentCell().
|
---|
113 | * Note that CheckBounds() is used and has to be tested already.
|
---|
114 | */
|
---|
115 | void LinkedCellTest::GetCurrentCellTest()
|
---|
116 | {
|
---|
117 | // within bounds
|
---|
118 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
119 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[0 * 3*3 + 0 * 3 + 0], LC->GetCurrentCell() );
|
---|
120 | LC->n[0] = LC->n[1] = LC->n[2] = 1;
|
---|
121 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[1 * 3*3 + 1 * 3 + 1], LC->GetCurrentCell() );
|
---|
122 | LC->n[0] = LC->n[1] = LC->n[2] = 2;
|
---|
123 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[2 * 3*3 + 2 * 3 + 2], LC->GetCurrentCell() );
|
---|
124 |
|
---|
125 | // out of bounds
|
---|
126 | LC->n[0] = LC->n[1] = LC->n[2] = 3;
|
---|
127 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
128 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetCurrentCell() );
|
---|
129 | LC->n[0] = LC->n[1] = LC->n[2] = -1;
|
---|
130 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
131 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetCurrentCell() );
|
---|
132 | };
|
---|
133 |
|
---|
134 | /** UnitTest for LinkedCell::GetRelativeToCurrentCell().
|
---|
135 | */
|
---|
136 | void LinkedCellTest::GetRelativeToCurrentCellTest()
|
---|
137 | {
|
---|
138 | int offset[3];
|
---|
139 |
|
---|
140 | // offset to (0,0,0) always
|
---|
141 | offset[0] = offset[1] = offset[2] = 0;
|
---|
142 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
143 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[0], LC->GetRelativeToCurrentCell(offset) );
|
---|
144 | offset[0] = offset[1] = offset[2] = -1;
|
---|
145 | LC->n[0] = LC->n[1] = LC->n[2] = 1;
|
---|
146 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[0], LC->GetRelativeToCurrentCell(offset) );
|
---|
147 | offset[0] = offset[1] = offset[2] = -2;
|
---|
148 | LC->n[0] = LC->n[1] = LC->n[2] = 2;
|
---|
149 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[0], LC->GetRelativeToCurrentCell(offset) );
|
---|
150 |
|
---|
151 | // offset to (0,0,0) - 1.*(x/y/z) out of bounds
|
---|
152 | offset[0] = offset[1] = offset[2] = 0;
|
---|
153 | offset[0] = -1;
|
---|
154 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
155 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
156 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
157 | offset[0] = offset[1] = offset[2] = 0;
|
---|
158 | offset[1] = -1;
|
---|
159 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
160 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
161 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
162 | offset[0] = offset[1] = offset[2] = 0;
|
---|
163 | offset[2] = -1;
|
---|
164 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
165 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
166 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
167 |
|
---|
168 | // out of bounds
|
---|
169 | offset[0] = offset[1] = offset[2] = -5054932;
|
---|
170 | LC->n[0] = LC->n[1] = LC->n[2] = 1;
|
---|
171 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
172 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
173 | offset[0] = offset[1] = offset[2] = 192345;
|
---|
174 | LC->n[0] = LC->n[1] = LC->n[2] = 1;
|
---|
175 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
176 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
177 |
|
---|
178 | // index is out of bounds, offset points within
|
---|
179 | offset[0] = offset[1] = offset[2] = -2;
|
---|
180 | LC->n[0] = LC->n[1] = LC->n[2] = 4;
|
---|
181 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[2 * 3*3 + 2 * 3 + 2], LC->GetRelativeToCurrentCell(offset) );
|
---|
182 |
|
---|
183 | // index is within bounds, offset points out
|
---|
184 | offset[0] = offset[1] = offset[2] = 2;
|
---|
185 | LC->n[0] = LC->n[1] = LC->n[2] = 2;
|
---|
186 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
187 | CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
188 | };
|
---|
189 |
|
---|
190 |
|
---|
191 | /** UnitTest for LinkedCell::SetIndexToNode().
|
---|
192 | */
|
---|
193 | void LinkedCellTest::SetIndexToNodeTest()
|
---|
194 | {
|
---|
195 | // check all atoms
|
---|
196 | atom *Walker = TestMolecule->start;
|
---|
197 | while (Walker->next != TestMolecule->end) {
|
---|
198 | Walker = Walker->next;
|
---|
199 | CPPUNIT_ASSERT_EQUAL( true, LC->SetIndexToNode(Walker) );
|
---|
200 | }
|
---|
201 |
|
---|
202 | // check internal vectors, returns false, because this atom is not in LC-list!
|
---|
203 | Walker = new atom();
|
---|
204 | Walker->Name = Malloc<char>(6, "LinkedCellTest::SetIndexToNodeTest - Walker");
|
---|
205 | strcpy(Walker->Name, "test");
|
---|
206 | Walker->x.Init(1,1,1);
|
---|
207 | CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToNode(Walker) );
|
---|
208 | delete(Walker);
|
---|
209 |
|
---|
210 | // check out of bounds vectors
|
---|
211 | Walker = new atom();
|
---|
212 | Walker->Name = Malloc<char>(6, "LinkedCellTest::SetIndexToNodeTest - Walker");
|
---|
213 | strcpy(Walker->Name, "test");
|
---|
214 | Walker->x.Init(0,-1,0);
|
---|
215 | CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToNode(Walker) );
|
---|
216 | delete(Walker);
|
---|
217 | };
|
---|
218 |
|
---|
219 |
|
---|
220 | /** UnitTest for LinkedCell::SetIndexToVector().
|
---|
221 | */
|
---|
222 | void LinkedCellTest::SetIndexToVectorTest()
|
---|
223 | {
|
---|
224 | Vector tester;
|
---|
225 |
|
---|
226 | // check center of each cell
|
---|
227 | for (double x=0.5;x<3;x+=1.)
|
---|
228 | for (double y=0.5;y<3;y+=1.)
|
---|
229 | for (double z=0.5;z<3;z+=1.) {
|
---|
230 | tester.Init(x,y,z);
|
---|
231 | CPPUNIT_ASSERT_EQUAL( true, LC->SetIndexToVector(&tester) );
|
---|
232 | }
|
---|
233 | // check corners of each cell
|
---|
234 | for (double x=1.;x<4;x+=1.)
|
---|
235 | for (double y=1.;y<4;y+=1.)
|
---|
236 | for (double z=1.;z<4;z+=1.) {
|
---|
237 | tester.Init(x,y,z);
|
---|
238 | cout << "Tester is at " << tester << "." << endl;
|
---|
239 | CPPUNIT_ASSERT_EQUAL( true, LC->SetIndexToVector(&tester) );
|
---|
240 | }
|
---|
241 | // check out of bounds
|
---|
242 | for (double x=0.5-1e-10;x<5;x+=3.1)
|
---|
243 | for (double y=0.5-1e-10;y<5;y+=3.1)
|
---|
244 | for (double z=0.5-1e-10;z<5;z+=3.1) {
|
---|
245 | tester.Init(x,y,z);
|
---|
246 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
247 | CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToVector(&tester) );
|
---|
248 | }
|
---|
249 | // check nonsense vectors
|
---|
250 | tester.Init(-423598,3245978,29349);
|
---|
251 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
252 | CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToVector(&tester) );
|
---|
253 | };
|
---|
254 |
|
---|
255 |
|
---|
256 | /** UnitTest for LinkedCell::GetNeighbourBounds().
|
---|
257 | */
|
---|
258 | void LinkedCellTest::GetNeighbourBoundsTest()
|
---|
259 | {
|
---|
260 | Vector tester;
|
---|
261 | int lower[NDIM], upper[NDIM];
|
---|
262 |
|
---|
263 | tester.Init(0.5,0.5,0.5);
|
---|
264 | LC->SetIndexToVector(&tester);
|
---|
265 | LC->GetNeighbourBounds(lower, upper);
|
---|
266 | for (int i=0;i<NDIM;i++)
|
---|
267 | CPPUNIT_ASSERT_EQUAL( 0, lower[i]);
|
---|
268 | for (int i=0;i<NDIM;i++)
|
---|
269 | CPPUNIT_ASSERT_EQUAL( 1, upper[i]);
|
---|
270 | };
|
---|
271 |
|
---|
272 |
|
---|
273 | /** UnitTest for LinkedCell::GetallNeighbours().
|
---|
274 | */
|
---|
275 | void LinkedCellTest::GetallNeighboursTest()
|
---|
276 | {
|
---|
277 | Vector tester;
|
---|
278 | LinkedCell::LinkedNodes *ListOfPoints = NULL;
|
---|
279 | atom *Walker = NULL;
|
---|
280 | size_t size = 0;
|
---|
281 |
|
---|
282 | // get all atoms
|
---|
283 | tester.Init(1.5,1.5,1.5);
|
---|
284 | CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
|
---|
285 | ListOfPoints = LC->GetallNeighbours();
|
---|
286 | size = ListOfPoints->size();
|
---|
287 | CPPUNIT_ASSERT_EQUAL( (size_t)27, size );
|
---|
288 | Walker = TestMolecule->start;
|
---|
289 | Walker = TestMolecule->start;
|
---|
290 | while (Walker->next != TestMolecule->end) {
|
---|
291 | Walker = Walker->next;
|
---|
292 | ListOfPoints->remove(Walker);
|
---|
293 | size--;
|
---|
294 | CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
|
---|
295 | }
|
---|
296 | CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
|
---|
297 | CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
|
---|
298 | CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
|
---|
299 | delete(ListOfPoints);
|
---|
300 |
|
---|
301 | // get all atoms in one corner
|
---|
302 | tester.Init(0.5, 0.5, 0.5);
|
---|
303 | CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
|
---|
304 | ListOfPoints = LC->GetallNeighbours();
|
---|
305 | size=ListOfPoints->size();
|
---|
306 | CPPUNIT_ASSERT_EQUAL( (size_t)8, size );
|
---|
307 | Walker = TestMolecule->start;
|
---|
308 | while (Walker->next != TestMolecule->end) {
|
---|
309 | Walker = Walker->next;
|
---|
310 | if ((Walker->x.x[0] <2) && (Walker->x.x[1] <2) && (Walker->x.x[2] <2)) {
|
---|
311 | ListOfPoints->remove(Walker);
|
---|
312 | size--;
|
---|
313 | CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
|
---|
314 | }
|
---|
315 | }
|
---|
316 | CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
|
---|
317 | CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
|
---|
318 | CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
|
---|
319 | delete(ListOfPoints);
|
---|
320 |
|
---|
321 | // get all atoms from one corner
|
---|
322 | tester.Init(0.5, 0.5, 0.5);
|
---|
323 | CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
|
---|
324 | ListOfPoints = LC->GetallNeighbours(3);
|
---|
325 | size=ListOfPoints->size();
|
---|
326 | CPPUNIT_ASSERT_EQUAL( (size_t)27, size );
|
---|
327 | Walker = TestMolecule->start;
|
---|
328 | while (Walker->next != TestMolecule->end) {
|
---|
329 | Walker = Walker->next;
|
---|
330 | ListOfPoints->remove(Walker);
|
---|
331 | size--;
|
---|
332 | CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
|
---|
333 | }
|
---|
334 | CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
|
---|
335 | CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
|
---|
336 | CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
|
---|
337 | delete(ListOfPoints);
|
---|
338 | };
|
---|
339 |
|
---|
340 |
|
---|
341 | /** UnitTest for LinkedCell::GetPointsInsideSphere().
|
---|
342 | */
|
---|
343 | void LinkedCellTest::GetPointsInsideSphereTest()
|
---|
344 | {
|
---|
345 | Vector tester;
|
---|
346 | LinkedCell::LinkedNodes *ListOfPoints = NULL;
|
---|
347 | atom *Walker = NULL;
|
---|
348 | size_t size = 0;
|
---|
349 |
|
---|
350 | // get all points around central arom with radius 1.
|
---|
351 | tester.Init(1.5,1.5,1.5);
|
---|
352 | CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
|
---|
353 | ListOfPoints = LC->GetPointsInsideSphere(1., &tester);
|
---|
354 | size = ListOfPoints->size();
|
---|
355 | CPPUNIT_ASSERT_EQUAL( (size_t)7, size );
|
---|
356 | Walker = TestMolecule->start;
|
---|
357 | while (Walker->next != TestMolecule->end) {
|
---|
358 | Walker = Walker->next;
|
---|
359 | if ((Walker->x.DistanceSquared(&tester) - 1.) < MYEPSILON ) {
|
---|
360 | ListOfPoints->remove(Walker);
|
---|
361 | size--;
|
---|
362 | CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
|
---|
363 | }
|
---|
364 | }
|
---|
365 | CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
|
---|
366 | CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
|
---|
367 | CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
|
---|
368 | delete(ListOfPoints);
|
---|
369 | };
|
---|
370 |
|
---|
371 | /********************************************** Main routine **************************************/
|
---|
372 |
|
---|
373 | int main(int argc, char **argv)
|
---|
374 | {
|
---|
375 | // Get the top level suite from the registry
|
---|
376 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
---|
377 |
|
---|
378 | // Adds the test to the list of test to run
|
---|
379 | CppUnit::TextUi::TestRunner runner;
|
---|
380 | runner.addTest( suite );
|
---|
381 |
|
---|
382 | // Change the default outputter to a compiler error format outputter
|
---|
383 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
---|
384 | std::cerr ) );
|
---|
385 | // Run the tests.
|
---|
386 | bool wasSucessful = runner.run();
|
---|
387 |
|
---|
388 | // Return error code 1 if the one of test failed.
|
---|
389 | return wasSucessful ? 0 : 1;
|
---|
390 | };
|
---|
391 |
|
---|