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