OpenWalnut
1.4.0
|
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 WMATRIX_TEST_H 00026 #define WMATRIX_TEST_H 00027 00028 #include <cxxtest/TestSuite.h> 00029 00030 #include "../WMatrix.h" 00031 00032 /** 00033 * Tests for WMatrix. 00034 */ 00035 class WMatrixTest : public CxxTest::TestSuite 00036 { 00037 public: 00038 /** 00039 * Instantiation should throw nothing. 00040 */ 00041 void testInstantiation( void ) 00042 { 00043 TS_ASSERT_THROWS_NOTHING( WMatrix< double > matrix( 3, 2 ) ); 00044 TS_ASSERT_THROWS_NOTHING( WMatrix< float > matrix( 3, 2 ) ); 00045 } 00046 00047 /** 00048 * Instantiation with copy constructor should throw nothing. 00049 */ 00050 void testCopyInstantiation( void ) 00051 { 00052 WMatrix< double > matrix( 3, 2 ); 00053 TS_ASSERT_THROWS_NOTHING( WMatrix< double > matrix2( matrix ) ); 00054 } 00055 00056 /** 00057 * Number of rows and columns should be return correctly. 00058 */ 00059 void testGetNbRowsAndCols( void ) 00060 { 00061 const size_t nbRows = 3, nbCols = 2; 00062 WMatrix< double > matrix( nbRows, nbCols ); 00063 TS_ASSERT_EQUALS( matrix.getNbRows(), nbRows ); 00064 TS_ASSERT_EQUALS( matrix.getNbCols(), nbCols ); 00065 } 00066 00067 /** 00068 * Element access operator should work correctly. 00069 */ 00070 void testElementAccessOperator( void ) 00071 { 00072 const size_t nbRows = 3, nbCols = 2; 00073 WMatrix< double > matrix( nbRows, nbCols ); 00074 TS_ASSERT_EQUALS( matrix( 0 , 0 ), 0. ); 00075 TS_ASSERT_EQUALS( matrix( 0 , 1 ), 0. ); 00076 TS_ASSERT_EQUALS( matrix( 1 , 0 ), 0. ); 00077 TS_ASSERT_EQUALS( matrix( 1 , 1 ), 0. ); 00078 TS_ASSERT_EQUALS( matrix( 2 , 0 ), 0. ); 00079 TS_ASSERT_EQUALS( matrix( 2 , 1 ), 0. ); 00080 00081 const double a = 3.14; 00082 matrix( 2, 1 ) = a; 00083 TS_ASSERT_EQUALS( matrix( 2, 1 ), a ); 00084 } 00085 00086 /** 00087 * Constant element access operator should work correctly. 00088 */ 00089 void testConstElementAccessOperator( void ) 00090 { 00091 const size_t nbRows = 3, nbCols = 2; 00092 const WMatrix< double > matrix( nbRows, nbCols ); 00093 TS_ASSERT_EQUALS( matrix( 0 , 0 ), 0. ); 00094 TS_ASSERT_EQUALS( matrix( 0 , 1 ), 0. ); 00095 TS_ASSERT_EQUALS( matrix( 1 , 0 ), 0. ); 00096 TS_ASSERT_EQUALS( matrix( 1 , 1 ), 0. ); 00097 TS_ASSERT_EQUALS( matrix( 2 , 0 ), 0. ); 00098 TS_ASSERT_EQUALS( matrix( 2 , 1 ), 0. ); 00099 } 00100 00101 /** 00102 * Test for equality comparison of two matrices 00103 */ 00104 void testEqualityOperator( void ) 00105 { 00106 const size_t nbRows = 3, nbCols = 2; 00107 const double a = 1.2, b = 2.3, c = 3.4, d = 4.5, e = 5.6, f = 6.7; 00108 WMatrix< double > matrix1( nbRows, nbCols ); 00109 WMatrix< double > matrix2( nbRows, nbCols ); 00110 WMatrix< double > matrix3( nbCols, nbRows ); 00111 00112 matrix1( 0, 0 ) = a; 00113 matrix1( 0, 1 ) = b; 00114 matrix1( 1, 0 ) = c; 00115 matrix1( 1, 1 ) = d; 00116 matrix1( 2, 0 ) = e; 00117 matrix1( 2, 1 ) = f; 00118 00119 matrix2( 0, 0 ) = a; 00120 matrix2( 0, 1 ) = b; 00121 matrix2( 1, 0 ) = c; 00122 matrix2( 1, 1 ) = d; 00123 matrix2( 2, 0 ) = e; 00124 matrix2( 2, 1 ) = f; 00125 00126 matrix3( 0, 0 ) = a; 00127 matrix3( 0, 1 ) = b; 00128 matrix3( 0, 2 ) = c; 00129 matrix3( 1, 0 ) = d; 00130 matrix3( 1, 1 ) = e; 00131 matrix3( 1, 2 ) = f; 00132 00133 TS_ASSERT_EQUALS( matrix1 == matrix2, true ); 00134 TS_ASSERT_EQUALS( matrix1 == matrix3, false ); 00135 00136 matrix2( 0, 0 ) += 1.; 00137 00138 TS_ASSERT_EQUALS( matrix1 == matrix2, false ); 00139 } 00140 00141 /** 00142 * Test for inequality comparison of two matrices 00143 */ 00144 void testInequalityOperator( void ) 00145 { 00146 const size_t nbRows = 3, nbCols = 2; 00147 const double a = 1.2, b = 2.3, c = 3.4, d = 4.5, e = 5.6, f = 6.7; 00148 WMatrix< double > matrix1( nbRows, nbCols ); 00149 WMatrix< double > matrix2( nbRows, nbCols ); 00150 WMatrix< double > matrix3( nbCols, nbRows ); 00151 00152 matrix1( 0, 0 ) = a; 00153 matrix1( 0, 1 ) = b; 00154 matrix1( 1, 0 ) = c; 00155 matrix1( 1, 1 ) = d; 00156 matrix1( 2, 0 ) = e; 00157 matrix1( 2, 1 ) = f; 00158 00159 matrix2( 0, 0 ) = a; 00160 matrix2( 0, 1 ) = b; 00161 matrix2( 1, 0 ) = c; 00162 matrix2( 1, 1 ) = d; 00163 matrix2( 2, 0 ) = e; 00164 matrix2( 2, 1 ) = f; 00165 00166 matrix3( 0, 0 ) = a; 00167 matrix3( 0, 1 ) = b; 00168 matrix3( 0, 2 ) = c; 00169 matrix3( 1, 0 ) = d; 00170 matrix3( 1, 1 ) = e; 00171 matrix3( 1, 2 ) = f; 00172 00173 TS_ASSERT_EQUALS( matrix1 != matrix2, false ); 00174 TS_ASSERT_EQUALS( matrix1 != matrix3, true ); 00175 00176 matrix2( 0, 0 ) += 1.; 00177 00178 TS_ASSERT_EQUALS( matrix1 != matrix2, true ); 00179 } 00180 00181 /** 00182 * Test assignment operator of WMatrix 00183 */ 00184 void testAssignmentOperator( void ) 00185 { 00186 const size_t nbRows = 3, nbCols = 2; 00187 const double a = 1.2, b = 2.3, c = 3.4, d = 4.5, e = 5.6, f = 6.7; 00188 WMatrix< double > matrix1( nbRows, nbCols ); 00189 WMatrix< double > matrix2( nbRows, nbCols ); 00190 00191 matrix1( 0, 0 ) = a; 00192 matrix1( 0, 1 ) = b; 00193 matrix1( 1, 0 ) = c; 00194 matrix1( 1, 1 ) = d; 00195 matrix1( 2, 0 ) = e; 00196 matrix1( 2, 1 ) = f; 00197 00198 matrix2( 0, 0 ) = a + 1.; 00199 matrix2( 0, 1 ) = b + 2.; 00200 matrix2( 1, 0 ) = c + 3.; 00201 matrix2( 1, 1 ) = d + 4.; 00202 matrix2( 2, 0 ) = e + 5.; 00203 matrix2( 2, 1 ) = f + 6.; 00204 00205 // this should be the precondition for the test 00206 TS_ASSERT_EQUALS( matrix1 == matrix2, false ); 00207 00208 // test simple assignment 00209 matrix1 = matrix2; 00210 TS_ASSERT_EQUALS( matrix1 == matrix2, true ); 00211 00212 WMatrix< double > matrix3( nbRows, nbCols ); 00213 WMatrix< double > matrix4( nbRows, nbCols ); 00214 00215 // this should be the precondition for the test 00216 TS_ASSERT_EQUALS( matrix2 == matrix3, false ); 00217 TS_ASSERT_EQUALS( matrix2 == matrix4, false ); 00218 00219 // test whether return the reference to self works 00220 // for multiple assignment 00221 matrix4 = matrix3 = matrix2; 00222 TS_ASSERT_EQUALS( matrix2 == matrix3, true ); 00223 TS_ASSERT_EQUALS( matrix2 == matrix4, true ); 00224 TS_ASSERT_EQUALS( matrix3 == matrix4, true ); 00225 } 00226 00227 /** 00228 * Test transposed method of WMatrix 00229 */ 00230 void testTransposed( void ) 00231 { 00232 const size_t nbRows = 3, nbCols = 2; 00233 WMatrix< int > matrix( nbRows, nbCols ); 00234 00235 for( size_t row = 0; row < nbRows; row++ ) 00236 for( size_t col = 0; col < nbCols; col++ ) 00237 matrix( row, col ) = ( row+1 )*10 + col+1; 00238 00239 WMatrix< int > matrixTransposed( matrix.transposed() ); 00240 00241 //test dimensions 00242 TS_ASSERT_EQUALS( matrixTransposed.getNbCols(), matrix.getNbRows() ); 00243 TS_ASSERT_EQUALS( matrixTransposed.getNbRows(), matrix.getNbCols() ); 00244 00245 // test values 00246 for( size_t row = 0; row < nbRows; row++ ) 00247 for( size_t col = 0; col < nbCols; col++ ) 00248 TS_ASSERT_EQUALS( matrixTransposed( col, row ), ( row+1 )*10 + col + 1 ); 00249 } 00250 00251 /** 00252 * Test isIdentity method of WMatrix 00253 */ 00254 void testIsIdentity( void ) 00255 { 00256 WMatrix< double > a( 3, 2 ); 00257 a.makeIdentity(); 00258 TS_ASSERT_EQUALS( a.isIdentity(), false ); 00259 00260 WMatrix< double > b( 3, 3 ); 00261 b.makeIdentity(); 00262 TS_ASSERT_EQUALS( b.isIdentity(), true ); 00263 00264 b( 0, 0 ) += 1e-3; 00265 TS_ASSERT_EQUALS( b.isIdentity(), false ); 00266 TS_ASSERT_EQUALS( b.isIdentity( 1e-3 ), true ); 00267 00268 b( 0, 1 ) += 2e-3; 00269 TS_ASSERT_EQUALS( b.isIdentity( 1e-3 ), false ); 00270 TS_ASSERT_EQUALS( b.isIdentity( 2e-3 ), true ); 00271 } 00272 }; 00273 00274 #endif // WMATRIX_TEST_H