OpenWalnut  1.4.0
WTensor_test.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WTENSOR_TEST_H
00026 #define WTENSOR_TEST_H
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 #include <cxxtest/TestSuite.h>
00032 #include "../WTensor.h"
00033 
00034 /**
00035  * Test class for the WTensor template.
00036  */
00037 class WTensorTest : public CxxTest::TestSuite
00038 {
00039 public:
00040     /**
00041      * Test access operator ().
00042      */
00043     void testAccessOperator1()
00044     {
00045         WTensor< 3, 2 > w;
00046         w( 0, 0, 0 ) = 2;
00047         w( 0, 0, 1 ) = 3;
00048         w( 0, 1, 0 ) = 0;
00049         w( 0, 1, 1 ) = 5;
00050         w( 1, 0, 0 ) = 2;
00051         w( 1, 0, 1 ) = 1;
00052         w( 1, 1, 0 ) = 8;
00053         w( 1, 1, 1 ) = 10;
00054 
00055         TS_ASSERT_EQUALS( w( 0, 0, 0 ), 2 );
00056         TS_ASSERT_EQUALS( w( 0, 0, 1 ), 3 );
00057         TS_ASSERT_EQUALS( w( 0, 1, 0 ), 0 );
00058         TS_ASSERT_EQUALS( w( 0, 1, 1 ), 5 );
00059         TS_ASSERT_EQUALS( w( 1, 0, 0 ), 2 );
00060         TS_ASSERT_EQUALS( w( 1, 0, 1 ), 1 );
00061         TS_ASSERT_EQUALS( w( 1, 1, 0 ), 8 );
00062         TS_ASSERT_EQUALS( w( 1, 1, 1 ), 10 );
00063     }
00064 
00065     /**
00066      * Test access operator [].
00067      */
00068     void testAccessOperator2()
00069     {
00070         std::vector< unsigned int > v( 3, 0 );
00071         WTensor< 3, 4 > w;
00072 
00073         for( v[ 0 ] = 0; v[ 0 ] < 4; ++v[ 0 ] )
00074         {
00075             for( v[ 1 ] = 0; v[ 1 ] < 4; ++v[ 1 ] )
00076             {
00077                 for( v[ 2 ] = 0; v[ 2 ] < 4; ++v[ 2 ] )
00078                 {
00079                     w[ v ] = v[ 0 ] + v[ 1 ] + v[ 2 ];
00080                 }
00081             }
00082         }
00083 
00084         unsigned int f[] = { 0, 0, 0 };
00085         for( f[ 0 ] = 0; f[ 0 ] < 4; ++f[ 0 ] )
00086         {
00087             for( f[ 1 ] = 0; f[ 1 ] < 4; ++f[ 1 ] )
00088             {
00089                 for( f[ 2 ] = 0; f[ 2 ] < 4; ++f[ 2 ] )
00090                 {
00091                     TS_ASSERT_EQUALS( w[ f ], f[ 0 ] + f[ 1 ] + f[ 2 ] );
00092                 }
00093             }
00094         }
00095     }
00096 
00097     /**
00098      * Test the standard constructor.
00099      */
00100     void testStandardConstructor()
00101     {
00102         // create lots of tensors
00103         // these should all compile
00104         WTensor< 0, 0 > t00d;
00105         WTensor< 0, 3 > t03d;
00106         WTensor< 1, 1 > t11d;
00107         WTensor< 1, 2 > t12d;
00108         WTensor< 1, 3 > t13d;
00109         WTensor< 1, 4 > t14d;
00110         WTensor< 1, 1, float > t11f;
00111         WTensor< 1, 2, int > t12i;
00112         WTensor< 1, 3, char > t13c;
00113         WTensor< 1, 4, std::string > t14s;
00114         WTensor< 2, 1 > t21d;
00115         WTensor< 2, 2 > t22d;
00116         WTensor< 2, 3 > t23d;
00117         WTensor< 2, 4 > t24d;
00118         WTensor< 2, 1, int > t21i;
00119         WTensor< 2, 2, char > t22c;
00120         WTensor< 2, 3, float > t23f;
00121         WTensor< 2, 4, float > t24f;
00122         WTensor< 3, 5 > t35d;
00123         WTensor< 4, 3 > t43d;
00124         WTensor< 5, 2 > t52d;
00125         WTensor< 6, 3 > t63d;
00126 
00127         TS_ASSERT_EQUALS( t35d( 0, 4, 2 ), 0.0 );
00128         TS_ASSERT_EQUALS( t35d( 1, 4, 0 ), 0.0 );
00129         TS_ASSERT_EQUALS( t35d( 0, 3, 0 ), 0.0 );
00130         TS_ASSERT_EQUALS( t35d( 2, 4, 1 ), 0.0 );
00131         TS_ASSERT_EQUALS( t35d( 0, 2, 2 ), 0.0 );
00132         TS_ASSERT_EQUALS( t35d( 4, 1, 4 ), 0.0 );
00133         TS_ASSERT_EQUALS( t35d( 4, 4, 4 ), 0.0 );
00134         TS_ASSERT_EQUALS( t35d( 3, 4, 3 ), 0.0 );
00135 
00136         TS_ASSERT_EQUALS( t11d( 0 ), 0.0 );
00137         TS_ASSERT_EQUALS( t22d( 0, 1 ), 0.0 );
00138     }
00139 
00140     /**
00141      * Test copy constructor.
00142      */
00143     void testCopyConstructor()
00144     {
00145         WTensor< 2, 3 > w;
00146         w( 0, 1 ) = 2;
00147         w( 2, 1 ) = 0.456;
00148 
00149         WTensor< 2, 3 > m( w );
00150         TS_ASSERT_EQUALS( m( 0, 1 ), 2 );
00151         TS_ASSERT_EQUALS( m( 2, 1 ), 0.456 );
00152     }
00153 
00154     /**
00155      * Test copy operator.
00156      */
00157     void testCopyOperator()
00158     {
00159         WTensor< 6, 2 > w;
00160         w( 0, 0, 1, 1, 0, 1 ) = 4.0;
00161         w( 1, 1, 0, 0, 0, 0 ) = 0.56;
00162         WTensor< 6, 2 > m;
00163 
00164         {
00165             m = w;
00166             TS_ASSERT_EQUALS( m( 0, 0, 1, 1, 0, 1 ), 4.0 );
00167             TS_ASSERT_EQUALS( m( 1, 1, 0, 0, 0, 0 ), 0.56 );
00168             TS_ASSERT_EQUALS( m( 0, 0, 0, 1, 0, 0 ), 0.0 );
00169         }
00170     }
00171 
00172     /**
00173      * Test if all the WTensorSym->WTensor copy operators and copy constructors compile.
00174      */
00175     void testCopyFromTensorSym()
00176     {
00177         // order = 3
00178         {
00179             WTensorSym< 3, 3 > s;
00180             s( 2, 1, 0 ) = 2.0;
00181 
00182             // copy construct t from s
00183             WTensor< 3, 3 > t( s );
00184             TS_ASSERT_EQUALS( t( 1, 2, 0 ), 2.0 );
00185 
00186             WTensor< 3, 3 > w;
00187             w( 0, 0, 0 ) = 3.0;
00188 
00189             // copy from s
00190             w = s;
00191             TS_ASSERT_EQUALS( w( 0, 0, 0 ), 0.0 );
00192             TS_ASSERT_EQUALS( w( 0, 2, 1 ), 2.0 );
00193         }
00194         // order = 1
00195         {
00196             WTensorSym< 1, 3 > s;
00197             s( 2 ) = 2.0;
00198 
00199             // copy construct t from s
00200             WTensor< 1, 3 > t( s );
00201             TS_ASSERT_EQUALS( t( 2 ), 2.0 );
00202 
00203             WTensor< 1, 3 > w;
00204             w( 0 ) = 3.0;
00205 
00206             // copy from s
00207             w = s;
00208             TS_ASSERT_EQUALS( w( 0 ), 0.0 );
00209             TS_ASSERT_EQUALS( w( 2 ), 2.0 );
00210         }
00211         // order = 0
00212         {
00213             WTensorSym< 0, 3 > s;
00214             s() = 2.0;
00215 
00216             // copy construct t from s
00217             WTensor< 0, 3 > t( s );
00218             TS_ASSERT_EQUALS( t(), 2.0 );
00219 
00220             WTensor< 0, 3 > w;
00221             w() = 3.0;
00222 
00223             // copy from s
00224             w = s;
00225             TS_ASSERT_EQUALS( w(), 2.0 );
00226         }
00227     }
00228 
00229     /**
00230      * Test casts to Data_T, WValue or WMatrix, depending on the order of the Tensor.
00231      */
00232     void testCastToVariousTypes()
00233     {
00234         // make sure these casts compile
00235         // we don't actually want to thoroughly test functionality here
00236         // more sophisticated tests can be found in WTensorFuncTest
00237         // cast to Data_T
00238         {
00239             WTensor< 0, 0, double > t;
00240             t() = 3.0;
00241             double d = t;
00242             TS_ASSERT_EQUALS( d, 3.0 );
00243         }
00244         // cast to WValue
00245         {
00246             WTensor< 1, 2, int > t;
00247             t( 0 ) = 3.0;
00248             WValue< int > v = t;
00249             TS_ASSERT_EQUALS( v[ 0 ], 3.0 );
00250         }
00251         // cast to WMatrix
00252         {
00253             WTensor< 2, 3, float > t;
00254             t( 0, 1 ) = 3.0;
00255             WMatrix< float > m = t;
00256             TS_ASSERT_EQUALS( m( 0, 1 ), 3.0 );
00257         }
00258     }
00259 };
00260 
00261 #endif  // WTENSOR_TEST_H