[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"
|
---|
[9df5c6] | 23 | #include "Matrix.hpp"
|
---|
[54a746] | 24 |
|
---|
[9b6b2f] | 25 | #ifdef HAVE_TESTRUNNER
|
---|
| 26 | #include "UnitTestMain.hpp"
|
---|
| 27 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 28 |
|
---|
[1bd79e] | 29 | #include <iostream>
|
---|
| 30 |
|
---|
| 31 | using namespace std;
|
---|
| 32 |
|
---|
[54a746] | 33 | /********************************************** Test classes **************************************/
|
---|
| 34 |
|
---|
| 35 | // Registers the fixture into the 'registry'
|
---|
| 36 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | void VectorTest::setUp()
|
---|
| 40 | {
|
---|
[1bd79e] | 41 | zero = Vector(0.,0.,0.);
|
---|
| 42 | unit = Vector(1.,0.,0.);
|
---|
| 43 | otherunit = Vector(0.,1.,0.);
|
---|
| 44 | notunit = Vector(0.,1.,1.);
|
---|
| 45 | two = Vector(2.,1.,0.);
|
---|
[1829c4] | 46 | three = Vector(1,2,3);
|
---|
[54a746] | 47 | };
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | void VectorTest::tearDown()
|
---|
| 51 | {
|
---|
[e6fdbe] | 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 |
|
---|
[78b73c] | 218 |
|
---|
[89c8b2] | 219 | /**
|
---|
| 220 | * UnitTest for Vector::IsInParallelepiped().
|
---|
| 221 | */
|
---|
| 222 | void VectorTest::IsInParallelepipedTest()
|
---|
| 223 | {
|
---|
[9df5c6] | 224 | Matrix parallelepiped;
|
---|
| 225 | parallelepiped.at(0,0) = 1;
|
---|
| 226 | parallelepiped.at(1,0) = 0;
|
---|
| 227 | parallelepiped.at(2,0) = 0;
|
---|
| 228 | parallelepiped.at(0,1) = 0;
|
---|
| 229 | parallelepiped.at(1,1) = 1;
|
---|
| 230 | parallelepiped.at(2,1) = 0;
|
---|
| 231 | parallelepiped.at(0,2) = 0;
|
---|
| 232 | parallelepiped.at(1,2) = 0;
|
---|
| 233 | parallelepiped.at(2,2) = 1;
|
---|
[89c8b2] | 234 |
|
---|
[1bd79e] | 235 | fixture = zero;
|
---|
[89c8b2] | 236 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 237 | fixture = Vector(2.5,2.5,2.5);
|
---|
[89c8b2] | 238 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 239 | fixture = Vector(1.,1.,1.);
|
---|
[89c8b2] | 240 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 241 | fixture = Vector(3.5,3.5,3.5);
|
---|
[89c8b2] | 242 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 243 | fixture = Vector(2.,2.,2.);
|
---|
[89c8b2] | 244 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 245 | fixture = Vector(2.,3.,2.);
|
---|
[89c8b2] | 246 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
[1bd79e] | 247 | fixture = Vector(-2.,2.,-1.);
|
---|
[89c8b2] | 248 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsInParallelepiped(Vector(2.,2.,2.), parallelepiped) );
|
---|
| 249 | }
|
---|
| 250 |
|
---|