| 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 | logger::purgeInstance();
|
|---|
| 52 | errorLogger::purgeInstance();
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
|
|---|
| 56 | */
|
|---|
| 57 | void 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 | */
|
|---|
| 71 | void VectorTest::SimpleAlgebraTest()
|
|---|
| 72 | {
|
|---|
| 73 | double factor;
|
|---|
| 74 | // copy vector
|
|---|
| 75 | fixture = Vector(2.,3.,4.);
|
|---|
| 76 | CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
|
|---|
| 77 | // summation and scaling
|
|---|
| 78 | fixture = zero + unit;
|
|---|
| 79 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
|---|
| 80 | fixture = zero - unit;
|
|---|
| 81 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
|---|
| 82 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
|---|
| 83 | fixture = zero + zero;
|
|---|
| 84 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
|
|---|
| 85 | fixture = notunit - otherunit;
|
|---|
| 86 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
|---|
| 87 | fixture = unit - otherunit;
|
|---|
| 88 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
|---|
| 89 | fixture = notunit - unit - otherunit;
|
|---|
| 90 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
|---|
| 91 | fixture = 0.98 * unit;
|
|---|
| 92 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
|---|
| 93 | fixture = 1. * unit;
|
|---|
| 94 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
|---|
| 95 | factor = 0.98;
|
|---|
| 96 | fixture = factor * unit;
|
|---|
| 97 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
|---|
| 98 | factor = 1.;
|
|---|
| 99 | fixture = factor * unit;
|
|---|
| 100 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
|---|
| 101 | };
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | /** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
|
|---|
| 105 | */
|
|---|
| 106 | void VectorTest::OperatorAlgebraTest()
|
|---|
| 107 | {
|
|---|
| 108 | // summation and scaling
|
|---|
| 109 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
|---|
| 110 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
|---|
| 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() );
|
|---|
| 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) );
|
|---|
| 123 | };
|
|---|
| 124 |
|
|---|
| 125 | /** UnitTest for scalar products.
|
|---|
| 126 | */
|
|---|
| 127 | void VectorTest::EuclidianScalarProductTest()
|
|---|
| 128 | {
|
|---|
| 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) );
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /** UnitTest for norms.
|
|---|
| 143 | */
|
|---|
| 144 | void VectorTest::EuclidianNormTest()
|
|---|
| 145 | {
|
|---|
| 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() );
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /** UnitTest for distances.
|
|---|
| 157 | */
|
|---|
| 158 | void VectorTest::EuclidianDistancesTest()
|
|---|
| 159 | {
|
|---|
| 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) );
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /** UnitTest for angles.
|
|---|
| 168 | */
|
|---|
| 169 | void VectorTest::EuclidianAnglesTest()
|
|---|
| 170 | {
|
|---|
| 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 );
|
|---|
| 176 | };
|
|---|
| 177 |
|
|---|
| 178 | /** UnitTest for projections.
|
|---|
| 179 | */
|
|---|
| 180 | void VectorTest::ProjectionTest()
|
|---|
| 181 | {
|
|---|
| 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) );
|
|---|
| 186 | };
|
|---|
| 187 |
|
|---|
| 188 | /**
|
|---|
| 189 | * Unittest for operation with normals
|
|---|
| 190 | */
|
|---|
| 191 | void VectorTest::NormalsTest(){
|
|---|
| 192 | Vector testVector;
|
|---|
| 193 | // the zero Vector should produce an error
|
|---|
| 194 | CPPUNIT_ASSERT(!testVector.GetOneNormalVector(zero));
|
|---|
| 195 |
|
|---|
| 196 | // first one-component system
|
|---|
| 197 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(unit));
|
|---|
| 198 | CPPUNIT_ASSERT(testVector.ScalarProduct(unit) < MYEPSILON);
|
|---|
| 199 |
|
|---|
| 200 | // second one-component system
|
|---|
| 201 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(otherunit));
|
|---|
| 202 | CPPUNIT_ASSERT(testVector.ScalarProduct(otherunit) < MYEPSILON);
|
|---|
| 203 |
|
|---|
| 204 | // first two-component system
|
|---|
| 205 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(notunit));
|
|---|
| 206 | CPPUNIT_ASSERT(testVector.ScalarProduct(notunit) < MYEPSILON);
|
|---|
| 207 |
|
|---|
| 208 | // second two-component system
|
|---|
| 209 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(two));
|
|---|
| 210 | CPPUNIT_ASSERT(testVector.ScalarProduct(two) < MYEPSILON);
|
|---|
| 211 |
|
|---|
| 212 | // three component system
|
|---|
| 213 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(three));
|
|---|
| 214 | CPPUNIT_ASSERT(testVector.ScalarProduct(three) < MYEPSILON);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /** UnitTest for line intersections.
|
|---|
| 218 | */
|
|---|
| 219 | void VectorTest::LineIntersectionTest()
|
|---|
| 220 | {
|
|---|
| 221 | // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ???
|
|---|
| 222 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(unit, zero).GetIntersection(zero, two) );
|
|---|
| 223 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
|---|
| 224 |
|
|---|
| 225 | // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
|
|---|
| 226 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(otherunit, two).GetIntersection( unit, notunit) );
|
|---|
| 227 | CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
|
|---|
| 228 |
|
|---|
| 229 | // four vectors equal to zero
|
|---|
| 230 | CPPUNIT_ASSERT_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(zero, zero, zero, zero), LinearDependenceException);
|
|---|
| 231 | //CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
|---|
| 232 |
|
|---|
| 233 | // four vectors equal to unit
|
|---|
| 234 | CPPUNIT_ASSERT_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, unit, unit, unit), LinearDependenceException);
|
|---|
| 235 | //CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
|---|
| 236 |
|
|---|
| 237 | // two equal lines
|
|---|
| 238 | CPPUNIT_ASSERT_NO_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, unit, two));
|
|---|
| 239 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
|---|
| 240 |
|
|---|
| 241 | // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ???
|
|---|
| 242 | CPPUNIT_ASSERT_NO_THROW( fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, unit, otherunit) );
|
|---|
| 243 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
|---|
| 244 |
|
|---|
| 245 | // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ???
|
|---|
| 246 | CPPUNIT_ASSERT_NO_THROW( fixture = GetIntersectionOfTwoLinesOnPlane(unit, zero, zero, two) );
|
|---|
| 247 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
|---|
| 248 |
|
|---|
| 249 | // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ???
|
|---|
| 250 | CPPUNIT_ASSERT_NO_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, zero, otherunit) );
|
|---|
| 251 | CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), fixture );
|
|---|
| 252 | };
|
|---|
| 253 |
|
|---|
| 254 | /** UnitTest for vector rotations.
|
|---|
| 255 | */
|
|---|
| 256 | void VectorTest::VectorRotationTest()
|
|---|
| 257 | {
|
|---|
| 258 | fixture = Vector(-1.,0.,0.);
|
|---|
| 259 |
|
|---|
| 260 | // zero vector does not change
|
|---|
| 261 | fixture = RotateVector(zero,unit, 1.);
|
|---|
| 262 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
|---|
| 263 |
|
|---|
| 264 | fixture = RotateVector(zero, two, 1.);
|
|---|
| 265 | CPPUNIT_ASSERT_EQUAL( zero, fixture);
|
|---|
| 266 |
|
|---|
| 267 | // vector on axis does not change
|
|---|
| 268 | fixture = RotateVector(unit,unit, 1.);
|
|---|
| 269 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
|---|
| 270 |
|
|---|
| 271 | // rotations
|
|---|
| 272 | fixture = RotateVector(otherunit, unit, M_PI);
|
|---|
| 273 | CPPUNIT_ASSERT_EQUAL( Vector(0.,-1.,0.), fixture );
|
|---|
| 274 |
|
|---|
| 275 | fixture = RotateVector(otherunit, unit, 2. * M_PI);
|
|---|
| 276 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
|---|
| 277 |
|
|---|
| 278 | fixture = RotateVector(otherunit,unit, 0);
|
|---|
| 279 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
|---|
| 280 |
|
|---|
| 281 | fixture = RotateVector(Vector(0.,0.,1.), notunit, M_PI);
|
|---|
| 282 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /**
|
|---|
| 286 | * UnitTest for Vector::IsInParallelepiped().
|
|---|
| 287 | */
|
|---|
| 288 | void VectorTest::IsInParallelepipedTest()
|
|---|
| 289 | {
|
|---|
| 290 | double parallelepiped[NDIM*NDIM];
|
|---|
| 291 | parallelepiped[0] = 1;
|
|---|
| 292 | parallelepiped[1] = 0;
|
|---|
| 293 | parallelepiped[2] = 0;
|
|---|
| 294 | parallelepiped[3] = 0;
|
|---|
| 295 | parallelepiped[4] = 1;
|
|---|
| 296 | parallelepiped[5] = 0;
|
|---|
| 297 | parallelepiped[6] = 0;
|
|---|
| 298 | parallelepiped[7] = 0;
|
|---|
| 299 | parallelepiped[8] = 1;
|
|---|
| 300 |
|
|---|
| 301 | fixture = zero;
|
|---|
| 302 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
|---|
| 303 | fixture = Vector(2.5,2.5,2.5);
|
|---|
| 304 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
|---|
| 305 | fixture = Vector(1.,1.,1.);
|
|---|
| 306 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
|---|
| 307 | fixture = Vector(3.5,3.5,3.5);
|
|---|
| 308 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
|---|
| 309 | fixture = Vector(2.,2.,2.);
|
|---|
| 310 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
|---|
| 311 | fixture = Vector(2.,3.,2.);
|
|---|
| 312 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
|---|
| 313 | fixture = Vector(-2.,2.,-1.);
|
|---|
| 314 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|