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