/* * unittest.cpp * * Created on: Aug 17, 2009 * Author: heber */ using namespace std; #include #include #include #include "defs.hpp" #include "vector.hpp" #include "vectorunittest.hpp" /********************************************** Test classes **************************************/ // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( VectorTest ); void VectorTest::setUp() { zero.Init(0.,0.,0.); unit.Init(1.,0.,0.); otherunit.Init(0.,1.,0.); notunit.Init(0.,1.,1.); two.Init(2.,1.,0.); }; void VectorTest::tearDown() { }; /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne(). */ void VectorTest::UnityTest() { // unity and zero tests CPPUNIT_ASSERT_EQUAL( true, zero.IsZero() ); CPPUNIT_ASSERT_EQUAL( false, zero.IsOne() ); CPPUNIT_ASSERT_EQUAL( false, unit.IsZero() ); CPPUNIT_ASSERT_EQUAL( true, unit.IsOne() ); CPPUNIT_ASSERT_EQUAL( false, notunit.IsOne() ); CPPUNIT_ASSERT_EQUAL( true, otherunit.IsOne() ); CPPUNIT_ASSERT_EQUAL( false, otherunit.IsZero() ); }; /** UnitTest for Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale()/ */ void VectorTest::SimpleAlgebraTest() { Vector helper; double factor; // summation and scaling helper.CopyVector(&zero); helper.AddVector(&unit); CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() ); helper.CopyVector(&zero); helper.SubtractVector(&unit); CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() ); CPPUNIT_ASSERT_EQUAL( false, helper.IsZero() ); helper.CopyVector(&zero); helper.AddVector(&zero); CPPUNIT_ASSERT_EQUAL( true, helper.IsZero() ); helper.CopyVector(¬unit); helper.SubtractVector(&otherunit); CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() ); helper.CopyVector(&unit); helper.AddVector(&otherunit); CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() ); helper.CopyVector(¬unit); helper.SubtractVector(&unit); helper.SubtractVector(&otherunit); CPPUNIT_ASSERT_EQUAL( false, helper.IsZero() ); helper.CopyVector(&unit); helper.Scale(0.98); CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() ); helper.CopyVector(&unit); helper.Scale(1.); CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() ); helper.CopyVector(&unit); factor = 0.98; helper.Scale(factor); CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() ); helper.CopyVector(&unit); factor = 1.; helper.Scale(factor); CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() ); }; /** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale(). */ void VectorTest::OperatorAlgebraTest() { // summation and scaling CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() ); CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() ); CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() ); CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() ); CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() ); CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() ); CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() ); CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() ); CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() ); CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() ); CPPUNIT_ASSERT_EQUAL( unit, (zero+unit) ); CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,1.), (notunit-otherunit) ); CPPUNIT_ASSERT_EQUAL( Vector(-1, 0., 1.), (notunit-unit-otherunit) ); }; /** UnitTest for scalar products. */ void VectorTest::EuclidianScalarProductTest() { CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&zero) ); CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&unit) ); CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&otherunit) ); CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(¬unit) ); CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(&unit) ); CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) ); CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) ); CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(¬unit) ); CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(&unit) ); CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&otherunit) ); CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(¬unit) ); } /** UnitTest for norms. */ void VectorTest::EuclidianNormTest() { CPPUNIT_ASSERT_EQUAL( 0., zero.Norm() ); CPPUNIT_ASSERT_EQUAL( 0., zero.NormSquared() ); CPPUNIT_ASSERT_EQUAL( 1., unit.Norm() ); CPPUNIT_ASSERT_EQUAL( 1., unit.NormSquared() ); CPPUNIT_ASSERT_EQUAL( 1., otherunit.Norm() ); CPPUNIT_ASSERT_EQUAL( 1., otherunit.NormSquared() ); CPPUNIT_ASSERT_EQUAL( 2., notunit.NormSquared() ); CPPUNIT_ASSERT_EQUAL( sqrt(2.), notunit.Norm() ); } /** UnitTest for distances. */ void VectorTest::EuclidianDistancesTest() { CPPUNIT_ASSERT_EQUAL( 1., zero.Distance(&unit) ); CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.Distance(&unit) ); CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.Distance(¬unit) ); CPPUNIT_ASSERT_EQUAL( 1., otherunit.Distance(¬unit) ); CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.Distance(¬unit) ); } /** UnitTest for angles. */ void VectorTest::EuclidianAnglesTest() { CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(&unit) ); CPPUNIT_ASSERT_EQUAL( 0., unit.Angle(&unit) ); CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - otherunit.Angle(&unit)) < MYEPSILON ); CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/2. - unit.Angle(¬unit)) < MYEPSILON ); CPPUNIT_ASSERT_EQUAL( true, fabs(M_PI/4. - otherunit.Angle(¬unit)) < MYEPSILON ); }; /** UnitTest for projections. */ void VectorTest::ProjectionTest() { CPPUNIT_ASSERT_EQUAL( zero, zero.Projection(&unit) ); CPPUNIT_ASSERT_EQUAL( zero, otherunit.Projection(&unit) ); CPPUNIT_ASSERT_EQUAL( Vector(0.4,0.2,0.), otherunit.Projection(&two) ); CPPUNIT_ASSERT_EQUAL( Vector(0.,1.,0.), two.Projection(&otherunit) ); }; /** UnitTest for line intersections. */ void VectorTest::LineIntersectionTest() { Vector helper; // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ??? CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionWithPlane((ofstream *)&cout, &unit, &zero, &zero, &two) ); CPPUNIT_ASSERT_EQUAL( zero, helper ); // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ??? CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionWithPlane((ofstream *)&cout, &otherunit, &two, &unit, ¬unit) ); CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), helper ); // four vectors equal to zero CPPUNIT_ASSERT_EQUAL( false, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &zero, &zero, &zero, &zero, NULL) ); CPPUNIT_ASSERT_EQUAL( zero, helper ); // four vectors equal to unit CPPUNIT_ASSERT_EQUAL( false, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &unit, &unit, &unit, NULL) ); CPPUNIT_ASSERT_EQUAL( zero, helper ); // two equal lines CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &two, &unit, &two, NULL) ); CPPUNIT_ASSERT_EQUAL( unit, helper ); // line from (1,0,0) to (2,1,0) cuts line from (1,0,0) to (0,1,0) at ??? CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &two, &unit, &otherunit, NULL) ); CPPUNIT_ASSERT_EQUAL( unit, helper ); // line from (1,0,0) to (0,0,0) cuts line from (0,0,0) to (2,1,0) at ??? CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &zero, &zero, &two, NULL) ); CPPUNIT_ASSERT_EQUAL( zero, helper ); // line from (1,0,0) to (2,1,0) cuts line from (0,0,0) to (0,1,0) at ??? CPPUNIT_ASSERT_EQUAL( true, helper.GetIntersectionOfTwoLinesOnPlane((ofstream *)&cout, &unit, &two, &zero, &otherunit, NULL) ); CPPUNIT_ASSERT_EQUAL( Vector(0., -1., 0.), helper ); }; /********************************************** Main routine **************************************/ int main(int argc, char **argv) { // Get the top level suite from the registry CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); // Adds the test to the list of test to run CppUnit::TextUi::TestRunner runner; runner.addTest( suite ); // Change the default outputter to a compiler error format outputter runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(), std::cerr ) ); // Run the tests. bool wasSucessful = runner.run(); // Return error code 1 if the one of test failed. return wasSucessful ? 0 : 1; };