OpenWalnut  1.4.0
WValueSet_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 WVALUESET_TEST_H
26 #define WVALUESET_TEST_H
27 
28 #include <stdint.h>
29 #include <vector>
30 
31 #include <cxxtest/TestSuite.h>
32 
33 #include "../WValueSet.h"
34 #include "../WDataHandlerEnums.h"
35 
36 /**
37  * UnitTests the WValueSet class
38  */
39 class WValueSetTest : public CxxTest::TestSuite
40 {
41 public:
42  /**
43  * An instantiation should never throw an exception
44  */
45  void testInstantiation( void )
46  {
47  double a[2] = { 0.0, 3.1415 };
48  const boost::shared_ptr< std::vector< double > > v =
49  boost::shared_ptr< std::vector< double > >(
50  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
51  TS_ASSERT_THROWS_NOTHING( WValueSet< double > valueSet( 0, 1, v, W_DT_DOUBLE ) );
52  }
53 
54  /**
55  * The number of values retrieved is correct
56  */
57  void testGetNumberOfValues( void )
58  {
59  int a[4] = { 0, -5, 1, 2 };
60  const boost::shared_ptr< std::vector< int8_t > > v =
61  boost::shared_ptr< std::vector< int8_t > >(
62  new std::vector< int8_t >( a, a + sizeof( a ) / sizeof( int ) ) );
63  WValueSet< int8_t > first( 0, 1, v, W_DT_INT8 );
64  TS_ASSERT_EQUALS( first.size(), 4 );
65  WValueSet< int8_t > second( 1, 2, v, W_DT_INT8 );
66  TS_ASSERT_EQUALS( second.size(), 2 );
67  WValueSet< int8_t > third( 2, 2, v, W_DT_INT8 );
68  TS_ASSERT_EQUALS( third.size(), 1 );
69  }
70 
71  /**
72  * The raw size is the size of the number of integral elements inside
73  * this ValueSet.
74  */
75  void testRawSize( void )
76  {
77  int8_t a[4] = { 0, -5, 1, 2 };
78  const boost::shared_ptr< std::vector< int8_t > > v =
79  boost::shared_ptr< std::vector< int8_t > >(
80  new std::vector< int8_t >( a, a + sizeof( a ) / sizeof( int8_t ) ) );
81  WValueSet< int8_t > first( 0, 1, v, W_DT_INT8 );
82  TS_ASSERT_EQUALS( first.rawSize(), 4 );
83  WValueSet< int8_t > second( 2, 2, v, W_DT_INT8 );
84  TS_ASSERT_EQUALS( first.rawSize(), 4 );
85  }
86 
87  /**
88  * This function should return the i-th value if the value set is scalar.
89  */
90  void testGetScalar( void )
91  {
92  int8_t a[4] = { 0, -5, 1, 2 };
93  const boost::shared_ptr< std::vector< int8_t > > v =
94  boost::shared_ptr< std::vector< int8_t > >(
95  new std::vector< int8_t >( a, a + sizeof( a ) / sizeof( int8_t ) ) );
96  WValueSet< int8_t > set( 0, 1, v, W_DT_INT8 );
97  TS_ASSERT_EQUALS( set.getScalar( 0 ), a[0] );
98  TS_ASSERT_EQUALS( set.getScalar( 1 ), a[1] );
99  TS_ASSERT_EQUALS( set.getScalar( 2 ), a[2] );
100  TS_ASSERT_EQUALS( set.getScalar( 3 ), a[3] );
101  }
102 
103  /**
104  * Raw Access should provide data access to the underlying array.
105  */
107  {
108  double a[2] = { 0.0, 3.1415 };
109  const boost::shared_ptr< std::vector< double > > v =
110  boost::shared_ptr< std::vector< double > >(
111  new std::vector< double >( a, a + sizeof( a ) / sizeof( double ) ) );
112  WValueSet< double > valueSet( 0, 1, v, W_DT_DOUBLE );
113  const double * const b = valueSet.rawData();
114  TS_ASSERT_EQUALS( b[0], 0.0 );
115  TS_ASSERT_EQUALS( b[1], 3.1415 );
116  }
117 
118  /**
119  * This function should return the i-th WValue with of the used dimension (prerequisite the ValueSet has order 1)
120  */
121  void testGetWValue( void )
122  {
123  int8_t a[6] = { 1, 2, 3, 4, 5, 6 };
124  std::size_t dim = 2;
125 
126  const boost::shared_ptr< std::vector< int8_t > > v =
127  boost::shared_ptr< std::vector< int8_t > >(
128  new std::vector< int8_t >( a, a + sizeof( a ) / sizeof( int8_t ) ) );
129  WValueSet< int8_t > set( 1, dim, v, W_DT_INT8 );
130 
131  // testing for valid dimension and values of the returned WValue
132  for( std::size_t idx = 0; idx < v->size()/dim; idx++ )
133  {
134  WValue< int8_t > currentWValue( dim );
135  for( std::size_t i = 0; i < dim; i++ ) currentWValue[ i ] = ( *v )[ idx*dim + i ];
136  TS_ASSERT_EQUALS( set.getWValue( idx ), currentWValue );
137  TS_ASSERT_EQUALS( set.getWValue( idx ).size(), dim );
138  }
139 
140  // catch wrong indices?
141  TS_ASSERT_THROWS_ANYTHING( set.getWValue( v->size() ) );
142  TS_ASSERT_THROWS_ANYTHING( set.getWValue( v->size()*2 ) );
143 
144  // catch wrong order?
145  WValueSet< int8_t > set2( 2, dim, v, W_DT_INT8 );
146  TS_ASSERT_THROWS_ANYTHING( set2.getWValue( 0 ) );
147  }
148 
149  /**
150  * A subarray should never exceed the valuesets boundaries and should not have a length of 0.
151  */
153  {
154  int8_t a[4] = { 0, -5, 1, 2 };
155  const boost::shared_ptr< std::vector< int8_t > > v =
156  boost::shared_ptr< std::vector< int8_t > >(
157  new std::vector< int8_t >( a, a + sizeof( a ) / sizeof( int8_t ) ) );
158  WValueSet< int8_t > set( 1, 2, v, W_DT_INT8 );
159  TS_ASSERT_THROWS_NOTHING( set.getSubArray( 0, 2 ) );
160  TS_ASSERT_THROWS_NOTHING( set.getSubArray( 3, 1 ) );
161  TS_ASSERT_THROWS( set.getSubArray( 4, 1 ), WException );
162  TS_ASSERT_THROWS( set.getSubArray( 3, 2 ), WException );
163  TS_ASSERT_THROWS( set.getSubArray( 2, 0 ), WException );
164  }
165 
166  /**
167  * A subarray should return the correct elements.
168  */
170  {
171  int8_t a[ 8 ] = { 0, -5, 1, 2, -27, 6, 29, 8 };
172  const boost::shared_ptr< std::vector< int8_t > > v =
173  boost::shared_ptr< std::vector< int8_t > >(
174  new std::vector< int8_t >( a, a + sizeof( a ) / sizeof( int8_t ) ) );
175  WValueSet< int8_t > set( 1, 2, v, W_DT_INT8 );
176 
177  {
178  WValueSet< int8_t >::SubArray const s = set.getSubArray( 0, 2 );
179 
180  TS_ASSERT_THROWS_NOTHING( s[ 0 ] );
181  TS_ASSERT_THROWS_NOTHING( s[ 1 ] );
182  TS_ASSERT_THROWS_NOTHING( s[ 2 ] );
183  TS_ASSERT_THROWS_NOTHING( s[ 100 ] );
184 
185  TS_ASSERT_EQUALS( s[ 0 ], 0 );
186  TS_ASSERT_EQUALS( s[ 1 ], -5 );
187  TS_ASSERT_EQUALS( s[ 2 ], 0 );
188  TS_ASSERT_EQUALS( s[ 100 ], 0 );
189  }
190  {
191  WValueSet< int8_t >::SubArray const s = set.getSubArray( 1, 3 );
192 
193  TS_ASSERT_THROWS_NOTHING( s[ 0 ] );
194  TS_ASSERT_THROWS_NOTHING( s[ 1 ] );
195  TS_ASSERT_THROWS_NOTHING( s[ 2 ] );
196  TS_ASSERT_THROWS_NOTHING( s[ 100 ] );
197 
198  TS_ASSERT_EQUALS( s[ 0 ], -5 );
199  TS_ASSERT_EQUALS( s[ 1 ], 1 );
200  TS_ASSERT_EQUALS( s[ 2 ], 2 );
201  TS_ASSERT_EQUALS( s[ 100 ], -5 );
202  }
203  {
204  WValueSet< int8_t >::SubArray const s = set.getSubArray( 5, 3 );
205 
206  TS_ASSERT_THROWS_NOTHING( s[ 0 ] );
207  TS_ASSERT_THROWS_NOTHING( s[ 1 ] );
208  TS_ASSERT_THROWS_NOTHING( s[ 2 ] );
209  TS_ASSERT_THROWS_NOTHING( s[ 100 ] );
210 
211  TS_ASSERT_EQUALS( s[ 0 ], 6 );
212  TS_ASSERT_EQUALS( s[ 1 ], 29 );
213  TS_ASSERT_EQUALS( s[ 2 ], 8 );
214  TS_ASSERT_EQUALS( s[ 100 ], 6 );
215  }
216  }
217 };
218 
219 #endif // WVALUESET_TEST_H
void testGetNumberOfValues(void)
The number of values retrieved is correct.
Base class for all higher level values like tensors, vectors, matrices and so on. ...
Definition: WValue.h:40
const T * rawData() const
Sometimes we need raw access to the data array, for e.g.
Definition: WValueSet.h:240
void testGetScalar(void)
This function should return the i-th value if the value set is scalar.
SubArray const getSubArray(std::size_t start, std::size_t size) const
Request (read-) access object to a subarray of this valueset.
Definition: WValueSet.h:265
A helper class granting safe access to a certain part of the valueset.
Definition: WValueSet.h:64
void testSubArrayInstantiation()
A subarray should never exceed the valuesets boundaries and should not have a length of 0...
void testInstantiation(void)
An instantiation should never throw an exception.
void testReadOnlyRawAccess(void)
Raw Access should provide data access to the underlying array.
WValue< T > getWValue(size_t index) const
Get the i'th WValue with the dimension of WValueSet.
Definition: WValueSet.h:340
virtual T getScalar(size_t i) const
Definition: WValueSet.h:193
void testGetWValue(void)
This function should return the i-th WValue with of the used dimension (prerequisite the ValueSet has...
virtual size_t rawSize() const
Definition: WValueSet.h:184
UnitTests the WValueSet class.
virtual size_t size() const
Definition: WValueSet.h:162
Base Class for all value set types.
Definition: WValueSet.h:46
void testSubArrayAccess()
A subarray should return the correct elements.
void testRawSize(void)
The raw size is the size of the number of integral elements inside this ValueSet. ...
Basic exception handler.
Definition: WException.h:38