##// END OF EJS Templates
Merge pull request 231 from SCIQLOP-Initialisation develop...
perrinel -
r603:ea9f3c3ed3a9 merge
parent child
Show More
@@ -0,0 +1,82
1 #ifndef SCIQLOP_DATASERIESITERATOR_H
2 #define SCIQLOP_DATASERIESITERATOR_H
3
4 #include "CoreGlobal.h"
5
6 #include <memory>
7
8 /**
9 * @brief The DataSeriesIteratorValue class represents the current value of a data series iterator.
10 * It offers standard access methods for the data in the series (x-axis, values), but it is up to
11 * each series to define its own implementation of how to retrieve this data, by implementing the
12 * DataSeriesIteratorValue::Impl interface
13 *
14 * @sa DataSeriesIterator
15 */
16 class SCIQLOP_CORE_EXPORT DataSeriesIteratorValue {
17 public:
18 struct Impl {
19 virtual ~Impl() noexcept = default;
20 virtual std::unique_ptr<Impl> clone() const = 0;
21 virtual bool equals(const Impl &other) const = 0;
22 virtual void next() = 0;
23 virtual void prev() = 0;
24 virtual double x() const = 0;
25 virtual double value() const = 0;
26 virtual double value(int componentIndex) const = 0;
27 };
28
29 explicit DataSeriesIteratorValue(std::unique_ptr<Impl> impl);
30 DataSeriesIteratorValue(const DataSeriesIteratorValue &other);
31 DataSeriesIteratorValue(DataSeriesIteratorValue &&other) = default;
32 DataSeriesIteratorValue &operator=(DataSeriesIteratorValue other);
33
34 bool equals(const DataSeriesIteratorValue &other) const;
35
36 /// Advances to the next value
37 void next();
38 /// Moves back to the previous value
39 void prev();
40 /// Gets x-axis data
41 double x() const;
42 /// Gets value data
43 double value() const;
44 /// Gets value data depending on an index
45 double value(int componentIndex) const;
46
47 private:
48 std::unique_ptr<Impl> m_Impl;
49 };
50
51 /**
52 * @brief The DataSeriesIterator class represents an iterator used for data series. It defines all
53 * operators needed for a standard forward iterator
54 * @sa http://www.cplusplus.com/reference/iterator/
55 */
56 class SCIQLOP_CORE_EXPORT DataSeriesIterator {
57 public:
58 using iterator_category = std::forward_iterator_tag;
59 using value_type = const DataSeriesIteratorValue;
60 using difference_type = std::ptrdiff_t;
61 using pointer = value_type *;
62 using reference = value_type &;
63
64 explicit DataSeriesIterator(DataSeriesIteratorValue value);
65 virtual ~DataSeriesIterator() noexcept = default;
66 DataSeriesIterator(const DataSeriesIterator &) = default;
67 DataSeriesIterator(DataSeriesIterator &&) = default;
68 DataSeriesIterator &operator=(const DataSeriesIterator &) = default;
69 DataSeriesIterator &operator=(DataSeriesIterator &&) = default;
70
71 DataSeriesIterator &operator++();
72 DataSeriesIterator &operator--();
73 pointer operator->() const { return &m_CurrentValue; }
74 reference operator*() const { return m_CurrentValue; }
75 bool operator==(const DataSeriesIterator &other) const;
76 bool operator!=(const DataSeriesIterator &other) const;
77
78 private:
79 DataSeriesIteratorValue m_CurrentValue;
80 };
81
82 #endif // SCIQLOP_DATASERIESITERATOR_H
@@ -0,0 +1,75
1 #include "Data/DataSeriesIterator.h"
2
3 DataSeriesIteratorValue::DataSeriesIteratorValue(
4 std::unique_ptr<DataSeriesIteratorValue::Impl> impl)
5 : m_Impl{std::move(impl)}
6 {
7 }
8
9 DataSeriesIteratorValue::DataSeriesIteratorValue(const DataSeriesIteratorValue &other)
10 : m_Impl{other.m_Impl->clone()}
11 {
12 }
13
14 DataSeriesIteratorValue &DataSeriesIteratorValue::operator=(DataSeriesIteratorValue other)
15 {
16 std::swap(m_Impl, other.m_Impl);
17 return *this;
18 }
19
20 bool DataSeriesIteratorValue::equals(const DataSeriesIteratorValue &other) const
21 {
22 return m_Impl->equals(*other.m_Impl);
23 }
24
25 void DataSeriesIteratorValue::next()
26 {
27 m_Impl->next();
28 }
29
30 void DataSeriesIteratorValue::prev()
31 {
32 m_Impl->prev();
33 }
34
35 double DataSeriesIteratorValue::x() const
36 {
37 return m_Impl->x();
38 }
39
40 double DataSeriesIteratorValue::value() const
41 {
42 return m_Impl->value();
43 }
44
45 double DataSeriesIteratorValue::value(int componentIndex) const
46 {
47 return m_Impl->value(componentIndex);
48 }
49
50 DataSeriesIterator::DataSeriesIterator(DataSeriesIteratorValue value)
51 : m_CurrentValue{std::move(value)}
52 {
53 }
54
55 DataSeriesIterator &DataSeriesIterator::operator++()
56 {
57 m_CurrentValue.next();
58 return *this;
59 }
60
61 DataSeriesIterator &DataSeriesIterator::operator--()
62 {
63 m_CurrentValue.prev();
64 return *this;
65 }
66
67 bool DataSeriesIterator::operator==(const DataSeriesIterator &other) const
68 {
69 return m_CurrentValue.equals(other.m_CurrentValue);
70 }
71
72 bool DataSeriesIterator::operator!=(const DataSeriesIterator &other) const
73 {
74 return !(*this == other);
75 }
@@ -1,315 +1,328
1 1 #ifndef SCIQLOP_ARRAYDATA_H
2 2 #define SCIQLOP_ARRAYDATA_H
3 3
4 4 #include <Common/SortUtils.h>
5 5
6 6 #include <QReadLocker>
7 7 #include <QReadWriteLock>
8 8 #include <QVector>
9 9
10 10 #include <memory>
11 11
12 12 template <int Dim>
13 13 class ArrayData;
14 14
15 15 using DataContainer = QVector<QVector<double> >;
16 16
17 17 namespace arraydata_detail {
18 18
19 19 /// Struct used to sort ArrayData
20 20 template <int Dim>
21 21 struct Sort {
22 22 static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data,
23 23 const std::vector<int> &sortPermutation)
24 24 {
25 25 auto nbComponents = data.size();
26 26 auto sortedData = DataContainer(nbComponents);
27 27
28 28 for (auto i = 0; i < nbComponents; ++i) {
29 29 sortedData[i] = SortUtils::sort(data.at(i), sortPermutation);
30 30 }
31 31
32 32 return std::make_shared<ArrayData<Dim> >(std::move(sortedData));
33 33 }
34 34 };
35 35
36 36 /// Specialization for uni-dimensional ArrayData
37 37 template <>
38 38 struct Sort<1> {
39 39 static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data,
40 40 const std::vector<int> &sortPermutation)
41 41 {
42 42 return std::make_shared<ArrayData<1> >(SortUtils::sort(data.at(0), sortPermutation));
43 43 }
44 44 };
45 45
46 46 } // namespace arraydata_detail
47 47
48 48 /**
49 49 * @brief The ArrayData class represents a dataset for a data series.
50 50 *
51 51 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
52 52 * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same
53 53 * number of values
54 54 *
55 55 * @tparam Dim the dimension of the ArrayData (one or two)
56 56 * @sa IDataSeries
57 57 */
58 58 template <int Dim>
59 59 class ArrayData {
60 60 public:
61 61 class IteratorValue {
62 62 public:
63 63 explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{}
64 64 {
65 65 for (auto i = 0; i < container.size(); ++i) {
66 66 m_Its.push_back(begin ? container.at(i).cbegin() : container.at(i).cend());
67 67 }
68 68 }
69 69
70 70 double at(int index) const { return *m_Its.at(index); }
71 71 double first() const { return *m_Its.front(); }
72 72
73 73 void next()
74 74 {
75 75 for (auto &it : m_Its) {
76 76 ++it;
77 77 }
78 78 }
79 79
80 void prev()
81 {
82 for (auto &it : m_Its) {
83 --it;
84 }
85 }
86
80 87 bool operator==(const IteratorValue &other) const { return m_Its == other.m_Its; }
81 88
82 89 private:
83 90 std::vector<DataContainer::value_type::const_iterator> m_Its;
84 91 };
85 92
86 93 class Iterator {
87 94 public:
88 95 using iterator_category = std::forward_iterator_tag;
89 96 using value_type = const IteratorValue;
90 97 using difference_type = std::ptrdiff_t;
91 98 using pointer = value_type *;
92 99 using reference = value_type &;
93 100
94 101 Iterator(const DataContainer &container, bool begin) : m_CurrentValue{container, begin} {}
95 102
96 103 virtual ~Iterator() noexcept = default;
97 104 Iterator(const Iterator &) = default;
98 105 Iterator(Iterator &&) = default;
99 106 Iterator &operator=(const Iterator &) = default;
100 107 Iterator &operator=(Iterator &&) = default;
101 108
102 109 Iterator &operator++()
103 110 {
104 111 m_CurrentValue.next();
105 112 return *this;
106 113 }
107 114
115 Iterator &operator--()
116 {
117 m_CurrentValue.prev();
118 return *this;
119 }
120
108 121 pointer operator->() const { return &m_CurrentValue; }
109 122 reference operator*() const { return m_CurrentValue; }
110 123
111 124 bool operator==(const Iterator &other) const
112 125 {
113 126 return m_CurrentValue == other.m_CurrentValue;
114 127 }
115 128
116 129 bool operator!=(const Iterator &other) const { return !(*this == other); }
117 130
118 131 private:
119 132 IteratorValue m_CurrentValue;
120 133 };
121 134
122 135 // ///// //
123 136 // Ctors //
124 137 // ///// //
125 138
126 139 /**
127 140 * Ctor for a unidimensional ArrayData
128 141 * @param data the data the ArrayData will hold
129 142 */
130 143 template <int D = Dim, typename = std::enable_if_t<D == 1> >
131 144 explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}}
132 145 {
133 146 m_Data[0] = std::move(data);
134 147 }
135 148
136 149 /**
137 150 * Ctor for a two-dimensional ArrayData. The number of components (number of vectors) must be
138 151 * greater than 2 and each component must have the same number of values
139 152 * @param data the data the ArrayData will hold
140 153 * @throws std::invalid_argument if the number of components is less than 2
141 154 * @remarks if the number of values is not the same for each component, no value is set
142 155 */
143 156 template <int D = Dim, typename = std::enable_if_t<D == 2> >
144 157 explicit ArrayData(DataContainer data)
145 158 {
146 159 auto nbComponents = data.size();
147 160 if (nbComponents < 2) {
148 161 throw std::invalid_argument{
149 162 QString{"A multidimensional ArrayData must have at least 2 components (found: %1"}
150 163 .arg(data.size())
151 164 .toStdString()};
152 165 }
153 166
154 167 auto nbValues = data.front().size();
155 168 if (std::all_of(data.cbegin(), data.cend(), [nbValues](const auto &component) {
156 169 return component.size() == nbValues;
157 170 })) {
158 171 m_Data = std::move(data);
159 172 }
160 173 else {
161 174 m_Data = DataContainer{nbComponents, QVector<double>{}};
162 175 }
163 176 }
164 177
165 178 /// Copy ctor
166 179 explicit ArrayData(const ArrayData &other)
167 180 {
168 181 QReadLocker otherLocker{&other.m_Lock};
169 182 m_Data = other.m_Data;
170 183 }
171 184
172 185 // /////////////// //
173 186 // General methods //
174 187 // /////////////// //
175 188
176 189 /**
177 190 * Merges into the array data an other array data. The two array datas must have the same number
178 191 * of components so the merge can be done
179 192 * @param other the array data to merge with
180 193 * @param prepend if true, the other array data is inserted at the beginning, otherwise it is
181 194 * inserted at the end
182 195 */
183 196 void add(const ArrayData<Dim> &other, bool prepend = false)
184 197 {
185 198 QWriteLocker locker{&m_Lock};
186 199 QReadLocker otherLocker{&other.m_Lock};
187 200
188 201 auto nbComponents = m_Data.size();
189 202 if (nbComponents != other.m_Data.size()) {
190 203 return;
191 204 }
192 205
193 206 for (auto componentIndex = 0; componentIndex < nbComponents; ++componentIndex) {
194 207 if (prepend) {
195 208 const auto &otherData = other.data(componentIndex);
196 209 const auto otherDataSize = otherData.size();
197 210
198 211 auto &data = m_Data[componentIndex];
199 212 data.insert(data.begin(), otherDataSize, 0.);
200 213
201 214 for (auto i = 0; i < otherDataSize; ++i) {
202 215 data.replace(i, otherData.at(i));
203 216 }
204 217 }
205 218 else {
206 219 m_Data[componentIndex] += other.data(componentIndex);
207 220 }
208 221 }
209 222 }
210 223
211 224 void clear()
212 225 {
213 226 QWriteLocker locker{&m_Lock};
214 227
215 228 auto nbComponents = m_Data.size();
216 229 for (auto i = 0; i < nbComponents; ++i) {
217 230 m_Data[i].clear();
218 231 }
219 232 }
220 233
221 234 int componentCount() const noexcept { return m_Data.size(); }
222 235
223 236 /**
224 237 * @return the data of a component
225 238 * @param componentIndex the index of the component to retrieve the data
226 239 * @return the component's data, empty vector if the index is invalid
227 240 */
228 241 QVector<double> data(int componentIndex) const noexcept
229 242 {
230 243 QReadLocker locker{&m_Lock};
231 244
232 245 return (componentIndex >= 0 && componentIndex < m_Data.size()) ? m_Data.at(componentIndex)
233 246 : QVector<double>{};
234 247 }
235 248
236 249 /// @return the size (i.e. number of values) of a single component
237 250 /// @remarks in a case of a two-dimensional ArrayData, each component has the same size
238 251 int size() const
239 252 {
240 253 QReadLocker locker{&m_Lock};
241 254 return m_Data[0].size();
242 255 }
243 256
244 257 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation)
245 258 {
246 259 QReadLocker locker{&m_Lock};
247 260 return arraydata_detail::Sort<Dim>::sort(m_Data, sortPermutation);
248 261 }
249 262
250 263 // ///////// //
251 264 // Iterators //
252 265 // ///////// //
253 266
254 267 Iterator cbegin() const { return Iterator{m_Data, true}; }
255 268 Iterator cend() const { return Iterator{m_Data, false}; }
256 269
257 270 // ///////////// //
258 271 // 1-dim methods //
259 272 // ///////////// //
260 273
261 274 /**
262 275 * @return the data at a specified index
263 276 * @remarks index must be a valid position
264 277 * @remarks this method is only available for a unidimensional ArrayData
265 278 */
266 279 template <int D = Dim, typename = std::enable_if_t<D == 1> >
267 280 double at(int index) const noexcept
268 281 {
269 282 QReadLocker locker{&m_Lock};
270 283 return m_Data[0].at(index);
271 284 }
272 285
273 286 /**
274 287 * @return the data as a vector, as a const reference
275 288 * @remarks this method is only available for a unidimensional ArrayData
276 289 */
277 290 template <int D = Dim, typename = std::enable_if_t<D == 1> >
278 291 const QVector<double> &cdata() const noexcept
279 292 {
280 293 QReadLocker locker{&m_Lock};
281 294 return m_Data.at(0);
282 295 }
283 296
284 297 /**
285 298 * @return the data as a vector
286 299 * @remarks this method is only available for a unidimensional ArrayData
287 300 */
288 301 template <int D = Dim, typename = std::enable_if_t<D == 1> >
289 302 QVector<double> data() const noexcept
290 303 {
291 304 QReadLocker locker{&m_Lock};
292 305 return m_Data[0];
293 306 }
294 307
295 308 // ///////////// //
296 309 // 2-dim methods //
297 310 // ///////////// //
298 311
299 312 /**
300 313 * @return the data
301 314 * @remarks this method is only available for a two-dimensional ArrayData
302 315 */
303 316 template <int D = Dim, typename = std::enable_if_t<D == 2> >
304 317 DataContainer data() const noexcept
305 318 {
306 319 QReadLocker locker{&m_Lock};
307 320 return m_Data;
308 321 }
309 322
310 323 private:
311 324 DataContainer m_Data;
312 325 mutable QReadWriteLock m_Lock;
313 326 };
314 327
315 328 #endif // SCIQLOP_ARRAYDATA_H
@@ -1,293 +1,306
1 1 #ifndef SCIQLOP_DATASERIES_H
2 2 #define SCIQLOP_DATASERIES_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Common/SortUtils.h>
7 7
8 8 #include <Data/ArrayData.h>
9 9 #include <Data/IDataSeries.h>
10 10
11 11 #include <QLoggingCategory>
12 12 #include <QReadLocker>
13 13 #include <QReadWriteLock>
14 14 #include <memory>
15 15
16 16 // We don't use the Qt macro since the log is used in the header file, which causes multiple log
17 17 // definitions with inheritance. Inline method is used instead
18 18 inline const QLoggingCategory &LOG_DataSeries()
19 19 {
20 20 static const QLoggingCategory category{"DataSeries"};
21 21 return category;
22 22 }
23 23
24 /**
25 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
26 *
27 * It proposes to set a dimension for the values ​​data.
28 *
29 * A DataSeries is always sorted on its x-axis data.
30 *
31 * @tparam Dim The dimension of the values data
32 *
33 */
34 24 template <int Dim>
35 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
36 public:
37 class IteratorValue {
25 class DataSeries;
26
27 namespace dataseries_detail {
28
29 template <int Dim>
30 class IteratorValue : public DataSeriesIteratorValue::Impl {
38 31 public:
39 explicit IteratorValue(const DataSeries &dataSeries, bool begin)
32 explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin)
40 33 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
41 34 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
42 35 : dataSeries.valuesData()->cend())
43 36 {
44 37 }
38 IteratorValue(const IteratorValue &other) = default;
45 39
46 double x() const { return m_XIt->at(0); }
47 double value() const { return m_ValuesIt->at(0); }
48 double value(int componentIndex) const { return m_ValuesIt->at(componentIndex); }
49
50 void next()
40 std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override
51 41 {
52 ++m_XIt;
53 ++m_ValuesIt;
42 return std::make_unique<IteratorValue<Dim> >(*this);
54 43 }
55 44
56 bool operator==(const IteratorValue &other) const
57 {
58 return std::tie(m_XIt, m_ValuesIt) == std::tie(other.m_XIt, other.m_ValuesIt);
45 bool equals(const DataSeriesIteratorValue::Impl &other) const override try {
46 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
47 return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt);
48 }
49 catch (const std::bad_cast &) {
50 return false;
59 51 }
60 52
61 private:
62 ArrayData<1>::Iterator m_XIt;
63 typename ArrayData<Dim>::Iterator m_ValuesIt;
64 };
65
66 class Iterator {
67 public:
68 using iterator_category = std::forward_iterator_tag;
69 using value_type = const IteratorValue;
70 using difference_type = std::ptrdiff_t;
71 using pointer = value_type *;
72 using reference = value_type &;
73
74 Iterator(const DataSeries &dataSeries, bool begin) : m_CurrentValue{dataSeries, begin} {}
75 virtual ~Iterator() noexcept = default;
76 Iterator(const Iterator &) = default;
77 Iterator(Iterator &&) = default;
78 Iterator &operator=(const Iterator &) = default;
79 Iterator &operator=(Iterator &&) = default;
80
81 Iterator &operator++()
53 void next() override
82 54 {
83 m_CurrentValue.next();
84 return *this;
55 ++m_XIt;
56 ++m_ValuesIt;
85 57 }
86 58
87 pointer operator->() const { return &m_CurrentValue; }
88
89 reference operator*() const { return m_CurrentValue; }
90
91 bool operator==(const Iterator &other) const
59 void prev() override
92 60 {
93 return m_CurrentValue == other.m_CurrentValue;
61 --m_XIt;
62 --m_ValuesIt;
94 63 }
95 64
96 bool operator!=(const Iterator &other) const { return !(*this == other); }
65 double x() const override { return m_XIt->at(0); }
66 double value() const override { return m_ValuesIt->at(0); }
67 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
97 68
98 69 private:
99 IteratorValue m_CurrentValue;
70 ArrayData<1>::Iterator m_XIt;
71 typename ArrayData<Dim>::Iterator m_ValuesIt;
100 72 };
73 } // namespace dataseries_detail
101 74
75 /**
76 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
77 *
78 * It proposes to set a dimension for the values ​​data.
79 *
80 * A DataSeries is always sorted on its x-axis data.
81 *
82 * @tparam Dim The dimension of the values data
83 *
84 */
85 template <int Dim>
86 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
87 public:
102 88 /// @sa IDataSeries::xAxisData()
103 89 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
104 90 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
105 91
106 92 /// @sa IDataSeries::xAxisUnit()
107 93 Unit xAxisUnit() const override { return m_XAxisUnit; }
108 94
109 95 /// @return the values dataset
110 96 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
111 97 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
112 98
113 99 /// @sa IDataSeries::valuesUnit()
114 100 Unit valuesUnit() const override { return m_ValuesUnit; }
115 101
116 102
117 103 SqpRange range() const override
118 104 {
119 105 if (!m_XAxisData->cdata().isEmpty()) {
120 106 return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()};
121 107 }
122 108
123 109 return SqpRange{};
124 110 }
125 111
126 112 void clear()
127 113 {
128 114 m_XAxisData->clear();
129 115 m_ValuesData->clear();
130 116 }
131 117
132 118 /// Merges into the data series an other data series
133 119 /// @remarks the data series to merge with is cleared after the operation
134 120 void merge(IDataSeries *dataSeries) override
135 121 {
136 122 dataSeries->lockWrite();
137 123 lockWrite();
138 124
139 125 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
140 126 const auto &otherXAxisData = other->xAxisData()->cdata();
141 127 const auto &xAxisData = m_XAxisData->cdata();
142 128
143 129 // As data series are sorted, we can improve performances of merge, by call the sort
144 130 // method only if the two data series overlap.
145 131 if (!otherXAxisData.empty()) {
146 132 auto firstValue = otherXAxisData.front();
147 133 auto lastValue = otherXAxisData.back();
148 134
149 135 auto xAxisDataBegin = xAxisData.cbegin();
150 136 auto xAxisDataEnd = xAxisData.cend();
151 137
152 138 bool prepend;
153 139 bool sortNeeded;
154 140
155 141 if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) {
156 142 // Other data series if after data series
157 143 prepend = false;
158 144 sortNeeded = false;
159 145 }
160 146 else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue)
161 147 == xAxisDataBegin) {
162 148 // Other data series if before data series
163 149 prepend = true;
164 150 sortNeeded = false;
165 151 }
166 152 else {
167 153 // The two data series overlap
168 154 prepend = false;
169 155 sortNeeded = true;
170 156 }
171 157
172 158 // Makes the merge
173 159 m_XAxisData->add(*other->xAxisData(), prepend);
174 160 m_ValuesData->add(*other->valuesData(), prepend);
175 161
176 162 if (sortNeeded) {
177 163 sort();
178 164 }
179 165 }
180 166
181 167 // Clears the other data series
182 168 other->clear();
183 169 }
184 170 else {
185 171 qCWarning(LOG_DataSeries())
186 172 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
187 173 }
188 174 unlock();
189 175 dataSeries->unlock();
190 176 }
191 177
192 178 // ///////// //
193 179 // Iterators //
194 180 // ///////// //
195 181
196 Iterator cbegin() const { return Iterator{*this, true}; }
182 DataSeriesIterator cbegin() const override
183 {
184 return DataSeriesIterator{DataSeriesIteratorValue{
185 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}};
186 }
187
188 DataSeriesIterator cend() const override
189 {
190 return DataSeriesIterator{DataSeriesIteratorValue{
191 std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}};
192 }
193
194 /// @sa IDataSeries::minData()
195 DataSeriesIterator minData(double minXAxisData) const override
196 {
197 return std::lower_bound(
198 cbegin(), cend(), minXAxisData,
199 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
200 }
197 201
198 Iterator cend() const { return Iterator{*this, false}; }
202 /// @sa IDataSeries::maxData()
203 DataSeriesIterator maxData(double maxXAxisData) const override
204 {
205 // Gets the first element that greater than max value
206 auto it = std::upper_bound(
207 cbegin(), cend(), maxXAxisData,
208 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
209
210 return it == cbegin() ? cend() : --it;
211 }
199 212
200 std::pair<Iterator, Iterator> subData(double min, double max) const
213 std::pair<DataSeriesIterator, DataSeriesIterator> subData(double min, double max) const override
201 214 {
202 215 if (min > max) {
203 216 std::swap(min, max);
204 217 }
205 218
206 219 auto begin = cbegin();
207 220 auto end = cend();
208 221
209 222 auto lowerIt
210 223 = std::lower_bound(begin, end, min, [](const auto &itValue, const auto &value) {
211 224 return itValue.x() < value;
212 225 });
213 226 auto upperIt
214 227 = std::upper_bound(begin, end, max, [](const auto &value, const auto &itValue) {
215 228 return value < itValue.x();
216 229 });
217 230
218 231 return std::make_pair(lowerIt, upperIt);
219 232 }
220 233
221 234 // /////// //
222 235 // Mutexes //
223 236 // /////// //
224 237
225 238 virtual void lockRead() { m_Lock.lockForRead(); }
226 239 virtual void lockWrite() { m_Lock.lockForWrite(); }
227 240 virtual void unlock() { m_Lock.unlock(); }
228 241
229 242 protected:
230 243 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
231 244 /// DataSeries with no values will be created.
232 245 /// @remarks data series is automatically sorted on its x-axis data
233 246 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
234 247 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
235 248 : m_XAxisData{xAxisData},
236 249 m_XAxisUnit{xAxisUnit},
237 250 m_ValuesData{valuesData},
238 251 m_ValuesUnit{valuesUnit}
239 252 {
240 253 if (m_XAxisData->size() != m_ValuesData->size()) {
241 254 clear();
242 255 }
243 256
244 257 // Sorts data if it's not the case
245 258 const auto &xAxisCData = m_XAxisData->cdata();
246 259 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
247 260 sort();
248 261 }
249 262 }
250 263
251 264 /// Copy ctor
252 265 explicit DataSeries(const DataSeries<Dim> &other)
253 266 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
254 267 m_XAxisUnit{other.m_XAxisUnit},
255 268 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
256 269 m_ValuesUnit{other.m_ValuesUnit}
257 270 {
258 271 // Since a series is ordered from its construction and is always ordered, it is not
259 272 // necessary to call the sort method here ('other' is sorted)
260 273 }
261 274
262 275 /// Assignment operator
263 276 template <int D>
264 277 DataSeries &operator=(DataSeries<D> other)
265 278 {
266 279 std::swap(m_XAxisData, other.m_XAxisData);
267 280 std::swap(m_XAxisUnit, other.m_XAxisUnit);
268 281 std::swap(m_ValuesData, other.m_ValuesData);
269 282 std::swap(m_ValuesUnit, other.m_ValuesUnit);
270 283
271 284 return *this;
272 285 }
273 286
274 287 private:
275 288 /**
276 289 * Sorts data series on its x-axis data
277 290 */
278 291 void sort() noexcept
279 292 {
280 293 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
281 294 m_XAxisData = m_XAxisData->sort(permutation);
282 295 m_ValuesData = m_ValuesData->sort(permutation);
283 296 }
284 297
285 298 std::shared_ptr<ArrayData<1> > m_XAxisData;
286 299 Unit m_XAxisUnit;
287 300 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
288 301 Unit m_ValuesUnit;
289 302
290 303 QReadWriteLock m_Lock;
291 304 };
292 305
293 306 #endif // SCIQLOP_DATASERIES_H
@@ -1,73 +1,96
1 1 #ifndef SCIQLOP_IDATASERIES_H
2 2 #define SCIQLOP_IDATASERIES_H
3 3
4 4 #include <Common/MetaTypes.h>
5 #include <Data/DataSeriesIterator.h>
5 6 #include <Data/SqpRange.h>
6 7
7 8 #include <memory>
8 9
9 10 #include <QString>
10 11
11 12 template <int Dim>
12 13 class ArrayData;
13 14
14 15 struct Unit {
15 16 explicit Unit(const QString &name = {}, bool timeUnit = false)
16 17 : m_Name{name}, m_TimeUnit{timeUnit}
17 18 {
18 19 }
19 20
20 21 inline bool operator==(const Unit &other) const
21 22 {
22 23 return std::tie(m_Name, m_TimeUnit) == std::tie(other.m_Name, other.m_TimeUnit);
23 24 }
24 25 inline bool operator!=(const Unit &other) const { return !(*this == other); }
25 26
26 27 QString m_Name; ///< Unit name
27 28 bool m_TimeUnit; ///< The unit is a unit of time (UTC)
28 29 };
29 30
30 31 /**
31 32 * @brief The IDataSeries aims to declare a data series.
32 33 *
33 34 * A data series is an entity that contains at least :
34 35 * - one dataset representing the x-axis
35 36 * - one dataset representing the values
36 37 *
37 38 * Each dataset is represented by an ArrayData, and is associated with a unit.
38 39 *
39 40 * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the
40 41 * IDataSeries. The x-axis dataset is always unidimensional.
41 42 *
42 43 * @sa ArrayData
43 44 */
44 45 class IDataSeries {
45 46 public:
46 47 virtual ~IDataSeries() noexcept = default;
47 48
48 49 /// Returns the x-axis dataset
49 50 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
50 51
51 52 /// Returns the x-axis dataset (as const)
52 53 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
53 54
54 55 virtual Unit xAxisUnit() const = 0;
55 56
56 57 virtual Unit valuesUnit() const = 0;
57 58
58 59 virtual void merge(IDataSeries *dataSeries) = 0;
59 60 /// @todo Review the name and signature of this method
60 61 virtual std::shared_ptr<IDataSeries> subDataSeries(const SqpRange &range) = 0;
61 62
62 63 virtual std::unique_ptr<IDataSeries> clone() const = 0;
63 64 virtual SqpRange range() const = 0;
64 65
66 // ///////// //
67 // Iterators //
68 // ///////// //
69
70 virtual DataSeriesIterator cbegin() const = 0;
71 virtual DataSeriesIterator cend() const = 0;
72
73 /// @return the iterator to the first entry of the data series whose x-axis data is greater than
74 /// or equal to the value passed in parameter, or the end iterator if there is no matching value
75 virtual DataSeriesIterator minData(double minXAxisData) const = 0;
76
77 /// @return the iterator to the last entry of the data series whose x-axis data is less than or
78 /// equal to the value passed in parameter, or the end iterator if there is no matching value
79 virtual DataSeriesIterator maxData(double maxXAxisData) const = 0;
80
81 virtual std::pair<DataSeriesIterator, DataSeriesIterator> subData(double min,
82 double max) const = 0;
83
84 // /////// //
85 // Mutexes //
86 // /////// //
87
65 88 virtual void lockRead() = 0;
66 89 virtual void lockWrite() = 0;
67 90 virtual void unlock() = 0;
68 91 };
69 92
70 93 // Required for using shared_ptr in signals/slots
71 94 SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>)
72 95
73 96 #endif // SCIQLOP_IDATASERIES_H
@@ -1,258 +1,271
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableModel.h>
3 3
4 4 #include <Common/DateUtils.h>
5 5
6 6 #include <Data/IDataSeries.h>
7 7
8 8 #include <QSize>
9 9 #include <unordered_map>
10 10
11 11 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
12 12
13 13 namespace {
14 14
15 15 // Column indexes
16 16 const auto NAME_COLUMN = 0;
17 17 const auto TSTART_COLUMN = 1;
18 18 const auto TEND_COLUMN = 2;
19 19 const auto UNIT_COLUMN = 3;
20 20 const auto MISSION_COLUMN = 4;
21 21 const auto PLUGIN_COLUMN = 5;
22 22 const auto NB_COLUMNS = 6;
23 23
24 24 // Column properties
25 25 const auto DEFAULT_HEIGHT = 25;
26 26 const auto DEFAULT_WIDTH = 100;
27 27
28 28 struct ColumnProperties {
29 29 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
30 30 int height = DEFAULT_HEIGHT)
31 31 : m_Name{name}, m_Width{width}, m_Height{height}
32 32 {
33 33 }
34 34
35 35 QString m_Name;
36 36 int m_Width;
37 37 int m_Height;
38 38 };
39 39
40 40 const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{
41 41 {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
42 42 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {UNIT_COLUMN, {QObject::tr("Unit")}},
43 43 {MISSION_COLUMN, {QObject::tr("Mission")}}, {PLUGIN_COLUMN, {QObject::tr("Plugin")}}};
44 44
45 45 /// Format for datetimes
46 46 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
47 47
48 48
49 49 } // namespace
50 50
51 51 struct VariableModel::VariableModelPrivate {
52 52 /// Variables created in SciQlop
53 53 std::vector<std::shared_ptr<Variable> > m_Variables;
54 54 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
55 55
56 56 /// Return the row index of the variable. -1 if it's not found
57 57 int indexOfVariable(Variable *variable) const noexcept;
58 58 };
59 59
60 60 VariableModel::VariableModel(QObject *parent)
61 61 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
62 62 {
63 63 }
64 64
65 65 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
66 66 const SqpRange &dateTime,
67 67 const QVariantHash &metadata) noexcept
68 68 {
69 69 auto insertIndex = rowCount();
70 70 beginInsertRows({}, insertIndex, insertIndex);
71 71
72 72 auto variable = std::make_shared<Variable>(name, dateTime, metadata);
73 73
74 74 impl->m_Variables.push_back(variable);
75 75 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
76 76
77 77 endInsertRows();
78 78
79 79 return variable;
80 80 }
81 81
82 82 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
83 83 {
84 84 if (!variable) {
85 85 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
86 86 return;
87 87 }
88 88
89 89 // Finds variable in the model
90 90 auto begin = impl->m_Variables.cbegin();
91 91 auto end = impl->m_Variables.cend();
92 92 auto it = std::find(begin, end, variable);
93 93 if (it != end) {
94 94 auto removeIndex = std::distance(begin, it);
95 95
96 96 // Deletes variable
97 97 beginRemoveRows({}, removeIndex, removeIndex);
98 98 impl->m_Variables.erase(it);
99 99 endRemoveRows();
100 100 }
101 101 else {
102 102 qCritical(LOG_VariableModel())
103 103 << tr("Can't delete variable %1 from the model: the variable is not in the model")
104 104 .arg(variable->name());
105 105 }
106 106
107 107 // Removes variable from progress map
108 108 impl->m_VariableToProgress.erase(variable);
109 109 }
110 110
111 111
112 112 std::shared_ptr<Variable> VariableModel::variable(int index) const
113 113 {
114 114 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
115 115 }
116 116
117 117 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
118 118 {
119 119 if (progress > 0.0) {
120 120 impl->m_VariableToProgress[variable] = progress;
121 121 }
122 122 else {
123 123 impl->m_VariableToProgress.erase(variable);
124 124 }
125 125 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
126 126
127 127 emit dataChanged(modelIndex, modelIndex);
128 128 }
129 129
130 130 int VariableModel::columnCount(const QModelIndex &parent) const
131 131 {
132 132 Q_UNUSED(parent);
133 133
134 134 return NB_COLUMNS;
135 135 }
136 136
137 137 int VariableModel::rowCount(const QModelIndex &parent) const
138 138 {
139 139 Q_UNUSED(parent);
140 140
141 141 return impl->m_Variables.size();
142 142 }
143 143
144 144 QVariant VariableModel::data(const QModelIndex &index, int role) const
145 145 {
146 146 if (!index.isValid()) {
147 147 return QVariant{};
148 148 }
149 149
150 150 if (index.row() < 0 || index.row() >= rowCount()) {
151 151 return QVariant{};
152 152 }
153 153
154 154 if (role == Qt::DisplayRole) {
155 155 if (auto variable = impl->m_Variables.at(index.row()).get()) {
156 156 /// Lambda function that builds the variant to return for a time value
157 auto dateTimeVariant = [](double secs) {
158 auto dateTime = DateUtils::dateTime(secs);
159 return dateTime.toString(DATETIME_FORMAT);
157 /// @param getValueFun function used to get for a data series the iterator on the entry
158 /// that contains the time value to display
159 auto dateTimeVariant = [variable](const auto &getValueFun) {
160 if (auto dataSeries = variable->dataSeries()) {
161 auto it = getValueFun(*dataSeries);
162 return (it != dataSeries->cend())
163 ? DateUtils::dateTime(it->x()).toString(DATETIME_FORMAT)
164 : QVariant{};
165 }
166 else {
167 return QVariant{};
168 }
160 169 };
161 170
162 171 switch (index.column()) {
163 172 case NAME_COLUMN:
164 173 return variable->name();
165 174 case TSTART_COLUMN:
166 return dateTimeVariant(variable->range().m_TStart);
175 // Shows the min value of the data series above the range tstart
176 return dateTimeVariant([min = variable->range().m_TStart](
177 const auto &dataSeries) { return dataSeries.minData(min); });
167 178 case TEND_COLUMN:
168 return dateTimeVariant(variable->range().m_TEnd);
179 // Shows the max value of the data series under the range tend
180 return dateTimeVariant([max = variable->range().m_TEnd](
181 const auto &dataSeries) { return dataSeries.maxData(max); });
169 182 case UNIT_COLUMN:
170 183 return variable->metadata().value(QStringLiteral("units"));
171 184 case MISSION_COLUMN:
172 185 return variable->metadata().value(QStringLiteral("mission"));
173 186 case PLUGIN_COLUMN:
174 187 return variable->metadata().value(QStringLiteral("plugin"));
175 188 default:
176 189 // No action
177 190 break;
178 191 }
179 192
180 193 qWarning(LOG_VariableModel())
181 194 << tr("Can't get data (unknown column %1)").arg(index.column());
182 195 }
183 196 else {
184 197 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
185 198 }
186 199 }
187 200 else if (role == VariableRoles::ProgressRole) {
188 201 if (auto variable = impl->m_Variables.at(index.row())) {
189 202
190 203 auto it = impl->m_VariableToProgress.find(variable);
191 204 if (it != impl->m_VariableToProgress.cend()) {
192 205 return it->second;
193 206 }
194 207 }
195 208 }
196 209
197 210 return QVariant{};
198 211 }
199 212
200 213 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
201 214 {
202 215 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
203 216 return QVariant{};
204 217 }
205 218
206 219 if (orientation == Qt::Horizontal) {
207 220 auto propertiesIt = COLUMN_PROPERTIES.find(section);
208 221 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
209 222 // Role is either DisplayRole or SizeHintRole
210 223 return (role == Qt::DisplayRole)
211 224 ? QVariant{propertiesIt->m_Name}
212 225 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
213 226 }
214 227 else {
215 228 qWarning(LOG_VariableModel())
216 229 << tr("Can't get header data (unknown column %1)").arg(section);
217 230 }
218 231 }
219 232
220 233 return QVariant{};
221 234 }
222 235
223 236 void VariableModel::abortProgress(const QModelIndex &index)
224 237 {
225 238 if (auto variable = impl->m_Variables.at(index.row())) {
226 239 emit abortProgessRequested(variable);
227 240 }
228 241 }
229 242
230 243 void VariableModel::onVariableUpdated() noexcept
231 244 {
232 245 // Finds variable that has been updated in the model
233 246 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
234 247 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
235 248
236 249 if (updatedVariableIndex > -1) {
237 250 emit dataChanged(createIndex(updatedVariableIndex, 0),
238 251 createIndex(updatedVariableIndex, columnCount() - 1));
239 252 }
240 253 }
241 254 }
242 255
243 256 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
244 257 {
245 258 auto begin = std::cbegin(m_Variables);
246 259 auto end = std::cend(m_Variables);
247 260 auto it
248 261 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
249 262
250 263 if (it != end) {
251 264 // Gets the index of the variable in the model: we assume here that views have the same
252 265 // order as the model
253 266 return std::distance(begin, it);
254 267 }
255 268 else {
256 269 return -1;
257 270 }
258 271 }
@@ -1,232 +1,354
1 1 #include "Data/DataSeries.h"
2 2 #include "Data/ScalarSeries.h"
3 3
4 4 #include <QObject>
5 5 #include <QtTest>
6 6
7 7 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
8 8
9 9 class TestDataSeries : public QObject {
10 10 Q_OBJECT
11 11 private slots:
12 12 /// Input test data
13 13 /// @sa testCtor()
14 14 void testCtor_data();
15 15
16 16 /// Tests construction of a data series
17 17 void testCtor();
18 18
19 19 /// Input test data
20 20 /// @sa testMerge()
21 21 void testMerge_data();
22 22
23 23 /// Tests merge of two data series
24 24 void testMerge();
25 25
26 26 /// Input test data
27 /// @sa testMinData()
28 void testMinData_data();
29
30 /// Tests get min data of a data series
31 void testMinData();
32
33 /// Input test data
34 /// @sa testMaxData()
35 void testMaxData_data();
36
37 /// Tests get max data of a data series
38 void testMaxData();
39
40 /// Input test data
27 41 /// @sa testSubdata()
28 42 void testSubdata_data();
29 43
30 44 /// Tests get subdata of two data series
31 45 void testSubdata();
32 46 };
33 47
34 48 void TestDataSeries::testCtor_data()
35 49 {
36 50 // ////////////// //
37 51 // Test structure //
38 52 // ////////////// //
39 53
40 54 // x-axis data
41 55 QTest::addColumn<QVector<double> >("xAxisData");
42 56 // values data
43 57 QTest::addColumn<QVector<double> >("valuesData");
44 58
45 59 // expected x-axis data
46 60 QTest::addColumn<QVector<double> >("expectedXAxisData");
47 61 // expected values data
48 62 QTest::addColumn<QVector<double> >("expectedValuesData");
49 63
50 64 // ////////// //
51 65 // Test cases //
52 66 // ////////// //
53 67
54 68 QTest::newRow("invalidData (different sizes of vectors)")
55 69 << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 200., 300.}
56 70 << QVector<double>{} << QVector<double>{};
57 71
58 72 QTest::newRow("sortedData") << QVector<double>{1., 2., 3., 4., 5.}
59 73 << QVector<double>{100., 200., 300., 400., 500.}
60 74 << QVector<double>{1., 2., 3., 4., 5.}
61 75 << QVector<double>{100., 200., 300., 400., 500.};
62 76
63 77 QTest::newRow("unsortedData") << QVector<double>{5., 4., 3., 2., 1.}
64 78 << QVector<double>{100., 200., 300., 400., 500.}
65 79 << QVector<double>{1., 2., 3., 4., 5.}
66 80 << QVector<double>{500., 400., 300., 200., 100.};
67 81
68 82 QTest::newRow("unsortedData2")
69 83 << QVector<double>{1., 4., 3., 5., 2.} << QVector<double>{100., 200., 300., 400., 500.}
70 84 << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 500., 300., 200., 400.};
71 85 }
72 86
73 87 void TestDataSeries::testCtor()
74 88 {
75 89 // Creates series
76 90 QFETCH(QVector<double>, xAxisData);
77 91 QFETCH(QVector<double>, valuesData);
78 92
79 93 auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData),
80 94 Unit{}, Unit{});
81 95
82 96 // Validates results : we check that the data series is sorted on its x-axis data
83 97 QFETCH(QVector<double>, expectedXAxisData);
84 98 QFETCH(QVector<double>, expectedValuesData);
85 99
86 100 auto seriesXAxisData = series->xAxisData()->data();
87 101 auto seriesValuesData = series->valuesData()->data();
88 102
89 103 QVERIFY(
90 104 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
91 105 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
92 106 seriesValuesData.cbegin()));
93 107 }
94 108
95 109 namespace {
96 110
97 111 std::shared_ptr<ScalarSeries> createSeries(QVector<double> xAxisData, QVector<double> valuesData)
98 112 {
99 113 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{},
100 114 Unit{});
101 115 }
102 116
103 117 } // namespace
104 118
105 119 void TestDataSeries::testMerge_data()
106 120 {
107 121 // ////////////// //
108 122 // Test structure //
109 123 // ////////////// //
110 124
111 125 // Data series to merge
112 126 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
113 127 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2");
114 128
115 129 // Expected values in the first data series after merge
116 130 QTest::addColumn<QVector<double> >("expectedXAxisData");
117 131 QTest::addColumn<QVector<double> >("expectedValuesData");
118 132
119 133 // ////////// //
120 134 // Test cases //
121 135 // ////////// //
122 136
123 137 QTest::newRow("sortedMerge")
124 138 << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
125 139 << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.})
126 140 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
127 141 << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
128 142
129 143 QTest::newRow("unsortedMerge")
130 144 << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.})
131 145 << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
132 146 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
133 147 << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
134 148
135 149 QTest::newRow("unsortedMerge2")
136 150 << createSeries({1., 2., 8., 9., 10}, {100., 200., 300., 400., 500.})
137 151 << createSeries({3., 4., 5., 6., 7.}, {600., 700., 800., 900., 1000.})
138 152 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
139 153 << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.};
140 154
141 155 QTest::newRow("unsortedMerge3")
142 156 << createSeries({3., 5., 8., 7., 2}, {100., 200., 300., 400., 500.})
143 157 << createSeries({6., 4., 9., 10., 1.}, {600., 700., 800., 900., 1000.})
144 158 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
145 159 << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.};
146 160 }
147 161
148 162 void TestDataSeries::testMerge()
149 163 {
150 164 // Merges series
151 165 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
152 166 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2);
153 167
154 168 dataSeries->merge(dataSeries2.get());
155 169
156 170 // Validates results : we check that the merge is valid and the data series is sorted on its
157 171 // x-axis data
158 172 QFETCH(QVector<double>, expectedXAxisData);
159 173 QFETCH(QVector<double>, expectedValuesData);
160 174
161 175 auto seriesXAxisData = dataSeries->xAxisData()->data();
162 176 auto seriesValuesData = dataSeries->valuesData()->data();
163 177
164 178 QVERIFY(
165 179 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
166 180 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
167 181 seriesValuesData.cbegin()));
168 182 }
169 183
184 void TestDataSeries::testMinData_data()
185 {
186 // ////////////// //
187 // Test structure //
188 // ////////////// //
189
190 // Data series to get min data
191 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
192
193 // Min data
194 QTest::addColumn<double>("min");
195
196 // Expected results
197 QTest::addColumn<bool>(
198 "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator)
199 QTest::addColumn<double>(
200 "expectedMin"); // Expected value when method doesn't return end iterator
201
202 // ////////// //
203 // Test cases //
204 // ////////// //
205
206 QTest::newRow("minData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
207 << 0. << true << 1.;
208 QTest::newRow("minData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
209 << 1. << true << 1.;
210 QTest::newRow("minData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
211 << 1.1 << true << 2.;
212 QTest::newRow("minData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
213 << 5. << true << 5.;
214 QTest::newRow("minData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
215 << 5.1 << false << std::numeric_limits<double>::quiet_NaN();
216 QTest::newRow("minData6") << createSeries({}, {}) << 1.1 << false
217 << std::numeric_limits<double>::quiet_NaN();
218 }
219
220 void TestDataSeries::testMinData()
221 {
222 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
223 QFETCH(double, min);
224
225 QFETCH(bool, expectedOK);
226 QFETCH(double, expectedMin);
227
228 auto it = dataSeries->minData(min);
229
230 QCOMPARE(expectedOK, it != dataSeries->cend());
231
232 // If the method doesn't return a end iterator, checks with expected value
233 if (expectedOK) {
234 QCOMPARE(expectedMin, it->x());
235 }
236 }
237
238 void TestDataSeries::testMaxData_data()
239 {
240 // ////////////// //
241 // Test structure //
242 // ////////////// //
243
244 // Data series to get max data
245 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
246
247 // Max data
248 QTest::addColumn<double>("max");
249
250 // Expected results
251 QTest::addColumn<bool>(
252 "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator)
253 QTest::addColumn<double>(
254 "expectedMax"); // Expected value when method doesn't return end iterator
255
256 // ////////// //
257 // Test cases //
258 // ////////// //
259
260 QTest::newRow("maxData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
261 << 6. << true << 5.;
262 QTest::newRow("maxData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
263 << 5. << true << 5.;
264 QTest::newRow("maxData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
265 << 4.9 << true << 4.;
266 QTest::newRow("maxData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
267 << 1.1 << true << 1.;
268 QTest::newRow("maxData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
269 << 1. << true << 1.;
270 QTest::newRow("maxData6") << createSeries({}, {}) << 1.1 << false
271 << std::numeric_limits<double>::quiet_NaN();
272 }
273
274 void TestDataSeries::testMaxData()
275 {
276 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
277 QFETCH(double, max);
278
279 QFETCH(bool, expectedOK);
280 QFETCH(double, expectedMax);
281
282 auto it = dataSeries->maxData(max);
283
284 QCOMPARE(expectedOK, it != dataSeries->cend());
285
286 // If the method doesn't return a end iterator, checks with expected value
287 if (expectedOK) {
288 QCOMPARE(expectedMax, it->x());
289 }
290 }
291
170 292 void TestDataSeries::testSubdata_data()
171 293 {
172 294 // ////////////// //
173 295 // Test structure //
174 296 // ////////////// //
175 297
176 298 // Data series to get subdata
177 299 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
178 300
179 301 // Min/max values
180 302 QTest::addColumn<double>("min");
181 303 QTest::addColumn<double>("max");
182 304
183 305 // Expected values after subdata
184 306 QTest::addColumn<QVector<double> >("expectedXAxisData");
185 307 QTest::addColumn<QVector<double> >("expectedValuesData");
186 308
187 309 // ////////// //
188 310 // Test cases //
189 311 // ////////// //
190 312
191 313 QTest::newRow("subData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
192 314 << -1. << 3.2 << QVector<double>{1., 2., 3.}
193 315 << QVector<double>{100., 200., 300.};
194 316 QTest::newRow("subData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
195 317 << 1. << 4. << QVector<double>{1., 2., 3., 4.}
196 318 << QVector<double>{100., 200., 300., 400.};
197 319 QTest::newRow("subData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
198 320 << 1. << 3.9 << QVector<double>{1., 2., 3.}
199 321 << QVector<double>{100., 200., 300.};
200 322 QTest::newRow("subData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
201 323 << 0. << 0.9 << QVector<double>{} << QVector<double>{};
202 324 QTest::newRow("subData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
203 325 << 0. << 1. << QVector<double>{1.} << QVector<double>{100.};
204 326 QTest::newRow("subData6") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
205 327 << 2.1 << 6. << QVector<double>{3., 4., 5.}
206 328 << QVector<double>{300., 400., 500.};
207 329 QTest::newRow("subData7") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
208 330 << 6. << 9. << QVector<double>{} << QVector<double>{};
209 331 QTest::newRow("subData8") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
210 332 << 5. << 9. << QVector<double>{5.} << QVector<double>{500.};
211 333 }
212 334
213 335 void TestDataSeries::testSubdata()
214 336 {
215 337 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
216 338 QFETCH(double, min);
217 339 QFETCH(double, max);
218 340
219 341 QFETCH(QVector<double>, expectedXAxisData);
220 342 QFETCH(QVector<double>, expectedValuesData);
221 343
222 344 auto bounds = dataSeries->subData(min, max);
223 345 QVERIFY(std::equal(bounds.first, bounds.second, expectedXAxisData.cbegin(),
224 346 expectedXAxisData.cend(),
225 347 [](const auto &it, const auto &expectedX) { return it.x() == expectedX; }));
226 348 QVERIFY(std::equal(
227 349 bounds.first, bounds.second, expectedValuesData.cbegin(), expectedValuesData.cend(),
228 350 [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; }));
229 351 }
230 352
231 353 QTEST_MAIN(TestDataSeries)
232 354 #include "TestDataSeries.moc"
@@ -1,188 +1,174
1 1 #include <Variable/Variable.h>
2 2
3 3 #include <QObject>
4 4 #include <QtTest>
5 5
6 6 #include <memory>
7 7
8 8 class TestVariable : public QObject {
9 9 Q_OBJECT
10 10
11 11 private slots:
12 12 void testNotInCacheRangeList();
13 13
14 14 void testInCacheRangeList();
15 15 };
16 16
17 17
18 18 void TestVariable::testNotInCacheRangeList()
19 19 {
20 20 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
21 21 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
22 22
23 auto sqpR = SqpRange{static_cast<double>(varRS.toMSecsSinceEpoch()),
24 static_cast<double>(varRE.toMSecsSinceEpoch())};
23 auto sqpR = SqpRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)};
25 24
26 25 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
27 26 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())};
27 auto sqpCR
28 = SqpRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)};
30 29
31 30 Variable var{"Var test", sqpR};
32 31 var.setCacheRange(sqpCR);
33 32
34 33 // 1: [ts,te] < varTS
35 34 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
36 35 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())};
36 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
39 37
40 38 auto notInCach = var.provideNotInCacheRangeList(sqp);
41 39
42 40 QCOMPARE(notInCach.size(), 1);
43 41
44 42 auto notInCachRange = notInCach.first();
45 43
46 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
47 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
44 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
45 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
48 46
49 47 // 2: ts < varTS < te < varTE
50 48 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
51 49 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())};
50 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
54 51 notInCach = var.provideNotInCacheRangeList(sqp);
55 52 QCOMPARE(notInCach.size(), 1);
56 53 notInCachRange = notInCach.first();
57 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
58 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(varCRS.toMSecsSinceEpoch()));
54 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
55 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS));
59 56
60 57 // 3: varTS < ts < te < varTE
61 58 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
62 59 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())};
60 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
65 61 notInCach = var.provideNotInCacheRangeList(sqp);
66 62 QCOMPARE(notInCach.size(), 0);
67 63
68 64
69 65 // 4: varTS < ts < varTE < te
70 66 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
71 67 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())};
68 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
74 69 notInCach = var.provideNotInCacheRangeList(sqp);
75 70 QCOMPARE(notInCach.size(), 1);
76 71 notInCachRange = notInCach.first();
77 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(varCRE.toMSecsSinceEpoch()));
78 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
72 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE));
73 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
79 74
80 75 // 5: varTS < varTE < ts < te
81 76 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
82 77 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())};
78 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
85 79 notInCach = var.provideNotInCacheRangeList(sqp);
86 80 QCOMPARE(notInCach.size(), 1);
87 81 notInCachRange = notInCach.first();
88 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
89 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
82 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
83 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
90 84
91 85 // 6: ts <varTS < varTE < te
92 86 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
93 87 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())};
88 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
96 89 notInCach = var.provideNotInCacheRangeList(sqp);
97 90 QCOMPARE(notInCach.size(), 2);
98 91 notInCachRange = notInCach.first();
99 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
100 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(varCRS.toMSecsSinceEpoch()));
92 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
93 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRS));
101 94 notInCachRange = notInCach[1];
102 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(varCRE.toMSecsSinceEpoch()));
103 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
95 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRE));
96 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
104 97 }
105 98
106 99
107 100 void TestVariable::testInCacheRangeList()
108 101 {
109 102 auto varRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
110 103 auto varRE = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 40, 0}};
111 104
112 auto sqpR = SqpRange{static_cast<double>(varRS.toMSecsSinceEpoch()),
113 static_cast<double>(varRE.toMSecsSinceEpoch())};
105 auto sqpR = SqpRange{DateUtils::secondsSinceEpoch(varRS), DateUtils::secondsSinceEpoch(varRE)};
114 106
115 107 auto varCRS = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
116 108 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())};
109 auto sqpCR
110 = SqpRange{DateUtils::secondsSinceEpoch(varCRS), DateUtils::secondsSinceEpoch(varCRE)};
119 111
120 112 Variable var{"Var test", sqpR};
121 113 var.setCacheRange(sqpCR);
122 114
123 115 // 1: [ts,te] < varTS
124 116 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
125 117 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())};
118 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
128 119
129 120 auto notInCach = var.provideInCacheRangeList(sqp);
130 121
131 122 QCOMPARE(notInCach.size(), 0);
132 123
133 124 // 2: ts < varTS < te < varTE
134 125 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
135 126 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())};
127 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
138 128 notInCach = var.provideInCacheRangeList(sqp);
139 129 QCOMPARE(notInCach.size(), 1);
140 130 auto notInCachRange = notInCach.first();
141 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(varCRS.toMSecsSinceEpoch()));
142 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
131 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS));
132 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
143 133
144 134 // 3: varTS < ts < te < varTE
145 135 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
146 136 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())};
137 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
149 138 notInCach = var.provideInCacheRangeList(sqp);
150 139 QCOMPARE(notInCach.size(), 1);
151 140 notInCachRange = notInCach.first();
152 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
153 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
141 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
142 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(te));
154 143
155 144 // 4: varTS < ts < varTE < te
156 145 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 20, 0}};
157 146 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())};
147 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
160 148 notInCach = var.provideInCacheRangeList(sqp);
161 149 QCOMPARE(notInCach.size(), 1);
162 150 notInCachRange = notInCach.first();
163 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
164 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(varCRE.toMSecsSinceEpoch()));
151 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(ts));
152 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
165 153
166 154 // 5: varTS < varTE < ts < te
167 155 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 20, 0}};
168 156 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())};
157 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
171 158 notInCach = var.provideInCacheRangeList(sqp);
172 159 QCOMPARE(notInCach.size(), 0);
173 160
174 161 // 6: ts <varTS < varTE < te
175 162 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
176 163 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())};
164 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
179 165 notInCach = var.provideInCacheRangeList(sqp);
180 166 QCOMPARE(notInCach.size(), 1);
181 167 notInCachRange = notInCach.first();
182 QCOMPARE(notInCachRange.m_TStart, static_cast<double>(varCRS.toMSecsSinceEpoch()));
183 QCOMPARE(notInCachRange.m_TEnd, static_cast<double>(varCRE.toMSecsSinceEpoch()));
168 QCOMPARE(notInCachRange.m_TStart, DateUtils::secondsSinceEpoch(varCRS));
169 QCOMPARE(notInCachRange.m_TEnd, DateUtils::secondsSinceEpoch(varCRE));
184 170 }
185 171
186 172
187 173 QTEST_MAIN(TestVariable)
188 174 #include "TestVariable.moc"
@@ -1,353 +1,331
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 14
15 15 void testAddDateTime();
16 16 };
17 17
18 18
19 19 void TestVariableCacheController::testProvideNotInCacheDateTimeList()
20 20 {
21 21 VariableCacheController variableCacheController{};
22 22
23 23 auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
24 24 auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
25 auto sqp0 = SqpRange{static_cast<double>(ts0.toMSecsSinceEpoch()),
26 static_cast<double>(te0.toMSecsSinceEpoch())};
25 auto sqp0 = SqpRange{DateUtils::secondsSinceEpoch(ts0), DateUtils::secondsSinceEpoch(te0)};
27 26
28 27 auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
29 28 auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}};
30 auto sqp1 = SqpRange{static_cast<double>(ts1.toMSecsSinceEpoch()),
31 static_cast<double>(te1.toMSecsSinceEpoch())};
29 auto sqp1 = SqpRange{DateUtils::secondsSinceEpoch(ts1), DateUtils::secondsSinceEpoch(te1)};
32 30
33 31 auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}};
34 32 auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}};
35 auto sqp2 = SqpRange{static_cast<double>(ts2.toMSecsSinceEpoch()),
36 static_cast<double>(te2.toMSecsSinceEpoch())};
33 auto sqp2 = SqpRange{DateUtils::secondsSinceEpoch(ts2), DateUtils::secondsSinceEpoch(te2)};
37 34
38 35 auto var0 = std::make_shared<Variable>("", sqp0);
39 36
40 37 variableCacheController.addDateTime(var0, sqp0);
41 38 variableCacheController.addDateTime(var0, sqp1);
42 39 variableCacheController.addDateTime(var0, sqp2);
43 40
44 41 // first case [ts,te] < ts0
45 42 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
46 43 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
47 auto sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
48 static_cast<double>(te.toMSecsSinceEpoch())};
44 auto sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
49 45
50 46
51 47 auto notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
52 48
53 49 QCOMPARE(notInCach.size(), 1);
54 50 auto notInCacheSqp = notInCach.first();
55 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
56 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
51 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts));
52 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te));
57 53
58 54
59 55 // second case ts < ts0 && ts0 < te <= te0
60 56 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
61 57 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
62 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
63 static_cast<double>(te.toMSecsSinceEpoch())};
58 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
64 59
65 60
66 61 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
67 62
68 63 QCOMPARE(notInCach.size(), 1);
69 64 notInCacheSqp = notInCach.first();
70 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
71 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
65 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts));
66 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts0));
72 67
73 68 // 3th case ts < ts0 && te0 < te <= ts1
74 69 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
75 70 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
76 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
77 static_cast<double>(te.toMSecsSinceEpoch())};
71 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
78 72
79 73
80 74 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
81 75
82 76 QCOMPARE(notInCach.size(), 2);
83 77 notInCacheSqp = notInCach.first();
84 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
85 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
78 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts));
79 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts0));
86 80
87 81 notInCacheSqp = notInCach.at(1);
88 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
89 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
82 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0));
83 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te));
90 84
91 85 // 4th case ts < ts0 && ts1 < te <= te1
92 86 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
93 87 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}};
94 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
95 static_cast<double>(te.toMSecsSinceEpoch())};
88 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
96 89
97 90
98 91 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
99 92
100 93 QCOMPARE(notInCach.size(), 2);
101 94 notInCacheSqp = notInCach.first();
102 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
103 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
95 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts));
96 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts0));
104 97
105 98 notInCacheSqp = notInCach.at(1);
106 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
107 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
99 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0));
100 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts1));
108 101
109 102 // 5th case ts < ts0 && te3 < te
110 103 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
111 104 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}};
112 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
113 static_cast<double>(te.toMSecsSinceEpoch())};
105 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
114 106
115 107
116 108 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
117 109
118 110 QCOMPARE(notInCach.size(), 4);
119 111 notInCacheSqp = notInCach.first();
120 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
121 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
112 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts));
113 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts0));
122 114
123 115 notInCacheSqp = notInCach.at(1);
124 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
125 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
116 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0));
117 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts1));
126 118
127 119 notInCacheSqp = notInCach.at(2);
128 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
129 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts2.toMSecsSinceEpoch()));
120 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te1));
121 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts2));
130 122
131 123 notInCacheSqp = notInCach.at(3);
132 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te2.toMSecsSinceEpoch()));
133 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
124 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te2));
125 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te));
134 126
135 127
136 128 // 6th case ts2 < ts
137 129 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 45, 0, 0}};
138 130 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}};
139 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
140 static_cast<double>(te.toMSecsSinceEpoch())};
131 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
141 132
142 133
143 134 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
144 135
145 136 QCOMPARE(notInCach.size(), 1);
146 137 notInCacheSqp = notInCach.first();
147 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
148 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
138 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts));
139 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te));
149 140
150 141 // 7th case ts = te0 && te < ts1
151 142 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
152 143 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
153 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
154 static_cast<double>(te.toMSecsSinceEpoch())};
144 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
155 145
156 146
157 147 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
158 148
159 149 QCOMPARE(notInCach.size(), 1);
160 150 notInCacheSqp = notInCach.first();
161 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
162 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
151 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0));
152 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te));
163 153
164 154 // 8th case ts0 < ts < te0 && te < ts1
165 155 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
166 156 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
167 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
168 static_cast<double>(te.toMSecsSinceEpoch())};
157 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
169 158
170 159
171 160 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
172 161
173 162 QCOMPARE(notInCach.size(), 1);
174 163 notInCacheSqp = notInCach.first();
175 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
176 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
164 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0));
165 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te));
177 166
178 167 // 9th case ts0 < ts < te0 && ts1 < te < te1
179 168 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
180 169 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}};
181 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
182 static_cast<double>(te.toMSecsSinceEpoch())};
170 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
183 171
184 172
185 173 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
186 174
187 175 QCOMPARE(notInCach.size(), 1);
188 176 notInCacheSqp = notInCach.first();
189 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
190 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
177 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te0));
178 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts1));
191 179
192 180 // 10th case te1 < ts < te < ts2
193 181 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 9, 0, 0}};
194 182 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}};
195 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
196 static_cast<double>(te.toMSecsSinceEpoch())};
183 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
197 184
198 185
199 186 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
200 187
201 188 QCOMPARE(notInCach.size(), 1);
202 189 notInCacheSqp = notInCach.first();
203 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
204 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
190 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts));
191 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te));
205 192
206 193 // 11th case te0 < ts < ts1 && te3 < te
207 194 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
208 195 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}};
209 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
210 static_cast<double>(te.toMSecsSinceEpoch())};
196 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
211 197
212 198
213 199 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
214 200
215 201 QCOMPARE(notInCach.size(), 3);
216 202 notInCacheSqp = notInCach.first();
217 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
218 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
203 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts));
204 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts1));
219 205
220 206 notInCacheSqp = notInCach.at(1);
221 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
222 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts2.toMSecsSinceEpoch()));
207 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te1));
208 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts2));
223 209
224 210 notInCacheSqp = notInCach.at(2);
225 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te2.toMSecsSinceEpoch()));
226 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
211 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te2));
212 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te));
227 213
228 214 // 12th case te0 < ts < ts1 && te3 < te
229 215 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
230 216 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}};
231 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
232 static_cast<double>(te.toMSecsSinceEpoch())};
217 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
233 218
234 219 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
235 220
236 221 QCOMPARE(notInCach.size(), 2);
237 222 notInCacheSqp = notInCach.first();
238 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
239 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
223 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(ts));
224 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(ts1));
240 225
241 226 notInCacheSqp = notInCach.at(1);
242 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
243 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
227 QCOMPARE(notInCacheSqp.m_TStart, DateUtils::secondsSinceEpoch(te1));
228 QCOMPARE(notInCacheSqp.m_TEnd, DateUtils::secondsSinceEpoch(te));
244 229
245 230
246 231 // 12th case ts0 < ts < te0
247 232 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 10, 0}};
248 233 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 50, 0}};
249 sqp = SqpRange{static_cast<double>(ts.toMSecsSinceEpoch()),
250 static_cast<double>(te.toMSecsSinceEpoch())};
234 sqp = SqpRange{DateUtils::secondsSinceEpoch(ts), DateUtils::secondsSinceEpoch(te)};
251 235
252 236 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
253 237 QCOMPARE(notInCach.size(), 0);
254 238 }
255 239
256 240
257 241 void TestVariableCacheController::testAddDateTime()
258 242 {
259 243 VariableCacheController variableCacheController{};
260 244
261 245 auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
262 246 auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
263 auto sqp0 = SqpRange{static_cast<double>(ts0.toMSecsSinceEpoch()),
264 static_cast<double>(te0.toMSecsSinceEpoch())};
247 auto sqp0 = SqpRange{DateUtils::secondsSinceEpoch(ts0), DateUtils::secondsSinceEpoch(te0)};
265 248
266 249 auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
267 250 auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}};
268 auto sqp1 = SqpRange{static_cast<double>(ts1.toMSecsSinceEpoch()),
269 static_cast<double>(te1.toMSecsSinceEpoch())};
251 auto sqp1 = SqpRange{DateUtils::secondsSinceEpoch(ts1), DateUtils::secondsSinceEpoch(te1)};
270 252
271 253 auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}};
272 254 auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}};
273 auto sqp2 = SqpRange{static_cast<double>(ts2.toMSecsSinceEpoch()),
274 static_cast<double>(te2.toMSecsSinceEpoch())};
255 auto sqp2 = SqpRange{DateUtils::secondsSinceEpoch(ts2), DateUtils::secondsSinceEpoch(te2)};
275 256
276 257 auto ts01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
277 258 auto te01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
278 auto sqp01 = SqpRange{static_cast<double>(ts01.toMSecsSinceEpoch()),
279 static_cast<double>(te01.toMSecsSinceEpoch())};
259 auto sqp01 = SqpRange{DateUtils::secondsSinceEpoch(ts01), DateUtils::secondsSinceEpoch(te01)};
280 260
281 261 auto ts3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 14, 0, 0}};
282 262 auto te3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 16, 0, 0}};
283 auto sqp3 = SqpRange{static_cast<double>(ts3.toMSecsSinceEpoch()),
284 static_cast<double>(te3.toMSecsSinceEpoch())};
263 auto sqp3 = SqpRange{DateUtils::secondsSinceEpoch(ts3), DateUtils::secondsSinceEpoch(te3)};
285 264
286 265 auto ts03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
287 266 auto te03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}};
288 auto sqp03 = SqpRange{static_cast<double>(ts03.toMSecsSinceEpoch()),
289 static_cast<double>(te03.toMSecsSinceEpoch())};
267 auto sqp03 = SqpRange{DateUtils::secondsSinceEpoch(ts03), DateUtils::secondsSinceEpoch(te03)};
290 268
291 269
292 270 auto var0 = std::make_shared<Variable>("", sqp0);
293 271
294 272
295 273 // First case: add the first interval to the variable :sqp0
296 274 variableCacheController.addDateTime(var0, sqp0);
297 275 auto dateCacheList = variableCacheController.dateCacheList(var0);
298 276 QCOMPARE(dateCacheList.count(), 1);
299 277 auto dateCache = dateCacheList.at(0);
300 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
301 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te0.toMSecsSinceEpoch()));
278 QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts0));
279 QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te0));
302 280
303 281 // 2nd case: add a second interval : sqp1 > sqp0
304 282 variableCacheController.addDateTime(var0, sqp1);
305 283 dateCacheList = variableCacheController.dateCacheList(var0);
306 284 QCOMPARE(dateCacheList.count(), 2);
307 285 dateCache = dateCacheList.at(0);
308 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
309 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te0.toMSecsSinceEpoch()));
286 QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts0));
287 QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te0));
310 288
311 289 dateCache = dateCacheList.at(1);
312 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts1.toMSecsSinceEpoch()));
313 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
290 QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts1));
291 QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te1));
314 292
315 293 // 3th case: merge sqp0 & sqp1 with sqp01
316 294 variableCacheController.addDateTime(var0, sqp01);
317 295 dateCacheList = variableCacheController.dateCacheList(var0);
318 296 QCOMPARE(dateCacheList.count(), 1);
319 297 dateCache = dateCacheList.at(0);
320 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
321 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
298 QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts0));
299 QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te1));
322 300
323 301
324 302 // 4th case: add a second interval : sqp1 > sqp0
325 303 variableCacheController.addDateTime(var0, sqp2);
326 304 variableCacheController.addDateTime(var0, sqp3);
327 305 dateCacheList = variableCacheController.dateCacheList(var0);
328 306 QCOMPARE(dateCacheList.count(), 3);
329 307 dateCache = dateCacheList.at(0);
330 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
331 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
308 QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts0));
309 QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te1));
332 310
333 311 dateCache = dateCacheList.at(1);
334 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts3.toMSecsSinceEpoch()));
335 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te3.toMSecsSinceEpoch()));
312 QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts3));
313 QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te3));
336 314
337 315 dateCache = dateCacheList.at(2);
338 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts2.toMSecsSinceEpoch()));
339 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te2.toMSecsSinceEpoch()));
316 QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts2));
317 QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te2));
340 318
341 319
342 320 // 5th case: merge all interval
343 321 variableCacheController.addDateTime(var0, sqp03);
344 322 dateCacheList = variableCacheController.dateCacheList(var0);
345 323 QCOMPARE(dateCacheList.count(), 1);
346 324 dateCache = dateCacheList.at(0);
347 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
348 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te03.toMSecsSinceEpoch()));
325 QCOMPARE(dateCache.m_TStart, DateUtils::secondsSinceEpoch(ts0));
326 QCOMPARE(dateCache.m_TEnd, DateUtils::secondsSinceEpoch(te03));
349 327 }
350 328
351 329
352 330 QTEST_MAIN(TestVariableCacheController)
353 331 #include "TestVariableCacheController.moc"
@@ -1,45 +1,52
1 1 # On ignore toutes les règles vera++ pour le fichier spimpl
2 2 Common/spimpl\.h:\d+:.*
3 3
4 4 # Ignore false positive relative to two class definitions in a same file
5 5 DataSourceItem\.h:\d+:.*IPSIS_S01.*
6 DataSeries\.h:\d+:.*IPSIS_S01.*
7 DataSeriesIterator\.h:\d+:.*IPSIS_S01.*
6 8
7 9 # Ignore false positive relative to a template class
8 10 ArrayData\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (D)
9 11 ArrayData\.h:\d+:.*IPSIS_S04_NAMESPACE.*found: (arraydata_detail)
10 12 ArrayData\.h:\d+:.*IPSIS_S06.*found: (D)
11 13 ArrayData\.h:\d+:.*IPSIS_S06.*found: (Dim)
12 14 DataSeries\.h:\d+:.*IPSIS_S04_METHOD.*found: LOG_DataSeries
13 15 DataSeries\.h:\d+:.*IPSIS_S04_VARIABLE.*
16 DataSeries\.h:\d+:.*IPSIS_S04_NAMESPACE.*found: (dataseries_detail)
14 17
15 18 # Ignore false positive relative to iterators
16 19 ArrayData\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (forward_iterator_tag)
17 20 ArrayData\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (IteratorValue)
18 21 ArrayData\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (ptrdiff_t)
19 22 ArrayData\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (value_type)
20 23 ArrayData\.h:\d+:.*IPSIS_S05.*
21 24 ArrayData\.h:\d+:.*IPSIS_S06.*found: (iterator_category)
22 25 ArrayData\.h:\d+:.*IPSIS_S06.*found: (forward_iterator_tag)
23 26 ArrayData\.h:\d+:.*IPSIS_S06.*found: (value_type)
24 27 ArrayData\.h:\d+:.*IPSIS_S06.*found: (IteratorValue)
25 28 ArrayData\.h:\d+:.*IPSIS_S06.*found: (difference_type)
26 29 ArrayData\.h:\d+:.*IPSIS_S06.*found: (ptrdiff_t)
27 30 ArrayData\.h:\d+:.*IPSIS_S06.*found: (pointer)
28 31 ArrayData\.h:\d+:.*IPSIS_S06.*found: (reference)
29 32 ArrayData\.h:\d+:.*IPSIS_S06.*found: (value_type)
30 DataSeries\.h:\d+:.*IPSIS_S05.*
31 DataSeries\.h:\d+:.*IPSIS_S06.*found: (iterator_category)
32 DataSeries\.h:\d+:.*IPSIS_S06.*found: (forward_iterator_tag)
33 DataSeries\.h:\d+:.*IPSIS_S06.*found: (value_type)
34 DataSeries\.h:\d+:.*IPSIS_S06.*found: (IteratorValue)
35 DataSeries\.h:\d+:.*IPSIS_S06.*found: (difference_type)
36 DataSeries\.h:\d+:.*IPSIS_S06.*found: (ptrdiff_t)
37 DataSeries\.h:\d+:.*IPSIS_S06.*found: (pointer)
38 DataSeries\.h:\d+:.*IPSIS_S06.*found: (reference)
39 DataSeries\.h:\d+:.*IPSIS_S06.*found: (value_type)
33 DataSeriesIterator\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (forward_iterator_tag)
34 DataSeriesIterator\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (DataSeriesIteratorValue)
35 DataSeriesIterator\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (ptrdiff_t)
36 DataSeriesIterator\.h:\d+:.*IPSIS_S04_VARIABLE.*found: (value_type)
37 DataSeriesIterator\.h:\d+:.*IPSIS_S05.*
38 DataSeriesIterator\.h:\d+:.*IPSIS_S06.*found: (iterator_category)
39 DataSeriesIterator\.h:\d+:.*IPSIS_S06.*found: (forward_iterator_tag)
40 DataSeriesIterator\.h:\d+:.*IPSIS_S06.*found: (value_type)
41 DataSeriesIterator\.h:\d+:.*IPSIS_S06.*found: (DataSeriesIteratorValue)
42 DataSeriesIterator\.h:\d+:.*IPSIS_S06.*found: (difference_type)
43 DataSeriesIterator\.h:\d+:.*IPSIS_S06.*found: (ptrdiff_t)
44 DataSeriesIterator\.h:\d+:.*IPSIS_S06.*found: (pointer)
45 DataSeriesIterator\.h:\d+:.*IPSIS_S06.*found: (reference)
46 DataSeriesIterator\.h:\d+:.*IPSIS_S06.*found: (value_type)
40 47
41 48 # Ignore false positive relative to an alias
42 49 DataSourceItemAction\.h:\d+:.*IPSIS_S06.*found: (ExecuteFunction)
43 50
44 51 # Ignore false positive relative to unnamed namespace
45 52 VariableController\.cpp:\d+:.*IPSIS_F13.*
General Comments 0
You need to be logged in to leave comments. Login now