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