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