##// END OF EJS Templates
Add test for cach providing methods of variable
perrinel -
r589:8e056a311468
parent child
Show More
@@ -0,0 +1,188
1 #include <Variable/Variable.h>
2
3 #include <QObject>
4 #include <QtTest>
5
6 #include <memory>
7
8 class TestVariable : public QObject {
9 Q_OBJECT
10
11 private slots:
12 void testNotInCacheRangeList();
13
14 void testInCacheRangeList();
15 };
16
17
18 void TestVariable::testNotInCacheRangeList()
19 {
20 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
21 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
22
23 auto sqpR = SqpRange{static_cast<double>(varRS.toMSecsSinceEpoch()),
24 static_cast<double>(varRE.toMSecsSinceEpoch())};
25
26 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
27 auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
28 auto sqpCR = SqpRange{static_cast<double>(varCRS.toMSecsSinceEpoch()),
29 static_cast<double>(varCRE.toMSecsSinceEpoch())};
30
31 Variable var{"Var test", sqpR};
32 var.setCacheRange(sqpCR);
33
34 // 1: [ts,te] < varTS
35 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
36 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
37 auto sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
38 static_cast<double>(te.toMSecsSinceEpoch())};
39
40 auto notInCach = var.provideNotInCacheRangeList(sqp);
41
42 QCOMPARE(notInCach.size(), 1);
43
44 auto notInCachRange = notInCach.first();
45
46 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
47 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
48
49 // 2: ts < varTS < te < varTE
50 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
51 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
52 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
53 static_cast<double>(te.toMSecsSinceEpoch())};
54 notInCach = var.provideNotInCacheRangeList(sqp);
55 QCOMPARE(notInCach.size(), 1);
56 notInCachRange = notInCach.first();
57 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
58 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(varCRS.toMSecsSinceEpoch()));
59
60 // 3: varTS < ts < te < varTE
61 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
62 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
63 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
64 static_cast<double>(te.toMSecsSinceEpoch())};
65 notInCach = var.provideNotInCacheRangeList(sqp);
66 QCOMPARE(notInCach.size(), 0);
67
68
69 // 4: varTS < ts < varTE < te
70 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
71 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
72 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
73 static_cast<double>(te.toMSecsSinceEpoch())};
74 notInCach = var.provideNotInCacheRangeList(sqp);
75 QCOMPARE(notInCach.size(), 1);
76 notInCachRange = notInCach.first();
77 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(varCRE.toMSecsSinceEpoch()));
78 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
79
80 // 5: varTS < varTE < ts < te
81 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
82 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
83 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
84 static_cast<double>(te.toMSecsSinceEpoch())};
85 notInCach = var.provideNotInCacheRangeList(sqp);
86 QCOMPARE(notInCach.size(), 1);
87 notInCachRange = notInCach.first();
88 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
89 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
90
91 // 6: ts <varTS < varTE < te
92 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
93 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
94 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
95 static_cast<double>(te.toMSecsSinceEpoch())};
96 notInCach = var.provideNotInCacheRangeList(sqp);
97 QCOMPARE(notInCach.size(), 2);
98 notInCachRange = notInCach.first();
99 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
100 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(varCRS.toMSecsSinceEpoch()));
101 notInCachRange = notInCach[1];
102 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(varCRE.toMSecsSinceEpoch()));
103 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
104 }
105
106
107 void TestVariable::testInCacheRangeList()
108 {
109 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
110 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
111
112 auto sqpR = SqpRange{static_cast<double>(varRS.toMSecsSinceEpoch()),
113 static_cast<double>(varRE.toMSecsSinceEpoch())};
114
115 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
116 auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
117 auto sqpCR = SqpRange{static_cast<double>(varCRS.toMSecsSinceEpoch()),
118 static_cast<double>(varCRE.toMSecsSinceEpoch())};
119
120 Variable var{"Var test", sqpR};
121 var.setCacheRange(sqpCR);
122
123 // 1: [ts,te] < varTS
124 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
125 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
126 auto sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
127 static_cast<double>(te.toMSecsSinceEpoch())};
128
129 auto notInCach = var.provideInCacheRangeList(sqp);
130
131 QCOMPARE(notInCach.size(), 0);
132
133 // 2: ts < varTS < te < varTE
134 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
135 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
136 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
137 static_cast<double>(te.toMSecsSinceEpoch())};
138 notInCach = var.provideInCacheRangeList(sqp);
139 QCOMPARE(notInCach.size(), 1);
140 auto notInCachRange = notInCach.first();
141 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(varCRS.toMSecsSinceEpoch()));
142 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
143
144 // 3: varTS < ts < te < varTE
145 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
146 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
147 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
148 static_cast<double>(te.toMSecsSinceEpoch())};
149 notInCach = var.provideInCacheRangeList(sqp);
150 QCOMPARE(notInCach.size(), 1);
151 notInCachRange = notInCach.first();
152 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
153 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
154
155 // 4: varTS < ts < varTE < te
156 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
157 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
158 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
159 static_cast<double>(te.toMSecsSinceEpoch())};
160 notInCach = var.provideInCacheRangeList(sqp);
161 QCOMPARE(notInCach.size(), 1);
162 notInCachRange = notInCach.first();
163 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
164 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(varCRE.toMSecsSinceEpoch()));
165
166 // 5: varTS < varTE < ts < te
167 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
168 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
169 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
170 static_cast<double>(te.toMSecsSinceEpoch())};
171 notInCach = var.provideInCacheRangeList(sqp);
172 QCOMPARE(notInCach.size(), 0);
173
174 // 6: ts <varTS < varTE < te
175 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
176 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
177 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
178 static_cast<double>(te.toMSecsSinceEpoch())};
179 notInCach = var.provideInCacheRangeList(sqp);
180 QCOMPARE(notInCach.size(), 1);
181 notInCachRange = notInCach.first();
182 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(varCRS.toMSecsSinceEpoch()));
183 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(varCRE.toMSecsSinceEpoch()));
184 }
185
186
187 QTEST_MAIN(TestVariable)
188 #include "TestVariable.moc"
@@ -1,243 +1,248
1 1 #include "Variable/Variable.h"
2 2
3 3 #include <Data/IDataSeries.h>
4 4 #include <Data/SqpRange.h>
5 5
6 6 #include <QMutex>
7 7 #include <QReadWriteLock>
8 8 #include <QThread>
9 9
10 10 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
11 11
12 12 struct Variable::VariablePrivate {
13 13 explicit VariablePrivate(const QString &name, const SqpRange &dateTime,
14 14 const QVariantHash &metadata)
15 15 : m_Name{name}, m_Range{dateTime}, m_Metadata{metadata}, m_DataSeries{nullptr}
16 16 {
17 17 }
18 18
19 19 void lockRead() { m_Lock.lockForRead(); }
20 20 void lockWrite() { m_Lock.lockForWrite(); }
21 21 void unlock() { m_Lock.unlock(); }
22 22
23 23 QString m_Name;
24 24
25 25 SqpRange m_Range;
26 26 SqpRange m_CacheRange;
27 27 QVariantHash m_Metadata;
28 28 std::shared_ptr<IDataSeries> m_DataSeries;
29 29
30 30 QReadWriteLock m_Lock;
31 31 };
32 32
33 33 Variable::Variable(const QString &name, const SqpRange &dateTime, const QVariantHash &metadata)
34 34 : impl{spimpl::make_unique_impl<VariablePrivate>(name, dateTime, metadata)}
35 35 {
36 36 }
37 37
38 38 QString Variable::name() const noexcept
39 39 {
40 40 impl->lockRead();
41 41 auto name = impl->m_Name;
42 42 impl->unlock();
43 43 return name;
44 44 }
45 45
46 46 SqpRange Variable::range() const noexcept
47 47 {
48 48 impl->lockRead();
49 49 auto range = impl->m_Range;
50 50 impl->unlock();
51 51 return range;
52 52 }
53 53
54 54 void Variable::setRange(const SqpRange &range) noexcept
55 55 {
56 56 impl->lockWrite();
57 57 impl->m_Range = range;
58 58 impl->unlock();
59 59 }
60 60
61 61 SqpRange Variable::cacheRange() const noexcept
62 62 {
63 63 impl->lockRead();
64 64 auto cacheRange = impl->m_CacheRange;
65 65 impl->unlock();
66 66 return cacheRange;
67 67 }
68 68
69 69 void Variable::setCacheRange(const SqpRange &cacheRange) noexcept
70 70 {
71 71 impl->lockWrite();
72 72 impl->m_CacheRange = cacheRange;
73 73 impl->unlock();
74 74 }
75 75
76 76 void Variable::setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
77 77 {
78 78 qCDebug(LOG_Variable()) << "TORM Variable::setDataSeries"
79 79 << QThread::currentThread()->objectName();
80 80 if (!dataSeries) {
81 81 /// @todo ALX : log
82 82 return;
83 83 }
84 84 impl->lockWrite();
85 85 impl->m_DataSeries = dataSeries->clone();
86 86 impl->unlock();
87 87 }
88 88
89 89 void Variable::mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
90 90 {
91 91 qCDebug(LOG_Variable()) << "TORM Variable::mergeDataSeries"
92 92 << QThread::currentThread()->objectName();
93 93 if (!dataSeries) {
94 94 /// @todo ALX : log
95 95 return;
96 96 }
97 97
98 98 // Add or merge the data
99 99 // Inits the data series of the variable
100 100 impl->lockWrite();
101 101 if (!impl->m_DataSeries) {
102 102 impl->m_DataSeries = dataSeries->clone();
103 103 }
104 104 else {
105 105 impl->m_DataSeries->merge(dataSeries.get());
106 106 }
107 107 impl->unlock();
108 108
109 109 // sub the data
110 110 auto subData = this->dataSeries()->subDataSeries(this->cacheRange());
111 111 qCDebug(LOG_Variable()) << "TORM: Variable::mergeDataSeries sub" << subData->range();
112 112 this->setDataSeries(subData);
113 113 qCDebug(LOG_Variable()) << "TORM: Variable::mergeDataSeries set" << this->dataSeries()->range();
114 114 }
115 115
116 116 std::shared_ptr<IDataSeries> Variable::dataSeries() const noexcept
117 117 {
118 118 impl->lockRead();
119 119 auto dataSeries = impl->m_DataSeries;
120 120 impl->unlock();
121 121
122 122 return dataSeries;
123 123 }
124 124
125 125 QVariantHash Variable::metadata() const noexcept
126 126 {
127 127 impl->lockRead();
128 128 auto metadata = impl->m_Metadata;
129 129 impl->unlock();
130 130 return metadata;
131 131 }
132 132
133 133 bool Variable::contains(const SqpRange &range) const noexcept
134 134 {
135 135 impl->lockRead();
136 136 auto res = impl->m_Range.contains(range);
137 137 impl->unlock();
138 138 return res;
139 139 }
140 140
141 141 bool Variable::intersect(const SqpRange &range) const noexcept
142 142 {
143 143
144 144 impl->lockRead();
145 145 auto res = impl->m_Range.intersect(range);
146 146 impl->unlock();
147 147 return res;
148 148 }
149 149
150 150 bool Variable::isInside(const SqpRange &range) const noexcept
151 151 {
152 152 impl->lockRead();
153 153 auto res = range.contains(SqpRange{impl->m_Range.m_TStart, impl->m_Range.m_TEnd});
154 154 impl->unlock();
155 155 return res;
156 156 }
157 157
158 158 bool Variable::cacheContains(const SqpRange &range) const noexcept
159 159 {
160 160 impl->lockRead();
161 161 auto res = impl->m_CacheRange.contains(range);
162 162 impl->unlock();
163 163 return res;
164 164 }
165 165
166 166 bool Variable::cacheIntersect(const SqpRange &range) const noexcept
167 167 {
168 168 impl->lockRead();
169 169 auto res = impl->m_CacheRange.intersect(range);
170 170 impl->unlock();
171 171 return res;
172 172 }
173 173
174 174 bool Variable::cacheIsInside(const SqpRange &range) const noexcept
175 175 {
176 176 impl->lockRead();
177 177 auto res = range.contains(SqpRange{impl->m_CacheRange.m_TStart, impl->m_CacheRange.m_TEnd});
178 178 impl->unlock();
179 179 return res;
180 180 }
181 181
182 182
183 183 QVector<SqpRange> Variable::provideNotInCacheRangeList(const SqpRange &range) const noexcept
184 184 {
185 185 // This code assume that cach in contigue. Can return 0, 1 or 2 SqpRange
186 186
187 187 auto notInCache = QVector<SqpRange>{};
188 188
189 189 if (!this->cacheContains(range)) {
190 190 if (range.m_TEnd <= impl->m_CacheRange.m_TStart
191 191 || range.m_TStart >= impl->m_CacheRange.m_TEnd) {
192 192 notInCache << range;
193 193 }
194 194 else if (range.m_TStart < impl->m_CacheRange.m_TStart
195 195 && range.m_TEnd <= impl->m_CacheRange.m_TEnd) {
196 196 notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart};
197 197 }
198 198 else if (range.m_TStart < impl->m_CacheRange.m_TStart
199 199 && range.m_TEnd > impl->m_CacheRange.m_TEnd) {
200 200 notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart}
201 201 << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd};
202 202 }
203 203 else if (range.m_TStart < impl->m_CacheRange.m_TEnd) {
204 204 notInCache << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd};
205 205 }
206 206 else {
207 207 qCCritical(LOG_Variable()) << tr("Detection of unknown case.")
208 208 << QThread::currentThread();
209 209 }
210 210 }
211 211
212 212 return notInCache;
213 213 }
214 214
215 215 QVector<SqpRange> Variable::provideInCacheRangeList(const SqpRange &range) const noexcept
216 216 {
217 217 // This code assume that cach in contigue. Can return 0 or 1 SqpRange
218 218
219 219 auto inCache = QVector<SqpRange>{};
220 220
221 221
222 if (this->cacheContains(range)) {
223 if (range.m_TStart <= impl->m_CacheRange.m_TEnd
224 && range.m_TEnd >= impl->m_CacheRange.m_TEnd) {
225 inCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TEnd};
222 if (this->intersect(range)) {
223 if (range.m_TStart <= impl->m_CacheRange.m_TStart
224 && range.m_TEnd >= impl->m_CacheRange.m_TStart
225 && range.m_TEnd < impl->m_CacheRange.m_TEnd) {
226 inCache << SqpRange{impl->m_CacheRange.m_TStart, range.m_TEnd};
226 227 }
227 228
228 229 else if (range.m_TStart >= impl->m_CacheRange.m_TStart
229 && range.m_TEnd < impl->m_CacheRange.m_TEnd) {
230 && range.m_TEnd <= impl->m_CacheRange.m_TEnd) {
230 231 inCache << range;
231 232 }
232 else if (range.m_TStart < impl->m_CacheRange.m_TStart
233 && range.m_TEnd >= impl->m_CacheRange.m_TStart) {
234 inCache << SqpRange{impl->m_CacheRange.m_TStart, range.m_TEnd};
233 else if (range.m_TStart > impl->m_CacheRange.m_TStart
234 && range.m_TEnd > impl->m_CacheRange.m_TEnd) {
235 inCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TEnd};
236 }
237 else if (range.m_TStart <= impl->m_CacheRange.m_TStart
238 && range.m_TEnd >= impl->m_CacheRange.m_TEnd) {
239 inCache << impl->m_CacheRange;
235 240 }
236 241 else {
237 242 qCCritical(LOG_Variable()) << tr("Detection of unknown case.")
238 243 << QThread::currentThread();
239 244 }
240 245 }
241 246
242 247 return inCache;
243 248 }
General Comments 0
You need to be logged in to leave comments. Login now