[54a746] | 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"
|
---|
[e6fdbe] | 16 | #include "log.hpp"
|
---|
| 17 | #include "memoryusageobserver.hpp"
|
---|
[d52e70c] | 18 | #include "vector.hpp"
|
---|
[0a4f7f] | 19 | #include "vector_ops.hpp"
|
---|
[d52e70c] | 20 | #include "vectorunittest.hpp"
|
---|
[0a4f7f] | 21 | #include "Plane.hpp"
|
---|
| 22 | #include "Exceptions/LinearDependenceException.hpp"
|
---|
[54a746] | 23 |
|
---|
[9b6b2f] | 24 | #ifdef HAVE_TESTRUNNER
|
---|
| 25 | #include "UnitTestMain.hpp"
|
---|
| 26 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 27 |
|
---|
[1bd79e] | 28 | #include <iostream>
|
---|
| 29 |
|
---|
| 30 | using namespace std;
|
---|
| 31 |
|
---|
[54a746] | 32 | /********************************************** Test classes **************************************/
|
---|
| 33 |
|
---|
| 34 | // Registers the fixture into the 'registry'
|
---|
| 35 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | void VectorTest::setUp()
|
---|
| 39 | {
|
---|
[1bd79e] | 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.);
|
---|
[1829c4] | 45 | three = Vector(1,2,3);
|
---|
[54a746] | 46 | };
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | void VectorTest::tearDown()
|
---|
| 50 | {
|
---|
[e6fdbe] | 51 | MemoryUsageObserver::purgeInstance();
|
---|
| 52 | logger::purgeInstance();
|
---|
| 53 | errorLogger::purgeInstance();
|
---|
[54a746] | 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;
|
---|
[78b73c] | 75 | // copy vector
|
---|
[1bd79e] | 76 | fixture = Vector(2.,3.,4.);
|
---|
[78b73c] | 77 | CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
|
---|
[54a746] | 78 | // summation and scaling
|
---|
[273382] | 79 | fixture = zero + unit;
|
---|
[78b73c] | 80 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
[273382] | 81 | fixture = zero - unit;
|
---|
[78b73c] | 82 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
| 83 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
---|
[273382] | 84 | fixture = zero + zero;
|
---|
[78b73c] | 85 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
|
---|
[273382] | 86 | fixture = notunit - otherunit;
|
---|
[78b73c] | 87 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
[273382] | 88 | fixture = unit - otherunit;
|
---|
[78b73c] | 89 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
[273382] | 90 | fixture = notunit - unit - otherunit;
|
---|
[78b73c] | 91 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
---|
[273382] | 92 | fixture = 0.98 * unit;
|
---|
[78b73c] | 93 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
[273382] | 94 | fixture = 1. * unit;
|
---|
[78b73c] | 95 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
[54a746] | 96 | factor = 0.98;
|
---|
[273382] | 97 | fixture = factor * unit;
|
---|
[78b73c] | 98 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
[54a746] | 99 | factor = 1.;
|
---|
[273382] | 100 | fixture = factor * unit;
|
---|
[78b73c] | 101 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
[54a746] | 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() );
|
---|
[ef9df36] | 111 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
[54a746] | 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() );
|
---|
[ef9df36] | 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) );
|
---|
[54a746] | 124 | };
|
---|
| 125 |
|
---|
[ef9df36] | 126 | /** UnitTest for scalar products.
|
---|
[54a746] | 127 | */
|
---|
[ef9df36] | 128 | void VectorTest::EuclidianScalarProductTest()
|
---|
[54a746] | 129 | {
|
---|
[273382] | 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) );
|
---|
[ef9df36] | 141 | }
|
---|
[54a746] | 142 |
|
---|
[ef9df36] | 143 | /** UnitTest for norms.
|
---|
| 144 | */
|
---|
| 145 | void VectorTest::EuclidianNormTest()
|
---|
| 146 | {
|
---|
[54a746] | 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() );
|
---|
[ef9df36] | 155 | }
|
---|
[54a746] | 156 |
|
---|
[ef9df36] | 157 | /** UnitTest for distances.
|
---|
| 158 | */
|
---|
| 159 | void VectorTest::EuclidianDistancesTest()
|
---|
| 160 | {
|
---|
[1513a74] | 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) );
|
---|
[ef9df36] | 166 | }
|
---|
[54a746] | 167 |
|
---|
[ef9df36] | 168 | /** UnitTest for angles.
|
---|
| 169 | */
|
---|
| 170 | void VectorTest::EuclidianAnglesTest()
|
---|
| 171 | {
|
---|
[273382] | 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 );
|
---|
[54a746] | 177 | };
|
---|
| 178 |
|
---|
[ef9df36] | 179 | /** UnitTest for projections.
|
---|
| 180 | */
|
---|
| 181 | void VectorTest::ProjectionTest()
|
---|
| 182 | {
|
---|
[273382] | 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) );
|
---|
[ef9df36] | 187 | };
|
---|
| 188 |
|
---|
[1829c4] | 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 |
|
---|
[ef9df36] | 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 ???
|
---|
[0a4f7f] | 223 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(unit, zero).GetIntersection(zero, two) );
|
---|
[78b73c] | 224 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
---|
[ef9df36] | 225 |
|
---|
| 226 | // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
|
---|
[0a4f7f] | 227 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(otherunit, two).GetIntersection( unit, notunit) );
|
---|
[78b73c] | 228 | CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
|
---|
[ef9df36] | 229 |
|
---|
| 230 | // four vectors equal to zero
|
---|
[0a4f7f] | 231 | CPPUNIT_ASSERT_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(zero, zero, zero, zero), LinearDependenceException);
|
---|
| 232 | //CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
---|
[ef9df36] | 233 |
|
---|
| 234 | // four vectors equal to unit
|
---|
[0a4f7f] | 235 | CPPUNIT_ASSERT_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, unit, unit, unit), LinearDependenceException);
|
---|
| 236 | //CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
---|
[ef9df36] | 237 |
|
---|
| 238 | // two equal lines
|
---|
[0a4f7f] | 239 | CPPUNIT_ASSERT_NO_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, unit, two));
|
---|
[78b73c] | 240 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
---|
[ef9df36] | 241 |
|
---|
| 242 | // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ???
|
---|
[0a4f7f] | 243 | CPPUNIT_ASSERT_NO_THROW( fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, unit, otherunit) );
|
---|
[78b73c] | 244 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
---|
[ef9df36] | 245 |
|
---|
| 246 | // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ???
|
---|
[0a4f7f] | 247 | CPPUNIT_ASSERT_NO_THROW( fixture = GetIntersectionOfTwoLinesOnPlane(unit, zero, zero, two) );
|
---|
[78b73c] | 248 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
---|
[ef9df36] | 249 |
|
---|
| 250 | // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ???
|
---|
[0a4f7f] | 251 | CPPUNIT_ASSERT_NO_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, zero, otherunit) );
|
---|
[78b73c] | 252 | CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), fixture );
|
---|
[ef9df36] | 253 | };
|
---|
[54a746] | 254 |
|
---|
[78b73c] | 255 | /** UnitTest for vector rotations.
|
---|
| 256 | */
|
---|
| 257 | void VectorTest::VectorRotationTest()
|
---|
| 258 | {
|
---|
[1bd79e] | 259 | fixture = Vector(-1.,0.,0.);
|
---|
[78b73c] | 260 |
|
---|
| 261 | // zero vector does not change
|
---|
[0a4f7f] | 262 | fixture = RotateVector(zero,unit, 1.);
|
---|
[78b73c] | 263 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
---|
| 264 |
|
---|
[0a4f7f] | 265 | fixture = RotateVector(zero, two, 1.);
|
---|
[78b73c] | 266 | CPPUNIT_ASSERT_EQUAL( zero, fixture);
|
---|
| 267 |
|
---|
| 268 | // vector on axis does not change
|
---|
[0a4f7f] | 269 | fixture = RotateVector(unit,unit, 1.);
|
---|
[78b73c] | 270 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
---|
| 271 |
|
---|
| 272 | // rotations
|
---|
[0a4f7f] | 273 | fixture = RotateVector(otherunit, unit, M_PI);
|
---|
[78b73c] | 274 | CPPUNIT_ASSERT_EQUAL( Vector(0.,-1.,0.), fixture );
|
---|
| 275 |
|
---|
[0a4f7f] | 276 | fixture = RotateVector(otherunit, unit, 2. * M_PI);
|
---|
[78b73c] | 277 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
---|
| 278 |
|
---|
[0a4f7f] | 279 | fixture = RotateVector(otherunit,unit, 0);
|
---|
[78b73c] | 280 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
---|
| 281 |
|
---|
[0a4f7f] | 282 | fixture = RotateVector(Vector(0.,0.,1.), notunit, M_PI);
|
---|
[78b73c] | 283 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[89c8b2] | 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 |
|
---|
[1bd79e] | 302 | fixture = zero;
|
---|
[89c8b2] | 303 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 304 | fixture = Vector(2.5,2.5,2.5);
|
---|
[89c8b2] | 305 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 306 | fixture = Vector(1.,1.,1.);
|
---|
[89c8b2] | 307 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 308 | fixture = Vector(3.5,3.5,3.5);
|
---|
[89c8b2] | 309 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 310 | fixture = Vector(2.,2.,2.);
|
---|
[89c8b2] | 311 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 312 | fixture = Vector(2.,3.,2.);
|
---|
[89c8b2] | 313 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 314 | fixture = Vector(-2.,2.,-1.);
|
---|
[89c8b2] | 315 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
| 316 | }
|
---|
| 317 |
|
---|