OpenWalnut  1.5.0dev
WMatrix_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WMATRIX_TEST_H
26 #define WMATRIX_TEST_H
27 
28 #include <cxxtest/TestSuite.h>
29 
30 #include "../WMatrix.h"
31 
32 /**
33  * Tests for WMatrix.
34  */
35 class WMatrixTest : public CxxTest::TestSuite
36 {
37 public:
38  /**
39  * Instantiation should throw nothing.
40  */
41  void testInstantiation( void )
42  {
43  TS_ASSERT_THROWS_NOTHING( WMatrix< double > matrix( 3, 2 ) );
44  TS_ASSERT_THROWS_NOTHING( WMatrix< float > matrix( 3, 2 ) );
45  }
46 
47  /**
48  * Instantiation with copy constructor should throw nothing.
49  */
50  void testCopyInstantiation( void )
51  {
52  WMatrix< double > matrix( 3, 2 );
53  TS_ASSERT_THROWS_NOTHING( WMatrix< double > matrix2( matrix ) );
54  }
55 
56  /**
57  * Number of rows and columns should be return correctly.
58  */
59  void testGetNbRowsAndCols( void )
60  {
61  const size_t nbRows = 3, nbCols = 2;
62  WMatrix< double > matrix( nbRows, nbCols );
63  TS_ASSERT_EQUALS( matrix.getNbRows(), nbRows );
64  TS_ASSERT_EQUALS( matrix.getNbCols(), nbCols );
65  }
66 
67  /**
68  * Element access operator should work correctly.
69  */
71  {
72  const size_t nbRows = 3, nbCols = 2;
73  WMatrix< double > matrix( nbRows, nbCols );
74  TS_ASSERT_EQUALS( matrix( 0 , 0 ), 0. );
75  TS_ASSERT_EQUALS( matrix( 0 , 1 ), 0. );
76  TS_ASSERT_EQUALS( matrix( 1 , 0 ), 0. );
77  TS_ASSERT_EQUALS( matrix( 1 , 1 ), 0. );
78  TS_ASSERT_EQUALS( matrix( 2 , 0 ), 0. );
79  TS_ASSERT_EQUALS( matrix( 2 , 1 ), 0. );
80 
81  const double a = 3.14;
82  matrix( 2, 1 ) = a;
83  TS_ASSERT_EQUALS( matrix( 2, 1 ), a );
84  }
85 
86  /**
87  * Constant element access operator should work correctly.
88  */
90  {
91  const size_t nbRows = 3, nbCols = 2;
92  const WMatrix< double > matrix( nbRows, nbCols );
93  TS_ASSERT_EQUALS( matrix( 0 , 0 ), 0. );
94  TS_ASSERT_EQUALS( matrix( 0 , 1 ), 0. );
95  TS_ASSERT_EQUALS( matrix( 1 , 0 ), 0. );
96  TS_ASSERT_EQUALS( matrix( 1 , 1 ), 0. );
97  TS_ASSERT_EQUALS( matrix( 2 , 0 ), 0. );
98  TS_ASSERT_EQUALS( matrix( 2 , 1 ), 0. );
99  }
100 
101  /**
102  * Test for equality comparison of two matrices
103  */
104  void testEqualityOperator( void )
105  {
106  const size_t nbRows = 3, nbCols = 2;
107  const double a = 1.2, b = 2.3, c = 3.4, d = 4.5, e = 5.6, f = 6.7;
108  WMatrix< double > matrix1( nbRows, nbCols );
109  WMatrix< double > matrix2( nbRows, nbCols );
110  WMatrix< double > matrix3( nbCols, nbRows );
111 
112  matrix1( 0, 0 ) = a;
113  matrix1( 0, 1 ) = b;
114  matrix1( 1, 0 ) = c;
115  matrix1( 1, 1 ) = d;
116  matrix1( 2, 0 ) = e;
117  matrix1( 2, 1 ) = f;
118 
119  matrix2( 0, 0 ) = a;
120  matrix2( 0, 1 ) = b;
121  matrix2( 1, 0 ) = c;
122  matrix2( 1, 1 ) = d;
123  matrix2( 2, 0 ) = e;
124  matrix2( 2, 1 ) = f;
125 
126  matrix3( 0, 0 ) = a;
127  matrix3( 0, 1 ) = b;
128  matrix3( 0, 2 ) = c;
129  matrix3( 1, 0 ) = d;
130  matrix3( 1, 1 ) = e;
131  matrix3( 1, 2 ) = f;
132 
133  TS_ASSERT_EQUALS( matrix1 == matrix2, true );
134  TS_ASSERT_EQUALS( matrix1 == matrix3, false );
135 
136  matrix2( 0, 0 ) += 1.;
137 
138  TS_ASSERT_EQUALS( matrix1 == matrix2, false );
139  }
140 
141  /**
142  * Test for inequality comparison of two matrices
143  */
145  {
146  const size_t nbRows = 3, nbCols = 2;
147  const double a = 1.2, b = 2.3, c = 3.4, d = 4.5, e = 5.6, f = 6.7;
148  WMatrix< double > matrix1( nbRows, nbCols );
149  WMatrix< double > matrix2( nbRows, nbCols );
150  WMatrix< double > matrix3( nbCols, nbRows );
151 
152  matrix1( 0, 0 ) = a;
153  matrix1( 0, 1 ) = b;
154  matrix1( 1, 0 ) = c;
155  matrix1( 1, 1 ) = d;
156  matrix1( 2, 0 ) = e;
157  matrix1( 2, 1 ) = f;
158 
159  matrix2( 0, 0 ) = a;
160  matrix2( 0, 1 ) = b;
161  matrix2( 1, 0 ) = c;
162  matrix2( 1, 1 ) = d;
163  matrix2( 2, 0 ) = e;
164  matrix2( 2, 1 ) = f;
165 
166  matrix3( 0, 0 ) = a;
167  matrix3( 0, 1 ) = b;
168  matrix3( 0, 2 ) = c;
169  matrix3( 1, 0 ) = d;
170  matrix3( 1, 1 ) = e;
171  matrix3( 1, 2 ) = f;
172 
173  TS_ASSERT_EQUALS( matrix1 != matrix2, false );
174  TS_ASSERT_EQUALS( matrix1 != matrix3, true );
175 
176  matrix2( 0, 0 ) += 1.;
177 
178  TS_ASSERT_EQUALS( matrix1 != matrix2, true );
179  }
180 
181  /**
182  * Test assignment operator of WMatrix
183  */
185  {
186  const size_t nbRows = 3, nbCols = 2;
187  const double a = 1.2, b = 2.3, c = 3.4, d = 4.5, e = 5.6, f = 6.7;
188  WMatrix< double > matrix1( nbRows, nbCols );
189  WMatrix< double > matrix2( nbRows, nbCols );
190 
191  matrix1( 0, 0 ) = a;
192  matrix1( 0, 1 ) = b;
193  matrix1( 1, 0 ) = c;
194  matrix1( 1, 1 ) = d;
195  matrix1( 2, 0 ) = e;
196  matrix1( 2, 1 ) = f;
197 
198  matrix2( 0, 0 ) = a + 1.;
199  matrix2( 0, 1 ) = b + 2.;
200  matrix2( 1, 0 ) = c + 3.;
201  matrix2( 1, 1 ) = d + 4.;
202  matrix2( 2, 0 ) = e + 5.;
203  matrix2( 2, 1 ) = f + 6.;
204 
205  // this should be the precondition for the test
206  TS_ASSERT_EQUALS( matrix1 == matrix2, false );
207 
208  // test simple assignment
209  matrix1 = matrix2;
210  TS_ASSERT_EQUALS( matrix1 == matrix2, true );
211 
212  WMatrix< double > matrix3( nbRows, nbCols );
213  WMatrix< double > matrix4( nbRows, nbCols );
214 
215  // this should be the precondition for the test
216  TS_ASSERT_EQUALS( matrix2 == matrix3, false );
217  TS_ASSERT_EQUALS( matrix2 == matrix4, false );
218 
219  // test whether return the reference to self works
220  // for multiple assignment
221  matrix4 = matrix3 = matrix2;
222  TS_ASSERT_EQUALS( matrix2 == matrix3, true );
223  TS_ASSERT_EQUALS( matrix2 == matrix4, true );
224  TS_ASSERT_EQUALS( matrix3 == matrix4, true );
225  }
226 
227  /**
228  * Test transposed method of WMatrix
229  */
230  void testTransposed( void )
231  {
232  const size_t nbRows = 3, nbCols = 2;
233  WMatrix< int > matrix( nbRows, nbCols );
234 
235  for( size_t row = 0; row < nbRows; row++ )
236  for( size_t col = 0; col < nbCols; col++ )
237  matrix( row, col ) = ( row+1 )*10 + col+1;
238 
239  WMatrix< int > matrixTransposed( matrix.transposed() );
240 
241  //test dimensions
242  TS_ASSERT_EQUALS( matrixTransposed.getNbCols(), matrix.getNbRows() );
243  TS_ASSERT_EQUALS( matrixTransposed.getNbRows(), matrix.getNbCols() );
244 
245  // test values
246  for( size_t row = 0; row < nbRows; row++ )
247  for( size_t col = 0; col < nbCols; col++ )
248  TS_ASSERT_EQUALS( matrixTransposed( col, row ), ( row+1 )*10 + col + 1 );
249  }
250 
251  /**
252  * Test isIdentity method of WMatrix
253  */
254  void testIsIdentity( void )
255  {
256  WMatrix< double > a( 3, 2 );
257  a.makeIdentity();
258  TS_ASSERT_EQUALS( a.isIdentity(), false );
259 
260  WMatrix< double > b( 3, 3 );
261  b.makeIdentity();
262  TS_ASSERT_EQUALS( b.isIdentity(), true );
263 
264  b( 0, 0 ) += 1e-3;
265  TS_ASSERT_EQUALS( b.isIdentity(), false );
266  TS_ASSERT_EQUALS( b.isIdentity( 1e-3 ), true );
267 
268  b( 0, 1 ) += 2e-3;
269  TS_ASSERT_EQUALS( b.isIdentity( 1e-3 ), false );
270  TS_ASSERT_EQUALS( b.isIdentity( 2e-3 ), true );
271  }
272 };
273 
274 #endif // WMATRIX_TEST_H
Tests for WMatrix.
Definition: WMatrix_test.h:36
void testAssignmentOperator(void)
Test assignment operator of WMatrix.
Definition: WMatrix_test.h:184
void testGetNbRowsAndCols(void)
Number of rows and columns should be return correctly.
Definition: WMatrix_test.h:59
void testInstantiation(void)
Instantiation should throw nothing.
Definition: WMatrix_test.h:41
void testInequalityOperator(void)
Test for inequality comparison of two matrices.
Definition: WMatrix_test.h:144
void testTransposed(void)
Test transposed method of WMatrix.
Definition: WMatrix_test.h:230
void testEqualityOperator(void)
Test for equality comparison of two matrices.
Definition: WMatrix_test.h:104
void testCopyInstantiation(void)
Instantiation with copy constructor should throw nothing.
Definition: WMatrix_test.h:50
void testElementAccessOperator(void)
Element access operator should work correctly.
Definition: WMatrix_test.h:70
void testIsIdentity(void)
Test isIdentity method of WMatrix.
Definition: WMatrix_test.h:254
void testConstElementAccessOperator(void)
Constant element access operator should work correctly.
Definition: WMatrix_test.h:89
size_t getNbRows() const
Get number of rows.
Definition: WMatrix.h:375
bool isIdentity(T delta=T(0.0)) const
Returns true if the matrix is a identity matrix.
Definition: WMatrix.h:510
WMatrix transposed() const
Returns the transposed matrix.
Definition: WMatrix.h:447
size_t getNbCols() const
Get number of columns.
Definition: WMatrix.h:383
WMatrix & makeIdentity()
Makes the matrix contain the identity matrix, i.e.
Definition: WMatrix.h:352