| 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 | };
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | void VectorTest::tearDown()
|
|---|
| 31 | {
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | /** UnitTest for Constructors and Vector::IsZero() and Vector::IsOne().
|
|---|
| 35 | */
|
|---|
| 36 | void VectorTest::UnityTest()
|
|---|
| 37 | {
|
|---|
| 38 | Vector zero(0.,0.,0.);
|
|---|
| 39 | Vector unit(1.,0.,0.);
|
|---|
| 40 | Vector otherunit(1./sqrt(2.),0.,1./sqrt(2.));
|
|---|
| 41 | Vector notunit(0.,1.,1.);
|
|---|
| 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 zero(0.,0.,0.);
|
|---|
| 58 | Vector unit(1.,0.,0.);
|
|---|
| 59 | Vector otherunit(0.,1.,0.);
|
|---|
| 60 | Vector notunit(0.,1.,1.);
|
|---|
| 61 | Vector helper;
|
|---|
| 62 | double factor;
|
|---|
| 63 |
|
|---|
| 64 | // summation and scaling
|
|---|
| 65 | helper.CopyVector(&zero);
|
|---|
| 66 | helper.AddVector(&unit);
|
|---|
| 67 | CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
|
|---|
| 68 | helper.CopyVector(&zero);
|
|---|
| 69 | helper.SubtractVector(&unit);
|
|---|
| 70 | CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
|
|---|
| 71 | CPPUNIT_ASSERT_EQUAL( false, helper.IsZero() );
|
|---|
| 72 | helper.CopyVector(&zero);
|
|---|
| 73 | helper.AddVector(&zero);
|
|---|
| 74 | CPPUNIT_ASSERT_EQUAL( true, helper.IsZero() );
|
|---|
| 75 | helper.CopyVector(¬unit);
|
|---|
| 76 | helper.SubtractVector(&otherunit);
|
|---|
| 77 | CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
|
|---|
| 78 | helper.CopyVector(&unit);
|
|---|
| 79 | helper.AddVector(&otherunit);
|
|---|
| 80 | CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() );
|
|---|
| 81 | helper.CopyVector(¬unit);
|
|---|
| 82 | helper.SubtractVector(&unit);
|
|---|
| 83 | helper.SubtractVector(&otherunit);
|
|---|
| 84 | CPPUNIT_ASSERT_EQUAL( false, helper.IsZero() );
|
|---|
| 85 | helper.CopyVector(&unit);
|
|---|
| 86 | helper.Scale(0.98);
|
|---|
| 87 | CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() );
|
|---|
| 88 | helper.CopyVector(&unit);
|
|---|
| 89 | helper.Scale(1.);
|
|---|
| 90 | CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
|
|---|
| 91 | helper.CopyVector(&unit);
|
|---|
| 92 | factor = 0.98;
|
|---|
| 93 | helper.Scale(factor);
|
|---|
| 94 | CPPUNIT_ASSERT_EQUAL( false, helper.IsOne() );
|
|---|
| 95 | helper.CopyVector(&unit);
|
|---|
| 96 | factor = 1.;
|
|---|
| 97 | helper.Scale(factor);
|
|---|
| 98 | CPPUNIT_ASSERT_EQUAL( true, helper.IsOne() );
|
|---|
| 99 | };
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | /** UnitTest for operator versions of Vector::CopyVector(), Vector::AddVector, Vector::SubtractVector() and Vector::Scale().
|
|---|
| 103 | */
|
|---|
| 104 | void VectorTest::OperatorAlgebraTest()
|
|---|
| 105 | {
|
|---|
| 106 | Vector zero(0.,0.,0.);
|
|---|
| 107 | Vector unit(1.,0.,0.);
|
|---|
| 108 | Vector otherunit(0.,1.,0.);
|
|---|
| 109 | Vector notunit(0.,1.,1.);
|
|---|
| 110 |
|
|---|
| 111 | // summation and scaling
|
|---|
| 112 | CPPUNIT_ASSERT_EQUAL( true, (zero+unit).IsOne() );
|
|---|
| 113 | CPPUNIT_ASSERT_EQUAL( true, (zero-unit).IsOne() );
|
|---|
| 114 | CPPUNIT_ASSERT_EQUAL( false, (zero-unit).IsZero() );
|
|---|
| 115 | CPPUNIT_ASSERT_EQUAL( true, (zero+zero).IsZero() );
|
|---|
| 116 | CPPUNIT_ASSERT_EQUAL( true, (notunit-otherunit).IsOne() );
|
|---|
| 117 | CPPUNIT_ASSERT_EQUAL( false, (unit+otherunit).IsOne() );
|
|---|
| 118 | CPPUNIT_ASSERT_EQUAL( false, (notunit-unit-otherunit).IsZero() );
|
|---|
| 119 | CPPUNIT_ASSERT_EQUAL( false, (unit*0.98).IsOne() );
|
|---|
| 120 | CPPUNIT_ASSERT_EQUAL( true, (unit*1.).IsOne() );
|
|---|
| 121 | };
|
|---|
| 122 |
|
|---|
| 123 | /** UnitTest for scalar products, norms, distances and angles.
|
|---|
| 124 | */
|
|---|
| 125 | void VectorTest::EuclidianTest()
|
|---|
| 126 | {
|
|---|
| 127 | Vector zero(0.,0.,0.);
|
|---|
| 128 | Vector unit(1.,0.,0.);
|
|---|
| 129 | Vector otherunit(0.,1.,0.);
|
|---|
| 130 | Vector notunit(0.,1.,1.);
|
|---|
| 131 | Vector two(2.,1.,0.);
|
|---|
| 132 |
|
|---|
| 133 | // SCP
|
|---|
| 134 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&zero) );
|
|---|
| 135 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&unit) );
|
|---|
| 136 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(&otherunit) );
|
|---|
| 137 | CPPUNIT_ASSERT_EQUAL( 0., zero.ScalarProduct(¬unit) );
|
|---|
| 138 | CPPUNIT_ASSERT_EQUAL( 1., unit.ScalarProduct(&unit) );
|
|---|
| 139 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
|
|---|
| 140 | CPPUNIT_ASSERT_EQUAL( 0., otherunit.ScalarProduct(&unit) );
|
|---|
| 141 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.ScalarProduct(¬unit) );
|
|---|
| 142 | CPPUNIT_ASSERT_EQUAL( 2., two.ScalarProduct(&unit) );
|
|---|
| 143 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(&otherunit) );
|
|---|
| 144 | CPPUNIT_ASSERT_EQUAL( 1., two.ScalarProduct(¬unit) );
|
|---|
| 145 |
|
|---|
| 146 | // Norms
|
|---|
| 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() );
|
|---|
| 155 |
|
|---|
| 156 | // distances
|
|---|
| 157 | CPPUNIT_ASSERT_EQUAL( 1., zero.Distance(&unit) );
|
|---|
| 158 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), otherunit.Distance(&unit) );
|
|---|
| 159 | CPPUNIT_ASSERT_EQUAL( sqrt(2.), zero.Distance(¬unit) );
|
|---|
| 160 | CPPUNIT_ASSERT_EQUAL( 1., otherunit.Distance(¬unit) );
|
|---|
| 161 | CPPUNIT_ASSERT_EQUAL( sqrt(5.), two.Distance(¬unit) );
|
|---|
| 162 |
|
|---|
| 163 | // Angles
|
|---|
| 164 | CPPUNIT_ASSERT_EQUAL( M_PI, zero.Angle(&unit) );
|
|---|
| 165 | };
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | /********************************************** Main routine **************************************/
|
|---|
| 169 |
|
|---|
| 170 | int main(int argc, char **argv)
|
|---|
| 171 | {
|
|---|
| 172 | // Get the top level suite from the registry
|
|---|
| 173 | CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
|---|
| 174 |
|
|---|
| 175 | // Adds the test to the list of test to run
|
|---|
| 176 | CppUnit::TextUi::TestRunner runner;
|
|---|
| 177 | runner.addTest( suite );
|
|---|
| 178 |
|
|---|
| 179 | // Change the default outputter to a compiler error format outputter
|
|---|
| 180 | runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
|
|---|
| 181 | std::cerr ) );
|
|---|
| 182 | // Run the tests.
|
|---|
| 183 | bool wasSucessful = runner.run();
|
|---|
| 184 |
|
|---|
| 185 | // Return error code 1 if the one of test failed.
|
|---|
| 186 | return wasSucessful ? 0 : 1;
|
|---|
| 187 | };
|
|---|