| 1 | /*
 | 
|---|
| 2 |  * ActOnAllUnitTest.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: 04.10.2009
 | 
|---|
| 5 |  *      Author: FrederikHeber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | using namespace std;
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 11 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 12 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #include "../test/ActOnAlltest.hpp"
 | 
|---|
| 15 | #include "ActOnAllUnitTest.hpp"
 | 
|---|
| 16 | #include "memoryallocator.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | /********************************************** Test classes **************************************/
 | 
|---|
| 19 | 
 | 
|---|
| 20 | // Registers the fixture into the 'registry'
 | 
|---|
| 21 | CPPUNIT_TEST_SUITE_REGISTRATION( ActOnAllTest );
 | 
|---|
| 22 | 
 | 
|---|
| 23 | 
 | 
|---|
| 24 | void ActOnAllTest::setUp()
 | 
|---|
| 25 | {
 | 
|---|
| 26 |   Vector *temp = NULL;
 | 
|---|
| 27 |   // fill list with vectors
 | 
|---|
| 28 |   temp = new Vector(1,0,0);
 | 
|---|
| 29 |   VL.AddVector(temp);
 | 
|---|
| 30 |   temp = new Vector(0,1,0);
 | 
|---|
| 31 |   VL.AddVector(temp);
 | 
|---|
| 32 |   temp = new Vector(0,0,1);
 | 
|---|
| 33 |   VL.AddVector(temp);
 | 
|---|
| 34 |   Ref = VL;
 | 
|---|
| 35 | };
 | 
|---|
| 36 | 
 | 
|---|
| 37 | 
 | 
|---|
| 38 | void ActOnAllTest::tearDown()
 | 
|---|
| 39 | {
 | 
|---|
| 40 |   VL.EmptyList();
 | 
|---|
| 41 | };
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /** UnitTest for VectorList::ActOnAll() and Vector::AddVector(), Vector::SubtractVector(),
 | 
|---|
| 44 |  */
 | 
|---|
| 45 | void ActOnAllTest::AddSubtractTest()
 | 
|---|
| 46 | {
 | 
|---|
| 47 |   const Vector test(1.,0.,0.);
 | 
|---|
| 48 | 
 | 
|---|
| 49 |   // adding, subtracting
 | 
|---|
| 50 |   VL.ActOnAll( &Vector::AddVector, &test );
 | 
|---|
| 51 |   CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
 | 
|---|
| 52 |   VL.ActOnAll( &Vector::SubtractVector, &test );
 | 
|---|
| 53 |   CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
 | 
|---|
| 54 | };
 | 
|---|
| 55 | 
 | 
|---|
| 56 | /** UnitTest for VectorList::ActOnAll()
 | 
|---|
| 57 |  */
 | 
|---|
| 58 | void ActOnAllTest::ScaleTest()
 | 
|---|
| 59 | {
 | 
|---|
| 60 |   double factor=2.;
 | 
|---|
| 61 |   double inverse=1./2.;
 | 
|---|
| 62 | 
 | 
|---|
| 63 |   // scaling by value
 | 
|---|
| 64 |   VL.ActOnAll( (void (Vector::*)(const double)) &Vector::Scale, 2. );
 | 
|---|
| 65 |   CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   VL.ActOnAll( (void (Vector::*)(const double)) &Vector::Scale, 0.5 );
 | 
|---|
| 68 |   CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   // scaling by ref
 | 
|---|
| 71 |   VL.ActOnAll( (void (Vector::*)(const double * const)) &Vector::Scale, (const double * const)&factor );
 | 
|---|
| 72 |   CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   VL.ActOnAll( (void (Vector::*)(const double * const)) &Vector::Scale, (const double * const)&inverse );
 | 
|---|
| 75 |   CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   // scaling by three values
 | 
|---|
| 78 |   double *factors = Malloc<double>(NDIM, "ActOnAllTest::ScaleTest - factors");
 | 
|---|
| 79 |   double *inverses = Malloc<double>(NDIM, "ActOnAllTest::ScaleTest - inverses");
 | 
|---|
| 80 |   for (int i=0;i<NDIM;++i) {
 | 
|---|
| 81 |     factors[i] = 2.;
 | 
|---|
| 82 |     inverses[i] = 1./factors[i];
 | 
|---|
| 83 |   }
 | 
|---|
| 84 |   VL.ActOnAll( (void (Vector::*)(const double ** const)) &Vector::Scale, (const double ** const)&factors );
 | 
|---|
| 85 |   CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   VL.ActOnAll( (void (Vector::*)(const double ** const)) &Vector::Scale, (const double ** const)&inverses );
 | 
|---|
| 88 |   CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
 | 
|---|
| 89 | };
 | 
|---|
| 90 | 
 | 
|---|
| 91 | /** UnitTest for VectorList::ActOnAll() and Vector::MakeNormalVector()
 | 
|---|
| 92 |  */
 | 
|---|
| 93 | void ActOnAllTest::NormalizeTest()
 | 
|---|
| 94 | {
 | 
|---|
| 95 |   const Vector xaxis(1.,0.,0.);
 | 
|---|
| 96 |   const Vector yaxis(0.,1.,0.);
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   // normalize with respect to x and y axis
 | 
|---|
| 99 |   bool (Vector::*f)(const Vector *, const Vector *) = &Vector::MakeNormalVector;
 | 
|---|
| 100 |   VL.ActOnAll( f, &xaxis, &yaxis );
 | 
|---|
| 101 |   CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   // check that x and y components are zero
 | 
|---|
| 104 |   for (ListOfVectors::iterator Runner = VL.Vectors.begin(); Runner != VL.Vectors.end(); Runner++) {
 | 
|---|
| 105 |     CPPUNIT_ASSERT_EQUAL( (*Runner)->x[0] , 0. );
 | 
|---|
| 106 |     CPPUNIT_ASSERT_EQUAL( (*Runner)->x[1] , 0. );
 | 
|---|
| 107 |   }
 | 
|---|
| 108 | };
 | 
|---|
| 109 | 
 | 
|---|
| 110 | 
 | 
|---|
| 111 | /********************************************** Main routine **************************************/
 | 
|---|
| 112 | 
 | 
|---|
| 113 | int main(int argc, char **argv)
 | 
|---|
| 114 | {
 | 
|---|
| 115 |   // Get the top level suite from the registry
 | 
|---|
| 116 |   CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
 | 
|---|
| 117 | 
 | 
|---|
| 118 |   // Adds the test to the list of test to run
 | 
|---|
| 119 |   CppUnit::TextUi::TestRunner runner;
 | 
|---|
| 120 |   runner.addTest( suite );
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   // Change the default outputter to a compiler error format outputter
 | 
|---|
| 123 |   runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
 | 
|---|
| 124 |                                                        std::cerr ) );
 | 
|---|
| 125 |   // Run the tests.
 | 
|---|
| 126 |   bool wasSucessful = runner.run();
 | 
|---|
| 127 | 
 | 
|---|
| 128 |   // Return error code 1 if the one of test failed.
 | 
|---|
| 129 |   return wasSucessful ? 0 : 1;
 | 
|---|
| 130 | };
 | 
|---|