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