##// END OF EJS Templates
Adds Variable::realRange() test
Alexandre Leroux -
r801:3e9153eacdd5
parent child
Show More
@@ -1,262 +1,340
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 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 36 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
11 37
12 38 class TestVariable : public QObject {
13 39 Q_OBJECT
14 40
15 41 private slots:
16 42 void testClone_data();
17 43 void testClone();
18 44
19 45 void testNotInCacheRangeList();
20 46 void testInCacheRangeList();
47
48 void testRealRange_data();
49 void testRealRange();
21 50 };
22 51
23 52 void TestVariable::testClone_data()
24 53 {
25 54 // ////////////// //
26 55 // Test structure //
27 56 // ////////////// //
28 57
29 58 QTest::addColumn<QString>("name");
30 59 QTest::addColumn<QVariantHash>("metadata");
31 60 QTest::addColumn<SqpRange>("range");
32 61 QTest::addColumn<SqpRange>("cacheRange");
33 62 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
34 63
35 64 // ////////// //
36 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 68 auto cacheRange = SqpRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)};
61 69 QTest::newRow("clone1") << QStringLiteral("var1")
62 70 << QVariantHash{{"data1", 1}, {"data2", "abc"}}
63 71 << SqpRange{date(2017, 1, 1, 12, 30, 0), (date(2017, 1, 1, 12, 45, 0))}
64 72 << cacheRange << dataSeries(cacheRange);
65 73 }
66 74
67 75 void TestVariable::testClone()
68 76 {
69 77 // Creates variable
70 78 QFETCH(QString, name);
71 79 QFETCH(QVariantHash, metadata);
72 80 QFETCH(SqpRange, range);
73 81 QFETCH(SqpRange, cacheRange);
74 82 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
75 83
76 84 Variable variable{name, metadata};
77 85 variable.setRange(range);
78 86 variable.setCacheRange(cacheRange);
79 87 variable.mergeDataSeries(dataSeries);
80 88
81 89 // Clones variable
82 90 auto clone = variable.clone();
83 91
84 92 // Checks cloned variable's state
85 93 QCOMPARE(clone->name(), name);
86 94 QCOMPARE(clone->metadata(), metadata);
87 95 QCOMPARE(clone->range(), range);
88 96 QCOMPARE(clone->cacheRange(), cacheRange);
89 97
90 98 // Compares data series
91 99 if (dataSeries != nullptr) {
92 100 QVERIFY(clone->dataSeries() != nullptr);
93 101 QVERIFY(std::equal(dataSeries->cbegin(), dataSeries->cend(), clone->dataSeries()->cbegin(),
94 102 clone->dataSeries()->cend(), [](const auto &it1, const auto &it2) {
95 103 return it1.x() == it2.x() && it1.value() == it2.value();
96 104 }));
97 105 }
98 106 else {
99 107 QVERIFY(clone->dataSeries() == nullptr);
100 108 }
101 109 }
102 110
103 111 void TestVariable::testNotInCacheRangeList()
104 112 {
105 113 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
106 114 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
107 115
108 116 auto sqpR = SqpRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)};
109 117
110 118 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
111 119 auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
112 120
113 121 auto sqpCR
114 122 = SqpRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)};
115 123
116 124 Variable var{"Var test"};
117 125 var.setRange(sqpR);
118 126 var.setCacheRange(sqpCR);
119 127
120 128 // 1: [ts,te] < varTS
121 129 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
122 130 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
123 131 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
124 132
125 133 auto notInCach = var.provideNotInCacheRangeList(sqp);
126 134
127 135 QCOMPARE(notInCach.size(), 1);
128 136
129 137 auto notInCachRange = notInCach.first();
130 138
131 139 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
132 140 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
133 141
134 142 // 2: ts < varTS < te < varTE
135 143 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
136 144 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
137 145 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
138 146 notInCach = var.provideNotInCacheRangeList(sqp);
139 147 QCOMPARE(notInCach.size(), 1);
140 148 notInCachRange = notInCach.first();
141 149 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
142 150 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS));
143 151
144 152 // 3: varTS < ts < te < varTE
145 153 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
146 154 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
147 155 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
148 156 notInCach = var.provideNotInCacheRangeList(sqp);
149 157 QCOMPARE(notInCach.size(), 0);
150 158
151 159
152 160 // 4: varTS < ts < varTE < te
153 161 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
154 162 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
155 163 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
156 164 notInCach = var.provideNotInCacheRangeList(sqp);
157 165 QCOMPARE(notInCach.size(), 1);
158 166 notInCachRange = notInCach.first();
159 167 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE));
160 168 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
161 169
162 170 // 5: varTS < varTE < ts < te
163 171 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
164 172 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
165 173 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
166 174 notInCach = var.provideNotInCacheRangeList(sqp);
167 175 QCOMPARE(notInCach.size(), 1);
168 176 notInCachRange = notInCach.first();
169 177 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
170 178 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
171 179
172 180 // 6: ts <varTS < varTE < te
173 181 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
174 182 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
175 183 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
176 184 notInCach = var.provideNotInCacheRangeList(sqp);
177 185 QCOMPARE(notInCach.size(), 2);
178 186 notInCachRange = notInCach.first();
179 187 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
180 188 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS));
181 189 notInCachRange = notInCach[1];
182 190 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE));
183 191 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
184 192 }
185 193
186 194
187 195 void TestVariable::testInCacheRangeList()
188 196 {
189 197 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
190 198 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
191 199
192 200 auto sqpR = SqpRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)};
193 201
194 202 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
195 203 auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
196 204 auto sqpCR
197 205 = SqpRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)};
198 206
199 207 Variable var{"Var test"};
200 208 var.setRange(sqpR);
201 209 var.setCacheRange(sqpCR);
202 210
203 211 // 1: [ts,te] < varTS
204 212 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
205 213 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
206 214 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
207 215
208 216 auto notInCach = var.provideInCacheRangeList(sqp);
209 217
210 218 QCOMPARE(notInCach.size(), 0);
211 219
212 220 // 2: ts < varTS < te < varTE
213 221 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
214 222 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
215 223 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
216 224 notInCach = var.provideInCacheRangeList(sqp);
217 225 QCOMPARE(notInCach.size(), 1);
218 226 auto notInCachRange = notInCach.first();
219 227 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS));
220 228 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
221 229
222 230 // 3: varTS < ts < te < varTE
223 231 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
224 232 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
225 233 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
226 234 notInCach = var.provideInCacheRangeList(sqp);
227 235 QCOMPARE(notInCach.size(), 1);
228 236 notInCachRange = notInCach.first();
229 237 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
230 238 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
231 239
232 240 // 4: varTS < ts < varTE < te
233 241 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
234 242 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
235 243 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
236 244 notInCach = var.provideInCacheRangeList(sqp);
237 245 QCOMPARE(notInCach.size(), 1);
238 246 notInCachRange = notInCach.first();
239 247 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
240 248 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
241 249
242 250 // 5: varTS < varTE < ts < te
243 251 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
244 252 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
245 253 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
246 254 notInCach = var.provideInCacheRangeList(sqp);
247 255 QCOMPARE(notInCach.size(), 0);
248 256
249 257 // 6: ts <varTS < varTE < te
250 258 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
251 259 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
252 260 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
253 261 notInCach = var.provideInCacheRangeList(sqp);
254 262 QCOMPARE(notInCach.size(), 1);
255 263 notInCachRange = notInCach.first();
256 264 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS));
257 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 339 QTEST_MAIN(TestVariable)
262 340 #include "TestVariable.moc"
General Comments 0
You need to be logged in to leave comments. Login now