OpenWalnut  1.5.0dev
WReaderCSV_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 WREADERCSV_TEST_H
26 #define WREADERCSV_TEST_H
27 
28 #include <string>
29 
30 #include <cxxtest/TestSuite.h>
31 
32 #include "../WReaderCSV.h"
33 
34 
35 /**
36  * Test class of WReaderCSV class
37  */
38 
39 class WReaderCSVTest : public CxxTest::TestSuite
40 {
41 public:
42  /**
43  * if an empty CSV file is loaded, a "CSV file is empty!" exception should be thrown
44  */
45  void testEmptyCSV()
46  {
47  std::string fileName = W_FIXTURE_PATH + "CSVs/empty.csv";
48  std::cout << std::endl << "Test loading of " << fileName << "." << std::endl;
49 
50  TS_ASSERT_THROWS_EQUALS( WReaderCSV( fileName ).read(),
51  const WException &e,
52  strcmp( e.what(), "CSV file is empty!" ),
53  0 );
54  }
55 
56  /**
57  * if a CSV file only contains a header row, a "CSV File does not contain data!" exception should be thrown
58  */
60  {
61  std::string fileName = W_FIXTURE_PATH + "CSVs/onlyHeader.csv";
62  std::cout << std::endl << "Test loading of " << fileName << "." << std::endl;
63 
64  TS_ASSERT_THROWS_EQUALS( WReaderCSV( fileName ).read(),
65  const WException &e,
66  strcmp( e.what(), "CSV File does not contain data!" ),
67  0 );
68  }
69 
70  /**
71  * if a CSV file contains rows, whose column count differs from the header,
72  * a "Data row count does not equal header count!" exception should be thrown
73  */
75  {
76  std::string fileName = W_FIXTURE_PATH + "CSVs/invalidColumnCount.csv";
77  std::cout << std::endl << "Test loading of " << fileName << "." << std::endl;
78 
79  TS_ASSERT_THROWS_EQUALS( WReaderCSV( fileName ).read(),
80  const WException &e,
81  strcmp( e.what(), "Data row count does not equal header count!" ),
82  0 );
83  }
84 
85  /**
86  * if a valid CSV file is loaded, no exception should be thrown
87  */
88  void testValidCSV()
89  {
90  std::string fileName = W_FIXTURE_PATH + "CSVs/valid.csv";
91  std::cout << std::endl << "Test loading of " << fileName << "." << std::endl;
92 
93 
97  testHeader->push_back(
98  {
99  "PDGEncoding", "trackID", "parentID", "trackLocalTime", "time", "edep", "stepLength", "trackLength",
100  "posX", "posY", "posZ", "localPosX", "localPosY", "localPosZ", "momDirX", "momDirY", "momDirZ", "baseID",
101  "level1ID", "level2ID", "level3ID", "level4ID", "layerID", "photonID", "nPhantomCompton", "nCrystalCompton",
102  "nPhantomRayleigh", "nCrystalRayleigh", "primaryID", "sourcePosX", "sourcePosY", "sourcePosZ", "sourceID", "eventID",
103  "runID", "axialPos", "rotationAngle", "volumeID", "processName", "comptVolName", "RayleighVolName"
104  }
105  );
106  testDataFirstRow->push_back(
107  {
108  "2212", "1", "0", "4.20922e-09", "4.20922e-09", "0.0199097", "0.0250083", "725.314", "-1.86984", "4.55793", "225.244", "-1.86984",
109  "4.55793", "0.0125", "-0.00614681", "0.0255574", "0.999654", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "-0.154322",
110  "0.156973", "-500", "0", "1", "0", "277.4", "0", "[0;1;0;3;-1;-1;-1;-1;-1;-1]", "Transportation", "NULL", "NULL"
111  }
112  );
113  testDataLastRow->push_back(
114  {
115  "2212", "1", "0", "5.92057e-09", "5.92057e-09", "0.3312", "0.025598", "955.947", "14.127", "28.6775", "454.394", "14.127", "28.6775",
116  "0.0125", "0.216802", "0.0338896", "0.975627", "1", "22", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "-0.480459", "0.0634152",
117  "-500", "0", "2", "0", "461.8", "0", "[0;2;22;3;-1;-1;-1;-1;-1;-1]", "Transportation", "NULL", "NULL"
118  }
119  );
120 
121  WReaderCSV tmpCsvReader( fileName );
122  TS_ASSERT_THROWS_NOTHING( tmpCsvReader.read() );
123  // compare headers
124  TS_ASSERT_EQUALS( tmpCsvReader.read()->getHeader()->front(), testHeader->front() );
125  // compare first data rows
126  TS_ASSERT_EQUALS( tmpCsvReader.read()->getData()->front(), testDataFirstRow->front() );
127  // compare last data rows
128  TS_ASSERT_EQUALS( tmpCsvReader.read()->getData()->back(), testDataLastRow->front() );
129  }
130 
131  /**
132  * check \\r line endings
133  */
135  {
136  std::string fileName = W_FIXTURE_PATH + "CSVs/cr.csv";
137  std::cout << std::endl << "Test loading of " << fileName << "." << std::endl;
138 
139 
142  testHeader->push_back(
143  {
144  "header1", "header2", "header3"
145  }
146  );
147  testDataFirstRow->push_back(
148  {
149  "1", "2", "3"
150  }
151  );
152 
153  WReaderCSV tmpCsvReader( fileName );
154  TS_ASSERT_THROWS_NOTHING( tmpCsvReader.read() );
155  // compare headers
156  TS_ASSERT_EQUALS( tmpCsvReader.read()->getHeader()->front(), testHeader->front() );
157  // compare first data rows
158  TS_ASSERT_EQUALS( tmpCsvReader.read()->getData()->front(), testDataFirstRow->front() );
159  }
160 
161  /**
162  * check \\n line endings
163  */
165  {
166  std::string fileName = W_FIXTURE_PATH + "CSVs/lf.csv";
167  std::cout << std::endl << "Test loading of " << fileName << "." << std::endl;
168 
169 
172  testHeader->push_back(
173  {
174  "header1", "header2", "header3"
175  }
176  );
177  testDataFirstRow->push_back(
178  {
179  "1", "2", "3"
180  }
181  );
182 
183  WReaderCSV tmpCsvReader( fileName );
184  TS_ASSERT_THROWS_NOTHING( tmpCsvReader.read() );
185  // compare headers
186  TS_ASSERT_EQUALS( tmpCsvReader.read()->getHeader()->front(), testHeader->front() );
187  // compare first data rows
188  TS_ASSERT_EQUALS( tmpCsvReader.read()->getData()->front(), testDataFirstRow->front() );
189  }
190 
191  /**
192  * check \\r\\n line endings
193  */
195  {
196  std::string fileName = W_FIXTURE_PATH + "CSVs/crlf.csv";
197  std::cout << std::endl << "Test loading of " << fileName << "." << std::endl;
198 
199 
202  testHeader->push_back(
203  {
204  "header1", "header2", "header3"
205  }
206  );
207  testDataFirstRow->push_back(
208  {
209  "1", "2", "3"
210  }
211  );
212 
213  WReaderCSV tmpCsvReader( fileName );
214  TS_ASSERT_THROWS_NOTHING( tmpCsvReader.read() );
215  // compare headers
216  TS_ASSERT_EQUALS( tmpCsvReader.read()->getHeader()->front(), testHeader->front() );
217  // compare first data rows
218  TS_ASSERT_EQUALS( tmpCsvReader.read()->getData()->front(), testDataFirstRow->front() );
219  }
220 
221  /**
222  * Empty columns should not prevent the reader from reading all columns
223  */
225  {
226  std::string fileName = W_FIXTURE_PATH + "CSVs/emptyColumns.csv";
227  std::cout << std::endl << "Test loading of " << fileName << "." << std::endl;
228 
229 
232  testHeader->push_back(
233  {
234  "header1", "header2", "header3"
235  }
236  );
237  testDataFirstRow->push_back(
238  {
239  "", "", ""
240  }
241  );
242 
243  WReaderCSV tmpCsvReader( fileName );
244  TS_ASSERT_THROWS_NOTHING( tmpCsvReader.read() );
245  // compare headers
246  TS_ASSERT_EQUALS( tmpCsvReader.read()->getHeader()->front(), testHeader->front() );
247  // compare first data rows
248  TS_ASSERT_EQUALS( tmpCsvReader.read()->getData()->front(), testDataFirstRow->front() );
249  }
250 };
251 
252 #endif // WREADERCSV_TEST_H
std::shared_ptr< std::vector< std::vector< std::string > > > ContentSPtr
represents a pointer to the Content
Definition: WDataSetCSV.h:52
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 WReaderCSV class.
void testInvalidColumnNumber()
if a CSV file contains rows, whose column count differs from the header, a "Data row count does not e...
void testCRLFLineEnding()
check \r\n line endings
void testCRLineEnding()
check \r line endings
void testEmptyCSV()
if an empty CSV file is loaded, a "CSV file is empty!" exception should be thrown
void testLFLineEnding()
check \n line endings
void testValidCSV()
if a valid CSV file is loaded, no exception should be thrown
void testEmptyColumns()
Empty columns should not prevent the reader from reading all columns.
void testOnlyHeaderCSV()
if a CSV file only contains a header row, a "CSV File does not contain data!" exception should be thr...
Read content from a CSV file.
Definition: WReaderCSV.h:43
virtual std::shared_ptr< WDataSetCSV > read()
Read the file and create a dataset as a vector.
Definition: WReaderCSV.cpp:74