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

Last change on this file since e7ea64 was e7ea64, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Changed implementation of Vector to forward operations to contained objects

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