| 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 |   MemoryUsageObserver::purgeInstance();
 | 
|---|
| 52 |   logger::purgeInstance();
 | 
|---|
| 53 |   errorLogger::purgeInstance();
 | 
|---|
| 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;
 | 
|---|
| 75 |   // copy vector
 | 
|---|
| 76 |   fixture = Vector(2.,3.,4.);
 | 
|---|
| 77 |   CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
 | 
|---|
| 78 |   // summation and scaling
 | 
|---|
| 79 |   fixture = zero + unit;
 | 
|---|
| 80 |   CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
 | 
|---|
| 81 |   fixture = zero - unit;
 | 
|---|
| 82 |   CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
 | 
|---|
| 83 |   CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
 | 
|---|
| 84 |   fixture = zero + zero;
 | 
|---|
| 85 |   CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
 | 
|---|
| 86 |   fixture = notunit - otherunit;
 | 
|---|
| 87 |   CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
 | 
|---|
| 88 |   fixture = unit - otherunit;
 | 
|---|
| 89 |   CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
 | 
|---|
| 90 |   fixture = notunit - unit - otherunit;
 | 
|---|
| 91 |   CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
 | 
|---|
| 92 |   fixture = 0.98 * unit;
 | 
|---|
| 93 |   CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
 | 
|---|
| 94 |   fixture = 1. * unit;
 | 
|---|
| 95 |   CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
 | 
|---|
| 96 |   factor = 0.98;
 | 
|---|
| 97 |   fixture = factor * unit;
 | 
|---|
| 98 |   CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
 | 
|---|
| 99 |   factor = 1.;
 | 
|---|
| 100 |   fixture = factor * unit;
 | 
|---|
| 101 |   CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
 | 
|---|
| 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() );
 | 
|---|
| 111 |   CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
 | 
|---|
| 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() );
 | 
|---|
| 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) );
 | 
|---|
| 124 | };
 | 
|---|
| 125 | 
 | 
|---|
| 126 | /** UnitTest for scalar products.
 | 
|---|
| 127 |  */
 | 
|---|
| 128 | void VectorTest::EuclidianScalarProductTest()
 | 
|---|
| 129 | {
 | 
|---|
| 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) );
 | 
|---|
| 141 | }
 | 
|---|
| 142 | 
 | 
|---|
| 143 | /** UnitTest for norms.
 | 
|---|
| 144 |  */
 | 
|---|
| 145 | void VectorTest::EuclidianNormTest()
 | 
|---|
| 146 | {
 | 
|---|
| 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() );
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | /** UnitTest for distances.
 | 
|---|
| 158 |  */
 | 
|---|
| 159 | void VectorTest::EuclidianDistancesTest()
 | 
|---|
| 160 | {
 | 
|---|
| 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) );
 | 
|---|
| 166 | }
 | 
|---|
| 167 | 
 | 
|---|
| 168 | /** UnitTest for angles.
 | 
|---|
| 169 |  */
 | 
|---|
| 170 | void VectorTest::EuclidianAnglesTest()
 | 
|---|
| 171 | {
 | 
|---|
| 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 );
 | 
|---|
| 177 | };
 | 
|---|
| 178 | 
 | 
|---|
| 179 | /** UnitTest for projections.
 | 
|---|
| 180 |  */
 | 
|---|
| 181 | void VectorTest::ProjectionTest()
 | 
|---|
| 182 | {
 | 
|---|
| 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) );
 | 
|---|
| 187 | };
 | 
|---|
| 188 | 
 | 
|---|
| 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 | 
 | 
|---|
| 218 | 
 | 
|---|
| 219 | /**
 | 
|---|
| 220 |  * UnitTest for Vector::IsInParallelepiped().
 | 
|---|
| 221 |  */
 | 
|---|
| 222 | void VectorTest::IsInParallelepipedTest()
 | 
|---|
| 223 | {
 | 
|---|
| 224 |   double parallelepiped[NDIM*NDIM];
 | 
|---|
| 225 |   parallelepiped[0] = 1;
 | 
|---|
| 226 |   parallelepiped[1] = 0;
 | 
|---|
| 227 |   parallelepiped[2] = 0;
 | 
|---|
| 228 |   parallelepiped[3] = 0;
 | 
|---|
| 229 |   parallelepiped[4] = 1;
 | 
|---|
| 230 |   parallelepiped[5] = 0;
 | 
|---|
| 231 |   parallelepiped[6] = 0;
 | 
|---|
| 232 |   parallelepiped[7] = 0;
 | 
|---|
| 233 |   parallelepiped[8] = 1;
 | 
|---|
| 234 | 
 | 
|---|
| 235 |   fixture = zero;
 | 
|---|
| 236 |   CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
 | 
|---|
| 237 |   fixture = Vector(2.5,2.5,2.5);
 | 
|---|
| 238 |   CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
 | 
|---|
| 239 |   fixture = Vector(1.,1.,1.);
 | 
|---|
| 240 |   CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
 | 
|---|
| 241 |   fixture = Vector(3.5,3.5,3.5);
 | 
|---|
| 242 |   CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
 | 
|---|
| 243 |   fixture = Vector(2.,2.,2.);
 | 
|---|
| 244 |   CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
 | 
|---|
| 245 |   fixture = Vector(2.,3.,2.);
 | 
|---|
| 246 |   CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
 | 
|---|
| 247 |   fixture = Vector(-2.,2.,-1.);
 | 
|---|
| 248 |   CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
 | 
|---|
| 249 | }
 | 
|---|
| 250 | 
 | 
|---|