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