##// END OF EJS Templates
Adds Variable::realRange() test
Alexandre Leroux -
r801:3e9153eacdd5
parent child
Show More
@@ -7,6 +7,32
7
7
8 #include <memory>
8 #include <memory>
9
9
10 namespace {
11
12 /// Generates a date in double
13 auto date = [](int year, int month, int day, int hours, int minutes, int seconds) {
14 return DateUtils::secondsSinceEpoch(
15 QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC});
16 };
17
18 /// Generates a series of test data for a range
19 std::shared_ptr<ScalarSeries> dataSeries(const SqpRange &range)
20 {
21 auto xAxisData = std::vector<double>{};
22 auto valuesData = std::vector<double>{};
23
24 auto value = 0;
25 for (auto x = range.m_TStart; x <= range.m_TEnd; ++x, ++value) {
26 xAxisData.push_back(x);
27 valuesData.push_back(value);
28 }
29
30 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{},
31 Unit{});
32 }
33
34 } // namespace
35
10 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
36 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
11
37
12 class TestVariable : public QObject {
38 class TestVariable : public QObject {
@@ -18,6 +44,9 private slots:
18
44
19 void testNotInCacheRangeList();
45 void testNotInCacheRangeList();
20 void testInCacheRangeList();
46 void testInCacheRangeList();
47
48 void testRealRange_data();
49 void testRealRange();
21 };
50 };
22
51
23 void TestVariable::testClone_data()
52 void TestVariable::testClone_data()
@@ -36,27 +65,6 void TestVariable::testClone_data()
36 // Test cases //
65 // Test cases //
37 // ////////// //
66 // ////////// //
38
67
39 /// Generates a date in double
40 auto date = [](int year, int month, int day, int hours, int minutes, int seconds) {
41 return DateUtils::secondsSinceEpoch(
42 QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC});
43 };
44
45 /// Generates a data series for a range
46 auto dataSeries = [](const SqpRange &range) {
47 auto xAxisData = std::vector<double>{};
48 auto valuesData = std::vector<double>{};
49
50 auto value = 0;
51 for (auto x = range.m_TStart; x < range.m_TEnd; ++x, ++value) {
52 xAxisData.push_back(x);
53 valuesData.push_back(value);
54 }
55
56 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{},
57 Unit{});
58 };
59
60 auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)};
68 auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)};
61 QTest::newRow("clone1") << QStringLiteral("var1")
69 QTest::newRow("clone1") << QStringLiteral("var1")
62 << QVariantHash{{"data1", 1}, {"data2", "abc"}}
70 << QVariantHash{{"data1", 1}, {"data2", "abc"}}
@@ -257,6 +265,76 void TestVariable::testInCacheRangeList()
257 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
265 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
258 }
266 }
259
267
268 namespace {
269
270 /// Struct used to represent a range operation on a variable
271 /// @sa TestVariable::testRealRange()
272 struct RangeOperation {
273 SqpRange m_CacheRange; /// Range to set for the variable
274 std::shared_ptr<ScalarSeries> m_DataSeries; /// Series to merge in the variable
275 SqpRange m_ExpectedRealRange; /// Real Range expected after operation on the variable
276 };
277
278 using RangeOperations = std::vector<RangeOperation>;
279
280 } // namespace
281
282 Q_DECLARE_METATYPE(RangeOperations)
283
284 void TestVariable::testRealRange_data()
285 {
286 // ////////////// //
287 // Test structure //
288 // ////////////// //
289
290 QTest::addColumn<RangeOperations>("operations");
291
292 // ////////// //
293 // Test cases //
294 // ////////// //
295 RangeOperations operations{};
296
297 // Inits cache range and data series (expected real range = cache range)
298 auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)};
299 operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange});
300
301 // Changes cache range and updates data series (expected real range = cache range)
302 cacheRange = SqpRange{date(2017, 1, 1, 14, 0, 0), date(2017, 1, 1, 15, 0, 0)};
303 operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange});
304
305 // Changes cache range and update data series but with a lower range (expected real range =
306 // data series range)
307 cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 16, 0, 0)};
308 auto dataSeriesRange = SqpRange{date(2017, 1, 1, 14, 0, 0), date(2017, 1, 1, 15, 0, 0)};
309 operations.push_back({cacheRange, dataSeries(dataSeriesRange), dataSeriesRange});
310
311 // Changes cache range but DON'T update data series (expected real range = cache range
312 // before operation)
313 cacheRange = SqpRange{date(2017, 1, 1, 10, 0, 0), date(2017, 1, 1, 17, 0, 0)};
314 operations.push_back({cacheRange, nullptr, dataSeriesRange});
315
316 QTest::newRow("realRange1") << operations;
317 }
318
319 void TestVariable::testRealRange()
320 {
321 // Creates variable (real range is invalid)
322 Variable variable{"var"};
323 QCOMPARE(variable.realRange(), INVALID_RANGE);
324
325 QFETCH(RangeOperations, operations);
326 for (const auto &operation : operations) {
327 // Sets cache range and merge data series
328 variable.setCacheRange(operation.m_CacheRange);
329 if (operation.m_DataSeries != nullptr) {
330 variable.mergeDataSeries(operation.m_DataSeries);
331 }
332
333 // Checks real range
334 QCOMPARE(variable.realRange(), operation.m_ExpectedRealRange);
335 }
336 }
337
260
338
261 QTEST_MAIN(TestVariable)
339 QTEST_MAIN(TestVariable)
262 #include "TestVariable.moc"
340 #include "TestVariable.moc"
General Comments 0
You need to be logged in to leave comments. Login now