@@ -1,287 +1,295 | |||||
1 | #ifndef SCIQLOP_DATASERIES_H |
|
1 | #ifndef SCIQLOP_DATASERIES_H | |
2 | #define SCIQLOP_DATASERIES_H |
|
2 | #define SCIQLOP_DATASERIES_H | |
3 |
|
3 | |||
4 | #include "CoreGlobal.h" |
|
4 | #include "CoreGlobal.h" | |
5 |
|
5 | |||
6 | #include <Common/SortUtils.h> |
|
6 | #include <Common/SortUtils.h> | |
7 |
|
7 | |||
8 | #include <Data/ArrayData.h> |
|
8 | #include <Data/ArrayData.h> | |
9 | #include <Data/IDataSeries.h> |
|
9 | #include <Data/IDataSeries.h> | |
10 |
|
10 | |||
11 | #include <QLoggingCategory> |
|
11 | #include <QLoggingCategory> | |
12 | #include <QReadLocker> |
|
12 | #include <QReadLocker> | |
13 | #include <QReadWriteLock> |
|
13 | #include <QReadWriteLock> | |
14 | #include <memory> |
|
14 | #include <memory> | |
15 |
|
15 | |||
16 | // We don't use the Qt macro since the log is used in the header file, which causes multiple log |
|
16 | // We don't use the Qt macro since the log is used in the header file, which causes multiple log | |
17 | // definitions with inheritance. Inline method is used instead |
|
17 | // definitions with inheritance. Inline method is used instead | |
18 | inline const QLoggingCategory &LOG_DataSeries() |
|
18 | inline const QLoggingCategory &LOG_DataSeries() | |
19 | { |
|
19 | { | |
20 | static const QLoggingCategory category{"DataSeries"}; |
|
20 | static const QLoggingCategory category{"DataSeries"}; | |
21 | return category; |
|
21 | return category; | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | template <int Dim> |
|
24 | template <int Dim> | |
25 | class DataSeries; |
|
25 | class DataSeries; | |
26 |
|
26 | |||
27 | namespace dataseries_detail { |
|
27 | namespace dataseries_detail { | |
28 |
|
28 | |||
29 | template <int Dim> |
|
29 | template <int Dim> | |
30 | class IteratorValue : public DataSeriesIteratorValue::Impl { |
|
30 | class IteratorValue : public DataSeriesIteratorValue::Impl { | |
31 | public: |
|
31 | public: | |
32 | explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin) |
|
32 | explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin) | |
33 | : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()), |
|
33 | : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()), | |
34 | m_ValuesIt(begin ? dataSeries.valuesData()->cbegin() |
|
34 | m_ValuesIt(begin ? dataSeries.valuesData()->cbegin() | |
35 | : dataSeries.valuesData()->cend()) |
|
35 | : dataSeries.valuesData()->cend()) | |
36 | { |
|
36 | { | |
37 | } |
|
37 | } | |
38 | IteratorValue(const IteratorValue &other) = default; |
|
38 | IteratorValue(const IteratorValue &other) = default; | |
39 |
|
39 | |||
40 | std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override |
|
40 | std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override | |
41 | { |
|
41 | { | |
42 | return std::make_unique<IteratorValue<Dim> >(*this); |
|
42 | return std::make_unique<IteratorValue<Dim> >(*this); | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | bool equals(const DataSeriesIteratorValue::Impl &other) const override try { |
|
45 | bool equals(const DataSeriesIteratorValue::Impl &other) const override try { | |
46 | const auto &otherImpl = dynamic_cast<const IteratorValue &>(other); |
|
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); |
|
47 | return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt); | |
48 | } |
|
48 | } | |
49 | catch (const std::bad_cast &) { |
|
49 | catch (const std::bad_cast &) { | |
50 | return false; |
|
50 | return false; | |
51 | } |
|
51 | } | |
52 |
|
52 | |||
53 | void next() override |
|
53 | void next() override | |
54 | { |
|
54 | { | |
55 | ++m_XIt; |
|
55 | ++m_XIt; | |
56 | ++m_ValuesIt; |
|
56 | ++m_ValuesIt; | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 | void prev() override |
|
59 | void prev() override | |
60 | { |
|
60 | { | |
61 | --m_XIt; |
|
61 | --m_XIt; | |
62 | --m_ValuesIt; |
|
62 | --m_ValuesIt; | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | double x() const override { return m_XIt->at(0); } |
|
65 | double x() const override { return m_XIt->at(0); } | |
66 | double value() const override { return m_ValuesIt->at(0); } |
|
66 | double value() const override { return m_ValuesIt->at(0); } | |
67 | double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); } |
|
67 | double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); } | |
68 |
|
68 | |||
69 | private: |
|
69 | private: | |
70 | ArrayData<1>::Iterator m_XIt; |
|
70 | ArrayData<1>::Iterator m_XIt; | |
71 | typename ArrayData<Dim>::Iterator m_ValuesIt; |
|
71 | typename ArrayData<Dim>::Iterator m_ValuesIt; | |
72 | }; |
|
72 | }; | |
73 | } // namespace dataseries_detail |
|
73 | } // namespace dataseries_detail | |
74 |
|
74 | |||
75 | /** |
|
75 | /** | |
76 | * @brief The DataSeries class is the base (abstract) implementation of IDataSeries. |
|
76 | * @brief The DataSeries class is the base (abstract) implementation of IDataSeries. | |
77 | * |
|
77 | * | |
78 | * It proposes to set a dimension for the values ββdata. |
|
78 | * It proposes to set a dimension for the values ββdata. | |
79 | * |
|
79 | * | |
80 | * A DataSeries is always sorted on its x-axis data. |
|
80 | * A DataSeries is always sorted on its x-axis data. | |
81 | * |
|
81 | * | |
82 | * @tparam Dim The dimension of the values data |
|
82 | * @tparam Dim The dimension of the values data | |
83 | * |
|
83 | * | |
84 | */ |
|
84 | */ | |
85 | template <int Dim> |
|
85 | template <int Dim> | |
86 | class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries { |
|
86 | class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries { | |
87 | public: |
|
87 | public: | |
88 | /// @sa IDataSeries::xAxisData() |
|
88 | /// @sa IDataSeries::xAxisData() | |
89 | std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; } |
|
89 | std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; } | |
90 | const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; } |
|
90 | const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; } | |
91 |
|
91 | |||
92 | /// @sa IDataSeries::xAxisUnit() |
|
92 | /// @sa IDataSeries::xAxisUnit() | |
93 | Unit xAxisUnit() const override { return m_XAxisUnit; } |
|
93 | Unit xAxisUnit() const override { return m_XAxisUnit; } | |
94 |
|
94 | |||
95 | /// @return the values dataset |
|
95 | /// @return the values dataset | |
96 | std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; } |
|
96 | std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; } | |
97 | const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; } |
|
97 | const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; } | |
98 |
|
98 | |||
99 | /// @sa IDataSeries::valuesUnit() |
|
99 | /// @sa IDataSeries::valuesUnit() | |
100 | Unit valuesUnit() const override { return m_ValuesUnit; } |
|
100 | Unit valuesUnit() const override { return m_ValuesUnit; } | |
101 |
|
101 | |||
102 |
|
102 | |||
103 | SqpRange range() const override |
|
103 | SqpRange range() const override | |
104 | { |
|
104 | { | |
105 | if (!m_XAxisData->cdata().isEmpty()) { |
|
105 | if (!m_XAxisData->cdata().isEmpty()) { | |
106 | return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()}; |
|
106 | return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()}; | |
107 | } |
|
107 | } | |
108 |
|
108 | |||
109 | return SqpRange{}; |
|
109 | return SqpRange{}; | |
110 | } |
|
110 | } | |
111 |
|
111 | |||
112 | void clear() |
|
112 | void clear() | |
113 | { |
|
113 | { | |
114 | m_XAxisData->clear(); |
|
114 | m_XAxisData->clear(); | |
115 | m_ValuesData->clear(); |
|
115 | m_ValuesData->clear(); | |
116 | } |
|
116 | } | |
117 |
|
117 | |||
118 | /// Merges into the data series an other data series |
|
118 | /// Merges into the data series an other data series | |
119 | /// @remarks the data series to merge with is cleared after the operation |
|
119 | /// @remarks the data series to merge with is cleared after the operation | |
120 | void merge(IDataSeries *dataSeries) override |
|
120 | void merge(IDataSeries *dataSeries) override | |
121 | { |
|
121 | { | |
122 | dataSeries->lockWrite(); |
|
122 | dataSeries->lockWrite(); | |
123 | lockWrite(); |
|
123 | lockWrite(); | |
124 |
|
124 | |||
125 | if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) { |
|
125 | if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) { | |
126 | const auto &otherXAxisData = other->xAxisData()->cdata(); |
|
126 | const auto &otherXAxisData = other->xAxisData()->cdata(); | |
127 | const auto &xAxisData = m_XAxisData->cdata(); |
|
127 | const auto &xAxisData = m_XAxisData->cdata(); | |
128 |
|
128 | |||
129 | // As data series are sorted, we can improve performances of merge, by call the sort |
|
129 | // As data series are sorted, we can improve performances of merge, by call the sort | |
130 | // method only if the two data series overlap. |
|
130 | // method only if the two data series overlap. | |
131 | if (!otherXAxisData.empty()) { |
|
131 | if (!otherXAxisData.empty()) { | |
132 | auto firstValue = otherXAxisData.front(); |
|
132 | auto firstValue = otherXAxisData.front(); | |
133 | auto lastValue = otherXAxisData.back(); |
|
133 | auto lastValue = otherXAxisData.back(); | |
134 |
|
134 | |||
135 | auto xAxisDataBegin = xAxisData.cbegin(); |
|
135 | auto xAxisDataBegin = xAxisData.cbegin(); | |
136 | auto xAxisDataEnd = xAxisData.cend(); |
|
136 | auto xAxisDataEnd = xAxisData.cend(); | |
137 |
|
137 | |||
138 | bool prepend; |
|
138 | bool prepend; | |
139 | bool sortNeeded; |
|
139 | bool sortNeeded; | |
140 |
|
140 | |||
141 | if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) { |
|
141 | if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) { | |
142 | // Other data series if after data series |
|
142 | // Other data series if after data series | |
143 | prepend = false; |
|
143 | prepend = false; | |
144 | sortNeeded = false; |
|
144 | sortNeeded = false; | |
145 | } |
|
145 | } | |
146 | else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue) |
|
146 | else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue) | |
147 | == xAxisDataBegin) { |
|
147 | == xAxisDataBegin) { | |
148 | // Other data series if before data series |
|
148 | // Other data series if before data series | |
149 | prepend = true; |
|
149 | prepend = true; | |
150 | sortNeeded = false; |
|
150 | sortNeeded = false; | |
151 | } |
|
151 | } | |
152 | else { |
|
152 | else { | |
153 | // The two data series overlap |
|
153 | // The two data series overlap | |
154 | prepend = false; |
|
154 | prepend = false; | |
155 | sortNeeded = true; |
|
155 | sortNeeded = true; | |
156 | } |
|
156 | } | |
157 |
|
157 | |||
158 | // Makes the merge |
|
158 | // Makes the merge | |
159 | m_XAxisData->add(*other->xAxisData(), prepend); |
|
159 | m_XAxisData->add(*other->xAxisData(), prepend); | |
160 | m_ValuesData->add(*other->valuesData(), prepend); |
|
160 | m_ValuesData->add(*other->valuesData(), prepend); | |
161 |
|
161 | |||
162 | if (sortNeeded) { |
|
162 | if (sortNeeded) { | |
163 | sort(); |
|
163 | sort(); | |
164 | } |
|
164 | } | |
165 | } |
|
165 | } | |
166 |
|
166 | |||
167 | // Clears the other data series |
|
167 | // Clears the other data series | |
168 | other->clear(); |
|
168 | other->clear(); | |
169 | } |
|
169 | } | |
170 | else { |
|
170 | else { | |
171 | qCWarning(LOG_DataSeries()) |
|
171 | qCWarning(LOG_DataSeries()) | |
172 | << QObject::tr("Detection of a type of IDataSeries we cannot merge with !"); |
|
172 | << QObject::tr("Detection of a type of IDataSeries we cannot merge with !"); | |
173 | } |
|
173 | } | |
174 | unlock(); |
|
174 | unlock(); | |
175 | dataSeries->unlock(); |
|
175 | dataSeries->unlock(); | |
176 | } |
|
176 | } | |
177 |
|
177 | |||
178 | // ///////// // |
|
178 | // ///////// // | |
179 | // Iterators // |
|
179 | // Iterators // | |
180 | // ///////// // |
|
180 | // ///////// // | |
181 |
|
181 | |||
182 | DataSeriesIterator cbegin() const override |
|
182 | DataSeriesIterator cbegin() const override | |
183 | { |
|
183 | { | |
184 | return DataSeriesIterator{DataSeriesIteratorValue{ |
|
184 | return DataSeriesIterator{DataSeriesIteratorValue{ | |
185 | std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}}; |
|
185 | std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, true)}}; | |
186 | } |
|
186 | } | |
187 |
|
187 | |||
188 | DataSeriesIterator cend() const override |
|
188 | DataSeriesIterator cend() const override | |
189 | { |
|
189 | { | |
190 | return DataSeriesIterator{DataSeriesIteratorValue{ |
|
190 | return DataSeriesIterator{DataSeriesIteratorValue{ | |
191 | std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}}; |
|
191 | std::make_unique<dataseries_detail::IteratorValue<Dim> >(*this, false)}}; | |
192 | } |
|
192 | } | |
193 |
|
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 | } | |||
|
201 | ||||
194 | std::pair<DataSeriesIterator, DataSeriesIterator> subData(double min, double max) const override |
|
202 | std::pair<DataSeriesIterator, DataSeriesIterator> subData(double min, double max) const override | |
195 | { |
|
203 | { | |
196 | if (min > max) { |
|
204 | if (min > max) { | |
197 | std::swap(min, max); |
|
205 | std::swap(min, max); | |
198 | } |
|
206 | } | |
199 |
|
207 | |||
200 | auto begin = cbegin(); |
|
208 | auto begin = cbegin(); | |
201 | auto end = cend(); |
|
209 | auto end = cend(); | |
202 |
|
210 | |||
203 | auto lowerIt |
|
211 | auto lowerIt | |
204 | = std::lower_bound(begin, end, min, [](const auto &itValue, const auto &value) { |
|
212 | = std::lower_bound(begin, end, min, [](const auto &itValue, const auto &value) { | |
205 | return itValue.x() < value; |
|
213 | return itValue.x() < value; | |
206 | }); |
|
214 | }); | |
207 | auto upperIt |
|
215 | auto upperIt | |
208 | = std::upper_bound(begin, end, max, [](const auto &value, const auto &itValue) { |
|
216 | = std::upper_bound(begin, end, max, [](const auto &value, const auto &itValue) { | |
209 | return value < itValue.x(); |
|
217 | return value < itValue.x(); | |
210 | }); |
|
218 | }); | |
211 |
|
219 | |||
212 | return std::make_pair(lowerIt, upperIt); |
|
220 | return std::make_pair(lowerIt, upperIt); | |
213 | } |
|
221 | } | |
214 |
|
222 | |||
215 | // /////// // |
|
223 | // /////// // | |
216 | // Mutexes // |
|
224 | // Mutexes // | |
217 | // /////// // |
|
225 | // /////// // | |
218 |
|
226 | |||
219 | virtual void lockRead() { m_Lock.lockForRead(); } |
|
227 | virtual void lockRead() { m_Lock.lockForRead(); } | |
220 | virtual void lockWrite() { m_Lock.lockForWrite(); } |
|
228 | virtual void lockWrite() { m_Lock.lockForWrite(); } | |
221 | virtual void unlock() { m_Lock.unlock(); } |
|
229 | virtual void unlock() { m_Lock.unlock(); } | |
222 |
|
230 | |||
223 | protected: |
|
231 | protected: | |
224 | /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a |
|
232 | /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a | |
225 | /// DataSeries with no values will be created. |
|
233 | /// DataSeries with no values will be created. | |
226 | /// @remarks data series is automatically sorted on its x-axis data |
|
234 | /// @remarks data series is automatically sorted on its x-axis data | |
227 | explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit, |
|
235 | explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit, | |
228 | std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit) |
|
236 | std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit) | |
229 | : m_XAxisData{xAxisData}, |
|
237 | : m_XAxisData{xAxisData}, | |
230 | m_XAxisUnit{xAxisUnit}, |
|
238 | m_XAxisUnit{xAxisUnit}, | |
231 | m_ValuesData{valuesData}, |
|
239 | m_ValuesData{valuesData}, | |
232 | m_ValuesUnit{valuesUnit} |
|
240 | m_ValuesUnit{valuesUnit} | |
233 | { |
|
241 | { | |
234 | if (m_XAxisData->size() != m_ValuesData->size()) { |
|
242 | if (m_XAxisData->size() != m_ValuesData->size()) { | |
235 | clear(); |
|
243 | clear(); | |
236 | } |
|
244 | } | |
237 |
|
245 | |||
238 | // Sorts data if it's not the case |
|
246 | // Sorts data if it's not the case | |
239 | const auto &xAxisCData = m_XAxisData->cdata(); |
|
247 | const auto &xAxisCData = m_XAxisData->cdata(); | |
240 | if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) { |
|
248 | if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) { | |
241 | sort(); |
|
249 | sort(); | |
242 | } |
|
250 | } | |
243 | } |
|
251 | } | |
244 |
|
252 | |||
245 | /// Copy ctor |
|
253 | /// Copy ctor | |
246 | explicit DataSeries(const DataSeries<Dim> &other) |
|
254 | explicit DataSeries(const DataSeries<Dim> &other) | |
247 | : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)}, |
|
255 | : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)}, | |
248 | m_XAxisUnit{other.m_XAxisUnit}, |
|
256 | m_XAxisUnit{other.m_XAxisUnit}, | |
249 | m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)}, |
|
257 | m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)}, | |
250 | m_ValuesUnit{other.m_ValuesUnit} |
|
258 | m_ValuesUnit{other.m_ValuesUnit} | |
251 | { |
|
259 | { | |
252 | // Since a series is ordered from its construction and is always ordered, it is not |
|
260 | // Since a series is ordered from its construction and is always ordered, it is not | |
253 | // necessary to call the sort method here ('other' is sorted) |
|
261 | // necessary to call the sort method here ('other' is sorted) | |
254 | } |
|
262 | } | |
255 |
|
263 | |||
256 | /// Assignment operator |
|
264 | /// Assignment operator | |
257 | template <int D> |
|
265 | template <int D> | |
258 | DataSeries &operator=(DataSeries<D> other) |
|
266 | DataSeries &operator=(DataSeries<D> other) | |
259 | { |
|
267 | { | |
260 | std::swap(m_XAxisData, other.m_XAxisData); |
|
268 | std::swap(m_XAxisData, other.m_XAxisData); | |
261 | std::swap(m_XAxisUnit, other.m_XAxisUnit); |
|
269 | std::swap(m_XAxisUnit, other.m_XAxisUnit); | |
262 | std::swap(m_ValuesData, other.m_ValuesData); |
|
270 | std::swap(m_ValuesData, other.m_ValuesData); | |
263 | std::swap(m_ValuesUnit, other.m_ValuesUnit); |
|
271 | std::swap(m_ValuesUnit, other.m_ValuesUnit); | |
264 |
|
272 | |||
265 | return *this; |
|
273 | return *this; | |
266 | } |
|
274 | } | |
267 |
|
275 | |||
268 | private: |
|
276 | private: | |
269 | /** |
|
277 | /** | |
270 | * Sorts data series on its x-axis data |
|
278 | * Sorts data series on its x-axis data | |
271 | */ |
|
279 | */ | |
272 | void sort() noexcept |
|
280 | void sort() noexcept | |
273 | { |
|
281 | { | |
274 | auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>()); |
|
282 | auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>()); | |
275 | m_XAxisData = m_XAxisData->sort(permutation); |
|
283 | m_XAxisData = m_XAxisData->sort(permutation); | |
276 | m_ValuesData = m_ValuesData->sort(permutation); |
|
284 | m_ValuesData = m_ValuesData->sort(permutation); | |
277 | } |
|
285 | } | |
278 |
|
286 | |||
279 | std::shared_ptr<ArrayData<1> > m_XAxisData; |
|
287 | std::shared_ptr<ArrayData<1> > m_XAxisData; | |
280 | Unit m_XAxisUnit; |
|
288 | Unit m_XAxisUnit; | |
281 | std::shared_ptr<ArrayData<Dim> > m_ValuesData; |
|
289 | std::shared_ptr<ArrayData<Dim> > m_ValuesData; | |
282 | Unit m_ValuesUnit; |
|
290 | Unit m_ValuesUnit; | |
283 |
|
291 | |||
284 | QReadWriteLock m_Lock; |
|
292 | QReadWriteLock m_Lock; | |
285 | }; |
|
293 | }; | |
286 |
|
294 | |||
287 | #endif // SCIQLOP_DATASERIES_H |
|
295 | #endif // SCIQLOP_DATASERIES_H |
@@ -1,88 +1,92 | |||||
1 | #ifndef SCIQLOP_IDATASERIES_H |
|
1 | #ifndef SCIQLOP_IDATASERIES_H | |
2 | #define SCIQLOP_IDATASERIES_H |
|
2 | #define SCIQLOP_IDATASERIES_H | |
3 |
|
3 | |||
4 | #include <Common/MetaTypes.h> |
|
4 | #include <Common/MetaTypes.h> | |
5 | #include <Data/DataSeriesIterator.h> |
|
5 | #include <Data/DataSeriesIterator.h> | |
6 | #include <Data/SqpRange.h> |
|
6 | #include <Data/SqpRange.h> | |
7 |
|
7 | |||
8 | #include <memory> |
|
8 | #include <memory> | |
9 |
|
9 | |||
10 | #include <QString> |
|
10 | #include <QString> | |
11 |
|
11 | |||
12 | template <int Dim> |
|
12 | template <int Dim> | |
13 | class ArrayData; |
|
13 | class ArrayData; | |
14 |
|
14 | |||
15 | struct Unit { |
|
15 | struct Unit { | |
16 | explicit Unit(const QString &name = {}, bool timeUnit = false) |
|
16 | explicit Unit(const QString &name = {}, bool timeUnit = false) | |
17 | : m_Name{name}, m_TimeUnit{timeUnit} |
|
17 | : m_Name{name}, m_TimeUnit{timeUnit} | |
18 | { |
|
18 | { | |
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 | inline bool operator==(const Unit &other) const |
|
21 | inline bool operator==(const Unit &other) const | |
22 | { |
|
22 | { | |
23 | return std::tie(m_Name, m_TimeUnit) == std::tie(other.m_Name, other.m_TimeUnit); |
|
23 | return std::tie(m_Name, m_TimeUnit) == std::tie(other.m_Name, other.m_TimeUnit); | |
24 | } |
|
24 | } | |
25 | inline bool operator!=(const Unit &other) const { return !(*this == other); } |
|
25 | inline bool operator!=(const Unit &other) const { return !(*this == other); } | |
26 |
|
26 | |||
27 | QString m_Name; ///< Unit name |
|
27 | QString m_Name; ///< Unit name | |
28 | bool m_TimeUnit; ///< The unit is a unit of time (UTC) |
|
28 | bool m_TimeUnit; ///< The unit is a unit of time (UTC) | |
29 | }; |
|
29 | }; | |
30 |
|
30 | |||
31 | /** |
|
31 | /** | |
32 | * @brief The IDataSeries aims to declare a data series. |
|
32 | * @brief The IDataSeries aims to declare a data series. | |
33 | * |
|
33 | * | |
34 | * A data series is an entity that contains at least : |
|
34 | * A data series is an entity that contains at least : | |
35 | * - one dataset representing the x-axis |
|
35 | * - one dataset representing the x-axis | |
36 | * - one dataset representing the values |
|
36 | * - one dataset representing the values | |
37 | * |
|
37 | * | |
38 | * Each dataset is represented by an ArrayData, and is associated with a unit. |
|
38 | * Each dataset is represented by an ArrayData, and is associated with a unit. | |
39 | * |
|
39 | * | |
40 | * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the |
|
40 | * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the | |
41 | * IDataSeries. The x-axis dataset is always unidimensional. |
|
41 | * IDataSeries. The x-axis dataset is always unidimensional. | |
42 | * |
|
42 | * | |
43 | * @sa ArrayData |
|
43 | * @sa ArrayData | |
44 | */ |
|
44 | */ | |
45 | class IDataSeries { |
|
45 | class IDataSeries { | |
46 | public: |
|
46 | public: | |
47 | virtual ~IDataSeries() noexcept = default; |
|
47 | virtual ~IDataSeries() noexcept = default; | |
48 |
|
48 | |||
49 | /// Returns the x-axis dataset |
|
49 | /// Returns the x-axis dataset | |
50 | virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0; |
|
50 | virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0; | |
51 |
|
51 | |||
52 | /// Returns the x-axis dataset (as const) |
|
52 | /// Returns the x-axis dataset (as const) | |
53 | virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0; |
|
53 | virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0; | |
54 |
|
54 | |||
55 | virtual Unit xAxisUnit() const = 0; |
|
55 | virtual Unit xAxisUnit() const = 0; | |
56 |
|
56 | |||
57 | virtual Unit valuesUnit() const = 0; |
|
57 | virtual Unit valuesUnit() const = 0; | |
58 |
|
58 | |||
59 | virtual void merge(IDataSeries *dataSeries) = 0; |
|
59 | virtual void merge(IDataSeries *dataSeries) = 0; | |
60 | /// @todo Review the name and signature of this method |
|
60 | /// @todo Review the name and signature of this method | |
61 | virtual std::shared_ptr<IDataSeries> subDataSeries(const SqpRange &range) = 0; |
|
61 | virtual std::shared_ptr<IDataSeries> subDataSeries(const SqpRange &range) = 0; | |
62 |
|
62 | |||
63 | virtual std::unique_ptr<IDataSeries> clone() const = 0; |
|
63 | virtual std::unique_ptr<IDataSeries> clone() const = 0; | |
64 | virtual SqpRange range() const = 0; |
|
64 | virtual SqpRange range() const = 0; | |
65 |
|
65 | |||
66 | // ///////// // |
|
66 | // ///////// // | |
67 | // Iterators // |
|
67 | // Iterators // | |
68 | // ///////// // |
|
68 | // ///////// // | |
69 |
|
69 | |||
70 | virtual DataSeriesIterator cbegin() const = 0; |
|
70 | virtual DataSeriesIterator cbegin() const = 0; | |
71 | virtual DataSeriesIterator cend() const = 0; |
|
71 | virtual DataSeriesIterator cend() const = 0; | |
72 |
|
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 | ||||
73 | virtual std::pair<DataSeriesIterator, DataSeriesIterator> subData(double min, |
|
77 | virtual std::pair<DataSeriesIterator, DataSeriesIterator> subData(double min, | |
74 | double max) const = 0; |
|
78 | double max) const = 0; | |
75 |
|
79 | |||
76 | // /////// // |
|
80 | // /////// // | |
77 | // Mutexes // |
|
81 | // Mutexes // | |
78 | // /////// // |
|
82 | // /////// // | |
79 |
|
83 | |||
80 | virtual void lockRead() = 0; |
|
84 | virtual void lockRead() = 0; | |
81 | virtual void lockWrite() = 0; |
|
85 | virtual void lockWrite() = 0; | |
82 | virtual void unlock() = 0; |
|
86 | virtual void unlock() = 0; | |
83 | }; |
|
87 | }; | |
84 |
|
88 | |||
85 | // Required for using shared_ptr in signals/slots |
|
89 | // Required for using shared_ptr in signals/slots | |
86 | SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>) |
|
90 | SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>) | |
87 |
|
91 | |||
88 | #endif // SCIQLOP_IDATASERIES_H |
|
92 | #endif // SCIQLOP_IDATASERIES_H |
@@ -1,232 +1,291 | |||||
1 | #include "Data/DataSeries.h" |
|
1 | #include "Data/DataSeries.h" | |
2 | #include "Data/ScalarSeries.h" |
|
2 | #include "Data/ScalarSeries.h" | |
3 |
|
3 | |||
4 | #include <QObject> |
|
4 | #include <QObject> | |
5 | #include <QtTest> |
|
5 | #include <QtTest> | |
6 |
|
6 | |||
7 | Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>) |
|
7 | Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>) | |
8 |
|
8 | |||
9 | class TestDataSeries : public QObject { |
|
9 | class TestDataSeries : public QObject { | |
10 | Q_OBJECT |
|
10 | Q_OBJECT | |
11 | private slots: |
|
11 | private slots: | |
12 | /// Input test data |
|
12 | /// Input test data | |
13 | /// @sa testCtor() |
|
13 | /// @sa testCtor() | |
14 | void testCtor_data(); |
|
14 | void testCtor_data(); | |
15 |
|
15 | |||
16 | /// Tests construction of a data series |
|
16 | /// Tests construction of a data series | |
17 | void testCtor(); |
|
17 | void testCtor(); | |
18 |
|
18 | |||
19 | /// Input test data |
|
19 | /// Input test data | |
20 | /// @sa testMerge() |
|
20 | /// @sa testMerge() | |
21 | void testMerge_data(); |
|
21 | void testMerge_data(); | |
22 |
|
22 | |||
23 | /// Tests merge of two data series |
|
23 | /// Tests merge of two data series | |
24 | void testMerge(); |
|
24 | void testMerge(); | |
25 |
|
25 | |||
26 | /// Input test data |
|
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 | ||||
27 | /// @sa testSubdata() |
|
33 | /// @sa testSubdata() | |
28 | void testSubdata_data(); |
|
34 | void testSubdata_data(); | |
29 |
|
35 | |||
30 | /// Tests get subdata of two data series |
|
36 | /// Tests get subdata of two data series | |
31 | void testSubdata(); |
|
37 | void testSubdata(); | |
32 | }; |
|
38 | }; | |
33 |
|
39 | |||
34 | void TestDataSeries::testCtor_data() |
|
40 | void TestDataSeries::testCtor_data() | |
35 | { |
|
41 | { | |
36 | // ////////////// // |
|
42 | // ////////////// // | |
37 | // Test structure // |
|
43 | // Test structure // | |
38 | // ////////////// // |
|
44 | // ////////////// // | |
39 |
|
45 | |||
40 | // x-axis data |
|
46 | // x-axis data | |
41 | QTest::addColumn<QVector<double> >("xAxisData"); |
|
47 | QTest::addColumn<QVector<double> >("xAxisData"); | |
42 | // values data |
|
48 | // values data | |
43 | QTest::addColumn<QVector<double> >("valuesData"); |
|
49 | QTest::addColumn<QVector<double> >("valuesData"); | |
44 |
|
50 | |||
45 | // expected x-axis data |
|
51 | // expected x-axis data | |
46 | QTest::addColumn<QVector<double> >("expectedXAxisData"); |
|
52 | QTest::addColumn<QVector<double> >("expectedXAxisData"); | |
47 | // expected values data |
|
53 | // expected values data | |
48 | QTest::addColumn<QVector<double> >("expectedValuesData"); |
|
54 | QTest::addColumn<QVector<double> >("expectedValuesData"); | |
49 |
|
55 | |||
50 | // ////////// // |
|
56 | // ////////// // | |
51 | // Test cases // |
|
57 | // Test cases // | |
52 | // ////////// // |
|
58 | // ////////// // | |
53 |
|
59 | |||
54 | QTest::newRow("invalidData (different sizes of vectors)") |
|
60 | QTest::newRow("invalidData (different sizes of vectors)") | |
55 | << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 200., 300.} |
|
61 | << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 200., 300.} | |
56 | << QVector<double>{} << QVector<double>{}; |
|
62 | << QVector<double>{} << QVector<double>{}; | |
57 |
|
63 | |||
58 | QTest::newRow("sortedData") << QVector<double>{1., 2., 3., 4., 5.} |
|
64 | QTest::newRow("sortedData") << QVector<double>{1., 2., 3., 4., 5.} | |
59 | << QVector<double>{100., 200., 300., 400., 500.} |
|
65 | << QVector<double>{100., 200., 300., 400., 500.} | |
60 | << QVector<double>{1., 2., 3., 4., 5.} |
|
66 | << QVector<double>{1., 2., 3., 4., 5.} | |
61 | << QVector<double>{100., 200., 300., 400., 500.}; |
|
67 | << QVector<double>{100., 200., 300., 400., 500.}; | |
62 |
|
68 | |||
63 | QTest::newRow("unsortedData") << QVector<double>{5., 4., 3., 2., 1.} |
|
69 | QTest::newRow("unsortedData") << QVector<double>{5., 4., 3., 2., 1.} | |
64 | << QVector<double>{100., 200., 300., 400., 500.} |
|
70 | << QVector<double>{100., 200., 300., 400., 500.} | |
65 | << QVector<double>{1., 2., 3., 4., 5.} |
|
71 | << QVector<double>{1., 2., 3., 4., 5.} | |
66 | << QVector<double>{500., 400., 300., 200., 100.}; |
|
72 | << QVector<double>{500., 400., 300., 200., 100.}; | |
67 |
|
73 | |||
68 | QTest::newRow("unsortedData2") |
|
74 | QTest::newRow("unsortedData2") | |
69 | << QVector<double>{1., 4., 3., 5., 2.} << QVector<double>{100., 200., 300., 400., 500.} |
|
75 | << QVector<double>{1., 4., 3., 5., 2.} << QVector<double>{100., 200., 300., 400., 500.} | |
70 | << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 500., 300., 200., 400.}; |
|
76 | << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 500., 300., 200., 400.}; | |
71 | } |
|
77 | } | |
72 |
|
78 | |||
73 | void TestDataSeries::testCtor() |
|
79 | void TestDataSeries::testCtor() | |
74 | { |
|
80 | { | |
75 | // Creates series |
|
81 | // Creates series | |
76 | QFETCH(QVector<double>, xAxisData); |
|
82 | QFETCH(QVector<double>, xAxisData); | |
77 | QFETCH(QVector<double>, valuesData); |
|
83 | QFETCH(QVector<double>, valuesData); | |
78 |
|
84 | |||
79 | auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), |
|
85 | auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), | |
80 | Unit{}, Unit{}); |
|
86 | Unit{}, Unit{}); | |
81 |
|
87 | |||
82 | // Validates results : we check that the data series is sorted on its x-axis data |
|
88 | // Validates results : we check that the data series is sorted on its x-axis data | |
83 | QFETCH(QVector<double>, expectedXAxisData); |
|
89 | QFETCH(QVector<double>, expectedXAxisData); | |
84 | QFETCH(QVector<double>, expectedValuesData); |
|
90 | QFETCH(QVector<double>, expectedValuesData); | |
85 |
|
91 | |||
86 | auto seriesXAxisData = series->xAxisData()->data(); |
|
92 | auto seriesXAxisData = series->xAxisData()->data(); | |
87 | auto seriesValuesData = series->valuesData()->data(); |
|
93 | auto seriesValuesData = series->valuesData()->data(); | |
88 |
|
94 | |||
89 | QVERIFY( |
|
95 | QVERIFY( | |
90 | std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin())); |
|
96 | std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin())); | |
91 | QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(), |
|
97 | QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(), | |
92 | seriesValuesData.cbegin())); |
|
98 | seriesValuesData.cbegin())); | |
93 | } |
|
99 | } | |
94 |
|
100 | |||
95 | namespace { |
|
101 | namespace { | |
96 |
|
102 | |||
97 | std::shared_ptr<ScalarSeries> createSeries(QVector<double> xAxisData, QVector<double> valuesData) |
|
103 | std::shared_ptr<ScalarSeries> createSeries(QVector<double> xAxisData, QVector<double> valuesData) | |
98 | { |
|
104 | { | |
99 | return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{}, |
|
105 | return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{}, | |
100 | Unit{}); |
|
106 | Unit{}); | |
101 | } |
|
107 | } | |
102 |
|
108 | |||
103 | } // namespace |
|
109 | } // namespace | |
104 |
|
110 | |||
105 | void TestDataSeries::testMerge_data() |
|
111 | void TestDataSeries::testMerge_data() | |
106 | { |
|
112 | { | |
107 | // ////////////// // |
|
113 | // ////////////// // | |
108 | // Test structure // |
|
114 | // Test structure // | |
109 | // ////////////// // |
|
115 | // ////////////// // | |
110 |
|
116 | |||
111 | // Data series to merge |
|
117 | // Data series to merge | |
112 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); |
|
118 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); | |
113 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2"); |
|
119 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2"); | |
114 |
|
120 | |||
115 | // Expected values in the first data series after merge |
|
121 | // Expected values in the first data series after merge | |
116 | QTest::addColumn<QVector<double> >("expectedXAxisData"); |
|
122 | QTest::addColumn<QVector<double> >("expectedXAxisData"); | |
117 | QTest::addColumn<QVector<double> >("expectedValuesData"); |
|
123 | QTest::addColumn<QVector<double> >("expectedValuesData"); | |
118 |
|
124 | |||
119 | // ////////// // |
|
125 | // ////////// // | |
120 | // Test cases // |
|
126 | // Test cases // | |
121 | // ////////// // |
|
127 | // ////////// // | |
122 |
|
128 | |||
123 | QTest::newRow("sortedMerge") |
|
129 | QTest::newRow("sortedMerge") | |
124 | << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
130 | << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
125 | << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) |
|
131 | << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) | |
126 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} |
|
132 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
127 | << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; |
|
133 | << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; | |
128 |
|
134 | |||
129 | QTest::newRow("unsortedMerge") |
|
135 | QTest::newRow("unsortedMerge") | |
130 | << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) |
|
136 | << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) | |
131 | << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
137 | << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
132 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} |
|
138 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
133 | << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; |
|
139 | << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; | |
134 |
|
140 | |||
135 | QTest::newRow("unsortedMerge2") |
|
141 | QTest::newRow("unsortedMerge2") | |
136 | << createSeries({1., 2., 8., 9., 10}, {100., 200., 300., 400., 500.}) |
|
142 | << createSeries({1., 2., 8., 9., 10}, {100., 200., 300., 400., 500.}) | |
137 | << createSeries({3., 4., 5., 6., 7.}, {600., 700., 800., 900., 1000.}) |
|
143 | << createSeries({3., 4., 5., 6., 7.}, {600., 700., 800., 900., 1000.}) | |
138 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} |
|
144 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
139 | << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.}; |
|
145 | << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.}; | |
140 |
|
146 | |||
141 | QTest::newRow("unsortedMerge3") |
|
147 | QTest::newRow("unsortedMerge3") | |
142 | << createSeries({3., 5., 8., 7., 2}, {100., 200., 300., 400., 500.}) |
|
148 | << createSeries({3., 5., 8., 7., 2}, {100., 200., 300., 400., 500.}) | |
143 | << createSeries({6., 4., 9., 10., 1.}, {600., 700., 800., 900., 1000.}) |
|
149 | << createSeries({6., 4., 9., 10., 1.}, {600., 700., 800., 900., 1000.}) | |
144 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} |
|
150 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
145 | << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.}; |
|
151 | << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.}; | |
146 | } |
|
152 | } | |
147 |
|
153 | |||
148 | void TestDataSeries::testMerge() |
|
154 | void TestDataSeries::testMerge() | |
149 | { |
|
155 | { | |
150 | // Merges series |
|
156 | // Merges series | |
151 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); |
|
157 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); | |
152 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2); |
|
158 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2); | |
153 |
|
159 | |||
154 | dataSeries->merge(dataSeries2.get()); |
|
160 | dataSeries->merge(dataSeries2.get()); | |
155 |
|
161 | |||
156 | // Validates results : we check that the merge is valid and the data series is sorted on its |
|
162 | // Validates results : we check that the merge is valid and the data series is sorted on its | |
157 | // x-axis data |
|
163 | // x-axis data | |
158 | QFETCH(QVector<double>, expectedXAxisData); |
|
164 | QFETCH(QVector<double>, expectedXAxisData); | |
159 | QFETCH(QVector<double>, expectedValuesData); |
|
165 | QFETCH(QVector<double>, expectedValuesData); | |
160 |
|
166 | |||
161 | auto seriesXAxisData = dataSeries->xAxisData()->data(); |
|
167 | auto seriesXAxisData = dataSeries->xAxisData()->data(); | |
162 | auto seriesValuesData = dataSeries->valuesData()->data(); |
|
168 | auto seriesValuesData = dataSeries->valuesData()->data(); | |
163 |
|
169 | |||
164 | QVERIFY( |
|
170 | QVERIFY( | |
165 | std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin())); |
|
171 | std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin())); | |
166 | QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(), |
|
172 | QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(), | |
167 | seriesValuesData.cbegin())); |
|
173 | seriesValuesData.cbegin())); | |
168 | } |
|
174 | } | |
169 |
|
175 | |||
|
176 | void TestDataSeries::testMinData_data() | |||
|
177 | { | |||
|
178 | // ////////////// // | |||
|
179 | // Test structure // | |||
|
180 | // ////////////// // | |||
|
181 | ||||
|
182 | // Data series to get min data | |||
|
183 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); | |||
|
184 | ||||
|
185 | // Min data | |||
|
186 | QTest::addColumn<double>("min"); | |||
|
187 | ||||
|
188 | // Expected results | |||
|
189 | QTest::addColumn<bool>( | |||
|
190 | "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator) | |||
|
191 | QTest::addColumn<double>( | |||
|
192 | "expectedMin"); // Expected value when method doesn't return end iterator | |||
|
193 | ||||
|
194 | // ////////// // | |||
|
195 | // Test cases // | |||
|
196 | // ////////// // | |||
|
197 | ||||
|
198 | QTest::newRow("minData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
199 | << 0. << true << 1.; | |||
|
200 | QTest::newRow("minData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
201 | << 1. << true << 1.; | |||
|
202 | QTest::newRow("minData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
203 | << 1.1 << true << 2.; | |||
|
204 | QTest::newRow("minData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
205 | << 5. << true << 5.; | |||
|
206 | QTest::newRow("minData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
207 | << 5.1 << false << std::numeric_limits<double>::quiet_NaN(); | |||
|
208 | QTest::newRow("minData6") << createSeries({}, {}) << 1.1 << false | |||
|
209 | << std::numeric_limits<double>::quiet_NaN(); | |||
|
210 | } | |||
|
211 | ||||
|
212 | void TestDataSeries::testMinData() | |||
|
213 | { | |||
|
214 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); | |||
|
215 | QFETCH(double, min); | |||
|
216 | ||||
|
217 | QFETCH(bool, expectedOK); | |||
|
218 | QFETCH(double, expectedMin); | |||
|
219 | ||||
|
220 | auto it = dataSeries->minData(min); | |||
|
221 | ||||
|
222 | QCOMPARE(expectedOK, it != dataSeries->cend()); | |||
|
223 | ||||
|
224 | // If the method doesn't return a end iterator, checks with expected value | |||
|
225 | if (expectedOK) { | |||
|
226 | QCOMPARE(expectedMin, it->x()); | |||
|
227 | } | |||
|
228 | } | |||
170 | void TestDataSeries::testSubdata_data() |
|
229 | void TestDataSeries::testSubdata_data() | |
171 | { |
|
230 | { | |
172 | // ////////////// // |
|
231 | // ////////////// // | |
173 | // Test structure // |
|
232 | // Test structure // | |
174 | // ////////////// // |
|
233 | // ////////////// // | |
175 |
|
234 | |||
176 | // Data series to get subdata |
|
235 | // Data series to get subdata | |
177 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); |
|
236 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); | |
178 |
|
237 | |||
179 | // Min/max values |
|
238 | // Min/max values | |
180 | QTest::addColumn<double>("min"); |
|
239 | QTest::addColumn<double>("min"); | |
181 | QTest::addColumn<double>("max"); |
|
240 | QTest::addColumn<double>("max"); | |
182 |
|
241 | |||
183 | // Expected values after subdata |
|
242 | // Expected values after subdata | |
184 | QTest::addColumn<QVector<double> >("expectedXAxisData"); |
|
243 | QTest::addColumn<QVector<double> >("expectedXAxisData"); | |
185 | QTest::addColumn<QVector<double> >("expectedValuesData"); |
|
244 | QTest::addColumn<QVector<double> >("expectedValuesData"); | |
186 |
|
245 | |||
187 | // ////////// // |
|
246 | // ////////// // | |
188 | // Test cases // |
|
247 | // Test cases // | |
189 | // ////////// // |
|
248 | // ////////// // | |
190 |
|
249 | |||
191 | QTest::newRow("subData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
250 | QTest::newRow("subData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
192 | << -1. << 3.2 << QVector<double>{1., 2., 3.} |
|
251 | << -1. << 3.2 << QVector<double>{1., 2., 3.} | |
193 | << QVector<double>{100., 200., 300.}; |
|
252 | << QVector<double>{100., 200., 300.}; | |
194 | QTest::newRow("subData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
253 | QTest::newRow("subData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
195 | << 1. << 4. << QVector<double>{1., 2., 3., 4.} |
|
254 | << 1. << 4. << QVector<double>{1., 2., 3., 4.} | |
196 | << QVector<double>{100., 200., 300., 400.}; |
|
255 | << QVector<double>{100., 200., 300., 400.}; | |
197 | QTest::newRow("subData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
256 | QTest::newRow("subData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
198 | << 1. << 3.9 << QVector<double>{1., 2., 3.} |
|
257 | << 1. << 3.9 << QVector<double>{1., 2., 3.} | |
199 | << QVector<double>{100., 200., 300.}; |
|
258 | << QVector<double>{100., 200., 300.}; | |
200 | QTest::newRow("subData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
259 | QTest::newRow("subData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
201 | << 0. << 0.9 << QVector<double>{} << QVector<double>{}; |
|
260 | << 0. << 0.9 << QVector<double>{} << QVector<double>{}; | |
202 | QTest::newRow("subData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
261 | QTest::newRow("subData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
203 | << 0. << 1. << QVector<double>{1.} << QVector<double>{100.}; |
|
262 | << 0. << 1. << QVector<double>{1.} << QVector<double>{100.}; | |
204 | QTest::newRow("subData6") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
263 | QTest::newRow("subData6") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
205 | << 2.1 << 6. << QVector<double>{3., 4., 5.} |
|
264 | << 2.1 << 6. << QVector<double>{3., 4., 5.} | |
206 | << QVector<double>{300., 400., 500.}; |
|
265 | << QVector<double>{300., 400., 500.}; | |
207 | QTest::newRow("subData7") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
266 | QTest::newRow("subData7") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
208 | << 6. << 9. << QVector<double>{} << QVector<double>{}; |
|
267 | << 6. << 9. << QVector<double>{} << QVector<double>{}; | |
209 | QTest::newRow("subData8") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
268 | QTest::newRow("subData8") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
210 | << 5. << 9. << QVector<double>{5.} << QVector<double>{500.}; |
|
269 | << 5. << 9. << QVector<double>{5.} << QVector<double>{500.}; | |
211 | } |
|
270 | } | |
212 |
|
271 | |||
213 | void TestDataSeries::testSubdata() |
|
272 | void TestDataSeries::testSubdata() | |
214 | { |
|
273 | { | |
215 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); |
|
274 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); | |
216 | QFETCH(double, min); |
|
275 | QFETCH(double, min); | |
217 | QFETCH(double, max); |
|
276 | QFETCH(double, max); | |
218 |
|
277 | |||
219 | QFETCH(QVector<double>, expectedXAxisData); |
|
278 | QFETCH(QVector<double>, expectedXAxisData); | |
220 | QFETCH(QVector<double>, expectedValuesData); |
|
279 | QFETCH(QVector<double>, expectedValuesData); | |
221 |
|
280 | |||
222 | auto bounds = dataSeries->subData(min, max); |
|
281 | auto bounds = dataSeries->subData(min, max); | |
223 | QVERIFY(std::equal(bounds.first, bounds.second, expectedXAxisData.cbegin(), |
|
282 | QVERIFY(std::equal(bounds.first, bounds.second, expectedXAxisData.cbegin(), | |
224 | expectedXAxisData.cend(), |
|
283 | expectedXAxisData.cend(), | |
225 | [](const auto &it, const auto &expectedX) { return it.x() == expectedX; })); |
|
284 | [](const auto &it, const auto &expectedX) { return it.x() == expectedX; })); | |
226 | QVERIFY(std::equal( |
|
285 | QVERIFY(std::equal( | |
227 | bounds.first, bounds.second, expectedValuesData.cbegin(), expectedValuesData.cend(), |
|
286 | bounds.first, bounds.second, expectedValuesData.cbegin(), expectedValuesData.cend(), | |
228 | [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; })); |
|
287 | [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; })); | |
229 | } |
|
288 | } | |
230 |
|
289 | |||
231 | QTEST_MAIN(TestDataSeries) |
|
290 | QTEST_MAIN(TestDataSeries) | |
232 | #include "TestDataSeries.moc" |
|
291 | #include "TestDataSeries.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now