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