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