OpenWalnut  1.5.0dev
WProtonData_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 WPROTONDATA_TEST_H
26 #define WPROTONDATA_TEST_H
27 
28 #include <string>
29 #include <vector>
30 
31 #include <cxxtest/TestSuite.h>
32 
33 #include <core/common/test/WException_test.h>
34 
35 #include "../WProtonData.h"
36 
37 
38 /**
39  * Test class of WMProtonData class
40  */
41 class WProtonDataTest : public CxxTest::TestSuite
42 {
43  /**
44  * A sample of column names
45  */
47  {
48  {
49  "PDGEncoding", "trackID", "parentID", "trackLocalTime", "time", "edep", "stepLength", "trackLength",
50  "posX", "posY", "posZ", "localPosX", "localPosY", "localPosZ", "momDirX", "momDirY", "momDirZ",
51  "baseID", "level1ID", "level2ID", "level3ID", "level4ID", "layerID", "photonID", "nPhantomCompton",
52  "nCrystalCompton", "nPhantomRayleigh", "nCrystalRayleigh", "primaryID", "sourcePosX", "sourcePosY",
53  "sourcePosZ", "sourceID", "eventID", "runID", "axialPos", "rotationAngle", "volumeID", "processName",
54  "comptVolName", "RayleighVolName"
55  }
56  };
57 
58  /**
59  * A sample of data rows
60  */
62  {
63  {
64  "2212", "1", "0", "4.20922e-09", "4.20922e-09", "0.0199097", "0.0250083", "725.314", "-1.86984", "4.55793",
65  "225.244", "-1.86984", "4.55793", "0.0125", "-0.00614681", "0.0255574", "0.999654", "0", "0", "0", "0", "0", "0",
66  "0", "0", "0", "0", "0", "1", "-0.154322", "0.156973", "-500", "0", "1", "0", "277.4", "0",
67  "[0;1;0;3;-1;-1;-1;-1;-1;-1]", "Transportation", "NULL", "NULL"
68  },
69  {
70  "2212", "1", "0", "4.54012e-09", "4.54012e-09", "0.0179175", "0.0250066", "777.726", "-2.04548", "5.6447",
71  "277.644", "-2.04548", "5.6447", "0.0125", "-0.00435673", "0.0234654", "0.999715", "0", "1", "0", "0", "0", "0",
72  "0", "0", "0", "0", "0", "1", "-0.154322", "0.156973", "-500", "0", "1", "0", "277.4", "0",
73  "[0;1;1;3;-1;-1;-1;-1;-1;-1]", "Transportation", "NULL", "NULL"
74  },
75  {
76  "2212", "1", "0", "4.89277e-09", "4.89277e-09", "0.143599", "0.0250046", "833.49", "-2.26821", "6.88172",
77  "333.394", "-2.26821", "6.88172", "0.0125", "-0.00791184", "0.0166431", "0.99983", "1", "0", "0", "0", "0", "0",
78  "0", "0", "0", "0", "0", "1", "-0.154322", "0.156973", "-500", "0", "1", "0", "461.8", "0",
79  "[0;2;0;3;-1;-1;-1;-1;-1;-1]", "Transportation", "NULL", "NULL"
80  },
81  {
82  "2212", "1", "0", "4.92791e-09", "4.92791e-09", "0.0204459", "0.0250016", "838.991", "-2.30588", "6.96663",
83  "338.894", "-2.30588", "6.96663", "0.0125", "0.0113033", "0.00148302", "0.999935", "1", "1", "0", "0", "0", "0",
84  "0", "0", "0", "0", "0", "1", "-0.154322", "0.156973", "-500", "0", "1", "0", "461.8", "0",
85  "[0;2;1;3;-1;-1;-1;-1;-1;-1]", "Transportation", "NULL", "NULL"
86  },
87  {
88  "2212", "1", "0", "4.96345e-09", "4.96345e-09", "0.0160251", "0.0250035", "844.492", "-2.25045", "6.96272",
89  "344.394", "-2.25045", "6.96272", "0.0125", "0.0167816", "-0.00606", "0.999841", "1", "2", "0", "0", "0", "0",
90  "0", "0", "0", "0", "0", "1", "-0.154322", "0.156973", "-500", "0", "1", "0", "461.8", "0",
91  "[0;2;2;3;-1;-1;-1;-1;-1;-1]", "Transportation", "NULL", "NULL"
92  }
93  };
94 public:
95  /**
96  * Test the constructor of WProtonData, so no exception is thrown, when input parameters are correct
97  */
99  {
100  TS_ASSERT_THROWS_NOTHING(
101  WProtonData( std::make_shared< WDataSetCSV::Content >( sampleColumnNames ),
102  std::make_shared< WDataSetCSV::Content >( sampleDataRows ) );
103  );
104  }
105 
106  /**
107  * Tests, if setCSVHeader throws an exception, when the new header is empty
108  */
110  {
111  WProtonData protonData( std::make_shared< WDataSetCSV::Content >( sampleColumnNames ),
112  std::make_shared< WDataSetCSV::Content >( sampleDataRows ) );
113 
115  TS_ASSERT_THROWS_EQUALS(
116  protonData.setCSVHeader( tmpEmptyHeader ),
117  WException &e,
118  strcmp( e.what(), "No empty header allowed!" ),
119  0
120  );
121  }
122 
123  /**
124  * Tests, if setCSVHeader throws an exception, when parameter is a null pointer
125  */
127  {
128  WProtonData protonData( std::make_shared< WDataSetCSV::Content >( sampleColumnNames ),
129  std::make_shared< WDataSetCSV::Content >( sampleDataRows ) );
130 
131  WDataSetCSV::ContentSPtr tmpHeaderNullptr = nullptr;
132  TS_ASSERT_THROWS_EQUALS(
133  protonData.setCSVHeader( tmpHeaderNullptr ),
134  WException &e,
135  strcmp( e.what(), "Can not set header! No header specified!" ),
136  0
137  );
138  }
139 
140  /**
141  * Tests, if setCSVHeader throws an exception, when parameter is a null pointer
142  */
144  {
145  WProtonData protonData( std::make_shared< WDataSetCSV::Content >( sampleColumnNames ),
146  std::make_shared< WDataSetCSV::Content >( sampleDataRows ) );
147 
148  WDataSetCSV::ContentSPtr tmpDataNullptr = nullptr;
149  TS_ASSERT_THROWS_EQUALS(
150  protonData.setCSVData( tmpDataNullptr ),
151  WException &e,
152  strcmp( e.what(), "Can not set data! No data specified!" ),
153  0
154  );
155  }
156 
157  /**
158  * Tests the setter and getter for the member variable -m_ColumnnMapSelectedIndex-
159  */
161  {
162  WProtonData protonData( std::make_shared< WDataSetCSV::Content >( sampleColumnNames ),
163  std::make_shared< WDataSetCSV::Content >( sampleDataRows ) );
164 
165  std::tuple< std::string, int > refData1( "test 1", 0 );
166  std::tuple< std::string, int > refData2( "test 2", 1 );
167  std::tuple< std::string, int > refData3( "test 3", 2 );
168  std::tuple< std::string, int > refData4( "test 4", 3 );
169 
170  protonData.setStateIndex( std::get< 0 >( refData1 ), std::get< 1 >( refData1 ) );
171  protonData.setStateIndex( std::get< 0 >( refData2 ), std::get< 1 >( refData2 ) );
172  protonData.setStateIndex( std::get< 0 >( refData3 ), std::get< 1 >( refData3 ) );
173  protonData.setStateIndex( std::get< 0 >( refData4 ), std::get< 1 >( refData4 ) );
174 
175  TS_ASSERT_EQUALS( protonData.getColumnIndexBySelection( std::get< 0 >( refData1 ) ), std::get< 1 >( refData1 ) );
176  TS_ASSERT_EQUALS( protonData.getColumnIndexBySelection( std::get< 0 >( refData2 ) ), std::get< 1 >( refData2 ) );
177  TS_ASSERT_EQUALS( protonData.getColumnIndexBySelection( std::get< 0 >( refData3 ) ), std::get< 1 >( refData3 ) );
178  TS_ASSERT_EQUALS( protonData.getColumnIndexBySelection( std::get< 0 >( refData4 ) ), std::get< 1 >( refData4 ) );
179  }
180 
181  /**
182  * Tests the availability of the single selections
183  */
185  {
186  WProtonData protonData( std::make_shared< WDataSetCSV::Content >( sampleColumnNames ),
187  std::make_shared< WDataSetCSV::Content >( sampleDataRows ) );
188 
189  std::tuple< std::string, int > refData1( "test 1", 0 );
190  std::tuple< std::string, int > refData2( "test 2", 1 );
191  std::tuple< std::string, int > refData3( "test 3", 2 );
192  std::tuple< std::string, int > refData4( "test 4", 3 );
193 
194  //-1 stands for no selection
195  std::tuple< std::string, int > refData5( "test 5", -1 );
196  std::tuple< std::string, int > refData6( "test 6", -1 );
197 
198  protonData.setStateIndex( std::get< 0 >( refData1 ), std::get< 1 >( refData1 ) );
199  protonData.setStateIndex( std::get< 0 >( refData2 ), std::get< 1 >( refData2 ) );
200  protonData.setStateIndex( std::get< 0 >( refData3 ), std::get< 1 >( refData3 ) );
201  protonData.setStateIndex( std::get< 0 >( refData4 ), std::get< 1 >( refData4 ) );
202  protonData.setStateIndex( std::get< 0 >( refData5 ), std::get< 1 >( refData5 ) );
203  protonData.setStateIndex( std::get< 0 >( refData6 ), std::get< 1 >( refData6 ) );
204 
205  TS_ASSERT_EQUALS( protonData.isColumnAvailable( std::get< 0 >( refData1 ) ), true );
206  TS_ASSERT_EQUALS( protonData.isColumnAvailable( std::get< 0 >( refData2 ) ), true );
207  TS_ASSERT_EQUALS( protonData.isColumnAvailable( std::get< 0 >( refData3 ) ), true );
208  TS_ASSERT_EQUALS( protonData.isColumnAvailable( std::get< 0 >( refData4 ) ), true );
209  TS_ASSERT_EQUALS( protonData.isColumnAvailable( std::get< 0 >( refData5 ) ), false );
210  TS_ASSERT_EQUALS( protonData.isColumnAvailable( std::get< 0 >( refData6 ) ), false );
211 
212  //name which is not in the m_ColumnnMapSelectedIndex
213  TS_ASSERT_EQUALS( protonData.isColumnAvailable( "No Name" ), false );
214  TS_ASSERT_EQUALS( protonData.isColumnAvailable( "unavailable" ), false );
215  }
216 
217  /**
218  * Tests the content of -m_columnMap-
219  */
221  {
222  WProtonData protonData( std::make_shared< WDataSetCSV::Content >( sampleColumnNames ),
223  std::make_shared< WDataSetCSV::Content >( sampleDataRows ) );
224 
225  std::vector< std::string > header = sampleColumnNames.at( 0 );
226 
227  for( size_t headerCounter = 0; headerCounter < header.size(); headerCounter++ )
228  {
229  TS_ASSERT_EQUALS( protonData.getColumnIndex( header.at( headerCounter ) ), headerCounter );
230  }
231 
232  //name which is not in the m_columnMap
233  TS_ASSERT_EQUALS( protonData.getColumnIndex( "No Name" ), -1 );
234  TS_ASSERT_EQUALS( protonData.getColumnIndex( "unavailable" ), -1 );
235  }
236 
237  /**
238  * Tests method -determineColumnTypeByString-
239  */
241  {
242  WProtonData protonData( std::make_shared< WDataSetCSV::Content >( sampleColumnNames ),
243  std::make_shared< WDataSetCSV::Content >( sampleDataRows ) );
244 
245  std::vector< std::string > testIntList
246  {
247  "1", "0", "10", "+1", "-1",
248  "1e+1", "+1e+1", "-1e+1",
249  "1e1", "+1e1", "-1e1",
250  };
251 
252  std::vector< std::string > testDoubleList
253  {
254  "1000.1", "1.", ".1", "1.1",
255  "+1.", "-1.", "+.1", "-.1",
256  "+1e-1", "0.001e-12",
257  "0.111111111111111"
258  };
259 
260  std::vector< std::string > testStringList
261  {
262  ".", "1a", "++1",
263  "+-1", "+", "-.",
264  "-", "--1.", "1.e.1",
265  "1e.1", "0+.e0"
266  };
267 
268  for( auto trueCondition : testIntList )
269  {
270  TS_ASSERT_EQUALS( protonData.determineColumnTypeByString( trueCondition ), "int" );
271  }
272 
273  for( auto trueCondition : testDoubleList )
274  {
275  TS_ASSERT_EQUALS( protonData.determineColumnTypeByString( trueCondition ), "double" );
276  }
277 
278  for( auto falseCondition : testStringList )
279  {
280  TS_ASSERT_EQUALS( protonData.determineColumnTypeByString( falseCondition ), "string" );
281  }
282  }
283 
284  /**
285  * Tests internal variable m_columnTypes with the method -getColumnTypes-
286  */
288  {
289  WProtonData protonData( std::make_shared< WDataSetCSV::Content >( sampleColumnNames ),
290  std::make_shared< WDataSetCSV::Content >( sampleDataRows ) );
291 
292  std::vector< std::string > refColumnTypes
293  {
294  "int", "int", "int", "double", "double", "double", "double", "double", "double", "double",
295  "double", "double", "double", "double", "double" , "double", "double" , "int", "int", "int", "int",
296  "int", "int", "int", "int", "int", "int", "int", "int", "double", "double",
297  "int", "int", "int", "int", "double", "int", "string", "string", "string", "string",
298  };
299 
300  WDataSetCSV::ContentElemSPtr testColumnTypes = protonData.getColumnTypes();
301 
302  size_t counter = 0;
303  for( auto columnTyp : *testColumnTypes )
304  {
305  TS_ASSERT_EQUALS( columnTyp, refColumnTypes.at( counter++ ) );
306  }
307  }
308 };
309 
310 #endif // WPROTONDATA_TEST_H
std::shared_ptr< std::vector< std::vector< std::string > > > ContentSPtr
represents a pointer to the Content
Definition: WDataSetCSV.h:52
std::shared_ptr< std::vector< std::string > > ContentElemSPtr
represents a shared pointer to a ContentElem object.
Definition: WDataSetCSV.h:67
std::vector< std::vector< std::string > > Content
represents a vector containing a vector of strings.
Definition: WDataSetCSV.h:47
Basic exception handler.
Definition: WException.h:39
virtual const char * what() const
Returns the message string set on throw.
Definition: WException.cpp:90
Test class of WMProtonData class.
WDataSetCSV::Content sampleColumnNames
A sample of column names.
void testSetCSVHeaderNull()
Tests, if setCSVHeader throws an exception, when parameter is a null pointer.
void testDetectColumnTypesFromCsvData()
Tests internal variable m_columnTypes with the method -getColumnTypes-.
void testSetCSVDataNull()
Tests, if setCSVHeader throws an exception, when parameter is a null pointer.
void testGetColumnIndex()
Tests the content of -m_columnMap-.
void testSetCSVHeaderEmpty()
Tests, if setCSVHeader throws an exception, when the new header is empty.
void testConstructor()
Test the constructor of WProtonData, so no exception is thrown, when input parameters are correct.
WDataSetCSV::Content sampleDataRows
A sample of data rows.
void testSetterGetterStateIndex()
Tests the setter and getter for the member variable -m_ColumnnMapSelectedIndex-.
void testIsColumnAvailable()
Tests the availability of the single selections.
void testDetermineColumnTypeByString()
Tests method -determineColumnTypeByString-.
Holds the csv data.
Definition: WProtonData.h:43
int getColumnIndex(std::string columnName)
getter
WDataSetCSV::ContentElemSPtr getColumnTypes()
Get column types, stored in a string vector.
int getColumnIndexBySelection(std::string selectedName)
getter
std::string determineColumnTypeByString(std::string cellValue)
Determines column type due to cellValue.
void setCSVData(WDataSetCSV::ContentSPtr csvData)
setter
Definition: WProtonData.cpp:64
void setStateIndex(std::string columnName, int index)
setter
Definition: WProtonData.cpp:91
void setCSVHeader(WDataSetCSV::ContentSPtr csvHeader)
setter
Definition: WProtonData.cpp:40
bool isColumnAvailable(std::string columnName)
checks whether columns are available
Definition: WProtonData.cpp:97