OpenWalnut  1.5.0dev
WTensor_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 WTENSOR_TEST_H
26 #define WTENSOR_TEST_H
27 
28 #include <string>
29 #include <vector>
30 
31 #include <cxxtest/TestSuite.h>
32 #include "../WTensor.h"
33 
34 /**
35  * Test class for the WTensor template.
36  */
37 class WTensorTest : public CxxTest::TestSuite
38 {
39 public:
40  /**
41  * Test access operator ().
42  */
44  {
46  w( 0, 0, 0 ) = 2;
47  w( 0, 0, 1 ) = 3;
48  w( 0, 1, 0 ) = 0;
49  w( 0, 1, 1 ) = 5;
50  w( 1, 0, 0 ) = 2;
51  w( 1, 0, 1 ) = 1;
52  w( 1, 1, 0 ) = 8;
53  w( 1, 1, 1 ) = 10;
54 
55  TS_ASSERT_EQUALS( w( 0, 0, 0 ), 2 );
56  TS_ASSERT_EQUALS( w( 0, 0, 1 ), 3 );
57  TS_ASSERT_EQUALS( w( 0, 1, 0 ), 0 );
58  TS_ASSERT_EQUALS( w( 0, 1, 1 ), 5 );
59  TS_ASSERT_EQUALS( w( 1, 0, 0 ), 2 );
60  TS_ASSERT_EQUALS( w( 1, 0, 1 ), 1 );
61  TS_ASSERT_EQUALS( w( 1, 1, 0 ), 8 );
62  TS_ASSERT_EQUALS( w( 1, 1, 1 ), 10 );
63  }
64 
65  /**
66  * Test access operator [].
67  */
69  {
70  std::vector< unsigned int > v( 3, 0 );
72 
73  for( v[ 0 ] = 0; v[ 0 ] < 4; ++v[ 0 ] )
74  {
75  for( v[ 1 ] = 0; v[ 1 ] < 4; ++v[ 1 ] )
76  {
77  for( v[ 2 ] = 0; v[ 2 ] < 4; ++v[ 2 ] )
78  {
79  w[ v ] = v[ 0 ] + v[ 1 ] + v[ 2 ];
80  }
81  }
82  }
83 
84  unsigned int f[] = { 0, 0, 0 };
85  for( f[ 0 ] = 0; f[ 0 ] < 4; ++f[ 0 ] )
86  {
87  for( f[ 1 ] = 0; f[ 1 ] < 4; ++f[ 1 ] )
88  {
89  for( f[ 2 ] = 0; f[ 2 ] < 4; ++f[ 2 ] )
90  {
91  TS_ASSERT_EQUALS( w[ f ], f[ 0 ] + f[ 1 ] + f[ 2 ] );
92  }
93  }
94  }
95  }
96 
97  /**
98  * Test the standard constructor.
99  */
101  {
102  // create lots of tensors
103  // these should all compile
104  WTensor< 0, 0 > t00d;
105  WTensor< 0, 3 > t03d;
106  WTensor< 1, 1 > t11d;
107  WTensor< 1, 2 > t12d;
108  WTensor< 1, 3 > t13d;
109  WTensor< 1, 4 > t14d;
114  WTensor< 2, 1 > t21d;
115  WTensor< 2, 2 > t22d;
116  WTensor< 2, 3 > t23d;
117  WTensor< 2, 4 > t24d;
122  WTensor< 3, 5 > t35d;
123  WTensor< 4, 3 > t43d;
124  WTensor< 5, 2 > t52d;
125  WTensor< 6, 3 > t63d;
126 
127  TS_ASSERT_EQUALS( t35d( 0, 4, 2 ), 0.0 );
128  TS_ASSERT_EQUALS( t35d( 1, 4, 0 ), 0.0 );
129  TS_ASSERT_EQUALS( t35d( 0, 3, 0 ), 0.0 );
130  TS_ASSERT_EQUALS( t35d( 2, 4, 1 ), 0.0 );
131  TS_ASSERT_EQUALS( t35d( 0, 2, 2 ), 0.0 );
132  TS_ASSERT_EQUALS( t35d( 4, 1, 4 ), 0.0 );
133  TS_ASSERT_EQUALS( t35d( 4, 4, 4 ), 0.0 );
134  TS_ASSERT_EQUALS( t35d( 3, 4, 3 ), 0.0 );
135 
136  TS_ASSERT_EQUALS( t11d( 0 ), 0.0 );
137  TS_ASSERT_EQUALS( t22d( 0, 1 ), 0.0 );
138  }
139 
140  /**
141  * Test copy constructor.
142  */
144  {
145  WTensor< 2, 3 > w;
146  w( 0, 1 ) = 2;
147  w( 2, 1 ) = 0.456;
148 
149  WTensor< 2, 3 > m( w );
150  TS_ASSERT_EQUALS( m( 0, 1 ), 2 );
151  TS_ASSERT_EQUALS( m( 2, 1 ), 0.456 );
152  }
153 
154  /**
155  * Test copy operator.
156  */
158  {
159  WTensor< 6, 2 > w;
160  w( 0, 0, 1, 1, 0, 1 ) = 4.0;
161  w( 1, 1, 0, 0, 0, 0 ) = 0.56;
162  WTensor< 6, 2 > m;
163 
164  {
165  m = w;
166  TS_ASSERT_EQUALS( m( 0, 0, 1, 1, 0, 1 ), 4.0 );
167  TS_ASSERT_EQUALS( m( 1, 1, 0, 0, 0, 0 ), 0.56 );
168  TS_ASSERT_EQUALS( m( 0, 0, 0, 1, 0, 0 ), 0.0 );
169  }
170  }
171 
172  /**
173  * Test if all the WTensorSym->WTensor copy operators and copy constructors compile.
174  */
176  {
177  // order = 3
178  {
180  s( 2, 1, 0 ) = 2.0;
181 
182  // copy construct t from s
183  WTensor< 3, 3 > t( s );
184  TS_ASSERT_EQUALS( t( 1, 2, 0 ), 2.0 );
185 
186  WTensor< 3, 3 > w;
187  w( 0, 0, 0 ) = 3.0;
188 
189  // copy from s
190  w = s;
191  TS_ASSERT_EQUALS( w( 0, 0, 0 ), 0.0 );
192  TS_ASSERT_EQUALS( w( 0, 2, 1 ), 2.0 );
193  }
194  // order = 1
195  {
197  s( 2 ) = 2.0;
198 
199  // copy construct t from s
200  WTensor< 1, 3 > t( s );
201  TS_ASSERT_EQUALS( t( 2 ), 2.0 );
202 
203  WTensor< 1, 3 > w;
204  w( 0 ) = 3.0;
205 
206  // copy from s
207  w = s;
208  TS_ASSERT_EQUALS( w( 0 ), 0.0 );
209  TS_ASSERT_EQUALS( w( 2 ), 2.0 );
210  }
211  // order = 0
212  {
214  s() = 2.0;
215 
216  // copy construct t from s
217  WTensor< 0, 3 > t( s );
218  TS_ASSERT_EQUALS( t(), 2.0 );
219 
220  WTensor< 0, 3 > w;
221  w() = 3.0;
222 
223  // copy from s
224  w = s;
225  TS_ASSERT_EQUALS( w(), 2.0 );
226  }
227  }
228 
229  /**
230  * Test casts to Data_T, WValue or WMatrix, depending on the order of the Tensor.
231  */
233  {
234  // make sure these casts compile
235  // we don't actually want to thoroughly test functionality here
236  // more sophisticated tests can be found in WTensorFuncTest
237  // cast to Data_T
238  {
240  t() = 3.0;
241  double d = t;
242  TS_ASSERT_EQUALS( d, 3.0 );
243  }
244  // cast to WValue
245  {
247  t( 0 ) = 3.0;
248  WValue< int > v = t;
249  TS_ASSERT_EQUALS( v[ 0 ], 3.0 );
250  }
251  // cast to WMatrix
252  {
254  t( 0, 1 ) = 3.0;
255  WMatrix< float > m = t;
256  TS_ASSERT_EQUALS( m( 0, 1 ), 3.0 );
257  }
258  }
259 };
260 
261 #endif // WTENSOR_TEST_H
Matrix template class with variable number of rows and columns.
Definition: WMatrix.h:44
Implements a symmetric tensor that has the same number of components in every direction.
Definition: WTensorSym.h:73
Test class for the WTensor template.
Definition: WTensor_test.h:38
void testCopyConstructor()
Test copy constructor.
Definition: WTensor_test.h:143
void testCastToVariousTypes()
Test casts to Data_T, WValue or WMatrix, depending on the order of the Tensor.
Definition: WTensor_test.h:232
void testCopyOperator()
Test copy operator.
Definition: WTensor_test.h:157
void testAccessOperator2()
Test access operator [].
Definition: WTensor_test.h:68
void testStandardConstructor()
Test the standard constructor.
Definition: WTensor_test.h:100
void testCopyFromTensorSym()
Test if all the WTensorSym->WTensor copy operators and copy constructors compile.
Definition: WTensor_test.h:175
void testAccessOperator1()
Test access operator ().
Definition: WTensor_test.h:43
Implements a tensor that has the same number of components in every direction.
Definition: WTensor.h:79
Base class for all higher level values like tensors, vectors, matrices and so on.
Definition: WValue.h:41