WVector3D_test.h 5.22 KB
Newer Older
wiebel's avatar
[ADD]  
wiebel committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
//---------------------------------------------------------------------------
//
// Project: OpenWalnut
//
// Copyright 2009 SomeCopyrightowner
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WVECTOR3D_TEST_H
#define WVECTOR3D_TEST_H

#include <cxxtest/TestSuite.h>
#include "../WVector3D.h"

30 31
using wmath::WVector3D;

wiebel's avatar
[ADD]  
wiebel committed
32 33
class WVector3DTest : public CxxTest::TestSuite
{
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
private:
    double delta;
    double deltaLarge;

public:

    /**
     * Called before every test.
     */
    void setUp( void )
    {
        delta = 1e-12;
        deltaLarge = 1e-10;
    }

wiebel's avatar
[ADD]  
wiebel committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
public:
    /**
     * Instatiation should throw nothing.
     */
    void testInstantiationStandard( void )
    {
        TS_ASSERT_THROWS_NOTHING( WVector3D vec() );
    }

    /**
     * Instatiation should throw nothing.
     */
    void testInstantiationWithDoubles( void )
    {
        TS_ASSERT_THROWS_NOTHING( WVector3D vec( 32.32, 42.42, 23.23 ) );
    }

    /**
     * Instatiation should throw nothing.
     */
    void testInstantiationWithCopyConstructor( void )
    {
        WVector3D vecToCopy;
        TS_ASSERT_THROWS_NOTHING( WVector3D vec( vecToCopy ) );
    }

    /**
     * Standard constructor should set values to zero
     */
    void testInitializationWithStandardConstructor( void )
    {
        WVector3D vec;
81 82 83
        TS_ASSERT_EQUALS( vec[0], 0. );
        TS_ASSERT_EQUALS( vec[1], 0. );
        TS_ASSERT_EQUALS( vec[2], 0. );
wiebel's avatar
[ADD]  
wiebel committed
84 85 86 87 88 89 90 91 92 93 94
    }

    /**
     * Double constructor should set values to the provided params
     */
    void testInitializationWithDoubleConstructor( void )
    {
        double a = 32.32;
        double b = 42.42;
        double c = 23.23;
        WVector3D vec( a, b, c );
95 96 97
        TS_ASSERT_EQUALS( vec[0], a );
        TS_ASSERT_EQUALS( vec[1], b );
        TS_ASSERT_EQUALS( vec[2], c );
wiebel's avatar
[ADD]  
wiebel committed
98 99 100 101 102 103 104 105 106
    }

    /**
     * Copy constructor should copy values
     */
    void testInitializationWithCopyConstructor( void )
    {
        WVector3D vecToCopy( 32.32, 42.42, 23.23 );
        WVector3D vec( vecToCopy );
107 108 109
        TS_ASSERT_EQUALS( vec[0], vecToCopy[0] );
        TS_ASSERT_EQUALS( vec[1], vecToCopy[1] );
        TS_ASSERT_EQUALS( vec[2], vecToCopy[2] );
wiebel's avatar
[ADD]  
wiebel committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    }

    /**
     * Element access operator should return the right values
     */
    void testElementAccessOperator( void )
    {
        double a = 32.32;
        double b = 42.42;
        double c = 23.23;
        WVector3D vec( a, b, c );
        TS_ASSERT_EQUALS( vec[0], a );
        TS_ASSERT_EQUALS( vec[1], b );
        TS_ASSERT_EQUALS( vec[2], c );
    }

    /**
     * Const element access operator should return the right values
     */
    void testConstElementAccessOperator( void )
    {
        double a = 32.32;
        double b = 42.42;
        double c = 23.23;
        const WVector3D vec( a, b, c );
        TS_ASSERT_EQUALS( vec[0], a );
        TS_ASSERT_EQUALS( vec[1], b );
        TS_ASSERT_EQUALS( vec[2], c );
    }
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

    /**
     * Test cross product
     */
    void testCrossProduct( void )
    {
        double a = 32.32;
        double b = 42.42;
        double c = 23.23;
        const WVector3D vec( a, b, c );
        const WVector3D vec2( a + 1.1, b + 2.2, c + 3.3 );

        WVector3D tmp;

        // product with self should be zero vector
        tmp = vec.crossProduct( vec );
        TS_ASSERT_DELTA( tmp[0], 0., delta );
        TS_ASSERT_DELTA( tmp[1], 0., delta );
        TS_ASSERT_DELTA( tmp[2], 0., delta );

        // test for some example
        tmp = vec.crossProduct( vec2 );
        TS_ASSERT_DELTA( tmp[0], 88.88, delta );
        TS_ASSERT_DELTA( tmp[1], -81.103, delta );
        TS_ASSERT_DELTA( tmp[2], 24.442, delta );

        // ensure orthogonality
        TS_ASSERT_DELTA( tmp.dotProduct( vec ), 0., deltaLarge );
        TS_ASSERT_DELTA( tmp.dotProduct( vec2 ), 0., deltaLarge );
    }

    /**
     * Test dot product
     */
    void testDotProduct( void )
    {
        double a = 32.32;
        double b = 42.42;
        double c = 23.23;
        const WVector3D vec( a, b, c );
        const WVector3D vec2( a + 1.1, b + 2.2, c + 3.3 );

        double tmp;

        // product with self has to be squared norm
        tmp = vec.dotProduct( vec );
        TS_ASSERT_EQUALS( tmp, vec.normSquare() );

        // test for some example
        tmp = vec.dotProduct( vec2 );
        TS_ASSERT_DELTA( tmp, 3589.2067, delta );

        // product with orthogonal vector has to be zero
        WVector3D tmpOrtho( 88.88, -81.103, 24.442 );
        tmp = vec.dotProduct( tmpOrtho );
        TS_ASSERT_DELTA( tmp, 0., delta );
    }
wiebel's avatar
[ADD]  
wiebel committed
196 197 198
};

#endif  // WVECTOR3D_TEST_H