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