@@ -1,19 +1,104 | |||||
1 | #include <Variable/Variable.h> |
|
1 | #include <Variable/Variable.h> | |
2 |
|
2 | |||
|
3 | #include <Data/ScalarSeries.h> | |||
|
4 | ||||
3 | #include <QObject> |
|
5 | #include <QObject> | |
4 | #include <QtTest> |
|
6 | #include <QtTest> | |
5 |
|
7 | |||
6 | #include <memory> |
|
8 | #include <memory> | |
7 |
|
9 | |||
|
10 | Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>) | |||
|
11 | ||||
8 | class TestVariable : public QObject { |
|
12 | class TestVariable : public QObject { | |
9 | Q_OBJECT |
|
13 | Q_OBJECT | |
10 |
|
14 | |||
11 | private slots: |
|
15 | private slots: | |
12 |
void test |
|
16 | void testClone_data(); | |
|
17 | void testClone(); | |||
13 |
|
18 | |||
|
19 | void testNotInCacheRangeList(); | |||
14 | void testInCacheRangeList(); |
|
20 | void testInCacheRangeList(); | |
15 | }; |
|
21 | }; | |
16 |
|
22 | |||
|
23 | void TestVariable::testClone_data() | |||
|
24 | { | |||
|
25 | // ////////////// // | |||
|
26 | // Test structure // | |||
|
27 | // ////////////// // | |||
|
28 | ||||
|
29 | QTest::addColumn<QString>("name"); | |||
|
30 | QTest::addColumn<QVariantHash>("metadata"); | |||
|
31 | QTest::addColumn<SqpRange>("range"); | |||
|
32 | QTest::addColumn<SqpRange>("cacheRange"); | |||
|
33 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); | |||
|
34 | ||||
|
35 | // ////////// // | |||
|
36 | // Test cases // | |||
|
37 | // ////////// // | |||
|
38 | ||||
|
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)}; | |||
|
61 | QTest::newRow("clone1") << QStringLiteral("var1") | |||
|
62 | << QVariantHash{{"data1", 1}, {"data2", "abc"}} | |||
|
63 | << SqpRange{date(2017, 1, 1, 12, 30, 0), (date(2017, 1, 1, 12, 45, 0))} | |||
|
64 | << cacheRange << dataSeries(cacheRange); | |||
|
65 | } | |||
|
66 | ||||
|
67 | void TestVariable::testClone() | |||
|
68 | { | |||
|
69 | // Creates variable | |||
|
70 | QFETCH(QString, name); | |||
|
71 | QFETCH(QVariantHash, metadata); | |||
|
72 | QFETCH(SqpRange, range); | |||
|
73 | QFETCH(SqpRange, cacheRange); | |||
|
74 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); | |||
|
75 | ||||
|
76 | Variable variable{name, metadata}; | |||
|
77 | variable.setRange(range); | |||
|
78 | variable.setCacheRange(cacheRange); | |||
|
79 | variable.mergeDataSeries(dataSeries); | |||
|
80 | ||||
|
81 | // Clones variable | |||
|
82 | auto clone = variable.clone(); | |||
|
83 | ||||
|
84 | // Checks cloned variable's state | |||
|
85 | QCOMPARE(clone->name(), name); | |||
|
86 | QCOMPARE(clone->metadata(), metadata); | |||
|
87 | QCOMPARE(clone->range(), range); | |||
|
88 | QCOMPARE(clone->cacheRange(), cacheRange); | |||
|
89 | ||||
|
90 | // Compares data series | |||
|
91 | if (dataSeries != nullptr) { | |||
|
92 | QVERIFY(clone->dataSeries() != nullptr); | |||
|
93 | QVERIFY(std::equal(dataSeries->cbegin(), dataSeries->cend(), clone->dataSeries()->cbegin(), | |||
|
94 | clone->dataSeries()->cend(), [](const auto &it1, const auto &it2) { | |||
|
95 | return it1.x() == it2.x() && it1.value() == it2.value(); | |||
|
96 | })); | |||
|
97 | } | |||
|
98 | else { | |||
|
99 | QVERIFY(clone->dataSeries() == nullptr); | |||
|
100 | } | |||
|
101 | } | |||
17 |
|
102 | |||
18 | void TestVariable::testNotInCacheRangeList() |
|
103 | void TestVariable::testNotInCacheRangeList() | |
19 | { |
|
104 | { |
General Comments 0
You need to be logged in to leave comments.
Login now