source: molecuilder/src/unittests/vectorunittest.cpp@ eb129c

Last change on this file since eb129c was 45cc89, checked in by Frederik Heber <heber@…>, 16 years ago

MEMLEAK: In many tests World, MemoryUsageObserver, logger or errorLogger being singletons were instantiated but not destroyed.

  • in some cases respective includes had to be added.
  • Property mode set to 100644
File size: 10.0 KB
RevLine 
[da84d6]1/*
2 * unittest.cpp
3 *
4 * Created on: Aug 17, 2009
5 * Author: heber
6 */
7
8
9using namespace std;
10
11#include <cppunit/CompilerOutputter.h>
12#include <cppunit/extensions/TestFactoryRegistry.h>
13#include <cppunit/ui/text/TestRunner.h>
14
15#include "defs.hpp"
[45cc89]16#include "log.hpp"
17#include "memoryusageobserver.hpp"
[01e6c5]18#include "vector.hpp"
19#include "vectorunittest.hpp"
[da84d6]20
[44becc]21#ifdef HAVE_TESTRUNNER
22#include "UnitTestMain.hpp"
23#endif /*HAVE_TESTRUNNER*/
24
[da84d6]25/********************************************** Test classes **************************************/
26
27// Registers the fixture into the 'registry'
28CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
29
30
31void VectorTest::setUp()
32{
[6c09a4]33 zero.Init(0.,0.,0.);
34 unit.Init(1.,0.,0.);
35 otherunit.Init(0.,1.,0.);
36 notunit.Init(0.,1.,1.);
37 two.Init(2.,1.,0.);
[da84d6]38};
39
40
41void VectorTest::tearDown()
42{
[45cc89]43 MemoryUsageObserver::purgeInstance();
44 logger::purgeInstance();
45 errorLogger::purgeInstance();
[da84d6]46};
47
48/** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
49 */
50void VectorTest::UnityTest()
51{
52 // unity and zero tests
53 CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
54 CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
55 CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
56 CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
57 CPPUNIT_ASSERT_EQUAL( false, notunit.IsOne() );
58 CPPUNIT_ASSERT_EQUAL( true, otherunit.IsOne() );
59 CPPUNIT_ASSERT_EQUAL( false, otherunit.IsZero() );
60};
61
62/** UnitTest for Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale()/
63 */
64void VectorTest::SimpleAlgebraTest()
65{
66 double factor;
[6c6d84]67 // copy vector
68 fixture.Init(2.,3.,4.);
69 CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
[da84d6]70 // summation and scaling
[6c6d84]71 fixture.CopyVector(&zero);
72 fixture.AddVector(&unit);
73 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
74 fixture.CopyVector(&zero);
75 fixture.SubtractVector(&unit);
76 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
77 CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
78 fixture.CopyVector(&zero);
79 fixture.AddVector(&zero);
80 CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
81 fixture.CopyVector(&notunit);
82 fixture.SubtractVector(&otherunit);
83 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
84 fixture.CopyVector(&unit);
85 fixture.AddVector(&otherunit);
86 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
87 fixture.CopyVector(&notunit);
88 fixture.SubtractVector(&unit);
89 fixture.SubtractVector(&otherunit);
90 CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
91 fixture.CopyVector(&unit);
92 fixture.Scale(0.98);
93 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
94 fixture.CopyVector(&unit);
95 fixture.Scale(1.);
96 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
97 fixture.CopyVector(&unit);
[da84d6]98 factor = 0.98;
[6c6d84]99 fixture.Scale(factor);
100 CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
101 fixture.CopyVector(&unit);
[da84d6]102 factor = 1.;
[6c6d84]103 fixture.Scale(factor);
104 CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
[da84d6]105};
106
107
108/** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
109 */
110void VectorTest::OperatorAlgebraTest()
111{
112 // summation and scaling
113 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
[6c09a4]114 CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
[da84d6]115 CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
116 CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
117 CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
118 CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
119 CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
120 CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
121 CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
122 CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
[6c09a4]123
124 CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
125 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) );
126 CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) );
[da84d6]127};
128
[6c09a4]129/** UnitTest for scalar products.
[da84d6]130 */
[6c09a4]131void VectorTest::EuclidianScalarProductTest()
[da84d6]132{
133 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&zero) );
134 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&unit) );
135 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&otherunit) );
136 CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&notunit) );
137 CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(&unit) );
138 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
139 CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
140 CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(&notunit) );
141 CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(&unit) );
142 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&otherunit) );
143 CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&notunit) );
[6c09a4]144}
[da84d6]145
[6c09a4]146/** UnitTest for norms.
147 */
148void VectorTest::EuclidianNormTest()
149{
[da84d6]150 CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() );
151 CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() );
152 CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() );
153 CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() );
154 CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() );
155 CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() );
156 CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() );
157 CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() );
[6c09a4]158}
[da84d6]159
[6c09a4]160/** UnitTest for distances.
161 */
162void VectorTest::EuclidianDistancesTest()
163{
[da84d6]164 CPPUNIT_ASSERT_EQUAL( 1., zero.Distance(&unit) );
165 CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.Distance(&unit) );
166 CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.Distance(&notunit) );
167 CPPUNIT_ASSERT_EQUAL( 1., otherunit.Distance(&notunit) );
168 CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.Distance(&notunit) );
[6c09a4]169}
[da84d6]170
[6c09a4]171/** UnitTest for angles.
172 */
173void VectorTest::EuclidianAnglesTest()
174{
[da84d6]175 CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(&unit) );
[6c09a4]176 CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(&unit) );
177 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(&unit)) < MYEPSILON );
178 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(&notunit)) < MYEPSILON );
179 CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(&notunit)) < MYEPSILON );
[da84d6]180};
181
[6c09a4]182/** UnitTest for projections.
183 */
184void VectorTest::ProjectionTest()
185{
186 CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(&unit) );
187 CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(&unit) );
188 CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.), otherunit.Projection(&two) );
189 CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(&otherunit) );
190};
191
192/** UnitTest for line intersections.
193 */
194void VectorTest::LineIntersectionTest()
195{
196 // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ???
[543ce4]197 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionWithPlane(&unit, &zero, &zero, &two) );
[6c6d84]198 CPPUNIT_ASSERT_EQUAL( zero, fixture );
[6c09a4]199
200 // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
[543ce4]201 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionWithPlane(&otherunit, &two, &unit, &notunit) );
[6c6d84]202 CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
[6c09a4]203
204 // four vectors equal to zero
[543ce4]205 CPPUNIT_ASSERT_EQUAL( false, fixture.GetIntersectionOfTwoLinesOnPlane(&zero, &zero, &zero, &zero, NULL) );
[6c6d84]206 CPPUNIT_ASSERT_EQUAL( zero, fixture );
[6c09a4]207
208 // four vectors equal to unit
[543ce4]209 CPPUNIT_ASSERT_EQUAL( false, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &unit, &unit, &unit, NULL) );
[6c6d84]210 CPPUNIT_ASSERT_EQUAL( zero, fixture );
[6c09a4]211
212 // two equal lines
[543ce4]213 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &two, &unit, &two, NULL) );
[6c6d84]214 CPPUNIT_ASSERT_EQUAL( unit, fixture );
[6c09a4]215
216 // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ???
[543ce4]217 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &two, &unit, &otherunit, NULL) );
[6c6d84]218 CPPUNIT_ASSERT_EQUAL( unit, fixture );
[6c09a4]219
220 // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ???
[543ce4]221 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &zero, &zero, &two, NULL) );
[6c6d84]222 CPPUNIT_ASSERT_EQUAL( zero, fixture );
[6c09a4]223
224 // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ???
[543ce4]225 CPPUNIT_ASSERT_EQUAL( true, fixture.GetIntersectionOfTwoLinesOnPlane(&unit, &two, &zero, &otherunit, NULL) );
[6c6d84]226 CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), fixture );
[6c09a4]227};
[da84d6]228
[6c6d84]229/** UnitTest for vector rotations.
230 */
231void VectorTest::VectorRotationTest()
232{
233 fixture.Init(-1.,0.,0.);
234
235 // zero vector does not change
236 fixture.CopyVector(&zero);
237 fixture.RotateVector(&unit, 1.);
238 CPPUNIT_ASSERT_EQUAL( zero, fixture );
239
240 fixture.RotateVector(&two, 1.);
241 CPPUNIT_ASSERT_EQUAL( zero, fixture);
242
243 // vector on axis does not change
244 fixture.CopyVector(&unit);
245 fixture.RotateVector(&unit, 1.);
246 CPPUNIT_ASSERT_EQUAL( unit, fixture );
247
248 fixture.RotateVector(&unit, 1.);
249 CPPUNIT_ASSERT_EQUAL( unit, fixture );
250
251 // rotations
252 fixture.CopyVector(&otherunit);
253 fixture.RotateVector(&unit, M_PI);
254 CPPUNIT_ASSERT_EQUAL( Vector(0.,-1.,0.), fixture );
255
256 fixture.CopyVector(&otherunit);
257 fixture.RotateVector(&unit, 2. * M_PI);
258 CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
259
260 fixture.CopyVector(&otherunit);
261 fixture.RotateVector(&unit, 0);
262 CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
263
264 fixture.Init(0.,0.,1.);
265 fixture.RotateVector(&notunit, M_PI);
266 CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
267}
268
[bc3953]269/**
270 * UnitTest for Vector::IsInParallelepiped().
271 */
272void VectorTest::IsInParallelepipedTest()
273{
274 double parallelepiped[NDIM*NDIM];
275 parallelepiped[0] = 1;
276 parallelepiped[1] = 0;
277 parallelepiped[2] = 0;
278 parallelepiped[3] = 0;
279 parallelepiped[4] = 1;
280 parallelepiped[5] = 0;
281 parallelepiped[6] = 0;
282 parallelepiped[7] = 0;
283 parallelepiped[8] = 1;
284
285 fixture.CopyVector(zero);
286 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
287 fixture.Init(2.5,2.5,2.5);
288 CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
289 fixture.Init(1.,1.,1.);
290 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
291 fixture.Init(3.5,3.5,3.5);
292 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
293 fixture.Init(2.,2.,2.);
294 CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
295 fixture.Init(2.,3.,2.);
296 CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
297 fixture.Init(-2.,2.,-1.);
298 CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
299}
300
Note: See TracBrowser for help on using the repository browser.