@@ -1,317 +1,357 | |||
|
1 | 1 | #include "AmdaResultParser.h" |
|
2 | 2 | |
|
3 | 3 | #include <Data/ScalarSeries.h> |
|
4 | 4 | #include <Data/VectorSeries.h> |
|
5 | 5 | |
|
6 | 6 | #include <QObject> |
|
7 | 7 | #include <QtTest> |
|
8 | 8 | |
|
9 | 9 | namespace { |
|
10 | 10 | |
|
11 | 11 | /// Path for the tests |
|
12 | 12 | const auto TESTS_RESOURCES_PATH |
|
13 | 13 | = QFileInfo{QString{AMDA_TESTS_RESOURCES_DIR}, "TestAmdaResultParser"}.absoluteFilePath(); |
|
14 | 14 | |
|
15 | 15 | QDateTime dateTime(int year, int month, int day, int hours, int minutes, int seconds) |
|
16 | 16 | { |
|
17 | 17 | return QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC}; |
|
18 | 18 | } |
|
19 | 19 | |
|
20 | 20 | QString inputFilePath(const QString &inputFileName) |
|
21 | 21 | { |
|
22 | 22 | return QFileInfo{TESTS_RESOURCES_PATH, inputFileName}.absoluteFilePath(); |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | 25 | template <typename T> |
|
26 | 26 | struct ExpectedResults { |
|
27 | 27 | |
|
28 | 28 | ExpectedResults &setParsingOK(bool parsingOK) |
|
29 | 29 | { |
|
30 | 30 | m_ParsingOK = parsingOK; |
|
31 | 31 | return *this; |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | ExpectedResults &setXAxisUnit(Unit xAxisUnit) |
|
35 | 35 | { |
|
36 | 36 | m_XAxisUnit = std::move(xAxisUnit); |
|
37 | 37 | return *this; |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | ExpectedResults &setXAxisData(const QVector<QDateTime> &xAxisData) |
|
41 | 41 | { |
|
42 | 42 | m_XAxisData.clear(); |
|
43 | 43 | |
|
44 | 44 | // Converts QVector<QDateTime> to QVector<double> |
|
45 | 45 | std::transform(xAxisData.cbegin(), xAxisData.cend(), std::back_inserter(m_XAxisData), |
|
46 | 46 | [](const auto &dateTime) { return dateTime.toMSecsSinceEpoch() / 1000.; }); |
|
47 | 47 | |
|
48 | 48 | return *this; |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | ExpectedResults &setValuesUnit(Unit valuesUnit) |
|
52 | 52 | { |
|
53 | 53 | m_ValuesUnit = std::move(valuesUnit); |
|
54 | 54 | return *this; |
|
55 | 55 | } |
|
56 | 56 | |
|
57 | 57 | ExpectedResults &setValuesData(QVector<double> valuesData) |
|
58 | 58 | { |
|
59 | 59 | m_ValuesData.clear(); |
|
60 | 60 | m_ValuesData.push_back(std::move(valuesData)); |
|
61 | 61 | return *this; |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | ExpectedResults &setValuesData(QVector<QVector<double> > valuesData) |
|
65 | 65 | { |
|
66 | 66 | m_ValuesData = std::move(valuesData); |
|
67 | 67 | return *this; |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | ExpectedResults &setYAxisEnabled(bool yAxisEnabled) | |
|
71 | { | |
|
72 | m_YAxisEnabled = yAxisEnabled; | |
|
73 | return *this; | |
|
74 | } | |
|
75 | ||
|
76 | ExpectedResults &setYAxisUnit(Unit yAxisUnit) | |
|
77 | { | |
|
78 | m_YAxisUnit = std::move(yAxisUnit); | |
|
79 | return *this; | |
|
80 | } | |
|
81 | ||
|
82 | ExpectedResults &setYAxisData(QVector<double> yAxisData) | |
|
83 | { | |
|
84 | m_YAxisData = std::move(yAxisData); | |
|
85 | return *this; | |
|
86 | } | |
|
87 | ||
|
70 | 88 | /** |
|
71 | 89 | * Validates a DataSeries compared to the expected results |
|
72 | 90 | * @param results the DataSeries to validate |
|
73 | 91 | */ |
|
74 | 92 | void validate(std::shared_ptr<IDataSeries> results) |
|
75 | 93 | { |
|
76 | 94 | if (m_ParsingOK) { |
|
77 | 95 | auto dataSeries = dynamic_cast<T *>(results.get()); |
|
78 | 96 | if (dataSeries == nullptr) { |
|
79 | 97 | |
|
80 | 98 | // No unit detected, parsink ok but data is nullptr |
|
81 | 99 | // TODO, improve the test to verify that the data is null |
|
82 | 100 | return; |
|
83 | 101 | } |
|
84 | 102 | |
|
85 | 103 | // Checks units |
|
86 | 104 | QVERIFY(dataSeries->xAxisUnit() == m_XAxisUnit); |
|
87 | 105 | QVERIFY(dataSeries->valuesUnit() == m_ValuesUnit); |
|
88 | 106 | |
|
89 | 107 | auto verifyRange = [dataSeries](const auto &expectedData, const auto &equalFun) { |
|
90 | 108 | QVERIFY(std::equal(dataSeries->cbegin(), dataSeries->cend(), expectedData.cbegin(), |
|
91 | 109 | expectedData.cend(), |
|
92 | 110 | [&equalFun](const auto &dataSeriesIt, const auto &expectedX) { |
|
93 | 111 | return equalFun(dataSeriesIt, expectedX); |
|
94 | 112 | })); |
|
95 | 113 | }; |
|
96 | 114 | |
|
97 | 115 | // Checks x-axis data |
|
98 | 116 | verifyRange(m_XAxisData, [](const auto &seriesIt, const auto &value) { |
|
99 | 117 | return seriesIt.x() == value; |
|
100 | 118 | }); |
|
101 | 119 | |
|
102 | 120 | // Checks values data of each component |
|
103 | 121 | for (auto i = 0; i < m_ValuesData.size(); ++i) { |
|
104 | 122 | verifyRange(m_ValuesData.at(i), [i](const auto &seriesIt, const auto &value) { |
|
105 | 123 | auto itValue = seriesIt.value(i); |
|
106 | 124 | return (std::isnan(itValue) && std::isnan(value)) || seriesIt.value(i) == value; |
|
107 | 125 | }); |
|
108 | 126 | } |
|
127 | ||
|
128 | // Checks y-axis (if defined) | |
|
129 | auto yAxis = dataSeries->yAxis(); | |
|
130 | QCOMPARE(yAxis.isDefined(), m_YAxisEnabled); | |
|
131 | ||
|
132 | if (m_YAxisEnabled) { | |
|
133 | // Unit | |
|
134 | QCOMPARE(yAxis.unit(), m_YAxisUnit); | |
|
135 | ||
|
136 | // Data | |
|
137 | auto yAxisSize = yAxis.size(); | |
|
138 | QCOMPARE(yAxisSize, m_YAxisData.size()); | |
|
139 | for (auto i = 0; i < yAxisSize; ++i) { | |
|
140 | QCOMPARE(yAxis.at(i), m_YAxisData.at(i)); | |
|
141 | } | |
|
142 | } | |
|
109 | 143 | } |
|
110 | 144 | else { |
|
111 | 145 | QVERIFY(results == nullptr); |
|
112 | 146 | } |
|
113 | 147 | } |
|
114 | 148 | |
|
115 | 149 | // Parsing was successfully completed |
|
116 | 150 | bool m_ParsingOK{false}; |
|
117 | 151 | // Expected x-axis unit |
|
118 | 152 | Unit m_XAxisUnit{}; |
|
119 | 153 | // Expected x-axis data |
|
120 | 154 | QVector<double> m_XAxisData{}; |
|
121 | 155 | // Expected values unit |
|
122 | 156 | Unit m_ValuesUnit{}; |
|
123 | 157 | // Expected values data |
|
124 | 158 | QVector<QVector<double> > m_ValuesData{}; |
|
159 | // Expected data series has y-axis | |
|
160 | bool m_YAxisEnabled{false}; | |
|
161 | // Expected y-axis unit (if axis defined) | |
|
162 | Unit m_YAxisUnit{}; | |
|
163 | // Expected y-axis data (if axis defined) | |
|
164 | QVector<double> m_YAxisData{}; | |
|
125 | 165 | }; |
|
126 | 166 | |
|
127 | 167 | } // namespace |
|
128 | 168 | |
|
129 | 169 | Q_DECLARE_METATYPE(ExpectedResults<ScalarSeries>) |
|
130 | 170 | Q_DECLARE_METATYPE(ExpectedResults<VectorSeries>) |
|
131 | 171 | |
|
132 | 172 | class TestAmdaResultParser : public QObject { |
|
133 | 173 | Q_OBJECT |
|
134 | 174 | private: |
|
135 | 175 | template <typename T> |
|
136 | 176 | void testReadDataStructure() |
|
137 | 177 | { |
|
138 | 178 | // ////////////// // |
|
139 | 179 | // Test structure // |
|
140 | 180 | // ////////////// // |
|
141 | 181 | |
|
142 | 182 | // Name of TXT file to read |
|
143 | 183 | QTest::addColumn<QString>("inputFileName"); |
|
144 | 184 | // Expected results |
|
145 | 185 | QTest::addColumn<ExpectedResults<T> >("expectedResults"); |
|
146 | 186 | } |
|
147 | 187 | |
|
148 | 188 | template <typename T> |
|
149 | 189 | void testRead(AmdaResultParser::ValueType valueType) |
|
150 | 190 | { |
|
151 | 191 | QFETCH(QString, inputFileName); |
|
152 | 192 | QFETCH(ExpectedResults<T>, expectedResults); |
|
153 | 193 | |
|
154 | 194 | // Parses file |
|
155 | 195 | auto filePath = inputFilePath(inputFileName); |
|
156 | 196 | auto results = AmdaResultParser::readTxt(filePath, valueType); |
|
157 | 197 | |
|
158 | 198 | // ///////////////// // |
|
159 | 199 | // Validates results // |
|
160 | 200 | // ///////////////// // |
|
161 | 201 | expectedResults.validate(results); |
|
162 | 202 | } |
|
163 | 203 | |
|
164 | 204 | private slots: |
|
165 | 205 | /// Input test data |
|
166 | 206 | /// @sa testReadScalarTxt() |
|
167 | 207 | void testReadScalarTxt_data(); |
|
168 | 208 | |
|
169 | 209 | /// Tests parsing scalar series of a TXT file |
|
170 | 210 | void testReadScalarTxt(); |
|
171 | 211 | |
|
172 | 212 | /// Input test data |
|
173 | 213 | /// @sa testReadVectorTxt() |
|
174 | 214 | void testReadVectorTxt_data(); |
|
175 | 215 | |
|
176 | 216 | /// Tests parsing vector series of a TXT file |
|
177 | 217 | void testReadVectorTxt(); |
|
178 | 218 | }; |
|
179 | 219 | |
|
180 | 220 | void TestAmdaResultParser::testReadScalarTxt_data() |
|
181 | 221 | { |
|
182 | 222 | testReadDataStructure<ScalarSeries>(); |
|
183 | 223 | |
|
184 | 224 | // ////////// // |
|
185 | 225 | // Test cases // |
|
186 | 226 | // ////////// // |
|
187 | 227 | |
|
188 | 228 | // Valid files |
|
189 | 229 | QTest::newRow("Valid file") |
|
190 | 230 | << QStringLiteral("ValidScalar1.txt") |
|
191 | 231 | << ExpectedResults<ScalarSeries>{} |
|
192 | 232 | .setParsingOK(true) |
|
193 | 233 | .setXAxisUnit(Unit{"nT", true}) |
|
194 | 234 | .setXAxisData({dateTime(2013, 9, 23, 9, 0, 30), dateTime(2013, 9, 23, 9, 1, 30), |
|
195 | 235 | dateTime(2013, 9, 23, 9, 2, 30), dateTime(2013, 9, 23, 9, 3, 30), |
|
196 | 236 | dateTime(2013, 9, 23, 9, 4, 30), dateTime(2013, 9, 23, 9, 5, 30), |
|
197 | 237 | dateTime(2013, 9, 23, 9, 6, 30), dateTime(2013, 9, 23, 9, 7, 30), |
|
198 | 238 | dateTime(2013, 9, 23, 9, 8, 30), dateTime(2013, 9, 23, 9, 9, 30)}) |
|
199 | 239 | .setValuesData({-2.83950, -2.71850, -2.52150, -2.57633, -2.58050, -2.48325, -2.63025, |
|
200 | 240 | -2.55800, -2.43250, -2.42200}); |
|
201 | 241 | |
|
202 | 242 | QTest::newRow("Valid file (value of first line is invalid but it is converted to NaN") |
|
203 | 243 | << QStringLiteral("WrongValue.txt") |
|
204 | 244 | << ExpectedResults<ScalarSeries>{} |
|
205 | 245 | .setParsingOK(true) |
|
206 | 246 | .setXAxisUnit(Unit{"nT", true}) |
|
207 | 247 | .setXAxisData({dateTime(2013, 9, 23, 9, 0, 30), dateTime(2013, 9, 23, 9, 1, 30), |
|
208 | 248 | dateTime(2013, 9, 23, 9, 2, 30)}) |
|
209 | 249 | .setValuesData({std::numeric_limits<double>::quiet_NaN(), -2.71850, -2.52150}); |
|
210 | 250 | |
|
211 | 251 | QTest::newRow("Valid file that contains NaN values") |
|
212 | 252 | << QStringLiteral("NaNValue.txt") |
|
213 | 253 | << ExpectedResults<ScalarSeries>{} |
|
214 | 254 | .setParsingOK(true) |
|
215 | 255 | .setXAxisUnit(Unit{("nT"), true}) |
|
216 | 256 | .setXAxisData({dateTime(2013, 9, 23, 9, 0, 30), dateTime(2013, 9, 23, 9, 1, 30), |
|
217 | 257 | dateTime(2013, 9, 23, 9, 2, 30)}) |
|
218 | 258 | .setValuesData({std::numeric_limits<double>::quiet_NaN(), -2.71850, -2.52150}); |
|
219 | 259 | |
|
220 | 260 | // Valid files but with some invalid lines (wrong unit, wrong values, etc.) |
|
221 | 261 | QTest::newRow("No unit file") |
|
222 | 262 | << QStringLiteral("NoUnit.txt") |
|
223 | 263 | << ExpectedResults<ScalarSeries>{}.setParsingOK(true).setXAxisUnit(Unit{"", true}); |
|
224 | 264 | |
|
225 | 265 | QTest::newRow("Wrong unit file") |
|
226 | 266 | << QStringLiteral("WrongUnit.txt") |
|
227 | 267 | << ExpectedResults<ScalarSeries>{} |
|
228 | 268 | .setParsingOK(true) |
|
229 | 269 | .setXAxisUnit(Unit{"", true}) |
|
230 | 270 | .setXAxisData({dateTime(2013, 9, 23, 9, 0, 30), dateTime(2013, 9, 23, 9, 1, 30), |
|
231 | 271 | dateTime(2013, 9, 23, 9, 2, 30)}) |
|
232 | 272 | .setValuesData({-2.83950, -2.71850, -2.52150}); |
|
233 | 273 | |
|
234 | 274 | QTest::newRow("Wrong results file (date of first line is invalid") |
|
235 | 275 | << QStringLiteral("WrongDate.txt") |
|
236 | 276 | << ExpectedResults<ScalarSeries>{} |
|
237 | 277 | .setParsingOK(true) |
|
238 | 278 | .setXAxisUnit(Unit{"nT", true}) |
|
239 | 279 | .setXAxisData({dateTime(2013, 9, 23, 9, 1, 30), dateTime(2013, 9, 23, 9, 2, 30)}) |
|
240 | 280 | .setValuesData({-2.71850, -2.52150}); |
|
241 | 281 | |
|
242 | 282 | QTest::newRow("Wrong results file (too many values for first line") |
|
243 | 283 | << QStringLiteral("TooManyValues.txt") |
|
244 | 284 | << ExpectedResults<ScalarSeries>{} |
|
245 | 285 | .setParsingOK(true) |
|
246 | 286 | .setXAxisUnit(Unit{"nT", true}) |
|
247 | 287 | .setXAxisData({dateTime(2013, 9, 23, 9, 1, 30), dateTime(2013, 9, 23, 9, 2, 30)}) |
|
248 | 288 | .setValuesData({-2.71850, -2.52150}); |
|
249 | 289 | |
|
250 | 290 | QTest::newRow("Wrong results file (x of first line is NaN") |
|
251 | 291 | << QStringLiteral("NaNX.txt") |
|
252 | 292 | << ExpectedResults<ScalarSeries>{} |
|
253 | 293 | .setParsingOK(true) |
|
254 | 294 | .setXAxisUnit(Unit{"nT", true}) |
|
255 | 295 | .setXAxisData({dateTime(2013, 9, 23, 9, 1, 30), dateTime(2013, 9, 23, 9, 2, 30)}) |
|
256 | 296 | .setValuesData({-2.71850, -2.52150}); |
|
257 | 297 | |
|
258 | 298 | QTest::newRow("Invalid file type (vector)") |
|
259 | 299 | << QStringLiteral("ValidVector1.txt") |
|
260 | 300 | << ExpectedResults<ScalarSeries>{}.setParsingOK(true).setXAxisUnit(Unit{"nT", true}); |
|
261 | 301 | |
|
262 | 302 | // Invalid files |
|
263 | 303 | QTest::newRow("Invalid file (unexisting file)") |
|
264 | 304 | << QStringLiteral("UnexistingFile.txt") |
|
265 | 305 | << ExpectedResults<ScalarSeries>{}.setParsingOK(false); |
|
266 | 306 | |
|
267 | 307 | QTest::newRow("Invalid file (file not found on server)") |
|
268 | 308 | << QStringLiteral("FileNotFound.txt") |
|
269 | 309 | << ExpectedResults<ScalarSeries>{}.setParsingOK(false); |
|
270 | 310 | } |
|
271 | 311 | |
|
272 | 312 | void TestAmdaResultParser::testReadScalarTxt() |
|
273 | 313 | { |
|
274 | 314 | testRead<ScalarSeries>(AmdaResultParser::ValueType::SCALAR); |
|
275 | 315 | } |
|
276 | 316 | |
|
277 | 317 | void TestAmdaResultParser::testReadVectorTxt_data() |
|
278 | 318 | { |
|
279 | 319 | testReadDataStructure<VectorSeries>(); |
|
280 | 320 | |
|
281 | 321 | // ////////// // |
|
282 | 322 | // Test cases // |
|
283 | 323 | // ////////// // |
|
284 | 324 | |
|
285 | 325 | // Valid files |
|
286 | 326 | QTest::newRow("Valid file") |
|
287 | 327 | << QStringLiteral("ValidVector1.txt") |
|
288 | 328 | << ExpectedResults<VectorSeries>{} |
|
289 | 329 | .setParsingOK(true) |
|
290 | 330 | .setXAxisUnit(Unit{"nT", true}) |
|
291 | 331 | .setXAxisData({dateTime(2013, 7, 2, 9, 13, 50), dateTime(2013, 7, 2, 9, 14, 6), |
|
292 | 332 | dateTime(2013, 7, 2, 9, 14, 22), dateTime(2013, 7, 2, 9, 14, 38), |
|
293 | 333 | dateTime(2013, 7, 2, 9, 14, 54), dateTime(2013, 7, 2, 9, 15, 10), |
|
294 | 334 | dateTime(2013, 7, 2, 9, 15, 26), dateTime(2013, 7, 2, 9, 15, 42), |
|
295 | 335 | dateTime(2013, 7, 2, 9, 15, 58), dateTime(2013, 7, 2, 9, 16, 14)}) |
|
296 | 336 | .setValuesData( |
|
297 | 337 | {{-0.332, -1.011, -1.457, -1.293, -1.217, -1.443, -1.278, -1.202, -1.22, -1.259}, |
|
298 | 338 | {3.206, 2.999, 2.785, 2.736, 2.612, 2.564, 2.892, 2.862, 2.859, 2.764}, |
|
299 | 339 | {0.058, 0.496, 1.018, 1.485, 1.662, 1.505, 1.168, 1.244, 1.15, 1.358}}); |
|
300 | 340 | |
|
301 | 341 | // Valid files but with some invalid lines (wrong unit, wrong values, etc.) |
|
302 | 342 | QTest::newRow("Invalid file type (scalar)") |
|
303 | 343 | << QStringLiteral("ValidScalar1.txt") |
|
304 | 344 | << ExpectedResults<VectorSeries>{} |
|
305 | 345 | .setParsingOK(true) |
|
306 | 346 | .setXAxisUnit(Unit{"nT", true}) |
|
307 | 347 | .setXAxisData({}) |
|
308 | 348 | .setValuesData(QVector<QVector<double> >{{}, {}, {}}); |
|
309 | 349 | } |
|
310 | 350 | |
|
311 | 351 | void TestAmdaResultParser::testReadVectorTxt() |
|
312 | 352 | { |
|
313 | 353 | testRead<VectorSeries>(AmdaResultParser::ValueType::VECTOR); |
|
314 | 354 | } |
|
315 | 355 | |
|
316 | 356 | QTEST_MAIN(TestAmdaResultParser) |
|
317 | 357 | #include "TestAmdaResultParser.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now