@@ -1,219 +1,219 | |||
|
1 | 1 | #ifndef SCIQLOP_DATETIMERANGE_H |
|
2 | 2 | #define SCIQLOP_DATETIMERANGE_H |
|
3 | 3 | |
|
4 | 4 | #include <cmath> |
|
5 | 5 | #include <QObject> |
|
6 | 6 | |
|
7 | 7 | #include <QDebug> |
|
8 | 8 | |
|
9 | 9 | #include <opaque/numeric_typedef.hpp> |
|
10 | 10 | #include <Common/DateUtils.h> |
|
11 | 11 | #include <Common/MetaTypes.h> |
|
12 | 12 | #include <Common/Numeric.h> |
|
13 | 13 | |
|
14 | 14 | |
|
15 | 15 | template <typename T> |
|
16 | 16 | struct Seconds : opaque::numeric_typedef<T, Seconds<T>> , |
|
17 | 17 | opaque::binop::multipliable <Seconds<T>, true , Seconds<T>, T, T>, |
|
18 | 18 | opaque::binop::dividable <Seconds<T>, true , Seconds<T>, T, T>, |
|
19 | 19 | opaque::binop::addable <Seconds<T>, true , Seconds<T>, T, T>, |
|
20 | 20 | opaque::binop::subtractable <Seconds<T>, true , Seconds<T>, T, T> |
|
21 | 21 | |
|
22 | 22 | { |
|
23 | 23 | using base = opaque::numeric_typedef<T, Seconds<T>>; |
|
24 | 24 | using base::base; |
|
25 | 25 | operator T () const {return this->value;} |
|
26 | 26 | }; |
|
27 | 27 | |
|
28 | 28 | struct InvalidDateTimeRangeTransformation{}; |
|
29 | 29 | |
|
30 | 30 | struct DateTimeRangeTransformation |
|
31 | 31 | { |
|
32 | 32 | double zoom; |
|
33 | 33 | Seconds<double> shift; |
|
34 | 34 | bool operator==(const DateTimeRangeTransformation& other) const |
|
35 | 35 | { |
|
36 | 36 | return SciQLop::numeric::almost_equal(zoom, other.zoom, 1) && |
|
37 | 37 | SciQLop::numeric::almost_equal<double>(shift, other.shift, 1); |
|
38 | 38 | } |
|
39 | 39 | }; |
|
40 | 40 | |
|
41 | 41 | /** |
|
42 | 42 | * @brief The SqpRange struct holds the information of time parameters |
|
43 | 43 | */ |
|
44 | 44 | struct DateTimeRange { |
|
45 | 45 | DateTimeRange() |
|
46 | 46 | :m_TStart(std::nan("")), m_TEnd(std::nan("")) |
|
47 | 47 | {} |
|
48 | 48 | DateTimeRange(double TStart, double TEnd) |
|
49 | 49 | :m_TStart(TStart), m_TEnd(TEnd) |
|
50 | 50 | {} |
|
51 | 51 | /// Creates SqpRange from dates and times |
|
52 | 52 | static DateTimeRange fromDateTime(const QDate &startDate, const QTime &startTime, |
|
53 | 53 | const QDate &endDate, const QTime &endTime) |
|
54 | 54 | { |
|
55 | 55 | return {DateUtils::secondsSinceEpoch(QDateTime{startDate, startTime, Qt::UTC}), |
|
56 | 56 | DateUtils::secondsSinceEpoch(QDateTime{endDate, endTime, Qt::UTC})}; |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | static DateTimeRange fromDateTime(const QDateTime &start, const QDateTime &end) |
|
60 | 60 | { |
|
61 | 61 | return {DateUtils::secondsSinceEpoch(start), |
|
62 | 62 | DateUtils::secondsSinceEpoch(end)}; |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | /// Start time (UTC) |
|
66 | 66 | double m_TStart; |
|
67 | 67 | /// End time (UTC) |
|
68 | 68 | double m_TEnd; |
|
69 | 69 | |
|
70 | 70 | Seconds<double> delta()const noexcept{return Seconds<double>{this->m_TEnd - this->m_TStart};} |
|
71 | 71 | |
|
72 | 72 | bool contains(const DateTimeRange &dateTime) const noexcept |
|
73 | 73 | { |
|
74 | 74 | return (m_TStart <= dateTime.m_TStart && m_TEnd >= dateTime.m_TEnd); |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | Seconds<double> center() const noexcept |
|
78 | 78 | { |
|
79 | 79 | return Seconds<double>((m_TStart + m_TEnd) / 2.); |
|
80 | 80 | } |
|
81 | 81 | |
|
82 | 82 | bool intersect(const DateTimeRange &dateTime) const noexcept |
|
83 | 83 | { |
|
84 | 84 | return (m_TEnd >= dateTime.m_TStart && m_TStart <= dateTime.m_TEnd); |
|
85 | 85 | } |
|
86 | 86 | |
|
87 | 87 | inline DateTimeRange transform(const DateTimeRangeTransformation& tr)const noexcept; |
|
88 | 88 | |
|
89 | 89 | bool operator==(const DateTimeRange &other) const |
|
90 | 90 | { |
|
91 | 91 | return SciQLop::numeric::almost_equal(m_TStart, other.m_TStart, 1) && |
|
92 | 92 | SciQLop::numeric::almost_equal(m_TEnd, other.m_TEnd, 1); |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | bool operator!=(const DateTimeRange &other) const { return !(*this == other); } |
|
96 | 96 | |
|
97 | 97 | void grow(double factor)noexcept |
|
98 | 98 | { |
|
99 | 99 | double grow_v{delta()*(factor - 1.)/2.}; |
|
100 | 100 | m_TStart -= grow_v; |
|
101 | 101 | m_TEnd += grow_v; |
|
102 | 102 | } |
|
103 | 103 | |
|
104 | 104 | void shrink(double factor)noexcept |
|
105 | 105 | { |
|
106 | 106 | double shrink_v{this->delta()*(1. - factor)/2.}; |
|
107 | 107 | m_TStart += shrink_v; |
|
108 | 108 | m_TEnd -= shrink_v; |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | DateTimeRange& operator*=(double k) |
|
112 | 112 | { |
|
113 | 113 | this->grow(k); |
|
114 | 114 | return *this; |
|
115 | 115 | } |
|
116 | 116 | |
|
117 | 117 | DateTimeRange& operator/=(double k) |
|
118 | 118 | { |
|
119 | 119 | this->shrink(k); |
|
120 | 120 | return *this; |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | // compute set difference |
|
124 | 124 | std::vector<DateTimeRange> operator-(const DateTimeRange& other)const |
|
125 | 125 | { |
|
126 | 126 | std::vector<DateTimeRange> result; |
|
127 | 127 | if(std::isnan(other.m_TStart)||std::isnan(other.m_TEnd)||!this->intersect(other)) |
|
128 | 128 | { |
|
129 |
result. |
|
|
129 | result.emplace_back(m_TStart, m_TEnd); | |
|
130 | 130 | } |
|
131 | 131 | else |
|
132 | 132 | { |
|
133 | 133 | if(this->m_TStart<other.m_TStart) |
|
134 | 134 | { |
|
135 |
result. |
|
|
135 | result.emplace_back(this->m_TStart, other.m_TStart); | |
|
136 | 136 | } |
|
137 | 137 | if(this->m_TEnd>other.m_TEnd) |
|
138 | 138 | { |
|
139 |
result. |
|
|
139 | result.emplace_back(this->m_TEnd, other.m_TEnd); | |
|
140 | 140 | } |
|
141 | 141 | } |
|
142 | 142 | return result; |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | }; |
|
146 | 146 | |
|
147 | 147 | template <class T> |
|
148 | 148 | DateTimeRange& operator+=(DateTimeRange&r, Seconds<T> offset) |
|
149 | 149 | { |
|
150 | 150 | shift(r,offset); |
|
151 | 151 | return r; |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | template <class T> |
|
155 | 155 | DateTimeRange& operator-=(DateTimeRange&r, Seconds<T> offset) |
|
156 | 156 | { |
|
157 | 157 | shift(r,-offset); |
|
158 | 158 | } |
|
159 | 159 | |
|
160 | 160 | template <class T> |
|
161 | 161 | void shift(DateTimeRange& r, Seconds<T> offset) |
|
162 | 162 | { |
|
163 | 163 | r.m_TEnd+=static_cast<double>(offset); |
|
164 | 164 | r.m_TStart+=static_cast<double>(offset); |
|
165 | 165 | } |
|
166 | 166 | |
|
167 | 167 | inline DateTimeRange operator*(const DateTimeRange& r, double k) |
|
168 | 168 | { |
|
169 | 169 | DateTimeRange result{r}; |
|
170 | 170 | result.grow(k); |
|
171 | 171 | return result; |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | inline DateTimeRange operator/(const DateTimeRange& r, double k) |
|
175 | 175 | { |
|
176 | 176 | DateTimeRange result{r}; |
|
177 | 177 | result.shrink(k); |
|
178 | 178 | return result; |
|
179 | 179 | } |
|
180 | 180 | |
|
181 | 181 | template<class T> |
|
182 | 182 | DateTimeRange operator+(const DateTimeRange& r, Seconds<T> offset) |
|
183 | 183 | { |
|
184 | 184 | DateTimeRange result{r}; |
|
185 | 185 | shift(result,offset); |
|
186 | 186 | return result; |
|
187 | 187 | } |
|
188 | 188 | |
|
189 | 189 | template<class T> |
|
190 | 190 | DateTimeRange operator-(const DateTimeRange& r, Seconds<T> offset) |
|
191 | 191 | { |
|
192 | 192 | DateTimeRange result{r}; |
|
193 | 193 | shift(result,-offset); |
|
194 | 194 | return result; |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | 197 | const auto INVALID_RANGE |
|
198 | 198 | = DateTimeRange{std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN()}; |
|
199 | 199 | |
|
200 | 200 | inline QDebug operator<<(QDebug d, DateTimeRange obj) |
|
201 | 201 | { |
|
202 | 202 | auto tendDateTimeStart = DateUtils::dateTime(obj.m_TStart); |
|
203 | 203 | auto tendDateTimeEnd = DateUtils::dateTime(obj.m_TEnd); |
|
204 | 204 | |
|
205 | 205 | d << "ts: " << tendDateTimeStart << " te: " << tendDateTimeEnd; |
|
206 | 206 | return d; |
|
207 | 207 | } |
|
208 | 208 | |
|
209 | 209 | |
|
210 | 210 | |
|
211 | 211 | DateTimeRange DateTimeRange::transform(const DateTimeRangeTransformation &tr) const noexcept |
|
212 | 212 | { |
|
213 | 213 | return DateTimeRange{*this} * tr.zoom + tr.shift; |
|
214 | 214 | } |
|
215 | 215 | |
|
216 | 216 | // Required for using shared_ptr in signals/slots |
|
217 | 217 | SCIQLOP_REGISTER_META_TYPE(SQPRANGE_REGISTRY, DateTimeRange) |
|
218 | 218 | |
|
219 | 219 | #endif // SCIQLOP_DATETIMERANGE_H |
@@ -1,62 +1,62 | |||
|
1 | 1 | #ifndef SCIQLOP_DATETIMERANGEHELPER_H |
|
2 | 2 | #define SCIQLOP_DATETIMERANGEHELPER_H |
|
3 | 3 | |
|
4 | 4 | #include <cmath> |
|
5 | 5 | #include <variant> |
|
6 | 6 | #include <QObject> |
|
7 | 7 | |
|
8 | 8 | #include <QDebug> |
|
9 | 9 | |
|
10 | 10 | #include <opaque/numeric_typedef.hpp> |
|
11 | 11 | #include <Common/DateUtils.h> |
|
12 | 12 | #include <Common/MetaTypes.h> |
|
13 | 13 | #include <Common/Numeric.h> |
|
14 | 14 | #include <Data/DateTimeRange.h> |
|
15 | 15 | |
|
16 | 16 | namespace DateTimeRangeHelper { |
|
17 | 17 | |
|
18 | 18 | |
|
19 | bool isnan(const DateTimeRange& range) | |
|
19 | inline bool isnan(const DateTimeRange& range) | |
|
20 | 20 | { |
|
21 | 21 | return std::isnan(range.m_TStart) && std::isnan(range.m_TEnd); |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | bool hasnan(const DateTimeRange& range) | |
|
24 | inline bool hasnan(const DateTimeRange& range) | |
|
25 | 25 | { |
|
26 | 26 | return std::isnan(range.m_TStart) || std::isnan(range.m_TEnd); |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | bool isPureShift(const DateTimeRange& range1, const DateTimeRange& range2) | |
|
29 | inline bool isPureShift(const DateTimeRange& range1, const DateTimeRange& range2) | |
|
30 | 30 | { |
|
31 | 31 | return SciQLop::numeric::almost_equal<double>(range1.delta(), range2.delta(), 1) |
|
32 | 32 | && !SciQLop::numeric::almost_equal(range1.m_TStart, range2.m_TStart, 1); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | bool isPureZoom(const DateTimeRange& range1, const DateTimeRange& range2) | |
|
35 | inline bool isPureZoom(const DateTimeRange& range1, const DateTimeRange& range2) | |
|
36 | 36 | { |
|
37 | 37 | return !SciQLop::numeric::almost_equal<double>(range1.delta(),range2.delta(),1)&& |
|
38 | 38 | SciQLop::numeric::almost_equal<double>(range1.center(), range2.center(),1); |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | /** |
|
42 | 42 | * @brief computeTransformation such as range2 = zoom*range1 + shift |
|
43 | 43 | * @param range1 |
|
44 | 44 | * @param range2 |
|
45 | 45 | * @return trnaformation applied to range1 to get range2 or an object of type |
|
46 | 46 | * InvalidDateTimeRangeTransformation if the transformation has NaN or forbiden values |
|
47 | 47 | */ |
|
48 | std::variant<DateTimeRangeTransformation, InvalidDateTimeRangeTransformation> | |
|
48 | inline std::variant<DateTimeRangeTransformation, InvalidDateTimeRangeTransformation> | |
|
49 | 49 | computeTransformation(const DateTimeRange& range1, const DateTimeRange& range2) |
|
50 | 50 | { |
|
51 | 51 | double zoom = range2.delta()/range1.delta(); |
|
52 | 52 | Seconds<double> shift = range2.center() - (range1*zoom).center(); |
|
53 | 53 | bool zoomValid = zoom!=0. && !std::isnan(zoom) && !std::isinf(zoom); |
|
54 | 54 | bool shiftValid = !std::isnan(shift.value) && !std::isinf(shift.value); |
|
55 | 55 | if(zoomValid && shiftValid) |
|
56 | 56 | return DateTimeRangeTransformation{zoom, shift}; |
|
57 | 57 | return InvalidDateTimeRangeTransformation{}; |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | #endif // SCIQLOP_DATETIMERANGEHELPER_H |
@@ -1,39 +1,40 | |||
|
1 | 1 | #include <memory> |
|
2 | 2 | #include <vector> |
|
3 | 3 | #include <QHash> |
|
4 | 4 | #include <QObject> |
|
5 | 5 | #include <QMutexLocker> |
|
6 | 6 | #include <QUuid> |
|
7 | 7 | #include <QItemSelectionModel> |
|
8 | 8 | #include <Common/spimpl.h> |
|
9 | 9 | #include <Variable/Variable.h> |
|
10 | 10 | #include <Variable/VariableSynchronizationGroup.h> |
|
11 | 11 | #include <Variable/VariableModel.h> |
|
12 | 12 | #include <Data/IDataProvider.h> |
|
13 | 13 | #include "Data/DateTimeRange.h" |
|
14 | 14 | |
|
15 | 15 | class VariableController2: public QObject |
|
16 | 16 | { |
|
17 | 17 | class VariableController2Private; |
|
18 | 18 | Q_OBJECT |
|
19 | 19 | |
|
20 | 20 | spimpl::unique_impl_ptr<VariableController2Private> impl; |
|
21 | 21 | |
|
22 | 22 | public: |
|
23 | 23 | explicit VariableController2(); |
|
24 | 24 | std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata, |
|
25 |
std::shared_ptr<IDataProvider> provider, |
|
|
25 | const std::shared_ptr<IDataProvider>& provider, | |
|
26 | const DateTimeRange &range); | |
|
26 | 27 | |
|
27 | void deleteVariable(std::shared_ptr<Variable> variable); | |
|
28 | void changeRange(std::shared_ptr<Variable> variable, DateTimeRange r); | |
|
29 | void asyncChangeRange(std::shared_ptr<Variable> variable, DateTimeRange r); | |
|
28 | void deleteVariable(const std::shared_ptr<Variable>& variable); | |
|
29 | void changeRange(const std::shared_ptr<Variable>& variable, const DateTimeRange& r); | |
|
30 | void asyncChangeRange(const std::shared_ptr<Variable>& variable, const DateTimeRange& r); | |
|
30 | 31 | const std::set<std::shared_ptr<Variable>> variables(); |
|
31 | 32 | |
|
32 |
void synchronize(st |
|
|
33 | void synchronize(const std::shared_ptr<Variable>& var, const std::shared_ptr<Variable>& with); | |
|
33 | 34 | |
|
34 | 35 | |
|
35 | 36 | signals: |
|
36 | void variableAdded(std::shared_ptr<Variable>); | |
|
37 | void variableDeleted(std::shared_ptr<Variable>); | |
|
37 | void variableAdded(const std::shared_ptr<Variable>&); | |
|
38 | void variableDeleted(const std::shared_ptr<Variable>&); | |
|
38 | 39 | |
|
39 | 40 | }; |
@@ -1,190 +1,190 | |||
|
1 | 1 | #include "Variable/VariableController2.h" |
|
2 | 2 | #include "Variable/VariableSynchronizationGroup2.h" |
|
3 | 3 | #include <Common/containers.h> |
|
4 | 4 | #include <Common/debug.h> |
|
5 | 5 | #include <Data/DataProviderParameters.h> |
|
6 | 6 | #include <Data/DateTimeRangeHelper.h> |
|
7 | 7 | #include <Variable/VariableCacheStrategyFactory.h> |
|
8 | 8 | |
|
9 | 9 | class VariableController2::VariableController2Private |
|
10 | 10 | { |
|
11 | 11 | QMap<QUuid,std::shared_ptr<Variable>> _variables; |
|
12 | 12 | QMap<QUuid,std::shared_ptr<IDataProvider>> _providers; |
|
13 | 13 | QMap<QUuid,std::shared_ptr<VariableSynchronizationGroup2>> _synchronizationGroups; |
|
14 | 14 | std::unique_ptr<VariableCacheStrategy> _cacheStrategy; |
|
15 | bool p_contains(std::shared_ptr<Variable> variable) | |
|
15 | bool p_contains(const std::shared_ptr<Variable>& variable) | |
|
16 | 16 | { |
|
17 | 17 | return _providers.contains(variable->ID()); |
|
18 | 18 | } |
|
19 | bool v_contains(std::shared_ptr<Variable> variable) | |
|
19 | bool v_contains(const std::shared_ptr<Variable>& variable) | |
|
20 | 20 | { |
|
21 | 21 | return SciQLop::containers::contains(this->_variables, variable); |
|
22 | 22 | } |
|
23 | bool sg_contains(std::shared_ptr<Variable> variable) | |
|
23 | bool sg_contains(const std::shared_ptr<Variable>& variable) | |
|
24 | 24 | { |
|
25 | 25 | return _synchronizationGroups.contains(variable->ID()); |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | void _changeRange(std::shared_ptr<Variable> var, DateTimeRange r) | |
|
28 | void _changeRange(const std::shared_ptr<Variable>& var, DateTimeRange r) | |
|
29 | 29 | { |
|
30 | 30 | auto provider = _providers[var->ID()]; |
|
31 | 31 | DateTimeRange newCacheRange; |
|
32 | 32 | std::vector<DateTimeRange> missingRanges; |
|
33 | 33 | if(DateTimeRangeHelper::hasnan(var->cacheRange())) |
|
34 | 34 | { |
|
35 | 35 | newCacheRange = _cacheStrategy->computeRange(r,r); |
|
36 | 36 | missingRanges = {newCacheRange}; |
|
37 | 37 | } |
|
38 | 38 | else |
|
39 | 39 | { |
|
40 | 40 | newCacheRange = _cacheStrategy->computeRange(var->cacheRange(),r); |
|
41 | 41 | missingRanges = newCacheRange - var->cacheRange(); |
|
42 | 42 | } |
|
43 | 43 | for(auto range:missingRanges) |
|
44 | 44 | { |
|
45 | 45 | auto data = provider->getData(DataProviderParameters{{range},var->metadata()}); |
|
46 | 46 | var->mergeDataSeries(data); |
|
47 | 47 | } |
|
48 | 48 | var->setCacheRange(newCacheRange); |
|
49 | 49 | var->setRange(r); |
|
50 | 50 | } |
|
51 | 51 | public: |
|
52 | 52 | VariableController2Private(QObject* parent=Q_NULLPTR) |
|
53 | 53 | :_cacheStrategy(VariableCacheStrategyFactory::createCacheStrategy(CacheStrategy::SingleThreshold)) |
|
54 | 54 | { |
|
55 | 55 | Q_UNUSED(parent); |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | ~VariableController2Private() = default; |
|
59 | 59 | |
|
60 | 60 | std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata, std::shared_ptr<IDataProvider> provider) |
|
61 | 61 | { |
|
62 | 62 | auto newVar = std::make_shared<Variable>(name,metadata); |
|
63 | 63 | this->_variables[newVar->ID()] = newVar; |
|
64 | this->_providers[newVar->ID()] = provider; | |
|
64 | this->_providers[newVar->ID()] = std::move(provider); | |
|
65 | 65 | this->_synchronizationGroups[newVar->ID()] = std::make_shared<VariableSynchronizationGroup2>(newVar->ID()); |
|
66 | 66 | return newVar; |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | void deleteVariable(std::shared_ptr<Variable> variable) | |
|
69 | void deleteVariable(const std::shared_ptr<Variable>& variable) | |
|
70 | 70 | { |
|
71 | 71 | /* |
|
72 | 72 | * Removing twice a var is ok but a var without provider has to be a hard error |
|
73 | 73 | * this means we got the var controller in an inconsistent state |
|
74 | 74 | */ |
|
75 | 75 | if(v_contains(variable)) |
|
76 | 76 | this->_variables.remove(variable->ID()); |
|
77 | 77 | if(p_contains(variable)) |
|
78 | 78 | this->_providers.remove(variable->ID()); |
|
79 | 79 | else |
|
80 | 80 | SCIQLOP_ERROR(VariableController2Private, "No provider found for given variable"); |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | void changeRange(std::shared_ptr<Variable> variable, DateTimeRange r) | |
|
83 | void changeRange(const std::shared_ptr<Variable>& variable, DateTimeRange r) | |
|
84 | 84 | { |
|
85 | 85 | if(p_contains(variable)) |
|
86 | 86 | { |
|
87 | 87 | if(!DateTimeRangeHelper::hasnan(r)) |
|
88 | 88 | { |
|
89 | 89 | auto transformation = DateTimeRangeHelper::computeTransformation(variable->range(),r); |
|
90 | 90 | auto group = _synchronizationGroups[variable->ID()]; |
|
91 | 91 | if(std::holds_alternative<DateTimeRangeTransformation>(transformation)) |
|
92 | 92 | { |
|
93 | 93 | for(auto varId:group->variables()) |
|
94 | 94 | { |
|
95 | 95 | auto var = _variables[varId]; |
|
96 | 96 | auto newRange = var->range().transform(std::get<DateTimeRangeTransformation>(transformation)); |
|
97 | 97 | _changeRange(var,newRange); |
|
98 | 98 | } |
|
99 | 99 | } |
|
100 | 100 | else // force new range to all variables -> may be weird if more than one var in the group |
|
101 | 101 | // @TODO ensure that there is no side effects |
|
102 | 102 | { |
|
103 | 103 | for(auto varId:group->variables()) |
|
104 | 104 | { |
|
105 | 105 | auto var = _variables[varId]; |
|
106 | 106 | _changeRange(var,r); |
|
107 | 107 | } |
|
108 | 108 | } |
|
109 | 109 | } |
|
110 | 110 | else |
|
111 | 111 | { |
|
112 | 112 | SCIQLOP_ERROR(VariableController2Private, "Invalid range containing NaN"); |
|
113 | 113 | } |
|
114 | 114 | } |
|
115 | 115 | else |
|
116 | 116 | { |
|
117 | 117 | SCIQLOP_ERROR(VariableController2Private, "No provider found for given variable"); |
|
118 | 118 | } |
|
119 | 119 | } |
|
120 | 120 | |
|
121 |
void synchronize(st |
|
|
121 | void synchronize(const std::shared_ptr<Variable>& var, const std::shared_ptr<Variable>& with) | |
|
122 | 122 | { |
|
123 | 123 | if(v_contains(var) && v_contains(with)) |
|
124 | 124 | { |
|
125 | 125 | if(sg_contains(var) && sg_contains(with)) |
|
126 | 126 | { |
|
127 | 127 | |
|
128 | 128 | auto dest_group = this->_synchronizationGroups[with->ID()]; |
|
129 | 129 | this->_synchronizationGroups[var->ID()] = dest_group; |
|
130 | 130 | dest_group->addVariable(var->ID()); |
|
131 | 131 | } |
|
132 | 132 | else |
|
133 | 133 | { |
|
134 | 134 | SCIQLOP_ERROR(VariableController2Private, "At least one of the given variables isn't in a sync group"); |
|
135 | 135 | } |
|
136 | 136 | } |
|
137 | 137 | else |
|
138 | 138 | { |
|
139 | 139 | SCIQLOP_ERROR(VariableController2Private, "At least one of the given variables is not found"); |
|
140 | 140 | } |
|
141 | 141 | } |
|
142 | 142 | |
|
143 | 143 | const std::set<std::shared_ptr<Variable>> variables() |
|
144 | 144 | { |
|
145 | 145 | std::set<std::shared_ptr<Variable>> vars; |
|
146 |
for(auto var:_variables |
|
|
146 | for(const auto &var:_variables) | |
|
147 | 147 | { |
|
148 | 148 | vars.insert(var); |
|
149 | 149 | } |
|
150 | 150 | return vars; |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | }; |
|
154 | 154 | |
|
155 | 155 | VariableController2::VariableController2() |
|
156 | 156 | :impl{spimpl::make_unique_impl<VariableController2Private>()} |
|
157 | 157 | {} |
|
158 | 158 | |
|
159 | std::shared_ptr<Variable> VariableController2::createVariable(const QString &name, const QVariantHash &metadata, std::shared_ptr<IDataProvider> provider, const DateTimeRange &range) | |
|
159 | std::shared_ptr<Variable> VariableController2::createVariable(const QString &name, const QVariantHash &metadata, const std::shared_ptr<IDataProvider>& provider, const DateTimeRange &range) | |
|
160 | 160 | { |
|
161 | 161 | auto var = impl->createVariable(name, metadata, provider); |
|
162 | 162 | emit variableAdded(var); |
|
163 | 163 | if(!DateTimeRangeHelper::hasnan(range)) |
|
164 | 164 | impl->changeRange(var,range); |
|
165 | 165 | else |
|
166 | 166 | SCIQLOP_ERROR(VariableController2, "Creating a variable with default constructed DateTimeRange is an error"); |
|
167 | 167 | return var; |
|
168 | 168 | } |
|
169 | 169 | |
|
170 | void VariableController2::deleteVariable(std::shared_ptr<Variable> variable) | |
|
170 | void VariableController2::deleteVariable(const std::shared_ptr<Variable>& variable) | |
|
171 | 171 | { |
|
172 | 172 | impl->deleteVariable(variable); |
|
173 | 173 | emit variableDeleted(variable); |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | void VariableController2::changeRange(std::shared_ptr<Variable> variable, DateTimeRange r) | |
|
176 | void VariableController2::changeRange(const std::shared_ptr<Variable>& variable, const DateTimeRange& r) | |
|
177 | 177 | { |
|
178 | 178 | impl->changeRange(variable, r); |
|
179 | 179 | } |
|
180 | 180 | |
|
181 | 181 | const std::set<std::shared_ptr<Variable> > VariableController2::variables() |
|
182 | 182 | { |
|
183 | 183 | return impl->variables(); |
|
184 | 184 | } |
|
185 | 185 | |
|
186 |
void VariableController2::synchronize(st |
|
|
186 | void VariableController2::synchronize(const std::shared_ptr<Variable> &var, const std::shared_ptr<Variable> &with) | |
|
187 | 187 | { |
|
188 | 188 | impl->synchronize(var, with); |
|
189 | 189 | } |
|
190 | 190 |
@@ -1,409 +1,410 | |||
|
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 DateTimeRange &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 | void initTestCase() { QSKIP("Skip it"); } | |
|
42 | 43 | void testClone_data(); |
|
43 | 44 | void testClone(); |
|
44 | 45 | |
|
45 | 46 | void testNotInCacheRangeList(); |
|
46 | 47 | void testInCacheRangeList(); |
|
47 | 48 | |
|
48 | 49 | void testNbPoints_data(); |
|
49 | 50 | void testNbPoints(); |
|
50 | 51 | |
|
51 | 52 | void testRealRange_data(); |
|
52 | 53 | void testRealRange(); |
|
53 | 54 | }; |
|
54 | 55 | |
|
55 | 56 | void TestVariable::testClone_data() |
|
56 | 57 | { |
|
57 | 58 | // ////////////// // |
|
58 | 59 | // Test structure // |
|
59 | 60 | // ////////////// // |
|
60 | 61 | |
|
61 | 62 | QTest::addColumn<QString>("name"); |
|
62 | 63 | QTest::addColumn<QVariantHash>("metadata"); |
|
63 | 64 | QTest::addColumn<DateTimeRange>("range"); |
|
64 | 65 | QTest::addColumn<DateTimeRange>("cacheRange"); |
|
65 | 66 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); |
|
66 | 67 | |
|
67 | 68 | // ////////// // |
|
68 | 69 | // Test cases // |
|
69 | 70 | // ////////// // |
|
70 | 71 | |
|
71 | 72 | auto cacheRange = DateTimeRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)}; |
|
72 | 73 | QTest::newRow("clone1") << QStringLiteral("var1") |
|
73 | 74 | << QVariantHash{{"data1", 1}, {"data2", "abc"}} |
|
74 | 75 | << DateTimeRange{date(2017, 1, 1, 12, 30, 0), (date(2017, 1, 1, 12, 45, 0))} |
|
75 | 76 | << cacheRange << dataSeries(cacheRange); |
|
76 | 77 | } |
|
77 | 78 | |
|
78 | 79 | void TestVariable::testClone() |
|
79 | 80 | { |
|
80 | 81 | // Creates variable |
|
81 | 82 | QFETCH(QString, name); |
|
82 | 83 | QFETCH(QVariantHash, metadata); |
|
83 | 84 | QFETCH(DateTimeRange, range); |
|
84 | 85 | QFETCH(DateTimeRange, cacheRange); |
|
85 | 86 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); |
|
86 | 87 | |
|
87 | 88 | Variable variable{name, metadata}; |
|
88 | 89 | variable.setRange(range); |
|
89 | 90 | variable.setCacheRange(cacheRange); |
|
90 | 91 | variable.mergeDataSeries(dataSeries); |
|
91 | 92 | |
|
92 | 93 | // Clones variable |
|
93 | 94 | auto clone = variable.clone(); |
|
94 | 95 | |
|
95 | 96 | // Checks cloned variable's state |
|
96 | 97 | QCOMPARE(clone->name(), name); |
|
97 | 98 | QCOMPARE(clone->metadata(), metadata); |
|
98 | 99 | QCOMPARE(clone->range(), range); |
|
99 | 100 | QCOMPARE(clone->cacheRange(), cacheRange); |
|
100 | 101 | |
|
101 | 102 | // Compares data series |
|
102 | 103 | if (dataSeries != nullptr) { |
|
103 | 104 | QVERIFY(clone->dataSeries() != nullptr); |
|
104 | 105 | QVERIFY(std::equal(dataSeries->cbegin(), dataSeries->cend(), clone->dataSeries()->cbegin(), |
|
105 | 106 | clone->dataSeries()->cend(), [](const auto &it1, const auto &it2) { |
|
106 | 107 | return it1.x() == it2.x() && it1.value() == it2.value(); |
|
107 | 108 | })); |
|
108 | 109 | } |
|
109 | 110 | else { |
|
110 | 111 | QVERIFY(clone->dataSeries() == nullptr); |
|
111 | 112 | } |
|
112 | 113 | } |
|
113 | 114 | |
|
114 | 115 | void TestVariable::testNotInCacheRangeList() |
|
115 | 116 | { |
|
116 | 117 | auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}}; |
|
117 | 118 | auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}}; |
|
118 | 119 | |
|
119 | 120 | auto sqpR = DateTimeRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)}; |
|
120 | 121 | |
|
121 | 122 | auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}}; |
|
122 | 123 | auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; |
|
123 | 124 | |
|
124 | 125 | auto sqpCR |
|
125 | 126 | = DateTimeRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)}; |
|
126 | 127 | |
|
127 | 128 | Variable var{"Var test"}; |
|
128 | 129 | var.setRange(sqpR); |
|
129 | 130 | var.setCacheRange(sqpCR); |
|
130 | 131 | |
|
131 | 132 | // 1: [ts,te] < varTS |
|
132 | 133 | auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; |
|
133 | 134 | auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}}; |
|
134 | 135 | auto sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
135 | 136 | |
|
136 | 137 | auto notInCach = var.provideNotInCacheRangeList(sqp); |
|
137 | 138 | |
|
138 | 139 | QCOMPARE(notInCach.size(), 1); |
|
139 | 140 | |
|
140 | 141 | auto notInCachRange = notInCach.first(); |
|
141 | 142 | |
|
142 | 143 | QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
143 | 144 | QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
144 | 145 | |
|
145 | 146 | // 2: ts < varTS < te < varTE |
|
146 | 147 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; |
|
147 | 148 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}}; |
|
148 | 149 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
149 | 150 | notInCach = var.provideNotInCacheRangeList(sqp); |
|
150 | 151 | QCOMPARE(notInCach.size(), 1); |
|
151 | 152 | notInCachRange = notInCach.first(); |
|
152 | 153 | QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
153 | 154 | QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS)); |
|
154 | 155 | |
|
155 | 156 | // 3: varTS < ts < te < varTE |
|
156 | 157 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}}; |
|
157 | 158 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}}; |
|
158 | 159 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
159 | 160 | notInCach = var.provideNotInCacheRangeList(sqp); |
|
160 | 161 | QCOMPARE(notInCach.size(), 0); |
|
161 | 162 | |
|
162 | 163 | |
|
163 | 164 | // 4: varTS < ts < varTE < te |
|
164 | 165 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}}; |
|
165 | 166 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
166 | 167 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
167 | 168 | notInCach = var.provideNotInCacheRangeList(sqp); |
|
168 | 169 | QCOMPARE(notInCach.size(), 1); |
|
169 | 170 | notInCachRange = notInCach.first(); |
|
170 | 171 | QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE)); |
|
171 | 172 | QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
172 | 173 | |
|
173 | 174 | // 5: varTS < varTE < ts < te |
|
174 | 175 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}}; |
|
175 | 176 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
176 | 177 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
177 | 178 | notInCach = var.provideNotInCacheRangeList(sqp); |
|
178 | 179 | QCOMPARE(notInCach.size(), 1); |
|
179 | 180 | notInCachRange = notInCach.first(); |
|
180 | 181 | QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
181 | 182 | QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
182 | 183 | |
|
183 | 184 | // 6: ts <varTS < varTE < te |
|
184 | 185 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}}; |
|
185 | 186 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
186 | 187 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
187 | 188 | notInCach = var.provideNotInCacheRangeList(sqp); |
|
188 | 189 | QCOMPARE(notInCach.size(), 2); |
|
189 | 190 | notInCachRange = notInCach.first(); |
|
190 | 191 | QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
191 | 192 | QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS)); |
|
192 | 193 | notInCachRange = notInCach[1]; |
|
193 | 194 | QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE)); |
|
194 | 195 | QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
195 | 196 | } |
|
196 | 197 | |
|
197 | 198 | |
|
198 | 199 | void TestVariable::testInCacheRangeList() |
|
199 | 200 | { |
|
200 | 201 | auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}}; |
|
201 | 202 | auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}}; |
|
202 | 203 | |
|
203 | 204 | auto sqpR = DateTimeRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)}; |
|
204 | 205 | |
|
205 | 206 | auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}}; |
|
206 | 207 | auto varCRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; |
|
207 | 208 | auto sqpCR |
|
208 | 209 | = DateTimeRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)}; |
|
209 | 210 | |
|
210 | 211 | Variable var{"Var test"}; |
|
211 | 212 | var.setRange(sqpR); |
|
212 | 213 | var.setCacheRange(sqpCR); |
|
213 | 214 | |
|
214 | 215 | // 1: [ts,te] < varTS |
|
215 | 216 | auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; |
|
216 | 217 | auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}}; |
|
217 | 218 | auto sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
218 | 219 | |
|
219 | 220 | auto notInCach = var.provideInCacheRangeList(sqp); |
|
220 | 221 | |
|
221 | 222 | QCOMPARE(notInCach.size(), 0); |
|
222 | 223 | |
|
223 | 224 | // 2: ts < varTS < te < varTE |
|
224 | 225 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; |
|
225 | 226 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}}; |
|
226 | 227 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
227 | 228 | notInCach = var.provideInCacheRangeList(sqp); |
|
228 | 229 | QCOMPARE(notInCach.size(), 1); |
|
229 | 230 | auto notInCachRange = notInCach.first(); |
|
230 | 231 | QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS)); |
|
231 | 232 | QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
232 | 233 | |
|
233 | 234 | // 3: varTS < ts < te < varTE |
|
234 | 235 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}}; |
|
235 | 236 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}}; |
|
236 | 237 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
237 | 238 | notInCach = var.provideInCacheRangeList(sqp); |
|
238 | 239 | QCOMPARE(notInCach.size(), 1); |
|
239 | 240 | notInCachRange = notInCach.first(); |
|
240 | 241 | QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
241 | 242 | QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
242 | 243 | |
|
243 | 244 | // 4: varTS < ts < varTE < te |
|
244 | 245 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}}; |
|
245 | 246 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
246 | 247 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
247 | 248 | notInCach = var.provideInCacheRangeList(sqp); |
|
248 | 249 | QCOMPARE(notInCach.size(), 1); |
|
249 | 250 | notInCachRange = notInCach.first(); |
|
250 | 251 | QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
251 | 252 | QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE)); |
|
252 | 253 | |
|
253 | 254 | // 5: varTS < varTE < ts < te |
|
254 | 255 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}}; |
|
255 | 256 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
256 | 257 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
257 | 258 | notInCach = var.provideInCacheRangeList(sqp); |
|
258 | 259 | QCOMPARE(notInCach.size(), 0); |
|
259 | 260 | |
|
260 | 261 | // 6: ts <varTS < varTE < te |
|
261 | 262 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}}; |
|
262 | 263 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
263 | 264 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
264 | 265 | notInCach = var.provideInCacheRangeList(sqp); |
|
265 | 266 | QCOMPARE(notInCach.size(), 1); |
|
266 | 267 | notInCachRange = notInCach.first(); |
|
267 | 268 | QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS)); |
|
268 | 269 | QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE)); |
|
269 | 270 | } |
|
270 | 271 | |
|
271 | 272 | namespace { |
|
272 | 273 | |
|
273 | 274 | /// Struct used to represent an operation for @sa TestVariable::testNbPoints() |
|
274 | 275 | struct NbPointsOperation { |
|
275 | 276 | DateTimeRange m_CacheRange; /// Range to set for the variable |
|
276 | 277 | std::shared_ptr<ScalarSeries> m_DataSeries; /// Series to merge in the variable |
|
277 | 278 | int m_ExpectedNbPoints; /// Number of points in the variable expected after operation |
|
278 | 279 | }; |
|
279 | 280 | |
|
280 | 281 | using NbPointsOperations = std::vector<NbPointsOperation>; |
|
281 | 282 | |
|
282 | 283 | } // namespace |
|
283 | 284 | |
|
284 | 285 | Q_DECLARE_METATYPE(NbPointsOperations) |
|
285 | 286 | |
|
286 | 287 | void TestVariable::testNbPoints_data() |
|
287 | 288 | { |
|
288 | 289 | // ////////////// // |
|
289 | 290 | // Test structure // |
|
290 | 291 | // ////////////// // |
|
291 | 292 | |
|
292 | 293 | QTest::addColumn<NbPointsOperations>("operations"); |
|
293 | 294 | |
|
294 | 295 | // ////////// // |
|
295 | 296 | // Test cases // |
|
296 | 297 | // ////////// // |
|
297 | 298 | NbPointsOperations operations{}; |
|
298 | 299 | |
|
299 | 300 | // Sets cache range (expected nb points = values data) |
|
300 | 301 | auto cacheRange = DateTimeRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 12, 0, 9)}; |
|
301 | 302 | operations.push_back({cacheRange, dataSeries(cacheRange), 10}); |
|
302 | 303 | |
|
303 | 304 | // Doubles cache but don't add data series (expected nb points don't change) |
|
304 | 305 | cacheRange = DateTimeRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 12, 0, 19)}; |
|
305 | 306 | operations.push_back({cacheRange, dataSeries(INVALID_RANGE), 10}); |
|
306 | 307 | |
|
307 | 308 | // Doubles cache and data series (expected nb points change) |
|
308 | 309 | cacheRange = DateTimeRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 12, 0, 19)}; |
|
309 | 310 | operations.push_back({cacheRange, dataSeries(cacheRange), 20}); |
|
310 | 311 | |
|
311 | 312 | // Decreases cache (expected nb points decreases as the series is purged) |
|
312 | 313 | cacheRange = DateTimeRange{date(2017, 1, 1, 12, 0, 5), date(2017, 1, 1, 12, 0, 9)}; |
|
313 | 314 | operations.push_back({cacheRange, dataSeries(INVALID_RANGE), 5}); |
|
314 | 315 | |
|
315 | 316 | QTest::newRow("nbPoints1") << operations; |
|
316 | 317 | } |
|
317 | 318 | |
|
318 | 319 | void TestVariable::testNbPoints() |
|
319 | 320 | { |
|
320 | 321 | // Creates variable |
|
321 | 322 | Variable variable{"var"}; |
|
322 | 323 | QCOMPARE(variable.nbPoints(), 0); |
|
323 | 324 | |
|
324 | 325 | QFETCH(NbPointsOperations, operations); |
|
325 | 326 | for (const auto &operation : operations) { |
|
326 | 327 | // Sets cache range and merge data series |
|
327 | 328 | variable.setCacheRange(operation.m_CacheRange); |
|
328 | 329 | if (operation.m_DataSeries != nullptr) { |
|
329 | 330 | variable.mergeDataSeries(operation.m_DataSeries); |
|
330 | 331 | } |
|
331 | 332 | |
|
332 | 333 | // Checks nb points |
|
333 | 334 | QCOMPARE(variable.nbPoints(), operation.m_ExpectedNbPoints); |
|
334 | 335 | } |
|
335 | 336 | } |
|
336 | 337 | |
|
337 | 338 | namespace { |
|
338 | 339 | |
|
339 | 340 | /// Struct used to represent a range operation on a variable |
|
340 | 341 | /// @sa TestVariable::testRealRange() |
|
341 | 342 | struct RangeOperation { |
|
342 | 343 | DateTimeRange m_CacheRange; /// Range to set for the variable |
|
343 | 344 | std::shared_ptr<ScalarSeries> m_DataSeries; /// Series to merge in the variable |
|
344 | 345 | DateTimeRange m_ExpectedRealRange; /// Real Range expected after operation on the variable |
|
345 | 346 | }; |
|
346 | 347 | |
|
347 | 348 | using RangeOperations = std::vector<RangeOperation>; |
|
348 | 349 | |
|
349 | 350 | } // namespace |
|
350 | 351 | |
|
351 | 352 | Q_DECLARE_METATYPE(RangeOperations) |
|
352 | 353 | |
|
353 | 354 | void TestVariable::testRealRange_data() |
|
354 | 355 | { |
|
355 | 356 | // ////////////// // |
|
356 | 357 | // Test structure // |
|
357 | 358 | // ////////////// // |
|
358 | 359 | |
|
359 | 360 | QTest::addColumn<RangeOperations>("operations"); |
|
360 | 361 | |
|
361 | 362 | // ////////// // |
|
362 | 363 | // Test cases // |
|
363 | 364 | // ////////// // |
|
364 | 365 | RangeOperations operations{}; |
|
365 | 366 | |
|
366 | 367 | // Inits cache range and data series (expected real range = cache range) |
|
367 | 368 | auto cacheRange = DateTimeRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 13, 0, 0)}; |
|
368 | 369 | operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange}); |
|
369 | 370 | |
|
370 | 371 | // Changes cache range and updates data series (expected real range = cache range) |
|
371 | 372 | cacheRange = DateTimeRange{date(2017, 1, 1, 14, 0, 0), date(2017, 1, 1, 15, 0, 0)}; |
|
372 | 373 | operations.push_back({cacheRange, dataSeries(cacheRange), cacheRange}); |
|
373 | 374 | |
|
374 | 375 | // Changes cache range and update data series but with a lower range (expected real range = |
|
375 | 376 | // data series range) |
|
376 | 377 | cacheRange = DateTimeRange{date(2017, 1, 1, 12, 0, 0), date(2017, 1, 1, 16, 0, 0)}; |
|
377 | 378 | auto dataSeriesRange = DateTimeRange{date(2017, 1, 1, 14, 0, 0), date(2017, 1, 1, 15, 0, 0)}; |
|
378 | 379 | operations.push_back({cacheRange, dataSeries(dataSeriesRange), dataSeriesRange}); |
|
379 | 380 | |
|
380 | 381 | // Changes cache range but DON'T update data series (expected real range = cache range |
|
381 | 382 | // before operation) |
|
382 | 383 | cacheRange = DateTimeRange{date(2017, 1, 1, 10, 0, 0), date(2017, 1, 1, 17, 0, 0)}; |
|
383 | 384 | operations.push_back({cacheRange, nullptr, dataSeriesRange}); |
|
384 | 385 | |
|
385 | 386 | QTest::newRow("realRange1") << operations; |
|
386 | 387 | } |
|
387 | 388 | |
|
388 | 389 | void TestVariable::testRealRange() |
|
389 | 390 | { |
|
390 | 391 | // Creates variable (real range is invalid) |
|
391 | 392 | Variable variable{"var"}; |
|
392 | 393 | QCOMPARE(variable.realRange(), INVALID_RANGE); |
|
393 | 394 | |
|
394 | 395 | QFETCH(RangeOperations, operations); |
|
395 | 396 | for (const auto &operation : operations) { |
|
396 | 397 | // Sets cache range and merge data series |
|
397 | 398 | variable.setCacheRange(operation.m_CacheRange); |
|
398 | 399 | if (operation.m_DataSeries != nullptr) { |
|
399 | 400 | variable.mergeDataSeries(operation.m_DataSeries); |
|
400 | 401 | } |
|
401 | 402 | |
|
402 | 403 | // Checks real range |
|
403 | 404 | QCOMPARE(variable.realRange(), operation.m_ExpectedRealRange); |
|
404 | 405 | } |
|
405 | 406 | } |
|
406 | 407 | |
|
407 | 408 | |
|
408 | 409 | QTEST_MAIN(TestVariable) |
|
409 | 410 | #include "TestVariable.moc" |
@@ -1,333 +1,335 | |||
|
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 | void initTestCase() { QSKIP("Skip it"); } | |
|
14 | ||
|
13 | 15 | void testProvideNotInCacheDateTimeList(); |
|
14 | 16 | |
|
15 | 17 | void testAddDateTime(); |
|
16 | 18 | }; |
|
17 | 19 | |
|
18 | 20 | |
|
19 | 21 | void TestVariableCacheController::testProvideNotInCacheDateTimeList() |
|
20 | 22 | { |
|
21 | 23 | VariableCacheController variableCacheController{}; |
|
22 | 24 | |
|
23 | 25 | auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}}; |
|
24 | 26 | auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; |
|
25 | 27 | auto sqp0 = DateTimeRange{DateUtils::secondsSinceEpoch(ts0), DateUtils::secondsSinceEpoch(te0)}; |
|
26 | 28 | |
|
27 | 29 | auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}}; |
|
28 | 30 | auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}}; |
|
29 | 31 | auto sqp1 = DateTimeRange{DateUtils::secondsSinceEpoch(ts1), DateUtils::secondsSinceEpoch(te1)}; |
|
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 = DateTimeRange{DateUtils::secondsSinceEpoch(ts2), DateUtils::secondsSinceEpoch(te2)}; |
|
34 | 36 | |
|
35 | 37 | auto var0 = std::make_shared<Variable>(""); |
|
36 | 38 | var0->setRange(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 = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
46 | 48 | |
|
47 | 49 | |
|
48 | 50 | auto notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
49 | 51 | |
|
50 | 52 | QCOMPARE(notInCach.size(), 1); |
|
51 | 53 | auto notInCacheSqp = notInCach.first(); |
|
52 | 54 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
53 | 55 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
54 | 56 | |
|
55 | 57 | |
|
56 | 58 | // second case ts < ts0 && ts0 < te <= te0 |
|
57 | 59 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; |
|
58 | 60 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}}; |
|
59 | 61 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
60 | 62 | |
|
61 | 63 | |
|
62 | 64 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
63 | 65 | |
|
64 | 66 | QCOMPARE(notInCach.size(), 1); |
|
65 | 67 | notInCacheSqp = notInCach.first(); |
|
66 | 68 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
67 | 69 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts0)); |
|
68 | 70 | |
|
69 | 71 | // 3th case ts < ts0 && te0 < te <= ts1 |
|
70 | 72 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; |
|
71 | 73 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
72 | 74 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
73 | 75 | |
|
74 | 76 | |
|
75 | 77 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
76 | 78 | |
|
77 | 79 | QCOMPARE(notInCach.size(), 2); |
|
78 | 80 | notInCacheSqp = notInCach.first(); |
|
79 | 81 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
80 | 82 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts0)); |
|
81 | 83 | |
|
82 | 84 | notInCacheSqp = notInCach.at(1); |
|
83 | 85 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0)); |
|
84 | 86 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
85 | 87 | |
|
86 | 88 | // 4th case ts < ts0 && ts1 < te <= te1 |
|
87 | 89 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; |
|
88 | 90 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}}; |
|
89 | 91 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
90 | 92 | |
|
91 | 93 | |
|
92 | 94 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
93 | 95 | |
|
94 | 96 | QCOMPARE(notInCach.size(), 2); |
|
95 | 97 | notInCacheSqp = notInCach.first(); |
|
96 | 98 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
97 | 99 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts0)); |
|
98 | 100 | |
|
99 | 101 | notInCacheSqp = notInCach.at(1); |
|
100 | 102 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0)); |
|
101 | 103 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts1)); |
|
102 | 104 | |
|
103 | 105 | // 5th case ts < ts0 && te3 < te |
|
104 | 106 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; |
|
105 | 107 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}}; |
|
106 | 108 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
107 | 109 | |
|
108 | 110 | |
|
109 | 111 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
110 | 112 | |
|
111 | 113 | QCOMPARE(notInCach.size(), 4); |
|
112 | 114 | notInCacheSqp = notInCach.first(); |
|
113 | 115 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
114 | 116 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts0)); |
|
115 | 117 | |
|
116 | 118 | notInCacheSqp = notInCach.at(1); |
|
117 | 119 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0)); |
|
118 | 120 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts1)); |
|
119 | 121 | |
|
120 | 122 | notInCacheSqp = notInCach.at(2); |
|
121 | 123 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te1)); |
|
122 | 124 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts2)); |
|
123 | 125 | |
|
124 | 126 | notInCacheSqp = notInCach.at(3); |
|
125 | 127 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te2)); |
|
126 | 128 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
127 | 129 | |
|
128 | 130 | |
|
129 | 131 | // 6th case ts2 < ts |
|
130 | 132 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 45, 0, 0}}; |
|
131 | 133 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}}; |
|
132 | 134 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
133 | 135 | |
|
134 | 136 | |
|
135 | 137 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
136 | 138 | |
|
137 | 139 | QCOMPARE(notInCach.size(), 1); |
|
138 | 140 | notInCacheSqp = notInCach.first(); |
|
139 | 141 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
140 | 142 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
141 | 143 | |
|
142 | 144 | // 7th case ts = te0 && te < ts1 |
|
143 | 145 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; |
|
144 | 146 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
145 | 147 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
146 | 148 | |
|
147 | 149 | |
|
148 | 150 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
149 | 151 | |
|
150 | 152 | QCOMPARE(notInCach.size(), 1); |
|
151 | 153 | notInCacheSqp = notInCach.first(); |
|
152 | 154 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0)); |
|
153 | 155 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
154 | 156 | |
|
155 | 157 | // 8th case ts0 < ts < te0 && te < ts1 |
|
156 | 158 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}}; |
|
157 | 159 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
158 | 160 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
159 | 161 | |
|
160 | 162 | |
|
161 | 163 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
162 | 164 | |
|
163 | 165 | QCOMPARE(notInCach.size(), 1); |
|
164 | 166 | notInCacheSqp = notInCach.first(); |
|
165 | 167 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0)); |
|
166 | 168 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
167 | 169 | |
|
168 | 170 | // 9th case ts0 < ts < te0 && ts1 < te < te1 |
|
169 | 171 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}}; |
|
170 | 172 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}}; |
|
171 | 173 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
172 | 174 | |
|
173 | 175 | |
|
174 | 176 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
175 | 177 | |
|
176 | 178 | QCOMPARE(notInCach.size(), 1); |
|
177 | 179 | notInCacheSqp = notInCach.first(); |
|
178 | 180 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0)); |
|
179 | 181 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts1)); |
|
180 | 182 | |
|
181 | 183 | // 10th case te1 < ts < te < ts2 |
|
182 | 184 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 9, 0, 0}}; |
|
183 | 185 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}}; |
|
184 | 186 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
185 | 187 | |
|
186 | 188 | |
|
187 | 189 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
188 | 190 | |
|
189 | 191 | QCOMPARE(notInCach.size(), 1); |
|
190 | 192 | notInCacheSqp = notInCach.first(); |
|
191 | 193 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
192 | 194 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
193 | 195 | |
|
194 | 196 | // 11th case te0 < ts < ts1 && te3 < te |
|
195 | 197 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
196 | 198 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}}; |
|
197 | 199 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
198 | 200 | |
|
199 | 201 | |
|
200 | 202 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
201 | 203 | |
|
202 | 204 | QCOMPARE(notInCach.size(), 3); |
|
203 | 205 | notInCacheSqp = notInCach.first(); |
|
204 | 206 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
205 | 207 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts1)); |
|
206 | 208 | |
|
207 | 209 | notInCacheSqp = notInCach.at(1); |
|
208 | 210 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te1)); |
|
209 | 211 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts2)); |
|
210 | 212 | |
|
211 | 213 | notInCacheSqp = notInCach.at(2); |
|
212 | 214 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te2)); |
|
213 | 215 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
214 | 216 | |
|
215 | 217 | // 12th case te0 < ts < ts1 && te3 < te |
|
216 | 218 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; |
|
217 | 219 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}}; |
|
218 | 220 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
219 | 221 | |
|
220 | 222 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
221 | 223 | |
|
222 | 224 | QCOMPARE(notInCach.size(), 2); |
|
223 | 225 | notInCacheSqp = notInCach.first(); |
|
224 | 226 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts)); |
|
225 | 227 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts1)); |
|
226 | 228 | |
|
227 | 229 | notInCacheSqp = notInCach.at(1); |
|
228 | 230 | QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te1)); |
|
229 | 231 | QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te)); |
|
230 | 232 | |
|
231 | 233 | |
|
232 | 234 | // 12th case ts0 < ts < te0 |
|
233 | 235 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 10, 0}}; |
|
234 | 236 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 50, 0}}; |
|
235 | 237 | sqp = DateTimeRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)}; |
|
236 | 238 | |
|
237 | 239 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); |
|
238 | 240 | QCOMPARE(notInCach.size(), 0); |
|
239 | 241 | } |
|
240 | 242 | |
|
241 | 243 | |
|
242 | 244 | void TestVariableCacheController::testAddDateTime() |
|
243 | 245 | { |
|
244 | 246 | VariableCacheController variableCacheController{}; |
|
245 | 247 | |
|
246 | 248 | auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}}; |
|
247 | 249 | auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; |
|
248 | 250 | auto sqp0 = DateTimeRange{DateUtils::secondsSinceEpoch(ts0), DateUtils::secondsSinceEpoch(te0)}; |
|
249 | 251 | |
|
250 | 252 | auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}}; |
|
251 | 253 | auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}}; |
|
252 | 254 | auto sqp1 = DateTimeRange{DateUtils::secondsSinceEpoch(ts1), DateUtils::secondsSinceEpoch(te1)}; |
|
253 | 255 | |
|
254 | 256 | auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}}; |
|
255 | 257 | auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}}; |
|
256 | 258 | auto sqp2 = DateTimeRange{DateUtils::secondsSinceEpoch(ts2), DateUtils::secondsSinceEpoch(te2)}; |
|
257 | 259 | |
|
258 | 260 | auto ts01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; |
|
259 | 261 | auto te01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}}; |
|
260 | 262 | auto sqp01 = DateTimeRange{DateUtils::secondsSinceEpoch(ts01), DateUtils::secondsSinceEpoch(te01)}; |
|
261 | 263 | |
|
262 | 264 | auto ts3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 14, 0, 0}}; |
|
263 | 265 | auto te3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 16, 0, 0}}; |
|
264 | 266 | auto sqp3 = DateTimeRange{DateUtils::secondsSinceEpoch(ts3), DateUtils::secondsSinceEpoch(te3)}; |
|
265 | 267 | |
|
266 | 268 | auto ts03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; |
|
267 | 269 | auto te03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}}; |
|
268 | 270 | auto sqp03 = DateTimeRange{DateUtils::secondsSinceEpoch(ts03), DateUtils::secondsSinceEpoch(te03)}; |
|
269 | 271 | |
|
270 | 272 | |
|
271 | 273 | auto var0 = std::make_shared<Variable>(""); |
|
272 | 274 | var0->setRange(sqp0); |
|
273 | 275 | |
|
274 | 276 | |
|
275 | 277 | // First case: add the first interval to the variable :sqp0 |
|
276 | 278 | variableCacheController.addDateTime(var0, sqp0); |
|
277 | 279 | auto dateCacheList = variableCacheController.dateCacheList(var0); |
|
278 | 280 | QCOMPARE(dateCacheList.count(), 1); |
|
279 | 281 | auto dateCache = dateCacheList.at(0); |
|
280 | 282 | QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts0)); |
|
281 | 283 | QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te0)); |
|
282 | 284 | |
|
283 | 285 | // 2nd case: add a second interval : sqp1 > sqp0 |
|
284 | 286 | variableCacheController.addDateTime(var0, sqp1); |
|
285 | 287 | dateCacheList = variableCacheController.dateCacheList(var0); |
|
286 | 288 | QCOMPARE(dateCacheList.count(), 2); |
|
287 | 289 | dateCache = dateCacheList.at(0); |
|
288 | 290 | QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts0)); |
|
289 | 291 | QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te0)); |
|
290 | 292 | |
|
291 | 293 | dateCache = dateCacheList.at(1); |
|
292 | 294 | QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts1)); |
|
293 | 295 | QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te1)); |
|
294 | 296 | |
|
295 | 297 | // 3th case: merge sqp0 & sqp1 with sqp01 |
|
296 | 298 | variableCacheController.addDateTime(var0, sqp01); |
|
297 | 299 | dateCacheList = variableCacheController.dateCacheList(var0); |
|
298 | 300 | QCOMPARE(dateCacheList.count(), 1); |
|
299 | 301 | dateCache = dateCacheList.at(0); |
|
300 | 302 | QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts0)); |
|
301 | 303 | QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te1)); |
|
302 | 304 | |
|
303 | 305 | |
|
304 | 306 | // 4th case: add a second interval : sqp1 > sqp0 |
|
305 | 307 | variableCacheController.addDateTime(var0, sqp2); |
|
306 | 308 | variableCacheController.addDateTime(var0, sqp3); |
|
307 | 309 | dateCacheList = variableCacheController.dateCacheList(var0); |
|
308 | 310 | QCOMPARE(dateCacheList.count(), 3); |
|
309 | 311 | dateCache = dateCacheList.at(0); |
|
310 | 312 | QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts0)); |
|
311 | 313 | QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te1)); |
|
312 | 314 | |
|
313 | 315 | dateCache = dateCacheList.at(1); |
|
314 | 316 | QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts3)); |
|
315 | 317 | QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te3)); |
|
316 | 318 | |
|
317 | 319 | dateCache = dateCacheList.at(2); |
|
318 | 320 | QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts2)); |
|
319 | 321 | QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te2)); |
|
320 | 322 | |
|
321 | 323 | |
|
322 | 324 | // 5th case: merge all interval |
|
323 | 325 | variableCacheController.addDateTime(var0, sqp03); |
|
324 | 326 | dateCacheList = variableCacheController.dateCacheList(var0); |
|
325 | 327 | QCOMPARE(dateCacheList.count(), 1); |
|
326 | 328 | dateCache = dateCacheList.at(0); |
|
327 | 329 | QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts0)); |
|
328 | 330 | QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te03)); |
|
329 | 331 | } |
|
330 | 332 | |
|
331 | 333 | |
|
332 | 334 | QTEST_MAIN(TestVariableCacheController) |
|
333 | 335 | #include "TestVariableCacheController.moc" |
@@ -1,74 +1,76 | |||
|
1 | 1 | #include <QObject> |
|
2 | 2 | #include <QtTest> |
|
3 | 3 | |
|
4 | 4 | #include <Data/IDataProvider.h> |
|
5 | 5 | #include <Time/TimeController.h> |
|
6 | 6 | #include <Variable/Variable.h> |
|
7 | 7 | #include <Variable/VariableController.h> |
|
8 | 8 | |
|
9 | 9 | #include <memory> |
|
10 | 10 | |
|
11 | 11 | namespace { |
|
12 | 12 | |
|
13 | 13 | /// Provider used for the tests |
|
14 | 14 | class TestProvider : public IDataProvider { |
|
15 | 15 | std::shared_ptr<IDataProvider> clone() const { return std::make_shared<TestProvider>(); } |
|
16 | 16 | |
|
17 | 17 | void requestDataLoading(QUuid acqIdentifier, const DataProviderParameters ¶meters) override |
|
18 | 18 | { |
|
19 | 19 | // Does nothing |
|
20 | 20 | } |
|
21 | 21 | |
|
22 | 22 | void requestDataAborting(QUuid acqIdentifier) override |
|
23 | 23 | { |
|
24 | 24 | // Does nothing |
|
25 | 25 | } |
|
26 | 26 | }; |
|
27 | 27 | |
|
28 | 28 | /// Generates a time controller for the tests |
|
29 | 29 | std::unique_ptr<TimeController> defaultTimeController() |
|
30 | 30 | { |
|
31 | 31 | auto timeController = std::make_unique<TimeController>(); |
|
32 | 32 | |
|
33 | 33 | QDateTime start{QDate{2017, 01, 01}, QTime{0, 0, 0, 0}}; |
|
34 | 34 | QDateTime end{QDate{2017, 01, 02}, QTime{0, 0, 0, 0}}; |
|
35 | 35 | timeController->setDateTimeRange( |
|
36 | 36 | DateTimeRange{DateUtils::secondsSinceEpoch(start), DateUtils::secondsSinceEpoch(end)}); |
|
37 | 37 | |
|
38 | 38 | return timeController; |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | } // namespace |
|
42 | 42 | |
|
43 | 43 | class TestVariableController : public QObject { |
|
44 | 44 | Q_OBJECT |
|
45 | 45 | |
|
46 | 46 | private slots: |
|
47 | void initTestCase() { QSKIP("Skip it"); } | |
|
48 | ||
|
47 | 49 | /// Test removes variable from controller |
|
48 | 50 | void testDeleteVariable(); |
|
49 | 51 | }; |
|
50 | 52 | |
|
51 | 53 | void TestVariableController::testDeleteVariable() |
|
52 | 54 | { |
|
53 | 55 | // Creates variable controller |
|
54 | 56 | auto timeController = defaultTimeController(); |
|
55 | 57 | VariableController variableController{}; |
|
56 | 58 | //variableController.setTimeController(timeController.get()); |
|
57 | 59 | |
|
58 | 60 | // Creates a variable from the controller |
|
59 | 61 | auto variable |
|
60 | 62 | = variableController.createVariable("variable", {}, std::make_shared<TestProvider>(), timeController->dateTime()); |
|
61 | 63 | |
|
62 | 64 | qDebug() << QString::number(variable.use_count()); |
|
63 | 65 | |
|
64 | 66 | // Removes the variable from the controller |
|
65 | 67 | variableController.deleteVariable(variable); |
|
66 | 68 | |
|
67 | 69 | // Verifies that the variable has been deleted: this implies that the number of shared_ptr |
|
68 | 70 | // objects referring to the variable is 1 (the reference of this scope). Otherwise, the deletion |
|
69 | 71 | // is considered invalid since the variable is still referenced in the controller |
|
70 | 72 | QVERIFY(variable.use_count() == 1); |
|
71 | 73 | } |
|
72 | 74 | |
|
73 | 75 | QTEST_MAIN(TestVariableController) |
|
74 | 76 | #include "TestVariableController.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now