##// END OF EJS Templates
Implementation of the addDateTime method of the cache
perrinel -
r230:b886fd9a8729
parent child
Show More
@@ -1,26 +1,33
1 1 #ifndef SCIQLOP_VARIABLECACHECONTROLLER_H
2 2 #define SCIQLOP_VARIABLECACHECONTROLLER_H
3 3
4 4 #include <QObject>
5 5
6 6 #include <Data/SqpDateTime.h>
7 7
8 8 #include <Common/spimpl.h>
9 9
10 10 class Variable;
11 11
12 12 /// This class aims to store in the cash all of the dateTime already requested to the variable.
13 13 class VariableCacheController : public QObject {
14 14 Q_OBJECT
15 15 public:
16 16 explicit VariableCacheController(QObject *parent = 0);
17 17
18 18
19 19 void addDateTime(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
20 20
21 /// Return all of the SqpDataTime part of the dateTime whose are not in the cache
22 QVector<SqpDateTime> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
23 const SqpDateTime &dateTime);
24
25
26 QVector<SqpDateTime> dateCacheList(std::shared_ptr<Variable> variable) const noexcept;
27
21 28 private:
22 29 class VariableCacheControllerPrivate;
23 30 spimpl::unique_impl_ptr<VariableCacheControllerPrivate> impl;
24 31 };
25 32
26 33 #endif // SCIQLOP_VARIABLECACHECONTROLLER_H
@@ -1,111 +1,173
1 1 #include "Variable/VariableCacheController.h"
2 2
3 3 #include "Variable/Variable.h"
4 4 #include <unordered_map>
5 5
6 6 struct VariableCacheController::VariableCacheControllerPrivate {
7 7
8 8 std::unordered_map<std::shared_ptr<Variable>, QVector<SqpDateTime> >
9 9 m_VariableToSqpDateTimeListMap;
10 10
11 11 void addInCacheDataByEnd(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
12 12 QVector<SqpDateTime> &notInCache, int cacheIndex,
13 13 double currentTStart);
14 14
15 15 void addInCacheDataByStart(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
16 16 QVector<SqpDateTime> &notInCache, int cacheIndex,
17 17 double currentTStart);
18
19
20 void addDateTimeRecurse(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
21 int cacheIndex);
18 22 };
19 23
20 24
21 25 VariableCacheController::VariableCacheController(QObject *parent)
22 26 : QObject(parent), impl{spimpl::make_unique_impl<VariableCacheControllerPrivate>()}
23 27 {
24 28 }
25 29
26 30 void VariableCacheController::addDateTime(std::shared_ptr<Variable> variable,
27 31 const SqpDateTime &dateTime)
28 32 {
29 33 if (variable) {
30 // TODO: squeeze the map to let it only some SqpDateTime without intersection
34 auto findVariableIte = impl->m_VariableToSqpDateTimeListMap.find(variable);
35 if (findVariableIte == impl->m_VariableToSqpDateTimeListMap.end()) {
31 36 impl->m_VariableToSqpDateTimeListMap[variable].push_back(dateTime);
32 37 }
38 else {
39
40 // addDateTime modify the list<SqpDateTime> of the variable in a way to ensure
41 // that the list is ordered : l(0) < l(1). We assume also a < b
42 // (with a & b of type SqpDateTime) means ts(b) > te(a)
43
44 // The algorithm will try the merge of two interval:
45 // - dateTime will be compare with the first interval of the list:
46 // A: if it is inferior, it will be inserted and it's finished.
47 // B: if it is in intersection, it will be merge then the merged one
48 // will be compared to the next interval. The old one is remove from the list
49 // C: if it is superior, we do the same with the next interval of the list
50
51 int cacheIndex = 0;
52 impl->addDateTimeRecurse(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable),
53 cacheIndex);
54 }
55 }
33 56 }
34 57
35 58 QVector<SqpDateTime>
36 59 VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
37 60 const SqpDateTime &dateTime)
38 61 {
39 62 auto notInCache = QVector<SqpDateTime>{};
40 63
41 64 // This algorithm is recursif. The idea is to localise the start time then the end time in the
42 65 // list of date time request associated to the variable
43 66 // We assume that the list is ordered in a way that l(0) < l(1). We assume also a < b
44 67 // (with a & b of type SqpDateTime) means ts(b) > te(a)
45 68
46 69 impl->addInCacheDataByStart(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable),
47 70 notInCache, 0, dateTime.m_TStart);
48 71
49 72 return notInCache;
50 73 }
51 74
75 QVector<SqpDateTime>
76 VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept
77 {
78 return impl->m_VariableToSqpDateTimeListMap.at(variable);
79 }
80
81 void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse(
82 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex)
83 {
84 const auto dateTimeListSize = dateTimeList.count();
85 if (cacheIndex >= dateTimeListSize) {
86 dateTimeList.push_back(dateTime);
87 // there is no anymore interval to compore, we can just push_back it
88 return;
89 }
90
91 auto currentDateTime = dateTimeList[cacheIndex];
92
93 if (dateTime.m_TEnd < currentDateTime.m_TStart) {
94 // The compared one is < to current one compared, we can insert it
95 dateTimeList.insert(cacheIndex, dateTime);
96 }
97
98 else if (dateTime.m_TStart > currentDateTime.m_TEnd) {
99 // The compared one is > to current one compared we can comparet if to the next one
100 addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex);
101 }
102 else {
103 // Merge cases: we need to merge the two interval, remove the old one from the list then
104 // rerun the algo from this index with the merged interval
105 auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart);
106 auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd);
107 auto mergeDateTime = SqpDateTime{mTStart, mTEnd};
108
109 dateTimeList.remove(cacheIndex);
110 addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex);
111 }
112 }
113
52 114
53 115 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd(
54 116 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
55 117 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
56 118 {
57 119 const auto dateTimeListSize = dateTimeList.count();
58 120 if (cacheIndex >= dateTimeListSize) {
59 121 if (currentTStart < dateTime.m_TEnd) {
60 122
61 123 // te localised after all other interval: The last interval is [currentTsart, te]
62 124 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
63 125 }
64 126 return;
65 127 }
66 128
67 129 auto currentDateTimeJ = dateTimeList[cacheIndex];
68 130 if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) {
69 131 // te localised between to interval: The last interval is [currentTsart, te]
70 132 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
71 133 }
72 134 else {
73 135 notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart});
74 136 if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) {
75 137 // te not localised before the current interval: we need to look at the next interval
76 138 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex,
77 139 currentDateTimeJ.m_TEnd);
78 140 }
79 141 }
80 142 }
81 143
82 144 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart(
83 145 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
84 146 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
85 147 {
86 148 const auto dateTimeListSize = dateTimeList.count();
87 149 if (cacheIndex >= dateTimeListSize) {
88 150 // ts localised after all other interval: The last interval is [ts, te]
89 151 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
90 152 return;
91 153 }
92 154
93 155 auto currentDateTimeI = dateTimeList[cacheIndex];
94 156 auto cacheIndexJ = cacheIndex;
95 157 if (currentTStart < currentDateTimeI.m_TStart) {
96 158
97 159 // ts localised between to interval: let's localized te
98 160 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndexJ, currentTStart);
99 161 }
100 162 else if (dateTime.m_TStart < currentDateTimeI.m_TEnd) {
101 163 // ts not localised before the current interval: we need to look at the next interval
102 164 // We can assume now current tstart is the last interval tend, because data between them are
103 165 // in the cache
104 166 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex,
105 167 currentDateTimeI.m_TEnd);
106 168 }
107 169 else {
108 170 // ts not localised before the current interval: we need to look at the next interval
109 171 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart);
110 172 }
111 173 }
@@ -1,245 +1,343
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableCacheController.h>
3 3
4 4 #include <QObject>
5 5 #include <QtTest>
6 6
7 7 #include <memory>
8 8
9 9 class TestVariableCacheController : public QObject {
10 10 Q_OBJECT
11 11
12 12 private slots:
13 13 void testProvideNotInCacheDateTimeList();
14
15 void testAddDateTime();
14 16 };
15 17
16 18
17 19 void TestVariableCacheController::testProvideNotInCacheDateTimeList()
18 20 {
19 21 VariableCacheController variableCacheController{};
20 22
21 23 auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
22 24 auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
23 25 auto sqp0 = SqpDateTime{static_cast<double>(ts0.toMSecsSinceEpoch()),
24 26 static_cast<double>(te0.toMSecsSinceEpoch())};
25 27
26 28 auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
27 29 auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}};
28 30 auto sqp1 = SqpDateTime{static_cast<double>(ts1.toMSecsSinceEpoch()),
29 31 static_cast<double>(te1.toMSecsSinceEpoch())};
30 32
31 33 auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}};
32 34 auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}};
33 35 auto sqp2 = SqpDateTime{static_cast<double>(ts2.toMSecsSinceEpoch()),
34 36 static_cast<double>(te2.toMSecsSinceEpoch())};
35 37
36 38 auto var0 = std::make_shared<Variable>("", "", "", sqp0);
37 39
38 40 variableCacheController.addDateTime(var0, sqp0);
39 41 variableCacheController.addDateTime(var0, sqp1);
40 42 variableCacheController.addDateTime(var0, sqp2);
41 43
42 44 // first case [ts,te] < ts0
43 45 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
44 46 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
45 47 auto sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
46 48 static_cast<double>(te.toMSecsSinceEpoch())};
47 49
48 50
49 51 auto notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
50 52
51 53 QCOMPARE(notInCach.size(), 1);
52 54 auto notInCashSqp = notInCach.first();
53 55 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
54 56 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
55 57
56 58
57 59 // second case ts < ts0 && ts0 < te <= te0
58 60 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
59 61 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
60 62 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
61 63 static_cast<double>(te.toMSecsSinceEpoch())};
62 64
63 65
64 66 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
65 67
66 68 QCOMPARE(notInCach.size(), 1);
67 69 notInCashSqp = notInCach.first();
68 70 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
69 71 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
70 72
71 73 // 3th case ts < ts0 && te0 < te <= ts1
72 74 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
73 75 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
74 76 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
75 77 static_cast<double>(te.toMSecsSinceEpoch())};
76 78
77 79
78 80 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
79 81
80 82 QCOMPARE(notInCach.size(), 2);
81 83 notInCashSqp = notInCach.first();
82 84 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
83 85 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
84 86
85 87 notInCashSqp = notInCach.at(1);
86 88 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
87 89 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
88 90
89 91 // 4th case ts < ts0 && ts1 < te <= te1
90 92 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
91 93 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}};
92 94 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
93 95 static_cast<double>(te.toMSecsSinceEpoch())};
94 96
95 97
96 98 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
97 99
98 100 QCOMPARE(notInCach.size(), 2);
99 101 notInCashSqp = notInCach.first();
100 102 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
101 103 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
102 104
103 105 notInCashSqp = notInCach.at(1);
104 106 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
105 107 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
106 108
107 109 // 5th case ts < ts0 && te3 < te
108 110 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
109 111 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}};
110 112 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
111 113 static_cast<double>(te.toMSecsSinceEpoch())};
112 114
113 115
114 116 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
115 117
116 118 QCOMPARE(notInCach.size(), 4);
117 119 notInCashSqp = notInCach.first();
118 120 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
119 121 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
120 122
121 123 notInCashSqp = notInCach.at(1);
122 124 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
123 125 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
124 126
125 127 notInCashSqp = notInCach.at(2);
126 128 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
127 129 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts2.toMSecsSinceEpoch()));
128 130
129 131 notInCashSqp = notInCach.at(3);
130 132 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te2.toMSecsSinceEpoch()));
131 133 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
132 134
133 135
134 136 // 6th case ts2 < ts
135 137 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 45, 0, 0}};
136 138 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}};
137 139 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
138 140 static_cast<double>(te.toMSecsSinceEpoch())};
139 141
140 142
141 143 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
142 144
143 145 QCOMPARE(notInCach.size(), 1);
144 146 notInCashSqp = notInCach.first();
145 147 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
146 148 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
147 149
148 150 // 7th case ts = te0 && te < ts1
149 151 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
150 152 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
151 153 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
152 154 static_cast<double>(te.toMSecsSinceEpoch())};
153 155
154 156
155 157 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
156 158
157 159 QCOMPARE(notInCach.size(), 1);
158 160 notInCashSqp = notInCach.first();
159 161 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
160 162 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
161 163
162 164 // 8th case ts0 < ts < te0 && te < ts1
163 165 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
164 166 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
165 167 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
166 168 static_cast<double>(te.toMSecsSinceEpoch())};
167 169
168 170
169 171 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
170 172
171 173 QCOMPARE(notInCach.size(), 1);
172 174 notInCashSqp = notInCach.first();
173 175 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
174 176 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
175 177
176 178 // 9th case ts0 < ts < te0 && ts1 < te < te1
177 179 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
178 180 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}};
179 181 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
180 182 static_cast<double>(te.toMSecsSinceEpoch())};
181 183
182 184
183 185 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
184 186
185 187 QCOMPARE(notInCach.size(), 1);
186 188 notInCashSqp = notInCach.first();
187 189 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
188 190 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
189 191
190 192 // 10th case te1 < ts < te < ts2
191 193 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 9, 0, 0}};
192 194 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}};
193 195 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
194 196 static_cast<double>(te.toMSecsSinceEpoch())};
195 197
196 198
197 199 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
198 200
199 201 QCOMPARE(notInCach.size(), 1);
200 202 notInCashSqp = notInCach.first();
201 203 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
202 204 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
203 205
204 206 // 11th case te0 < ts < ts1 && te3 < te
205 207 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
206 208 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}};
207 209 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
208 210 static_cast<double>(te.toMSecsSinceEpoch())};
209 211
210 212
211 213 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
212 214
213 215 QCOMPARE(notInCach.size(), 3);
214 216 notInCashSqp = notInCach.first();
215 217 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
216 218 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
217 219
218 220 notInCashSqp = notInCach.at(1);
219 221 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
220 222 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts2.toMSecsSinceEpoch()));
221 223
222 224 notInCashSqp = notInCach.at(2);
223 225 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te2.toMSecsSinceEpoch()));
224 226 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
225 227
226 228 // 12th case te0 < ts < ts1 && te3 < te
227 229 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
228 230 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}};
229 231 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
230 232 static_cast<double>(te.toMSecsSinceEpoch())};
231 233
232 234 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
233 235
234 236 QCOMPARE(notInCach.size(), 2);
235 237 notInCashSqp = notInCach.first();
236 238 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
237 239 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
238 240
239 241 notInCashSqp = notInCach.at(1);
240 242 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
241 243 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
242 244 }
243 245
246
247 void TestVariableCacheController::testAddDateTime()
248 {
249 VariableCacheController variableCacheController{};
250
251 auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
252 auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
253 auto sqp0 = SqpDateTime{static_cast<double>(ts0.toMSecsSinceEpoch()),
254 static_cast<double>(te0.toMSecsSinceEpoch())};
255
256 auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
257 auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}};
258 auto sqp1 = SqpDateTime{static_cast<double>(ts1.toMSecsSinceEpoch()),
259 static_cast<double>(te1.toMSecsSinceEpoch())};
260
261 auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}};
262 auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}};
263 auto sqp2 = SqpDateTime{static_cast<double>(ts2.toMSecsSinceEpoch()),
264 static_cast<double>(te2.toMSecsSinceEpoch())};
265
266 auto ts01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
267 auto te01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
268 auto sqp01 = SqpDateTime{static_cast<double>(ts01.toMSecsSinceEpoch()),
269 static_cast<double>(te01.toMSecsSinceEpoch())};
270
271 auto ts3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 14, 0, 0}};
272 auto te3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 16, 0, 0}};
273 auto sqp3 = SqpDateTime{static_cast<double>(ts3.toMSecsSinceEpoch()),
274 static_cast<double>(te3.toMSecsSinceEpoch())};
275
276 auto ts03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
277 auto te03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}};
278 auto sqp03 = SqpDateTime{static_cast<double>(ts03.toMSecsSinceEpoch()),
279 static_cast<double>(te03.toMSecsSinceEpoch())};
280
281
282 auto var0 = std::make_shared<Variable>("", "", "", sqp0);
283
284
285 // First case: add the first interval to the variable :sqp0
286 variableCacheController.addDateTime(var0, sqp0);
287 auto dateCacheList = variableCacheController.dateCacheList(var0);
288 QCOMPARE(dateCacheList.count(), 1);
289 auto dateCache = dateCacheList.at(0);
290 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
291 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te0.toMSecsSinceEpoch()));
292
293 // 2nd case: add a second interval : sqp1 > sqp0
294 variableCacheController.addDateTime(var0, sqp1);
295 dateCacheList = variableCacheController.dateCacheList(var0);
296 QCOMPARE(dateCacheList.count(), 2);
297 dateCache = dateCacheList.at(0);
298 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
299 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te0.toMSecsSinceEpoch()));
300
301 dateCache = dateCacheList.at(1);
302 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts1.toMSecsSinceEpoch()));
303 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
304
305 // 3th case: merge sqp0 & sqp1 with sqp01
306 variableCacheController.addDateTime(var0, sqp01);
307 dateCacheList = variableCacheController.dateCacheList(var0);
308 QCOMPARE(dateCacheList.count(), 1);
309 dateCache = dateCacheList.at(0);
310 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
311 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
312
313
314 // 4th case: add a second interval : sqp1 > sqp0
315 variableCacheController.addDateTime(var0, sqp2);
316 variableCacheController.addDateTime(var0, sqp3);
317 dateCacheList = variableCacheController.dateCacheList(var0);
318 QCOMPARE(dateCacheList.count(), 3);
319 dateCache = dateCacheList.at(0);
320 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
321 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
322
323 dateCache = dateCacheList.at(1);
324 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts3.toMSecsSinceEpoch()));
325 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te3.toMSecsSinceEpoch()));
326
327 dateCache = dateCacheList.at(2);
328 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts2.toMSecsSinceEpoch()));
329 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te2.toMSecsSinceEpoch()));
330
331
332 // 5th case: merge all interval
333 variableCacheController.addDateTime(var0, sqp03);
334 dateCacheList = variableCacheController.dateCacheList(var0);
335 QCOMPARE(dateCacheList.count(), 1);
336 dateCache = dateCacheList.at(0);
337 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
338 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te03.toMSecsSinceEpoch()));
339 }
340
341
244 342 QTEST_MAIN(TestVariableCacheController)
245 343 #include "TestVariableCacheController.moc"
General Comments 0
You need to be logged in to leave comments. Login now