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