| 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 | /********************************************** Test classes **************************************/
|
|---|
| 29 |
|
|---|
| 30 | // Registers the fixture into the 'registry'
|
|---|
| 31 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | void VectorTest::setUp()
|
|---|
| 35 | {
|
|---|
| 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.);
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | void VectorTest::tearDown()
|
|---|
| 45 | {
|
|---|
| 46 | MemoryUsageObserver::purgeInstance();
|
|---|
| 47 | logger::purgeInstance();
|
|---|
| 48 | errorLogger::purgeInstance();
|
|---|
| 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;
|
|---|
| 70 | // copy vector
|
|---|
| 71 | fixture.Init(2.,3.,4.);
|
|---|
| 72 | CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
|
|---|
| 73 | // summation and scaling
|
|---|
| 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);
|
|---|
| 101 | factor = 0.98;
|
|---|
| 102 | fixture.Scale(factor);
|
|---|
| 103 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
|---|
| 104 | fixture.CopyVector(&unit);
|
|---|
| 105 | factor = 1.;
|
|---|
| 106 | fixture.Scale(factor);
|
|---|
| 107 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
|---|
| 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() );
|
|---|
| 117 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
|---|
| 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() );
|
|---|
| 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) );
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | /** UnitTest for scalar products.
|
|---|
| 133 | */
|
|---|
| 134 | void VectorTest::EuclidianScalarProductTest()
|
|---|
| 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) );
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /** UnitTest for norms.
|
|---|
| 150 | */
|
|---|
| 151 | void VectorTest::EuclidianNormTest()
|
|---|
| 152 | {
|
|---|
| 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() );
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /** UnitTest for distances.
|
|---|
| 164 | */
|
|---|
| 165 | void VectorTest::EuclidianDistancesTest()
|
|---|
| 166 | {
|
|---|
| 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) );
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /** UnitTest for angles.
|
|---|
| 175 | */
|
|---|
| 176 | void VectorTest::EuclidianAnglesTest()
|
|---|
| 177 | {
|
|---|
| 178 | CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(&unit) );
|
|---|
| 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 );
|
|---|
| 183 | };
|
|---|
| 184 |
|
|---|
| 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 ???
|
|---|
| 200 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(unit, zero).GetIntersection(zero, two) );
|
|---|
| 201 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
|---|
| 202 |
|
|---|
| 203 | // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
|
|---|
| 204 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(otherunit, two).GetIntersection( unit, notunit) );
|
|---|
| 205 | CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
|
|---|
| 206 |
|
|---|
| 207 | // four vectors equal to zero
|
|---|
| 208 | CPPUNIT_ASSERT_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(zero, zero, zero, zero), LinearDependenceException);
|
|---|
| 209 | //CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
|---|
| 210 |
|
|---|
| 211 | // four vectors equal to unit
|
|---|
| 212 | CPPUNIT_ASSERT_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, unit, unit, unit), LinearDependenceException);
|
|---|
| 213 | //CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
|---|
| 214 |
|
|---|
| 215 | // two equal lines
|
|---|
| 216 | CPPUNIT_ASSERT_NO_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, unit, two));
|
|---|
| 217 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
|---|
| 218 |
|
|---|
| 219 | // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ???
|
|---|
| 220 | CPPUNIT_ASSERT_NO_THROW( fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, unit, otherunit) );
|
|---|
| 221 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
|---|
| 222 |
|
|---|
| 223 | // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ???
|
|---|
| 224 | CPPUNIT_ASSERT_NO_THROW( fixture = GetIntersectionOfTwoLinesOnPlane(unit, zero, zero, two) );
|
|---|
| 225 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
|---|
| 226 |
|
|---|
| 227 | // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ???
|
|---|
| 228 | CPPUNIT_ASSERT_NO_THROW(fixture = GetIntersectionOfTwoLinesOnPlane(unit, two, zero, otherunit) );
|
|---|
| 229 | CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), fixture );
|
|---|
| 230 | };
|
|---|
| 231 |
|
|---|
| 232 | /** UnitTest for vector rotations.
|
|---|
| 233 | */
|
|---|
| 234 | void VectorTest::VectorRotationTest()
|
|---|
| 235 | {
|
|---|
| 236 | fixture.Init(-1.,0.,0.);
|
|---|
| 237 |
|
|---|
| 238 | // zero vector does not change
|
|---|
| 239 | fixture = RotateVector(zero,unit, 1.);
|
|---|
| 240 | CPPUNIT_ASSERT_EQUAL( zero, fixture );
|
|---|
| 241 |
|
|---|
| 242 | fixture = RotateVector(zero, two, 1.);
|
|---|
| 243 | CPPUNIT_ASSERT_EQUAL( zero, fixture);
|
|---|
| 244 |
|
|---|
| 245 | // vector on axis does not change
|
|---|
| 246 | fixture = RotateVector(unit,unit, 1.);
|
|---|
| 247 | CPPUNIT_ASSERT_EQUAL( unit, fixture );
|
|---|
| 248 |
|
|---|
| 249 | // rotations
|
|---|
| 250 | fixture = RotateVector(otherunit, unit, M_PI);
|
|---|
| 251 | CPPUNIT_ASSERT_EQUAL( Vector(0.,-1.,0.), fixture );
|
|---|
| 252 |
|
|---|
| 253 | fixture = RotateVector(otherunit, unit, 2. * M_PI);
|
|---|
| 254 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
|---|
| 255 |
|
|---|
| 256 | fixture = RotateVector(otherunit,unit, 0);
|
|---|
| 257 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
|---|
| 258 |
|
|---|
| 259 | fixture = RotateVector(Vector(0.,0.,1.), notunit, M_PI);
|
|---|
| 260 | CPPUNIT_ASSERT_EQUAL( otherunit, fixture );
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 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 |
|
|---|