##// END OF EJS Templates
Adds Variable::nbPoints() test
Alexandre Leroux -
r802:605c8cb87267
parent child
Show More
@@ -1,340 +1,409
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2
2
3 #include <Data/ScalarSeries.h>
3 #include <Data/ScalarSeries.h>
4
4
5 #include <QObject>
5 #include <QObject>
6 #include <QtTest>
6 #include <QtTest>
7
7
8 #include <memory>
8 #include <memory>
9
9
10 namespace {
10 namespace {
11
11
12 /// Generates a date in double
12 /// Generates a date in double
13 auto date = [](int year, int month, int day, int hours, int minutes, int seconds) {
13 auto date = [](int year, int month, int day, int hours, int minutes, int seconds) {
14 return DateUtils::secondsSinceEpoch(
14 return DateUtils::secondsSinceEpoch(
15 QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC});
15 QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC});
16 };
16 };
17
17
18 /// Generates a series of test data for a range
18 /// Generates a series of test data for a range
19 std::shared_ptr<ScalarSeries> dataSeries(const SqpRange &range)
19 std::shared_ptr<ScalarSeries> dataSeries(const SqpRange &range)
20 {
20 {
21 auto xAxisData = std::vector<double>{};
21 auto xAxisData = std::vector<double>{};
22 auto valuesData = std::vector<double>{};
22 auto valuesData = std::vector<double>{};
23
23
24 auto value = 0;
24 auto value = 0;
25 for (auto x = range.m_TStart; x <= range.m_TEnd; ++x, ++value) {
25 for (auto x = range.m_TStart; x <= range.m_TEnd; ++x, ++value) {
26 xAxisData.push_back(x);
26 xAxisData.push_back(x);
27 valuesData.push_back(value);
27 valuesData.push_back(value);
28 }
28 }
29
29
30 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{},
30 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{},
31 Unit{});
31 Unit{});
32 }
32 }
33
33
34 } // namespace
34 } // namespace
35
35
36 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
36 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
37
37
38 class TestVariable : public QObject {
38 class TestVariable : public QObject {
39 Q_OBJECT
39 Q_OBJECT
40
40
41 private slots:
41 private slots:
42 void testClone_data();
42 void testClone_data();
43 void testClone();
43 void testClone();
44
44
45 void testNotInCacheRangeList();
45 void testNotInCacheRangeList();
46 void testInCacheRangeList();
46 void testInCacheRangeList();
47
47
48 void testNbPoints_data();
49 void testNbPoints();
50
48 void testRealRange_data();
51 void testRealRange_data();
49 void testRealRange();
52 void testRealRange();
50 };
53 };
51
54
52 void TestVariable::testClone_data()
55 void TestVariable::testClone_data()
53 {
56 {
54 // ////////////// //
57 // ////////////// //
55 // Test structure //
58 // Test structure //
56 // ////////////// //
59 // ////////////// //
57
60
58 QTest::addColumn<QString>("name");
61 QTest::addColumn<QString>("name");
59 QTest::addColumn<QVariantHash>("metadata");
62 QTest::addColumn<QVariantHash>("metadata");
60 QTest::addColumn<SqpRange>("range");
63 QTest::addColumn<SqpRange>("range");
61 QTest::addColumn<SqpRange>("cacheRange");
64 QTest::addColumn<SqpRange>("cacheRange");
62 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
65 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
63
66
64 // ////////// //
67 // ////////// //
65 // Test cases //
68 // Test cases //
66 // ////////// //
69 // ////////// //
67
70
68 auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)};
71 auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)};
69 QTest::newRow("clone1") << QStringLiteral("var1")
72 QTest::newRow("clone1") << QStringLiteral("var1")
70 << QVariantHash{{"data1", 1}, {"data2", "abc"}}
73 << QVariantHash{{"data1", 1}, {"data2", "abc"}}
71 << SqpRange{date(2017, 1, 1, 12, 30, 0), (date(2017, 1, 1, 12, 45, 0))}
74 << SqpRange{date(2017, 1, 1, 12, 30, 0), (date(2017, 1, 1, 12, 45, 0))}
72 << cacheRange << dataSeries(cacheRange);
75 << cacheRange << dataSeries(cacheRange);
73 }
76 }
74
77
75 void TestVariable::testClone()
78 void TestVariable::testClone()
76 {
79 {
77 // Creates variable
80 // Creates variable
78 QFETCH(QString, name);
81 QFETCH(QString, name);
79 QFETCH(QVariantHash, metadata);
82 QFETCH(QVariantHash, metadata);
80 QFETCH(SqpRange, range);
83 QFETCH(SqpRange, range);
81 QFETCH(SqpRange, cacheRange);
84 QFETCH(SqpRange, cacheRange);
82 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
85 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
83
86
84 Variable variable{name, metadata};
87 Variable variable{name, metadata};
85 variable.setRange(range);
88 variable.setRange(range);
86 variable.setCacheRange(cacheRange);
89 variable.setCacheRange(cacheRange);
87 variable.mergeDataSeries(dataSeries);
90 variable.mergeDataSeries(dataSeries);
88
91
89 // Clones variable
92 // Clones variable
90 auto clone = variable.clone();
93 auto clone = variable.clone();
91
94
92 // Checks cloned variable's state
95 // Checks cloned variable's state
93 QCOMPARE(clone->name(), name);
96 QCOMPARE(clone->name(), name);
94 QCOMPARE(clone->metadata(), metadata);
97 QCOMPARE(clone->metadata(), metadata);
95 QCOMPARE(clone->range(), range);
98 QCOMPARE(clone->range(), range);
96 QCOMPARE(clone->cacheRange(), cacheRange);
99 QCOMPARE(clone->cacheRange(), cacheRange);
97
100
98 // Compares data series
101 // Compares data series
99 if (dataSeries != nullptr) {
102 if (dataSeries != nullptr) {
100 QVERIFY(clone->dataSeries() != nullptr);
103 QVERIFY(clone->dataSeries() != nullptr);
101 QVERIFY(std::equal(dataSeries->cbegin(), dataSeries->cend(), clone->dataSeries()->cbegin(),
104 QVERIFY(std::equal(dataSeries->cbegin(), dataSeries->cend(), clone->dataSeries()->cbegin(),
102 clone->dataSeries()->cend(), [](const auto &it1, const auto &it2) {
105 clone->dataSeries()->cend(), [](const auto &it1, const auto &it2) {
103 return it1.x() == it2.x() && it1.value() == it2.value();
106 return it1.x() == it2.x() && it1.value() == it2.value();
104 }));
107 }));
105 }
108 }
106 else {
109 else {
107 QVERIFY(clone->dataSeries() == nullptr);
110 QVERIFY(clone->dataSeries() == nullptr);
108 }
111 }
109 }
112 }
110
113
111 void TestVariable::testNotInCacheRangeList()
114 void TestVariable::testNotInCacheRangeList()
112 {
115 {
113 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
116 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
114 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
117 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
115
118
116 auto sqpR = SqpRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)};
119 auto sqpR = SqpRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)};
117
120
118 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
121 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
119 auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
122 auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
120
123
121 auto sqpCR
124 auto sqpCR
122 = SqpRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)};
125 = SqpRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)};
123
126
124 Variable var{"Var test"};
127 Variable var{"Var test"};
125 var.setRange(sqpR);
128 var.setRange(sqpR);
126 var.setCacheRange(sqpCR);
129 var.setCacheRange(sqpCR);
127
130
128 // 1: [ts,te] < varTS
131 // 1: [ts,te] < varTS
129 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
132 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
130 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
133 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
131 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
134 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
132
135
133 auto notInCach = var.provideNotInCacheRangeList(sqp);
136 auto notInCach = var.provideNotInCacheRangeList(sqp);
134
137
135 QCOMPARE(notInCach.size(), 1);
138 QCOMPARE(notInCach.size(), 1);
136
139
137 auto notInCachRange = notInCach.first();
140 auto notInCachRange = notInCach.first();
138
141
139 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
142 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
140 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
143 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
141
144
142 // 2: ts < varTS < te < varTE
145 // 2: ts < varTS < te < varTE
143 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
146 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
144 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
147 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
145 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
148 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
146 notInCach = var.provideNotInCacheRangeList(sqp);
149 notInCach = var.provideNotInCacheRangeList(sqp);
147 QCOMPARE(notInCach.size(), 1);
150 QCOMPARE(notInCach.size(), 1);
148 notInCachRange = notInCach.first();
151 notInCachRange = notInCach.first();
149 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
152 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
150 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS));
153 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS));
151
154
152 // 3: varTS < ts < te < varTE
155 // 3: varTS < ts < te < varTE
153 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
156 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
154 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
157 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
155 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
158 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
156 notInCach = var.provideNotInCacheRangeList(sqp);
159 notInCach = var.provideNotInCacheRangeList(sqp);
157 QCOMPARE(notInCach.size(), 0);
160 QCOMPARE(notInCach.size(), 0);
158
161
159
162
160 // 4: varTS < ts < varTE < te
163 // 4: varTS < ts < varTE < te
161 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
164 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
162 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
165 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
163 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
166 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
164 notInCach = var.provideNotInCacheRangeList(sqp);
167 notInCach = var.provideNotInCacheRangeList(sqp);
165 QCOMPARE(notInCach.size(), 1);
168 QCOMPARE(notInCach.size(), 1);
166 notInCachRange = notInCach.first();
169 notInCachRange = notInCach.first();
167 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE));
170 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE));
168 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
171 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
169
172
170 // 5: varTS < varTE < ts < te
173 // 5: varTS < varTE < ts < te
171 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
174 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
172 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
175 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
173 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
176 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
174 notInCach = var.provideNotInCacheRangeList(sqp);
177 notInCach = var.provideNotInCacheRangeList(sqp);
175 QCOMPARE(notInCach.size(), 1);
178 QCOMPARE(notInCach.size(), 1);
176 notInCachRange = notInCach.first();
179 notInCachRange = notInCach.first();
177 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
180 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
178 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
181 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
179
182
180 // 6: ts <varTS < varTE < te
183 // 6: ts <varTS < varTE < te
181 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
184 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
182 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
185 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
183 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
186 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
184 notInCach = var.provideNotInCacheRangeList(sqp);
187 notInCach = var.provideNotInCacheRangeList(sqp);
185 QCOMPARE(notInCach.size(), 2);
188 QCOMPARE(notInCach.size(), 2);
186 notInCachRange = notInCach.first();
189 notInCachRange = notInCach.first();
187 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
190 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
188 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS));
191 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS));
189 notInCachRange = notInCach[1];
192 notInCachRange = notInCach[1];
190 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE));
193 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE));
191 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
194 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
192 }
195 }
193
196
194
197
195 void TestVariable::testInCacheRangeList()
198 void TestVariable::testInCacheRangeList()
196 {
199 {
197 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
200 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
198 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
201 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
199
202
200 auto sqpR = SqpRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)};
203 auto sqpR = SqpRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)};
201
204
202 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
205 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
203 auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
206 auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
204 auto sqpCR
207 auto sqpCR
205 = SqpRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)};
208 = SqpRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)};
206
209
207 Variable var{"Var test"};
210 Variable var{"Var test"};
208 var.setRange(sqpR);
211 var.setRange(sqpR);
209 var.setCacheRange(sqpCR);
212 var.setCacheRange(sqpCR);
210
213
211 // 1: [ts,te] < varTS
214 // 1: [ts,te] < varTS
212 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
215 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
213 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
216 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
214 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
217 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
215
218
216 auto notInCach = var.provideInCacheRangeList(sqp);
219 auto notInCach = var.provideInCacheRangeList(sqp);
217
220
218 QCOMPARE(notInCach.size(), 0);
221 QCOMPARE(notInCach.size(), 0);
219
222
220 // 2: ts < varTS < te < varTE
223 // 2: ts < varTS < te < varTE
221 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
224 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
222 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
225 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
223 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
226 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
224 notInCach = var.provideInCacheRangeList(sqp);
227 notInCach = var.provideInCacheRangeList(sqp);
225 QCOMPARE(notInCach.size(), 1);
228 QCOMPARE(notInCach.size(), 1);
226 auto notInCachRange = notInCach.first();
229 auto notInCachRange = notInCach.first();
227 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS));
230 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS));
228 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
231 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
229
232
230 // 3: varTS < ts < te < varTE
233 // 3: varTS < ts < te < varTE
231 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
234 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
232 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
235 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
233 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
236 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
234 notInCach = var.provideInCacheRangeList(sqp);
237 notInCach = var.provideInCacheRangeList(sqp);
235 QCOMPARE(notInCach.size(), 1);
238 QCOMPARE(notInCach.size(), 1);
236 notInCachRange = notInCach.first();
239 notInCachRange = notInCach.first();
237 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
240 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
238 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
241 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
239
242
240 // 4: varTS < ts < varTE < te
243 // 4: varTS < ts < varTE < te
241 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
244 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
242 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
245 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
243 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
246 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
244 notInCach = var.provideInCacheRangeList(sqp);
247 notInCach = var.provideInCacheRangeList(sqp);
245 QCOMPARE(notInCach.size(), 1);
248 QCOMPARE(notInCach.size(), 1);
246 notInCachRange = notInCach.first();
249 notInCachRange = notInCach.first();
247 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
250 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
248 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
251 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
249
252
250 // 5: varTS < varTE < ts < te
253 // 5: varTS < varTE < ts < te
251 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
254 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
252 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
255 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
253 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
256 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
254 notInCach = var.provideInCacheRangeList(sqp);
257 notInCach = var.provideInCacheRangeList(sqp);
255 QCOMPARE(notInCach.size(), 0);
258 QCOMPARE(notInCach.size(), 0);
256
259
257 // 6: ts <varTS < varTE < te
260 // 6: ts <varTS < varTE < te
258 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
261 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
259 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
262 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
260 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
263 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
261 notInCach = var.provideInCacheRangeList(sqp);
264 notInCach = var.provideInCacheRangeList(sqp);
262 QCOMPARE(notInCach.size(), 1);
265 QCOMPARE(notInCach.size(), 1);
263 notInCachRange = notInCach.first();
266 notInCachRange = notInCach.first();
264 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS));
267 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS));
265 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
268 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
266 }
269 }
267
270
268 namespace {
271 namespace {
269
272
273 /// Struct used to represent an operation for @sa TestVariable::testNbPoints()
274 struct NbPointsOperation {
275 SqpRange m_CacheRange; /// Range to set for the variable
276 std::shared_ptr<ScalarSeries> m_DataSeries; /// Series to merge in the variable
277 int m_ExpectedNbPoints; /// Number of points in the variable expected after operation
278 };
279
280 using NbPointsOperations = std::vector<NbPointsOperation>;
281
282 } // namespace
283
284 Q_DECLARE_METATYPE(NbPointsOperations)
285
286 void TestVariable::testNbPoints_data()
287 {
288 // ////////////// //
289 // Test structure //
290 // ////////////// //
291
292 QTest::addColumn<NbPointsOperations>("operations");
293
294 // ////////// //
295 // Test cases //
296 // ////////// //
297 NbPointsOperations operations{};
298
299 // Sets cache range (expected nb points = series xAxis data + series values data)
300 auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 12, 0, 9)};
301 operations.push_back({cacheRange, dataSeries(cacheRange), 20});
302
303 // Doubles cache but don't add data series (expected nb points don't change)
304 cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 12, 0, 19)};
305 operations.push_back({cacheRange, nullptr, 20});
306
307 // Doubles cache and data series (expected nb points change)
308 cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 12, 0, 19)};
309 operations.push_back({cacheRange, dataSeries(cacheRange), 40});
310
311 // Decreases cache (expected nb points decreases as the series is purged)
312 cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 5), date(2017, 1, 1, 12, 0, 9)};
313 operations.push_back({cacheRange, nullptr, 10});
314
315 QTest::newRow("nbPoints1") << operations;
316 }
317
318 void TestVariable::testNbPoints()
319 {
320 // Creates variable
321 Variable variable{"var"};
322 QCOMPARE(variable.nbPoints(), 0);
323
324 QFETCH(NbPointsOperations, operations);
325 for (const auto &operation : operations) {
326 // Sets cache range and merge data series
327 variable.setCacheRange(operation.m_CacheRange);
328 if (operation.m_DataSeries != nullptr) {
329 variable.mergeDataSeries(operation.m_DataSeries);
330 }
331
332 // Checks nb points
333 QCOMPARE(variable.nbPoints(), operation.m_ExpectedNbPoints);
334 }
335 }
336
337 namespace {
338
270 /// Struct used to represent a range operation on a variable
339 /// Struct used to represent a range operation on a variable
271 /// @sa TestVariable::testRealRange()
340 /// @sa TestVariable::testRealRange()
272 struct RangeOperation {
341 struct RangeOperation {
273 SqpRange m_CacheRange; /// Range to set for the variable
342 SqpRange m_CacheRange; /// Range to set for the variable
274 std::shared_ptr<ScalarSeries> m_DataSeries; /// Series to merge in the variable
343 std::shared_ptr<ScalarSeries> m_DataSeries; /// Series to merge in the variable
275 SqpRange m_ExpectedRealRange; /// Real Range expected after operation on the variable
344 SqpRange m_ExpectedRealRange; /// Real Range expected after operation on the variable
276 };
345 };
277
346
278 using RangeOperations = std::vector<RangeOperation>;
347 using RangeOperations = std::vector<RangeOperation>;
279
348
280 } // namespace
349 } // namespace
281
350
282 Q_DECLARE_METATYPE(RangeOperations)
351 Q_DECLARE_METATYPE(RangeOperations)
283
352
284 void TestVariable::testRealRange_data()
353 void TestVariable::testRealRange_data()
285 {
354 {
286 // ////////////// //
355 // ////////////// //
287 // Test structure //
356 // Test structure //
288 // ////////////// //
357 // ////////////// //
289
358
290 QTest::addColumn<RangeOperations>("operations");
359 QTest::addColumn<RangeOperations>("operations");
291
360
292 // ////////// //
361 // ////////// //
293 // Test cases //
362 // Test cases //
294 // ////////// //
363 // ////////// //
295 RangeOperations operations{};
364 RangeOperations operations{};
296
365
297 // Inits cache range and data series (expected real range = cache range)
366 // 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)};
367 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});
368 operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange});
300
369
301 // Changes cache range and updates data series (expected real range = cache range)
370 // 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)};
371 cacheRange = SqpRange{date(2017, 1, 1, 14, 0, 0), date(2017, 1, 1, 15, 0, 0)};
303 operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange});
372 operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange});
304
373
305 // Changes cache range and update data series but with a lower range (expected real range =
374 // Changes cache range and update data series but with a lower range (expected real range =
306 // data series range)
375 // data series range)
307 cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 16, 0, 0)};
376 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)};
377 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});
378 operations.push_back({cacheRange, dataSeries(dataSeriesRange), dataSeriesRange});
310
379
311 // Changes cache range but DON'T update data series (expected real range = cache range
380 // Changes cache range but DON'T update data series (expected real range = cache range
312 // before operation)
381 // before operation)
313 cacheRange = SqpRange{date(2017, 1, 1, 10, 0, 0), date(2017, 1, 1, 17, 0, 0)};
382 cacheRange = SqpRange{date(2017, 1, 1, 10, 0, 0), date(2017, 1, 1, 17, 0, 0)};
314 operations.push_back({cacheRange, nullptr, dataSeriesRange});
383 operations.push_back({cacheRange, nullptr, dataSeriesRange});
315
384
316 QTest::newRow("realRange1") << operations;
385 QTest::newRow("realRange1") << operations;
317 }
386 }
318
387
319 void TestVariable::testRealRange()
388 void TestVariable::testRealRange()
320 {
389 {
321 // Creates variable (real range is invalid)
390 // Creates variable (real range is invalid)
322 Variable variable{"var"};
391 Variable variable{"var"};
323 QCOMPARE(variable.realRange(), INVALID_RANGE);
392 QCOMPARE(variable.realRange(), INVALID_RANGE);
324
393
325 QFETCH(RangeOperations, operations);
394 QFETCH(RangeOperations, operations);
326 for (const auto &operation : operations) {
395 for (const auto &operation : operations) {
327 // Sets cache range and merge data series
396 // Sets cache range and merge data series
328 variable.setCacheRange(operation.m_CacheRange);
397 variable.setCacheRange(operation.m_CacheRange);
329 if (operation.m_DataSeries != nullptr) {
398 if (operation.m_DataSeries != nullptr) {
330 variable.mergeDataSeries(operation.m_DataSeries);
399 variable.mergeDataSeries(operation.m_DataSeries);
331 }
400 }
332
401
333 // Checks real range
402 // Checks real range
334 QCOMPARE(variable.realRange(), operation.m_ExpectedRealRange);
403 QCOMPARE(variable.realRange(), operation.m_ExpectedRealRange);
335 }
404 }
336 }
405 }
337
406
338
407
339 QTEST_MAIN(TestVariable)
408 QTEST_MAIN(TestVariable)
340 #include "TestVariable.moc"
409 #include "TestVariable.moc"
General Comments 0
You need to be logged in to leave comments. Login now