| 1 | /*
|
|---|
| 2 | * ActOnAllUnitTest.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: 04.10.2009
|
|---|
| 5 | * Author: FrederikHeber
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | // include config.h
|
|---|
| 9 | #ifdef HAVE_CONFIG_H
|
|---|
| 10 | #include <config.h>
|
|---|
| 11 | #endif
|
|---|
| 12 |
|
|---|
| 13 | using namespace std;
|
|---|
| 14 |
|
|---|
| 15 | #include <cppunit/CompilerOutputter.h>
|
|---|
| 16 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
|---|
| 17 | #include <cppunit/ui/text/TestRunner.h>
|
|---|
| 18 |
|
|---|
| 19 | #include "../test/ActOnAlltest.hpp"
|
|---|
| 20 | #include "ActOnAllUnitTest.hpp"
|
|---|
| 21 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 22 |
|
|---|
| 23 | #ifdef HAVE_TESTRUNNER
|
|---|
| 24 | #include "UnitTestMain.hpp"
|
|---|
| 25 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 26 |
|
|---|
| 27 | /********************************************** Test classes **************************************/
|
|---|
| 28 |
|
|---|
| 29 | // Registers the fixture into the 'registry'
|
|---|
| 30 | CPPUNIT_TEST_SUITE_REGISTRATION( ActOnAllTest );
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | void ActOnAllTest::setUp()
|
|---|
| 34 | {
|
|---|
| 35 | Vector *temp = NULL;
|
|---|
| 36 | // fill list with vectors
|
|---|
| 37 | temp = new Vector(1,0,0);
|
|---|
| 38 | VL.AddVector(temp);
|
|---|
| 39 | temp = new Vector(0,1,0);
|
|---|
| 40 | VL.AddVector(temp);
|
|---|
| 41 | temp = new Vector(0,0,1);
|
|---|
| 42 | VL.AddVector(temp);
|
|---|
| 43 | Ref = VL;
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | void ActOnAllTest::tearDown()
|
|---|
| 48 | {
|
|---|
| 49 | VL.EmptyList();
|
|---|
| 50 | // Ref was copy constructed, hence has to be cleaned, too!
|
|---|
| 51 | Ref.EmptyList();
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | /** UnitTest for VectorList::ActOnAll() and Vector::AddVector(), Vector::SubtractVector(),
|
|---|
| 55 | */
|
|---|
| 56 | void ActOnAllTest::AddSubtractTest()
|
|---|
| 57 | {
|
|---|
| 58 | const Vector test(1.,0.,0.);
|
|---|
| 59 |
|
|---|
| 60 | // adding, subtracting
|
|---|
| 61 | VL.ActOnAll( &Vector::AddVector, test );
|
|---|
| 62 | CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
|
|---|
| 63 | VL.ActOnAll( &Vector::SubtractVector, test );
|
|---|
| 64 | CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | /** UnitTest for VectorList::ActOnAll()
|
|---|
| 68 | */
|
|---|
| 69 | void ActOnAllTest::ScaleTest()
|
|---|
| 70 | {
|
|---|
| 71 | double factor=2.;
|
|---|
| 72 | double inverse=1./2.;
|
|---|
| 73 |
|
|---|
| 74 | // scaling by value
|
|---|
| 75 | VL.ActOnAll( (void (Vector::*)(const double)) &Vector::Scale, factor );
|
|---|
| 76 | CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
|
|---|
| 77 |
|
|---|
| 78 | VL.ActOnAll( (void (Vector::*)(const double)) &Vector::Scale, inverse );
|
|---|
| 79 | CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
|
|---|
| 80 |
|
|---|
| 81 | // scaling by three values
|
|---|
| 82 | double *factors = new double[NDIM];
|
|---|
| 83 | double *inverses = new double[NDIM];
|
|---|
| 84 | for (int i=0;i<NDIM;++i) {
|
|---|
| 85 | factors[i] = 2.;
|
|---|
| 86 | inverses[i] = 1./factors[i];
|
|---|
| 87 | }
|
|---|
| 88 | VL.ActOnAll<Vector,void,const double*>(&Vector::ScaleAll, factors );
|
|---|
| 89 | CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
|
|---|
| 90 |
|
|---|
| 91 | VL.ActOnAll<Vector,void,const double*>(&Vector::ScaleAll, inverses );
|
|---|
| 92 | CPPUNIT_ASSERT_EQUAL( VL == Ref , true );
|
|---|
| 93 | delete[](factors);
|
|---|
| 94 | delete[](inverses);
|
|---|
| 95 | };
|
|---|
| 96 |
|
|---|
| 97 | #if 0
|
|---|
| 98 |
|
|---|
| 99 | // Unitttest deactivated for now...
|
|---|
| 100 | // Reasons:
|
|---|
| 101 | // Vector::MakeNormalVector has been removed and is now part of Plane class
|
|---|
| 102 | // VectorList::ActOnAll is in the test directory and seems to be used only for unittests
|
|---|
| 103 | // MakeNormal never depended on the input Vector except in the case of linear dependence (i.e. failure)
|
|---|
| 104 | //
|
|---|
| 105 | // TODO: Rethink what exactely is being tested at this point and introduce a new unittest for the
|
|---|
| 106 | // tested behaviour. Most likely this should result in a thourough unittest of the plane class
|
|---|
| 107 |
|
|---|
| 108 | /** UnitTest for VectorList::ActOnAll() and Vector::MakeNormalVector()
|
|---|
| 109 | */
|
|---|
| 110 | void ActOnAllTest::NormalizeTest()
|
|---|
| 111 | {
|
|---|
| 112 | const Vector xaxis(1.,0.,0.);
|
|---|
| 113 | const Vector yaxis(0.,1.,0.);
|
|---|
| 114 |
|
|---|
| 115 | // normalize with respect to x and y axis
|
|---|
| 116 | bool (Vector::*f)(const Vector *, const Vector *) = &Vector::MakeNormalVector;
|
|---|
| 117 | VL.ActOnAll( f, &xaxis, &yaxis );
|
|---|
| 118 | CPPUNIT_ASSERT_EQUAL( VL == Ref , false );
|
|---|
| 119 |
|
|---|
| 120 | // check that x and y components are zero
|
|---|
| 121 | for (ListOfVectors::iterator Runner = VL.Vectors.begin(); Runner != VL.Vectors.end(); Runner++) {
|
|---|
| 122 | CPPUNIT_ASSERT_EQUAL( (*Runner)->at(0) , 0. );
|
|---|
| 123 | CPPUNIT_ASSERT_EQUAL( (*Runner)->at(1) , 0. );
|
|---|
| 124 | }
|
|---|
| 125 | };
|
|---|
| 126 |
|
|---|
| 127 | #endif
|
|---|