[54a746] | 1 | /*
|
---|
| 2 | * unittest.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Aug 17, 2009
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[bf3817] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
[54a746] | 12 |
|
---|
| 13 | using namespace std;
|
---|
| 14 |
|
---|
| 15 | #include <cppunit/CompilerOutputter.h>
|
---|
| 16 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 17 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 18 |
|
---|
| 19 | #include "defs.hpp"
|
---|
[952f38] | 20 | #include "Helpers/Log.hpp"
|
---|
[57f243] | 21 | #include "LinearAlgebra/Vector.hpp"
|
---|
[8f4df1] | 22 | #include "LinearAlgebra/vector_ops.hpp"
|
---|
[d52e70c] | 23 | #include "vectorunittest.hpp"
|
---|
[57f243] | 24 | #include "LinearAlgebra/Plane.hpp"
|
---|
[0a4f7f] | 25 | #include "Exceptions/LinearDependenceException.hpp"
|
---|
[57f243] | 26 | #include "LinearAlgebra/Matrix.hpp"
|
---|
[54a746] | 27 |
|
---|
[9b6b2f] | 28 | #ifdef HAVE_TESTRUNNER
|
---|
| 29 | #include "UnitTestMain.hpp"
|
---|
| 30 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 31 |
|
---|
[1bd79e] | 32 | #include <iostream>
|
---|
| 33 |
|
---|
| 34 | using namespace std;
|
---|
| 35 |
|
---|
[54a746] | 36 | /********************************************** Test classes **************************************/
|
---|
| 37 |
|
---|
| 38 | // Registers the fixture into the 'registry'
|
---|
| 39 | CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest );
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | void VectorTest::setUp()
|
---|
| 43 | {
|
---|
[1bd79e] | 44 | zero = Vector(0.,0.,0.);
|
---|
| 45 | unit = Vector(1.,0.,0.);
|
---|
| 46 | otherunit = Vector(0.,1.,0.);
|
---|
| 47 | notunit = Vector(0.,1.,1.);
|
---|
| 48 | two = Vector(2.,1.,0.);
|
---|
[1829c4] | 49 | three = Vector(1,2,3);
|
---|
[54a746] | 50 | };
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | void VectorTest::tearDown()
|
---|
| 54 | {
|
---|
[e6fdbe] | 55 | logger::purgeInstance();
|
---|
| 56 | errorLogger::purgeInstance();
|
---|
[54a746] | 57 | };
|
---|
| 58 |
|
---|
[d74077] | 59 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
|
---|
| 60 | */
|
---|
| 61 | void VectorTest::AssignmentTest()
|
---|
| 62 | {
|
---|
| 63 | // test with zero
|
---|
| 64 | zero.at(0) = 0;
|
---|
| 65 | zero.at(1) = 0;
|
---|
| 66 | zero.at(2) = 0;
|
---|
| 67 | double zero_array[3] = {0., 0., 0.};
|
---|
| 68 |
|
---|
| 69 | CPPUNIT_ASSERT_EQUAL( zero, Vector(0,0,0));
|
---|
| 70 | CPPUNIT_ASSERT_EQUAL( zero, Vector(0.,0.,0.));
|
---|
| 71 | CPPUNIT_ASSERT_EQUAL( zero, Vector(zero_array[0], zero_array[1], zero_array[2]));
|
---|
| 72 | CPPUNIT_ASSERT_EQUAL( zero, Vector(zero_array));
|
---|
| 73 |
|
---|
| 74 | // test with unit
|
---|
| 75 | unit.at(0) = 1;
|
---|
| 76 | unit.at(1) = 0;
|
---|
| 77 | unit.at(2) = 0;
|
---|
| 78 | double unit_array[3] = {1., 0., 0.};
|
---|
| 79 |
|
---|
| 80 | CPPUNIT_ASSERT_EQUAL( unit, Vector(1,0,0));
|
---|
| 81 | CPPUNIT_ASSERT_EQUAL( unit, Vector(1.,0.,0.));
|
---|
| 82 | CPPUNIT_ASSERT_EQUAL( unit, Vector(unit_array[0], unit_array[1], unit_array[2]));
|
---|
| 83 | CPPUNIT_ASSERT_EQUAL( unit, Vector(unit_array));
|
---|
| 84 |
|
---|
| 85 | // test with two
|
---|
| 86 | two.at(0) = 2;
|
---|
| 87 | two.at(1) = 1;
|
---|
| 88 | two.at(2) = 0;
|
---|
| 89 | double two_array[3] = {2., 1., 0.};
|
---|
| 90 |
|
---|
| 91 | CPPUNIT_ASSERT_EQUAL( two, Vector(2,1,0));
|
---|
| 92 | CPPUNIT_ASSERT_EQUAL( two, Vector(2.,1.,0.));
|
---|
| 93 | CPPUNIT_ASSERT_EQUAL( two, Vector(two_array[0], two_array[1], two_array[2]));
|
---|
| 94 | CPPUNIT_ASSERT_EQUAL( two, Vector(two_array));
|
---|
| 95 |
|
---|
| 96 | // test with three
|
---|
| 97 | three.at(0) = 1;
|
---|
| 98 | three.at(1) = 2;
|
---|
| 99 | three.at(2) = 3;
|
---|
| 100 | double three_array[3] = {1., 2., 3.};
|
---|
| 101 |
|
---|
| 102 | CPPUNIT_ASSERT_EQUAL( three, Vector(1,2,3));
|
---|
| 103 | CPPUNIT_ASSERT_EQUAL( three, Vector(1.,2.,3.));
|
---|
| 104 | CPPUNIT_ASSERT_EQUAL( three, Vector(three_array[0], three_array[1], three_array[2]));
|
---|
| 105 | CPPUNIT_ASSERT_EQUAL( three, Vector(three_array));
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[54a746] | 108 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
|
---|
| 109 | */
|
---|
| 110 | void VectorTest::UnityTest()
|
---|
| 111 | {
|
---|
| 112 | // unity and zero tests
|
---|
| 113 | CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() );
|
---|
| 114 | CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() );
|
---|
| 115 | CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() );
|
---|
| 116 | CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() );
|
---|
| 117 | CPPUNIT_ASSERT_EQUAL( false, notunit.IsOne() );
|
---|
| 118 | CPPUNIT_ASSERT_EQUAL( true, otherunit.IsOne() );
|
---|
| 119 | CPPUNIT_ASSERT_EQUAL( false, otherunit.IsZero() );
|
---|
| 120 | };
|
---|
| 121 |
|
---|
| 122 | /** UnitTest for Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale()/
|
---|
| 123 | */
|
---|
| 124 | void VectorTest::SimpleAlgebraTest()
|
---|
| 125 | {
|
---|
| 126 | double factor;
|
---|
[78b73c] | 127 | // copy vector
|
---|
[1bd79e] | 128 | fixture = Vector(2.,3.,4.);
|
---|
[78b73c] | 129 | CPPUNIT_ASSERT_EQUAL( Vector(2.,3.,4.), fixture );
|
---|
[54a746] | 130 | // summation and scaling
|
---|
[273382] | 131 | fixture = zero + unit;
|
---|
[78b73c] | 132 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
[273382] | 133 | fixture = zero - unit;
|
---|
[78b73c] | 134 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
| 135 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
---|
[273382] | 136 | fixture = zero + zero;
|
---|
[78b73c] | 137 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsZero() );
|
---|
[273382] | 138 | fixture = notunit - otherunit;
|
---|
[78b73c] | 139 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
[273382] | 140 | fixture = unit - otherunit;
|
---|
[78b73c] | 141 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
[273382] | 142 | fixture = notunit - unit - otherunit;
|
---|
[78b73c] | 143 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsZero() );
|
---|
[273382] | 144 | fixture = 0.98 * unit;
|
---|
[78b73c] | 145 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
[273382] | 146 | fixture = 1. * unit;
|
---|
[78b73c] | 147 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
[54a746] | 148 | factor = 0.98;
|
---|
[273382] | 149 | fixture = factor * unit;
|
---|
[78b73c] | 150 | CPPUNIT_ASSERT_EQUAL( false, fixture.IsOne() );
|
---|
[54a746] | 151 | factor = 1.;
|
---|
[273382] | 152 | fixture = factor * unit;
|
---|
[78b73c] | 153 | CPPUNIT_ASSERT_EQUAL( true, fixture.IsOne() );
|
---|
[54a746] | 154 | };
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
|
---|
| 158 | */
|
---|
| 159 | void VectorTest::OperatorAlgebraTest()
|
---|
| 160 | {
|
---|
| 161 | // summation and scaling
|
---|
| 162 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
[ef9df36] | 163 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
---|
[54a746] | 164 | CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
|
---|
| 165 | CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
|
---|
| 166 | CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
|
---|
| 167 | CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
|
---|
| 168 | CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
|
---|
| 169 | CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
|
---|
| 170 | CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
|
---|
| 171 | CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
|
---|
[ef9df36] | 172 |
|
---|
| 173 | CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) );
|
---|
| 174 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) );
|
---|
| 175 | CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) );
|
---|
[54a746] | 176 | };
|
---|
| 177 |
|
---|
[ef9df36] | 178 | /** UnitTest for scalar products.
|
---|
[54a746] | 179 | */
|
---|
[ef9df36] | 180 | void VectorTest::EuclidianScalarProductTest()
|
---|
[54a746] | 181 | {
|
---|
[273382] | 182 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(zero) );
|
---|
| 183 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(unit) );
|
---|
| 184 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(otherunit) );
|
---|
| 185 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(notunit) );
|
---|
| 186 | CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(unit) );
|
---|
| 187 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) );
|
---|
| 188 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(unit) );
|
---|
| 189 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(notunit) );
|
---|
| 190 | CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(unit) );
|
---|
| 191 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(otherunit) );
|
---|
| 192 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(notunit) );
|
---|
[ef9df36] | 193 | }
|
---|
[54a746] | 194 |
|
---|
[ef9df36] | 195 | /** UnitTest for norms.
|
---|
| 196 | */
|
---|
| 197 | void VectorTest::EuclidianNormTest()
|
---|
| 198 | {
|
---|
[54a746] | 199 | CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() );
|
---|
| 200 | CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() );
|
---|
| 201 | CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() );
|
---|
| 202 | CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() );
|
---|
| 203 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() );
|
---|
| 204 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() );
|
---|
| 205 | CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() );
|
---|
| 206 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() );
|
---|
[ef9df36] | 207 | }
|
---|
[54a746] | 208 |
|
---|
[ef9df36] | 209 | /** UnitTest for distances.
|
---|
| 210 | */
|
---|
| 211 | void VectorTest::EuclidianDistancesTest()
|
---|
| 212 | {
|
---|
[1513a74] | 213 | CPPUNIT_ASSERT_EQUAL( 1., zero.distance(unit) );
|
---|
| 214 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.distance(unit) );
|
---|
| 215 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.distance(notunit) );
|
---|
| 216 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.distance(notunit) );
|
---|
| 217 | CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.distance(notunit) );
|
---|
[ef9df36] | 218 | }
|
---|
[54a746] | 219 |
|
---|
[ef9df36] | 220 | /** UnitTest for angles.
|
---|
| 221 | */
|
---|
| 222 | void VectorTest::EuclidianAnglesTest()
|
---|
| 223 | {
|
---|
[273382] | 224 | CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(unit) );
|
---|
| 225 | CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(unit) );
|
---|
| 226 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(unit)) < MYEPSILON );
|
---|
| 227 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(notunit)) < MYEPSILON );
|
---|
| 228 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(notunit)) < MYEPSILON );
|
---|
[54a746] | 229 | };
|
---|
| 230 |
|
---|
[ef9df36] | 231 | /** UnitTest for projections.
|
---|
| 232 | */
|
---|
| 233 | void VectorTest::ProjectionTest()
|
---|
| 234 | {
|
---|
[273382] | 235 | CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(unit) );
|
---|
| 236 | CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(unit) );
|
---|
| 237 | CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.), otherunit.Projection(two) );
|
---|
| 238 | CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(otherunit) );
|
---|
[ef9df36] | 239 | };
|
---|
| 240 |
|
---|
[1829c4] | 241 | /**
|
---|
| 242 | * Unittest for operation with normals
|
---|
| 243 | */
|
---|
| 244 | void VectorTest::NormalsTest(){
|
---|
| 245 | Vector testVector;
|
---|
| 246 | // the zero Vector should produce an error
|
---|
| 247 | CPPUNIT_ASSERT(!testVector.GetOneNormalVector(zero));
|
---|
| 248 |
|
---|
| 249 | // first one-component system
|
---|
| 250 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(unit));
|
---|
| 251 | CPPUNIT_ASSERT(testVector.ScalarProduct(unit) < MYEPSILON);
|
---|
| 252 |
|
---|
| 253 | // second one-component system
|
---|
| 254 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(otherunit));
|
---|
| 255 | CPPUNIT_ASSERT(testVector.ScalarProduct(otherunit) < MYEPSILON);
|
---|
| 256 |
|
---|
| 257 | // first two-component system
|
---|
| 258 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(notunit));
|
---|
| 259 | CPPUNIT_ASSERT(testVector.ScalarProduct(notunit) < MYEPSILON);
|
---|
| 260 |
|
---|
| 261 | // second two-component system
|
---|
| 262 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(two));
|
---|
| 263 | CPPUNIT_ASSERT(testVector.ScalarProduct(two) < MYEPSILON);
|
---|
| 264 |
|
---|
| 265 | // three component system
|
---|
| 266 | CPPUNIT_ASSERT(testVector.GetOneNormalVector(three));
|
---|
| 267 | CPPUNIT_ASSERT(testVector.ScalarProduct(three) < MYEPSILON);
|
---|
| 268 | }
|
---|