| 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 "vectorunittest.hpp" | 
|---|
| 16 | #include "vector.hpp" | 
|---|
| 17 | #include "defs.hpp" | 
|---|
| 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 | { | 
|---|
| 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.); | 
|---|
| 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 | Vector helper; | 
|---|
| 58 | double factor; | 
|---|
| 59 | // summation and scaling | 
|---|
| 60 | helper.CopyVector(&zero); | 
|---|
| 61 | helper.AddVector(&unit); | 
|---|
| 62 | CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() ); | 
|---|
| 63 | helper.CopyVector(&zero); | 
|---|
| 64 | helper.SubtractVector(&unit); | 
|---|
| 65 | CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() ); | 
|---|
| 66 | CPPUNIT_ASSERT_EQUAL( false, helper.IsZero() ); | 
|---|
| 67 | helper.CopyVector(&zero); | 
|---|
| 68 | helper.AddVector(&zero); | 
|---|
| 69 | CPPUNIT_ASSERT_EQUAL( true, helper.IsZero() ); | 
|---|
| 70 | helper.CopyVector(¬unit); | 
|---|
| 71 | helper.SubtractVector(&otherunit); | 
|---|
| 72 | CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() ); | 
|---|
| 73 | helper.CopyVector(&unit); | 
|---|
| 74 | helper.AddVector(&otherunit); | 
|---|
| 75 | CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() ); | 
|---|
| 76 | helper.CopyVector(¬unit); | 
|---|
| 77 | helper.SubtractVector(&unit); | 
|---|
| 78 | helper.SubtractVector(&otherunit); | 
|---|
| 79 | CPPUNIT_ASSERT_EQUAL( false, helper.IsZero() ); | 
|---|
| 80 | helper.CopyVector(&unit); | 
|---|
| 81 | helper.Scale(0.98); | 
|---|
| 82 | CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() ); | 
|---|
| 83 | helper.CopyVector(&unit); | 
|---|
| 84 | helper.Scale(1.); | 
|---|
| 85 | CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() ); | 
|---|
| 86 | helper.CopyVector(&unit); | 
|---|
| 87 | factor = 0.98; | 
|---|
| 88 | helper.Scale(factor); | 
|---|
| 89 | CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() ); | 
|---|
| 90 | helper.CopyVector(&unit); | 
|---|
| 91 | factor = 1.; | 
|---|
| 92 | helper.Scale(factor); | 
|---|
| 93 | CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() ); | 
|---|
| 94 | }; | 
|---|
| 95 |  | 
|---|
| 96 |  | 
|---|
| 97 | /** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale(). | 
|---|
| 98 | */ | 
|---|
| 99 | void VectorTest::OperatorAlgebraTest() | 
|---|
| 100 | { | 
|---|
| 101 | // summation and scaling | 
|---|
| 102 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() ); | 
|---|
| 103 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() ); | 
|---|
| 104 | CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() ); | 
|---|
| 105 | CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() ); | 
|---|
| 106 | CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() ); | 
|---|
| 107 | CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() ); | 
|---|
| 108 | CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() ); | 
|---|
| 109 | CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() ); | 
|---|
| 110 | CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() ); | 
|---|
| 111 | CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() ); | 
|---|
| 112 |  | 
|---|
| 113 | CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) ); | 
|---|
| 114 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) ); | 
|---|
| 115 | CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) ); | 
|---|
| 116 | }; | 
|---|
| 117 |  | 
|---|
| 118 | /** UnitTest for scalar products. | 
|---|
| 119 | */ | 
|---|
| 120 | void VectorTest::EuclidianScalarProductTest() | 
|---|
| 121 | { | 
|---|
| 122 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&zero) ); | 
|---|
| 123 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&unit) ); | 
|---|
| 124 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&otherunit) ); | 
|---|
| 125 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(¬unit) ); | 
|---|
| 126 | CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(&unit) ); | 
|---|
| 127 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) ); | 
|---|
| 128 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) ); | 
|---|
| 129 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(¬unit) ); | 
|---|
| 130 | CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(&unit) ); | 
|---|
| 131 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&otherunit) ); | 
|---|
| 132 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(¬unit) ); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | /** UnitTest for norms. | 
|---|
| 136 | */ | 
|---|
| 137 | void VectorTest::EuclidianNormTest() | 
|---|
| 138 | { | 
|---|
| 139 | CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() ); | 
|---|
| 140 | CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() ); | 
|---|
| 141 | CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() ); | 
|---|
| 142 | CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() ); | 
|---|
| 143 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() ); | 
|---|
| 144 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() ); | 
|---|
| 145 | CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() ); | 
|---|
| 146 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() ); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | /** UnitTest for distances. | 
|---|
| 150 | */ | 
|---|
| 151 | void VectorTest::EuclidianDistancesTest() | 
|---|
| 152 | { | 
|---|
| 153 | CPPUNIT_ASSERT_EQUAL( 1., zero.Distance(&unit) ); | 
|---|
| 154 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.Distance(&unit) ); | 
|---|
| 155 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.Distance(¬unit) ); | 
|---|
| 156 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.Distance(¬unit) ); | 
|---|
| 157 | CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.Distance(¬unit) ); | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | /** UnitTest for angles. | 
|---|
| 161 | */ | 
|---|
| 162 | void VectorTest::EuclidianAnglesTest() | 
|---|
| 163 | { | 
|---|
| 164 | CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(&unit) ); | 
|---|
| 165 | CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(&unit) ); | 
|---|
| 166 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(&unit)) < MYEPSILON ); | 
|---|
| 167 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(¬unit)) < MYEPSILON ); | 
|---|
| 168 | CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(¬unit)) < MYEPSILON ); | 
|---|
| 169 | }; | 
|---|
| 170 |  | 
|---|
| 171 | /** UnitTest for projections. | 
|---|
| 172 | */ | 
|---|
| 173 | void VectorTest::ProjectionTest() | 
|---|
| 174 | { | 
|---|
| 175 | CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(&unit) ); | 
|---|
| 176 | CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(&unit) ); | 
|---|
| 177 | CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.),  otherunit.Projection(&two) ); | 
|---|
| 178 | CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.),  two.Projection(&otherunit) ); | 
|---|
| 179 | }; | 
|---|
| 180 |  | 
|---|
| 181 | /** UnitTest for line intersections. | 
|---|
| 182 | */ | 
|---|
| 183 | void VectorTest::LineIntersectionTest() | 
|---|
| 184 | { | 
|---|
| 185 | Vector helper; | 
|---|
| 186 | // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ??? | 
|---|
| 187 | CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionWithPlane((ofstream *)&cout, &unit, &zero, &zero, &two) ); | 
|---|
| 188 | CPPUNIT_ASSERT_EQUAL( zero, helper ); | 
|---|
| 189 |  | 
|---|
| 190 | // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ??? | 
|---|
| 191 | CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionWithPlane((ofstream *)&cout, &otherunit, &two, &unit, ¬unit) ); | 
|---|
| 192 | CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), helper ); | 
|---|
| 193 |  | 
|---|
| 194 | // four vectors equal to zero | 
|---|
| 195 | CPPUNIT_ASSERT_EQUAL( false, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &zero, &zero, &zero, &zero, NULL) ); | 
|---|
| 196 | CPPUNIT_ASSERT_EQUAL( zero, helper ); | 
|---|
| 197 |  | 
|---|
| 198 | // four vectors equal to unit | 
|---|
| 199 | CPPUNIT_ASSERT_EQUAL( false, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &unit, &unit, &unit, NULL) ); | 
|---|
| 200 | CPPUNIT_ASSERT_EQUAL( zero, helper ); | 
|---|
| 201 |  | 
|---|
| 202 | // two equal lines | 
|---|
| 203 | CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &two, &unit, &two, NULL) ); | 
|---|
| 204 | CPPUNIT_ASSERT_EQUAL( unit, helper ); | 
|---|
| 205 |  | 
|---|
| 206 | // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ??? | 
|---|
| 207 | CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &two, &unit, &otherunit, NULL) ); | 
|---|
| 208 | CPPUNIT_ASSERT_EQUAL( unit, helper ); | 
|---|
| 209 |  | 
|---|
| 210 | // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ??? | 
|---|
| 211 | CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &zero, &zero, &two, NULL) ); | 
|---|
| 212 | CPPUNIT_ASSERT_EQUAL( zero, helper ); | 
|---|
| 213 |  | 
|---|
| 214 | // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ??? | 
|---|
| 215 | CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &two, &zero, &otherunit, NULL) ); | 
|---|
| 216 | CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), helper ); | 
|---|
| 217 | }; | 
|---|
| 218 |  | 
|---|
| 219 | /********************************************** Main routine **************************************/ | 
|---|
| 220 |  | 
|---|
| 221 | int main(int argc, char **argv) | 
|---|
| 222 | { | 
|---|
| 223 | // Get the top level suite from the registry | 
|---|
| 224 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); | 
|---|
| 225 |  | 
|---|
| 226 | // Adds the test to the list of test to run | 
|---|
| 227 | CppUnit::TextUi::TestRunner runner; | 
|---|
| 228 | runner.addTest( suite ); | 
|---|
| 229 |  | 
|---|
| 230 | // Change the default outputter to a compiler error format outputter | 
|---|
| 231 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), | 
|---|
| 232 | std::cerr ) ); | 
|---|
| 233 | // Run the tests. | 
|---|
| 234 | bool wasSucessful = runner.run(); | 
|---|
| 235 |  | 
|---|
| 236 | // Return error code 1 if the one of test failed. | 
|---|
| 237 | return wasSucessful ? 0 : 1; | 
|---|
| 238 | }; | 
|---|