@@ -1,638 +1,636 | |||
|
1 | 1 | #include "Data/DataSeries.h" |
|
2 | 2 | #include "Data/ScalarSeries.h" |
|
3 | 3 | #include "Data/VectorSeries.h" |
|
4 | 4 | |
|
5 | 5 | #include <cmath> |
|
6 | 6 | |
|
7 | 7 | #include <QObject> |
|
8 | 8 | #include <QtTest> |
|
9 | 9 | |
|
10 | 10 | Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>) |
|
11 | 11 | Q_DECLARE_METATYPE(std::shared_ptr<VectorSeries>) |
|
12 | 12 | |
|
13 | 13 | namespace { |
|
14 | 14 | |
|
15 | void validateRange(DataSeriesIterator first, DataSeriesIterator last, const QVector<double> &xData, | |
|
16 | const QVector<double> &valuesData) | |
|
15 | using DataContainer = std::vector<double>; | |
|
16 | ||
|
17 | void validateRange(DataSeriesIterator first, DataSeriesIterator last, const DataContainer &xData, | |
|
18 | const DataContainer &valuesData) | |
|
17 | 19 | { |
|
18 | 20 | QVERIFY(std::equal(first, last, xData.cbegin(), xData.cend(), |
|
19 | 21 | [](const auto &it, const auto &expectedX) { return it.x() == expectedX; })); |
|
20 | 22 | QVERIFY(std::equal( |
|
21 | 23 | first, last, valuesData.cbegin(), valuesData.cend(), |
|
22 | 24 | [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; })); |
|
23 | 25 | } |
|
24 | 26 | |
|
25 |
void validateRange(DataSeriesIterator first, DataSeriesIterator last, const |
|
|
26 |
const |
|
|
27 | void validateRange(DataSeriesIterator first, DataSeriesIterator last, const DataContainer &xData, | |
|
28 | const std::vector<DataContainer> &valuesData) | |
|
27 | 29 | { |
|
28 | 30 | QVERIFY(std::equal(first, last, xData.cbegin(), xData.cend(), |
|
29 | 31 | [](const auto &it, const auto &expectedX) { return it.x() == expectedX; })); |
|
30 | 32 | for (auto i = 0; i < valuesData.size(); ++i) { |
|
31 | 33 | auto componentData = valuesData.at(i); |
|
32 | 34 | |
|
33 | 35 | QVERIFY(std::equal( |
|
34 | 36 | first, last, componentData.cbegin(), componentData.cend(), |
|
35 | 37 | [i](const auto &it, const auto &expectedVal) { return it.value(i) == expectedVal; })); |
|
36 | 38 | } |
|
37 | 39 | } |
|
38 | 40 | |
|
39 | 41 | } // namespace |
|
40 | 42 | |
|
41 | 43 | class TestDataSeries : public QObject { |
|
42 | 44 | Q_OBJECT |
|
43 | 45 | private: |
|
44 | 46 | template <typename T> |
|
45 | 47 | void testValuesBoundsStructure() |
|
46 | 48 | { |
|
47 | 49 | // ////////////// // |
|
48 | 50 | // Test structure // |
|
49 | 51 | // ////////////// // |
|
50 | 52 | |
|
51 | 53 | // Data series to get values bounds |
|
52 | 54 | QTest::addColumn<std::shared_ptr<T> >("dataSeries"); |
|
53 | 55 | |
|
54 | 56 | // x-axis range |
|
55 | 57 | QTest::addColumn<double>("minXAxis"); |
|
56 | 58 | QTest::addColumn<double>("maxXAxis"); |
|
57 | 59 | |
|
58 | 60 | // Expected results |
|
59 | 61 | QTest::addColumn<bool>( |
|
60 | 62 | "expectedOK"); // Test is expected to be ok (i.e. method doesn't return end iterators) |
|
61 | 63 | QTest::addColumn<double>("expectedMinValue"); |
|
62 | 64 | QTest::addColumn<double>("expectedMaxValue"); |
|
63 | 65 | } |
|
64 | 66 | |
|
65 | 67 | template <typename T> |
|
66 | 68 | void testValuesBounds() |
|
67 | 69 | { |
|
68 | 70 | QFETCH(std::shared_ptr<T>, dataSeries); |
|
69 | 71 | QFETCH(double, minXAxis); |
|
70 | 72 | QFETCH(double, maxXAxis); |
|
71 | 73 | |
|
72 | 74 | QFETCH(bool, expectedOK); |
|
73 | 75 | QFETCH(double, expectedMinValue); |
|
74 | 76 | QFETCH(double, expectedMaxValue); |
|
75 | 77 | |
|
76 | 78 | auto minMaxIts = dataSeries->valuesBounds(minXAxis, maxXAxis); |
|
77 | 79 | auto end = dataSeries->cend(); |
|
78 | 80 | |
|
79 | 81 | // Checks iterators with expected result |
|
80 | 82 | QCOMPARE(expectedOK, minMaxIts.first != end && minMaxIts.second != end); |
|
81 | 83 | |
|
82 | 84 | if (expectedOK) { |
|
83 | 85 | auto compare = [](const auto &v1, const auto &v2) { |
|
84 | 86 | return (std::isnan(v1) && std::isnan(v2)) || v1 == v2; |
|
85 | 87 | }; |
|
86 | 88 | |
|
87 | 89 | QVERIFY(compare(expectedMinValue, minMaxIts.first->minValue())); |
|
88 | 90 | QVERIFY(compare(expectedMaxValue, minMaxIts.second->maxValue())); |
|
89 | 91 | } |
|
90 | 92 | } |
|
91 | 93 | |
|
92 | 94 | template <typename T> |
|
93 | 95 | void testPurgeStructure() |
|
94 | 96 | { |
|
95 | 97 | // ////////////// // |
|
96 | 98 | // Test structure // |
|
97 | 99 | // ////////////// // |
|
98 | 100 | |
|
99 | 101 | // Data series to purge |
|
100 | 102 | QTest::addColumn<std::shared_ptr<T> >("dataSeries"); |
|
101 | 103 | QTest::addColumn<double>("min"); |
|
102 | 104 | QTest::addColumn<double>("max"); |
|
103 | 105 | |
|
104 | 106 | // Expected values after purge |
|
105 |
QTest::addColumn< |
|
|
106 |
QTest::addColumn< |
|
|
107 | QTest::addColumn<DataContainer>("expectedXAxisData"); | |
|
108 | QTest::addColumn<std::vector<DataContainer> >("expectedValuesData"); | |
|
107 | 109 | } |
|
108 | 110 | |
|
109 | 111 | template <typename T> |
|
110 | 112 | void testPurge() |
|
111 | 113 | { |
|
112 | 114 | QFETCH(std::shared_ptr<T>, dataSeries); |
|
113 | 115 | QFETCH(double, min); |
|
114 | 116 | QFETCH(double, max); |
|
115 | 117 | |
|
116 | 118 | dataSeries->purge(min, max); |
|
117 | 119 | |
|
118 | 120 | // Validates results |
|
119 |
QFETCH( |
|
|
120 |
QFETCH( |
|
|
121 | QFETCH(DataContainer, expectedXAxisData); | |
|
122 | QFETCH(std::vector<DataContainer>, expectedValuesData); | |
|
121 | 123 | |
|
122 | 124 | validateRange(dataSeries->cbegin(), dataSeries->cend(), expectedXAxisData, |
|
123 | 125 | expectedValuesData); |
|
124 | 126 | } |
|
125 | 127 | |
|
126 | 128 | private slots: |
|
127 | 129 | |
|
128 | 130 | /// Input test data |
|
129 | 131 | /// @sa testCtor() |
|
130 | 132 | void testCtor_data(); |
|
131 | 133 | |
|
132 | 134 | /// Tests construction of a data series |
|
133 | 135 | void testCtor(); |
|
134 | 136 | |
|
135 | 137 | /// Input test data |
|
136 | 138 | /// @sa testMerge() |
|
137 | 139 | void testMerge_data(); |
|
138 | 140 | |
|
139 | 141 | /// Tests merge of two data series |
|
140 | 142 | void testMerge(); |
|
141 | 143 | |
|
142 | 144 | /// Input test data |
|
143 | 145 | /// @sa testPurgeScalar() |
|
144 | 146 | void testPurgeScalar_data(); |
|
145 | 147 | |
|
146 | 148 | /// Tests purge of a scalar series |
|
147 | 149 | void testPurgeScalar(); |
|
148 | 150 | |
|
149 | 151 | /// Input test data |
|
150 | 152 | /// @sa testPurgeVector() |
|
151 | 153 | void testPurgeVector_data(); |
|
152 | 154 | |
|
153 | 155 | /// Tests purge of a vector series |
|
154 | 156 | void testPurgeVector(); |
|
155 | 157 | |
|
156 | 158 | /// Input test data |
|
157 | 159 | /// @sa testMinXAxisData() |
|
158 | 160 | void testMinXAxisData_data(); |
|
159 | 161 | |
|
160 | 162 | /// Tests get min x-axis data of a data series |
|
161 | 163 | void testMinXAxisData(); |
|
162 | 164 | |
|
163 | 165 | /// Input test data |
|
164 | 166 | /// @sa testMaxXAxisData() |
|
165 | 167 | void testMaxXAxisData_data(); |
|
166 | 168 | |
|
167 | 169 | /// Tests get max x-axis data of a data series |
|
168 | 170 | void testMaxXAxisData(); |
|
169 | 171 | |
|
170 | 172 | /// Input test data |
|
171 | 173 | /// @sa testXAxisRange() |
|
172 | 174 | void testXAxisRange_data(); |
|
173 | 175 | |
|
174 | 176 | /// Tests get x-axis range of a data series |
|
175 | 177 | void testXAxisRange(); |
|
176 | 178 | |
|
177 | 179 | /// Input test data |
|
178 | 180 | /// @sa testValuesBoundsScalar() |
|
179 | 181 | void testValuesBoundsScalar_data(); |
|
180 | 182 | |
|
181 | 183 | /// Tests get values bounds of a scalar series |
|
182 | 184 | void testValuesBoundsScalar(); |
|
183 | 185 | |
|
184 | 186 | /// Input test data |
|
185 | 187 | /// @sa testValuesBoundsVector() |
|
186 | 188 | void testValuesBoundsVector_data(); |
|
187 | 189 | |
|
188 | 190 | /// Tests get values bounds of a vector series |
|
189 | 191 | void testValuesBoundsVector(); |
|
190 | 192 | }; |
|
191 | 193 | |
|
192 | 194 | void TestDataSeries::testCtor_data() |
|
193 | 195 | { |
|
194 | 196 | // ////////////// // |
|
195 | 197 | // Test structure // |
|
196 | 198 | // ////////////// // |
|
197 | 199 | |
|
198 | 200 | // x-axis data |
|
199 |
QTest::addColumn< |
|
|
201 | QTest::addColumn<DataContainer>("xAxisData"); | |
|
200 | 202 | // values data |
|
201 |
QTest::addColumn< |
|
|
203 | QTest::addColumn<DataContainer>("valuesData"); | |
|
202 | 204 | |
|
203 | 205 | // expected x-axis data |
|
204 |
QTest::addColumn< |
|
|
206 | QTest::addColumn<DataContainer>("expectedXAxisData"); | |
|
205 | 207 | // expected values data |
|
206 |
QTest::addColumn< |
|
|
208 | QTest::addColumn<DataContainer>("expectedValuesData"); | |
|
207 | 209 | |
|
208 | 210 | // ////////// // |
|
209 | 211 | // Test cases // |
|
210 | 212 | // ////////// // |
|
211 | 213 | |
|
212 | 214 | QTest::newRow("invalidData (different sizes of vectors)") |
|
213 |
<< |
|
|
214 | << QVector<double>{} << QVector<double>{}; | |
|
215 | << DataContainer{1., 2., 3., 4., 5.} << DataContainer{100., 200., 300.} << DataContainer{} | |
|
216 | << DataContainer{}; | |
|
215 | 217 | |
|
216 |
QTest::newRow("sortedData") << |
|
|
217 |
<< |
|
|
218 |
<< |
|
|
219 |
<< |
|
|
218 | QTest::newRow("sortedData") << DataContainer{1., 2., 3., 4., 5.} | |
|
219 | << DataContainer{100., 200., 300., 400., 500.} | |
|
220 | << DataContainer{1., 2., 3., 4., 5.} | |
|
221 | << DataContainer{100., 200., 300., 400., 500.}; | |
|
220 | 222 | |
|
221 |
QTest::newRow("unsortedData") << |
|
|
222 |
<< |
|
|
223 |
<< |
|
|
224 |
<< |
|
|
223 | QTest::newRow("unsortedData") << DataContainer{5., 4., 3., 2., 1.} | |
|
224 | << DataContainer{100., 200., 300., 400., 500.} | |
|
225 | << DataContainer{1., 2., 3., 4., 5.} | |
|
226 | << DataContainer{500., 400., 300., 200., 100.}; | |
|
225 | 227 | |
|
226 | 228 | QTest::newRow("unsortedData2") |
|
227 |
<< |
|
|
228 |
<< |
|
|
229 | << DataContainer{1., 4., 3., 5., 2.} << DataContainer{100., 200., 300., 400., 500.} | |
|
230 | << DataContainer{1., 2., 3., 4., 5.} << DataContainer{100., 500., 300., 200., 400.}; | |
|
229 | 231 | } |
|
230 | 232 | |
|
231 | 233 | void TestDataSeries::testCtor() |
|
232 | 234 | { |
|
233 | 235 | // Creates series |
|
234 |
QFETCH( |
|
|
235 |
QFETCH( |
|
|
236 | QFETCH(DataContainer, xAxisData); | |
|
237 | QFETCH(DataContainer, valuesData); | |
|
236 | 238 | |
|
237 | 239 | auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), |
|
238 | 240 | Unit{}, Unit{}); |
|
239 | 241 | |
|
240 | 242 | // Validates results : we check that the data series is sorted on its x-axis data |
|
241 |
QFETCH( |
|
|
242 |
QFETCH( |
|
|
243 | QFETCH(DataContainer, expectedXAxisData); | |
|
244 | QFETCH(DataContainer, expectedValuesData); | |
|
243 | 245 | |
|
244 | 246 | validateRange(series->cbegin(), series->cend(), expectedXAxisData, expectedValuesData); |
|
245 | 247 | } |
|
246 | 248 | |
|
247 | 249 | namespace { |
|
248 | 250 | |
|
249 |
std::shared_ptr<ScalarSeries> createScalarSeries( |
|
|
250 | QVector<double> valuesData) | |
|
251 | std::shared_ptr<ScalarSeries> createScalarSeries(DataContainer xAxisData, DataContainer valuesData) | |
|
251 | 252 | { |
|
252 | 253 | return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{}, |
|
253 | 254 | Unit{}); |
|
254 | 255 | } |
|
255 | 256 | |
|
256 |
std::shared_ptr<VectorSeries> createVectorSeries( |
|
|
257 |
|
|
|
258 |
|
|
|
259 | QVector<double> zValuesData) | |
|
257 | std::shared_ptr<VectorSeries> createVectorSeries(DataContainer xAxisData, DataContainer xValuesData, | |
|
258 | DataContainer yValuesData, | |
|
259 | DataContainer zValuesData) | |
|
260 | 260 | { |
|
261 | 261 | return std::make_shared<VectorSeries>(std::move(xAxisData), std::move(xValuesData), |
|
262 | 262 | std::move(yValuesData), std::move(zValuesData), Unit{}, |
|
263 | 263 | Unit{}); |
|
264 | 264 | } |
|
265 | 265 | |
|
266 | 266 | } // namespace |
|
267 | 267 | |
|
268 | 268 | void TestDataSeries::testMerge_data() |
|
269 | 269 | { |
|
270 | 270 | // ////////////// // |
|
271 | 271 | // Test structure // |
|
272 | 272 | // ////////////// // |
|
273 | 273 | |
|
274 | 274 | // Data series to merge |
|
275 | 275 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); |
|
276 | 276 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2"); |
|
277 | 277 | |
|
278 | 278 | // Expected values in the first data series after merge |
|
279 |
QTest::addColumn< |
|
|
280 |
QTest::addColumn< |
|
|
279 | QTest::addColumn<DataContainer>("expectedXAxisData"); | |
|
280 | QTest::addColumn<DataContainer>("expectedValuesData"); | |
|
281 | 281 | |
|
282 | 282 | // ////////// // |
|
283 | 283 | // Test cases // |
|
284 | 284 | // ////////// // |
|
285 | 285 | |
|
286 | 286 | QTest::newRow("sortedMerge") |
|
287 | 287 | << createScalarSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
288 | 288 | << createScalarSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) |
|
289 |
<< |
|
|
290 |
<< |
|
|
289 | << DataContainer{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
|
290 | << DataContainer{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; | |
|
291 | 291 | |
|
292 | 292 | QTest::newRow("unsortedMerge") |
|
293 | 293 | << createScalarSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) |
|
294 | 294 | << createScalarSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
295 |
<< |
|
|
296 |
<< |
|
|
295 | << DataContainer{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
|
296 | << DataContainer{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; | |
|
297 | 297 | |
|
298 | QTest::newRow("unsortedMerge2") | |
|
299 |
<< createScalarSeries({1., 2., 8., 9., 10}, {100., 200., |
|
|
300 |
<< createScalarSeries({3., 4., 5., 6., 7.}, {600., 700. |
|
|
301 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
|
302 | << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.}; | |
|
298 | QTest::newRow("unsortedMerge2 (merge not made because source is in the bounds of dest)") | |
|
299 | << createScalarSeries({1., 2., 8., 9., 10}, {100., 200., 800., 900., 1000.}) | |
|
300 | << createScalarSeries({3., 4., 5., 6., 7.}, {300., 400., 500., 600., 700.}) | |
|
301 | << DataContainer{1., 2., 8., 9., 10.} << DataContainer{100., 200., 800., 900., 1000.}; | |
|
303 | 302 | |
|
304 | 303 | QTest::newRow("unsortedMerge3") |
|
305 |
<< createScalarSeries({3., |
|
|
306 |
<< createScalarSeries({ |
|
|
307 |
<< |
|
|
308 | << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.}; | |
|
304 | << createScalarSeries({3., 4., 5., 7., 8}, {300., 400., 500., 700., 800.}) | |
|
305 | << createScalarSeries({1., 2., 3., 7., 10.}, {100., 200., 333., 777., 1000.}) | |
|
306 | << DataContainer{1., 2., 3., 4., 5., 7., 8., 10.} | |
|
307 | << DataContainer{100., 200., 300., 400., 500., 700., 800., 1000.}; | |
|
309 | 308 | } |
|
310 | 309 | |
|
311 | 310 | void TestDataSeries::testMerge() |
|
312 | 311 | { |
|
313 | 312 | // Merges series |
|
314 | 313 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); |
|
315 | 314 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2); |
|
316 | 315 | |
|
317 | 316 | dataSeries->merge(dataSeries2.get()); |
|
318 | 317 | |
|
319 | 318 | // Validates results : we check that the merge is valid and the data series is sorted on its |
|
320 | 319 | // x-axis data |
|
321 |
QFETCH( |
|
|
322 |
QFETCH( |
|
|
320 | QFETCH(DataContainer, expectedXAxisData); | |
|
321 | QFETCH(DataContainer, expectedValuesData); | |
|
323 | 322 | |
|
324 | 323 | validateRange(dataSeries->cbegin(), dataSeries->cend(), expectedXAxisData, expectedValuesData); |
|
325 | 324 | } |
|
326 | 325 | |
|
327 | 326 | void TestDataSeries::testPurgeScalar_data() |
|
328 | 327 | { |
|
329 | 328 | testPurgeStructure<ScalarSeries>(); |
|
330 | 329 | |
|
331 | 330 | // ////////// // |
|
332 | 331 | // Test cases // |
|
333 | 332 | // ////////// // |
|
334 | 333 | |
|
335 | 334 | QTest::newRow("purgeScalar") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
336 | 335 | {100., 200., 300., 400., 500.}) |
|
337 |
<< 2. << 4. << |
|
|
338 |
<< |
|
|
336 | << 2. << 4. << DataContainer{2., 3., 4.} | |
|
337 | << std::vector<DataContainer>{{200., 300., 400.}}; | |
|
339 | 338 | QTest::newRow("purgeScalar2") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
340 | 339 | {100., 200., 300., 400., 500.}) |
|
341 |
<< 0. << 2.5 << |
|
|
342 |
<< |
|
|
340 | << 0. << 2.5 << DataContainer{1., 2.} | |
|
341 | << std::vector<DataContainer>{{100., 200.}}; | |
|
343 | 342 | QTest::newRow("purgeScalar3") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
344 | 343 | {100., 200., 300., 400., 500.}) |
|
345 |
<< 3.5 << 7. << |
|
|
346 |
<< |
|
|
344 | << 3.5 << 7. << DataContainer{4., 5.} | |
|
345 | << std::vector<DataContainer>{{400., 500.}}; | |
|
347 | 346 | QTest::newRow("purgeScalar4") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
348 | 347 | {100., 200., 300., 400., 500.}) |
|
349 |
<< 0. << 7. << |
|
|
350 |
<< |
|
|
348 | << 0. << 7. << DataContainer{1., 2., 3., 4., 5.} | |
|
349 | << std::vector<DataContainer>{{100., 200., 300., 400., 500.}}; | |
|
351 | 350 | QTest::newRow("purgeScalar5") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
352 | 351 | {100., 200., 300., 400., 500.}) |
|
353 |
<< 5.5 << 7. << |
|
|
354 | << QVector<QVector<double> >{{}}; | |
|
352 | << 5.5 << 7. << DataContainer{} << std::vector<DataContainer>{{}}; | |
|
355 | 353 | } |
|
356 | 354 | |
|
357 | 355 | void TestDataSeries::testPurgeScalar() |
|
358 | 356 | { |
|
359 | 357 | testPurge<ScalarSeries>(); |
|
360 | 358 | } |
|
361 | 359 | |
|
362 | 360 | void TestDataSeries::testPurgeVector_data() |
|
363 | 361 | { |
|
364 | 362 | testPurgeStructure<VectorSeries>(); |
|
365 | 363 | |
|
366 | 364 | // ////////// // |
|
367 | 365 | // Test cases // |
|
368 | 366 | // ////////// // |
|
369 | 367 | |
|
370 | 368 | QTest::newRow("purgeVector") << createVectorSeries({1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, |
|
371 | 369 | {11., 12., 13., 14., 15.}, |
|
372 | 370 | {16., 17., 18., 19., 20.}) |
|
373 |
<< 2. << 4. << |
|
|
374 |
<< |
|
|
371 | << 2. << 4. << DataContainer{2., 3., 4.} | |
|
372 | << std::vector<DataContainer>{ | |
|
375 | 373 | {7., 8., 9.}, {12., 13., 14.}, {17., 18., 19.}}; |
|
376 | 374 | } |
|
377 | 375 | |
|
378 | 376 | void TestDataSeries::testPurgeVector() |
|
379 | 377 | { |
|
380 | 378 | testPurge<VectorSeries>(); |
|
381 | 379 | } |
|
382 | 380 | |
|
383 | 381 | void TestDataSeries::testMinXAxisData_data() |
|
384 | 382 | { |
|
385 | 383 | // ////////////// // |
|
386 | 384 | // Test structure // |
|
387 | 385 | // ////////////// // |
|
388 | 386 | |
|
389 | 387 | // Data series to get min data |
|
390 | 388 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); |
|
391 | 389 | |
|
392 | 390 | // Min data |
|
393 | 391 | QTest::addColumn<double>("min"); |
|
394 | 392 | |
|
395 | 393 | // Expected results |
|
396 | 394 | QTest::addColumn<bool>( |
|
397 | 395 | "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator) |
|
398 | 396 | QTest::addColumn<double>( |
|
399 | 397 | "expectedMin"); // Expected value when method doesn't return end iterator |
|
400 | 398 | |
|
401 | 399 | // ////////// // |
|
402 | 400 | // Test cases // |
|
403 | 401 | // ////////// // |
|
404 | 402 | |
|
405 | 403 | QTest::newRow("minData1") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
406 | 404 | {100., 200., 300., 400., 500.}) |
|
407 | 405 | << 0. << true << 1.; |
|
408 | 406 | QTest::newRow("minData2") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
409 | 407 | {100., 200., 300., 400., 500.}) |
|
410 | 408 | << 1. << true << 1.; |
|
411 | 409 | QTest::newRow("minData3") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
412 | 410 | {100., 200., 300., 400., 500.}) |
|
413 | 411 | << 1.1 << true << 2.; |
|
414 | 412 | QTest::newRow("minData4") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
415 | 413 | {100., 200., 300., 400., 500.}) |
|
416 | 414 | << 5. << true << 5.; |
|
417 | 415 | QTest::newRow("minData5") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
418 | 416 | {100., 200., 300., 400., 500.}) |
|
419 | 417 | << 5.1 << false << std::numeric_limits<double>::quiet_NaN(); |
|
420 | 418 | QTest::newRow("minData6") << createScalarSeries({}, {}) << 1.1 << false |
|
421 | 419 | << std::numeric_limits<double>::quiet_NaN(); |
|
422 | 420 | } |
|
423 | 421 | |
|
424 | 422 | void TestDataSeries::testMinXAxisData() |
|
425 | 423 | { |
|
426 | 424 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); |
|
427 | 425 | QFETCH(double, min); |
|
428 | 426 | |
|
429 | 427 | QFETCH(bool, expectedOK); |
|
430 | 428 | QFETCH(double, expectedMin); |
|
431 | 429 | |
|
432 | 430 | auto it = dataSeries->minXAxisData(min); |
|
433 | 431 | |
|
434 | 432 | QCOMPARE(expectedOK, it != dataSeries->cend()); |
|
435 | 433 | |
|
436 | 434 | // If the method doesn't return a end iterator, checks with expected value |
|
437 | 435 | if (expectedOK) { |
|
438 | 436 | QCOMPARE(expectedMin, it->x()); |
|
439 | 437 | } |
|
440 | 438 | } |
|
441 | 439 | |
|
442 | 440 | void TestDataSeries::testMaxXAxisData_data() |
|
443 | 441 | { |
|
444 | 442 | // ////////////// // |
|
445 | 443 | // Test structure // |
|
446 | 444 | // ////////////// // |
|
447 | 445 | |
|
448 | 446 | // Data series to get max data |
|
449 | 447 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); |
|
450 | 448 | |
|
451 | 449 | // Max data |
|
452 | 450 | QTest::addColumn<double>("max"); |
|
453 | 451 | |
|
454 | 452 | // Expected results |
|
455 | 453 | QTest::addColumn<bool>( |
|
456 | 454 | "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator) |
|
457 | 455 | QTest::addColumn<double>( |
|
458 | 456 | "expectedMax"); // Expected value when method doesn't return end iterator |
|
459 | 457 | |
|
460 | 458 | // ////////// // |
|
461 | 459 | // Test cases // |
|
462 | 460 | // ////////// // |
|
463 | 461 | |
|
464 | 462 | QTest::newRow("maxData1") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
465 | 463 | {100., 200., 300., 400., 500.}) |
|
466 | 464 | << 6. << true << 5.; |
|
467 | 465 | QTest::newRow("maxData2") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
468 | 466 | {100., 200., 300., 400., 500.}) |
|
469 | 467 | << 5. << true << 5.; |
|
470 | 468 | QTest::newRow("maxData3") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
471 | 469 | {100., 200., 300., 400., 500.}) |
|
472 | 470 | << 4.9 << true << 4.; |
|
473 | 471 | QTest::newRow("maxData4") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
474 | 472 | {100., 200., 300., 400., 500.}) |
|
475 | 473 | << 1.1 << true << 1.; |
|
476 | 474 | QTest::newRow("maxData5") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
477 | 475 | {100., 200., 300., 400., 500.}) |
|
478 | 476 | << 1. << true << 1.; |
|
479 | 477 | QTest::newRow("maxData6") << createScalarSeries({}, {}) << 1.1 << false |
|
480 | 478 | << std::numeric_limits<double>::quiet_NaN(); |
|
481 | 479 | } |
|
482 | 480 | |
|
483 | 481 | void TestDataSeries::testMaxXAxisData() |
|
484 | 482 | { |
|
485 | 483 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); |
|
486 | 484 | QFETCH(double, max); |
|
487 | 485 | |
|
488 | 486 | QFETCH(bool, expectedOK); |
|
489 | 487 | QFETCH(double, expectedMax); |
|
490 | 488 | |
|
491 | 489 | auto it = dataSeries->maxXAxisData(max); |
|
492 | 490 | |
|
493 | 491 | QCOMPARE(expectedOK, it != dataSeries->cend()); |
|
494 | 492 | |
|
495 | 493 | // If the method doesn't return a end iterator, checks with expected value |
|
496 | 494 | if (expectedOK) { |
|
497 | 495 | QCOMPARE(expectedMax, it->x()); |
|
498 | 496 | } |
|
499 | 497 | } |
|
500 | 498 | |
|
501 | 499 | void TestDataSeries::testXAxisRange_data() |
|
502 | 500 | { |
|
503 | 501 | // ////////////// // |
|
504 | 502 | // Test structure // |
|
505 | 503 | // ////////////// // |
|
506 | 504 | |
|
507 | 505 | // Data series to get x-axis range |
|
508 | 506 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); |
|
509 | 507 | |
|
510 | 508 | // Min/max values |
|
511 | 509 | QTest::addColumn<double>("min"); |
|
512 | 510 | QTest::addColumn<double>("max"); |
|
513 | 511 | |
|
514 | 512 | // Expected values |
|
515 |
QTest::addColumn< |
|
|
516 |
QTest::addColumn< |
|
|
513 | QTest::addColumn<DataContainer>("expectedXAxisData"); | |
|
514 | QTest::addColumn<DataContainer>("expectedValuesData"); | |
|
517 | 515 | |
|
518 | 516 | // ////////// // |
|
519 | 517 | // Test cases // |
|
520 | 518 | // ////////// // |
|
521 | 519 | |
|
522 | 520 | QTest::newRow("xAxisRange1") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
523 | 521 | {100., 200., 300., 400., 500.}) |
|
524 |
<< -1. << 3.2 << |
|
|
525 |
<< |
|
|
522 | << -1. << 3.2 << DataContainer{1., 2., 3.} | |
|
523 | << DataContainer{100., 200., 300.}; | |
|
526 | 524 | QTest::newRow("xAxisRange2") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
527 | 525 | {100., 200., 300., 400., 500.}) |
|
528 |
<< 1. << 4. << |
|
|
529 |
<< |
|
|
526 | << 1. << 4. << DataContainer{1., 2., 3., 4.} | |
|
527 | << DataContainer{100., 200., 300., 400.}; | |
|
530 | 528 | QTest::newRow("xAxisRange3") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
531 | 529 | {100., 200., 300., 400., 500.}) |
|
532 |
<< 1. << 3.9 << |
|
|
533 |
<< |
|
|
530 | << 1. << 3.9 << DataContainer{1., 2., 3.} | |
|
531 | << DataContainer{100., 200., 300.}; | |
|
534 | 532 | QTest::newRow("xAxisRange4") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
535 | 533 | {100., 200., 300., 400., 500.}) |
|
536 |
<< 0. << 0.9 << |
|
|
534 | << 0. << 0.9 << DataContainer{} << DataContainer{}; | |
|
537 | 535 | QTest::newRow("xAxisRange5") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
538 | 536 | {100., 200., 300., 400., 500.}) |
|
539 |
<< 0. << 1. << |
|
|
537 | << 0. << 1. << DataContainer{1.} << DataContainer{100.}; | |
|
540 | 538 | QTest::newRow("xAxisRange6") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
541 | 539 | {100., 200., 300., 400., 500.}) |
|
542 |
<< 2.1 << 6. << |
|
|
543 |
<< |
|
|
540 | << 2.1 << 6. << DataContainer{3., 4., 5.} | |
|
541 | << DataContainer{300., 400., 500.}; | |
|
544 | 542 | QTest::newRow("xAxisRange7") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
545 | 543 | {100., 200., 300., 400., 500.}) |
|
546 |
<< 6. << 9. << |
|
|
544 | << 6. << 9. << DataContainer{} << DataContainer{}; | |
|
547 | 545 | QTest::newRow("xAxisRange8") << createScalarSeries({1., 2., 3., 4., 5.}, |
|
548 | 546 | {100., 200., 300., 400., 500.}) |
|
549 |
<< 5. << 9. << |
|
|
547 | << 5. << 9. << DataContainer{5.} << DataContainer{500.}; | |
|
550 | 548 | } |
|
551 | 549 | |
|
552 | 550 | void TestDataSeries::testXAxisRange() |
|
553 | 551 | { |
|
554 | 552 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); |
|
555 | 553 | QFETCH(double, min); |
|
556 | 554 | QFETCH(double, max); |
|
557 | 555 | |
|
558 |
QFETCH( |
|
|
559 |
QFETCH( |
|
|
556 | QFETCH(DataContainer, expectedXAxisData); | |
|
557 | QFETCH(DataContainer, expectedValuesData); | |
|
560 | 558 | |
|
561 | 559 | auto bounds = dataSeries->xAxisRange(min, max); |
|
562 | 560 | validateRange(bounds.first, bounds.second, expectedXAxisData, expectedValuesData); |
|
563 | 561 | } |
|
564 | 562 | |
|
565 | 563 | void TestDataSeries::testValuesBoundsScalar_data() |
|
566 | 564 | { |
|
567 | 565 | testValuesBoundsStructure<ScalarSeries>(); |
|
568 | 566 | |
|
569 | 567 | // ////////// // |
|
570 | 568 | // Test cases // |
|
571 | 569 | // ////////// // |
|
572 | 570 | auto nan = std::numeric_limits<double>::quiet_NaN(); |
|
573 | 571 | |
|
574 | 572 | QTest::newRow("scalarBounds1") |
|
575 | 573 | << createScalarSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) << 0. << 6. |
|
576 | 574 | << true << 100. << 500.; |
|
577 | 575 | QTest::newRow("scalarBounds2") |
|
578 | 576 | << createScalarSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) << 2. << 4. |
|
579 | 577 | << true << 200. << 400.; |
|
580 | 578 | QTest::newRow("scalarBounds3") |
|
581 | 579 | << createScalarSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) << 0. << 0.5 |
|
582 | 580 | << false << nan << nan; |
|
583 | 581 | QTest::newRow("scalarBounds4") |
|
584 | 582 | << createScalarSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) << 5.1 << 6. |
|
585 | 583 | << false << nan << nan; |
|
586 | 584 | QTest::newRow("scalarBounds5") << createScalarSeries({1.}, {100.}) << 0. << 2. << true << 100. |
|
587 | 585 | << 100.; |
|
588 | 586 | QTest::newRow("scalarBounds6") << createScalarSeries({}, {}) << 0. << 2. << false << nan << nan; |
|
589 | 587 | |
|
590 | 588 | // Tests with NaN values: NaN values are not included in min/max search |
|
591 | 589 | QTest::newRow("scalarBounds7") |
|
592 | 590 | << createScalarSeries({1., 2., 3., 4., 5.}, {nan, 200., 300., 400., nan}) << 0. << 6. |
|
593 | 591 | << true << 200. << 400.; |
|
594 | 592 | QTest::newRow("scalarBounds8") |
|
595 | 593 | << createScalarSeries({1., 2., 3., 4., 5.}, {nan, nan, nan, nan, nan}) << 0. << 6. << true |
|
596 | 594 | << std::numeric_limits<double>::quiet_NaN() << std::numeric_limits<double>::quiet_NaN(); |
|
597 | 595 | } |
|
598 | 596 | |
|
599 | 597 | void TestDataSeries::testValuesBoundsScalar() |
|
600 | 598 | { |
|
601 | 599 | testValuesBounds<ScalarSeries>(); |
|
602 | 600 | } |
|
603 | 601 | |
|
604 | 602 | void TestDataSeries::testValuesBoundsVector_data() |
|
605 | 603 | { |
|
606 | 604 | testValuesBoundsStructure<VectorSeries>(); |
|
607 | 605 | |
|
608 | 606 | // ////////// // |
|
609 | 607 | // Test cases // |
|
610 | 608 | // ////////// // |
|
611 | 609 | auto nan = std::numeric_limits<double>::quiet_NaN(); |
|
612 | 610 | |
|
613 | 611 | QTest::newRow("vectorBounds1") |
|
614 | 612 | << createVectorSeries({1., 2., 3., 4., 5.}, {10., 15., 20., 13., 12.}, |
|
615 | 613 | {35., 24., 10., 9., 0.3}, {13., 14., 12., 9., 24.}) |
|
616 | 614 | << 0. << 6. << true << 0.3 << 35.; // min/max in same component |
|
617 | 615 | QTest::newRow("vectorBounds2") |
|
618 | 616 | << createVectorSeries({1., 2., 3., 4., 5.}, {2.3, 15., 20., 13., 12.}, |
|
619 | 617 | {35., 24., 10., 9., 4.}, {13., 14., 12., 9., 24.}) |
|
620 | 618 | << 0. << 6. << true << 2.3 << 35.; // min/max in same entry |
|
621 | 619 | QTest::newRow("vectorBounds3") |
|
622 | 620 | << createVectorSeries({1., 2., 3., 4., 5.}, {2.3, 15., 20., 13., 12.}, |
|
623 | 621 | {35., 24., 10., 9., 4.}, {13., 14., 12., 9., 24.}) |
|
624 | 622 | << 2. << 3. << true << 10. << 24.; |
|
625 | 623 | |
|
626 | 624 | // Tests with NaN values: NaN values are not included in min/max search |
|
627 | 625 | QTest::newRow("vectorBounds4") |
|
628 | 626 | << createVectorSeries({1., 2.}, {nan, nan}, {nan, nan}, {nan, nan}) << 0. << 6. << true |
|
629 | 627 | << nan << nan; |
|
630 | 628 | } |
|
631 | 629 | |
|
632 | 630 | void TestDataSeries::testValuesBoundsVector() |
|
633 | 631 | { |
|
634 | 632 | testValuesBounds<VectorSeries>(); |
|
635 | 633 | } |
|
636 | 634 | |
|
637 | 635 | QTEST_MAIN(TestDataSeries) |
|
638 | 636 | #include "TestDataSeries.moc" |
@@ -1,181 +1,181 | |||
|
1 | 1 | #include "Data/ArrayData.h" |
|
2 | 2 | #include <QObject> |
|
3 | 3 | #include <QtTest> |
|
4 | 4 | |
|
5 | 5 | namespace { |
|
6 | 6 | |
|
7 | void verifyArrayData(const ArrayData<1> &arrayData, const QVector<double> &expectedData) | |
|
7 | using DataContainer = std::vector<double>; | |
|
8 | ||
|
9 | void verifyArrayData(const ArrayData<1> &arrayData, const DataContainer &expectedData) | |
|
8 | 10 | { |
|
9 | 11 | QVERIFY(std::equal( |
|
10 | 12 | arrayData.cbegin(), arrayData.cend(), expectedData.cbegin(), expectedData.cend(), |
|
11 | 13 | [](const auto &it, const auto &expectedData) { return it.at(0) == expectedData; })); |
|
12 | 14 | } |
|
13 | 15 | |
|
14 | 16 | } // namespace |
|
15 | 17 | |
|
16 | 18 | class TestOneDimArrayData : public QObject { |
|
17 | 19 | Q_OBJECT |
|
18 | 20 | private slots: |
|
19 | 21 | /// Tests @sa ArrayData::data() |
|
20 | 22 | void testData_data(); |
|
21 | 23 | void testData(); |
|
22 | 24 | |
|
23 | 25 | /// Tests @sa ArrayData::add() |
|
24 | 26 | void testAdd_data(); |
|
25 | 27 | void testAdd(); |
|
26 | 28 | |
|
27 | 29 | /// Tests @sa ArrayData::at(int index) |
|
28 | 30 | void testAt_data(); |
|
29 | 31 | void testAt(); |
|
30 | 32 | |
|
31 | 33 | /// Tests @sa ArrayData::clear() |
|
32 | 34 | void testClear_data(); |
|
33 | 35 | void testClear(); |
|
34 | 36 | |
|
35 | 37 | /// Tests @sa ArrayData::size() |
|
36 | 38 | void testSize_data(); |
|
37 | 39 | void testSize(); |
|
38 | 40 | |
|
39 | 41 | /// Tests @sa ArrayData::sort() |
|
40 | 42 | void testSort_data(); |
|
41 | 43 | void testSort(); |
|
42 | 44 | }; |
|
43 | 45 | |
|
44 | 46 | void TestOneDimArrayData::testData_data() |
|
45 | 47 | { |
|
46 | 48 | // Test structure |
|
47 |
QTest::addColumn< |
|
|
48 |
QTest::addColumn< |
|
|
49 | QTest::addColumn<DataContainer>("inputData"); // array's data input | |
|
50 | QTest::addColumn<DataContainer>("expectedData"); // expected data | |
|
49 | 51 | |
|
50 | 52 | // Test cases |
|
51 |
QTest::newRow("data1") << |
|
|
52 |
<< |
|
|
53 | QTest::newRow("data1") << DataContainer{1., 2., 3., 4., 5.} | |
|
54 | << DataContainer{1., 2., 3., 4., 5.}; | |
|
53 | 55 | } |
|
54 | 56 | |
|
55 | 57 | void TestOneDimArrayData::testData() |
|
56 | 58 | { |
|
57 |
QFETCH( |
|
|
58 |
QFETCH( |
|
|
59 | QFETCH(DataContainer, inputData); | |
|
60 | QFETCH(DataContainer, expectedData); | |
|
59 | 61 | |
|
60 | 62 | ArrayData<1> arrayData{inputData}; |
|
61 | 63 | verifyArrayData(arrayData, expectedData); |
|
62 | 64 | } |
|
63 | 65 | |
|
64 | 66 | void TestOneDimArrayData::testAdd_data() |
|
65 | 67 | { |
|
66 | 68 | // Test structure |
|
67 |
QTest::addColumn< |
|
|
68 |
QTest::addColumn< |
|
|
69 |
QTest::addColumn<bool>("prepend"); |
|
|
70 |
QTest::addColumn< |
|
|
69 | QTest::addColumn<DataContainer>("inputData"); // array's data input | |
|
70 | QTest::addColumn<DataContainer>("otherData"); // array data's input to merge with | |
|
71 | QTest::addColumn<bool>("prepend"); // prepend or append merge | |
|
72 | QTest::addColumn<DataContainer>("expectedData"); // expected data after merge | |
|
71 | 73 | |
|
72 | 74 | // Test cases |
|
73 |
QTest::newRow("appendMerge") << |
|
|
74 |
<< |
|
|
75 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8.}; | |
|
76 | QTest::newRow("prependMerge") << QVector<double>{1., 2., 3., 4., 5.} | |
|
77 | << QVector<double>{6., 7., 8.} << true | |
|
78 | << QVector<double>{6., 7., 8., 1., 2., 3., 4., 5.}; | |
|
75 | QTest::newRow("appendMerge") << DataContainer{1., 2., 3., 4., 5.} << DataContainer{6., 7., 8.} | |
|
76 | << false << DataContainer{1., 2., 3., 4., 5., 6., 7., 8.}; | |
|
77 | QTest::newRow("prependMerge") << DataContainer{1., 2., 3., 4., 5.} << DataContainer{6., 7., 8.} | |
|
78 | << true << DataContainer{6., 7., 8., 1., 2., 3., 4., 5.}; | |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | 81 | void TestOneDimArrayData::testAdd() |
|
82 | 82 | { |
|
83 |
QFETCH( |
|
|
84 |
QFETCH( |
|
|
83 | QFETCH(DataContainer, inputData); | |
|
84 | QFETCH(DataContainer, otherData); | |
|
85 | 85 | QFETCH(bool, prepend); |
|
86 |
QFETCH( |
|
|
86 | QFETCH(DataContainer, expectedData); | |
|
87 | 87 | |
|
88 | 88 | ArrayData<1> arrayData{inputData}; |
|
89 | 89 | ArrayData<1> other{otherData}; |
|
90 | 90 | |
|
91 | 91 | arrayData.add(other, prepend); |
|
92 | 92 | verifyArrayData(arrayData, expectedData); |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | void TestOneDimArrayData::testAt_data() |
|
96 | 96 | { |
|
97 | 97 | // Test structure |
|
98 |
QTest::addColumn< |
|
|
99 |
QTest::addColumn<int>("index"); |
|
|
100 |
QTest::addColumn<double>("expectedData"); |
|
|
98 | QTest::addColumn<DataContainer>("inputData"); // array data's input | |
|
99 | QTest::addColumn<int>("index"); // index to retrieve data | |
|
100 | QTest::addColumn<double>("expectedData"); // expected data at index | |
|
101 | 101 | |
|
102 | 102 | // Test cases |
|
103 |
QTest::newRow("data1") << |
|
|
104 |
QTest::newRow("data2") << |
|
|
103 | QTest::newRow("data1") << DataContainer{1., 2., 3., 4., 5.} << 0 << 1.; | |
|
104 | QTest::newRow("data2") << DataContainer{1., 2., 3., 4., 5.} << 3 << 4.; | |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | void TestOneDimArrayData::testAt() |
|
108 | 108 | { |
|
109 |
QFETCH( |
|
|
109 | QFETCH(DataContainer, inputData); | |
|
110 | 110 | QFETCH(int, index); |
|
111 | 111 | QFETCH(double, expectedData); |
|
112 | 112 | |
|
113 | 113 | ArrayData<1> arrayData{inputData}; |
|
114 | 114 | QVERIFY(arrayData.at(index) == expectedData); |
|
115 | 115 | } |
|
116 | 116 | |
|
117 | 117 | void TestOneDimArrayData::testClear_data() |
|
118 | 118 | { |
|
119 | 119 | // Test structure |
|
120 |
QTest::addColumn< |
|
|
120 | QTest::addColumn<DataContainer>("inputData"); // array data's input | |
|
121 | 121 | |
|
122 | 122 | // Test cases |
|
123 |
QTest::newRow("data1") << |
|
|
123 | QTest::newRow("data1") << DataContainer{1., 2., 3., 4., 5.}; | |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | void TestOneDimArrayData::testClear() |
|
127 | 127 | { |
|
128 |
QFETCH( |
|
|
128 | QFETCH(DataContainer, inputData); | |
|
129 | 129 | |
|
130 | 130 | ArrayData<1> arrayData{inputData}; |
|
131 | 131 | arrayData.clear(); |
|
132 |
verifyArrayData(arrayData, |
|
|
132 | verifyArrayData(arrayData, DataContainer{}); | |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | void TestOneDimArrayData::testSize_data() |
|
136 | 136 | { |
|
137 | 137 | // Test structure |
|
138 |
QTest::addColumn< |
|
|
139 |
QTest::addColumn<int>("expectedSize"); |
|
|
138 | QTest::addColumn<DataContainer>("inputData"); // array data's input | |
|
139 | QTest::addColumn<int>("expectedSize"); // expected array data size | |
|
140 | 140 | |
|
141 | 141 | // Test cases |
|
142 |
QTest::newRow("data1") << |
|
|
142 | QTest::newRow("data1") << DataContainer{1., 2., 3., 4., 5.} << 5; | |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | void TestOneDimArrayData::testSize() |
|
146 | 146 | { |
|
147 |
QFETCH( |
|
|
147 | QFETCH(DataContainer, inputData); | |
|
148 | 148 | QFETCH(int, expectedSize); |
|
149 | 149 | |
|
150 | 150 | ArrayData<1> arrayData{inputData}; |
|
151 | 151 | QVERIFY(arrayData.size() == expectedSize); |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | void TestOneDimArrayData::testSort_data() |
|
155 | 155 | { |
|
156 | 156 | // Test structure |
|
157 |
QTest::addColumn< |
|
|
157 | QTest::addColumn<DataContainer>("inputData"); // array data's input | |
|
158 | 158 | QTest::addColumn<std::vector<int> >("sortPermutation"); // permutation used to sort data |
|
159 |
QTest::addColumn< |
|
|
159 | QTest::addColumn<DataContainer>("expectedData"); // expected data after sorting | |
|
160 | 160 | |
|
161 | 161 | // Test cases |
|
162 |
QTest::newRow("data1") << |
|
|
163 |
<< |
|
|
164 |
QTest::newRow("data2") << |
|
|
165 |
<< |
|
|
162 | QTest::newRow("data1") << DataContainer{1., 2., 3., 4., 5.} << std::vector<int>{0, 2, 3, 1, 4} | |
|
163 | << DataContainer{1., 3., 4., 2., 5.}; | |
|
164 | QTest::newRow("data2") << DataContainer{1., 2., 3., 4., 5.} << std::vector<int>{4, 1, 2, 3, 0} | |
|
165 | << DataContainer{5., 2., 3., 4., 1.}; | |
|
166 | 166 | } |
|
167 | 167 | |
|
168 | 168 | void TestOneDimArrayData::testSort() |
|
169 | 169 | { |
|
170 |
QFETCH( |
|
|
170 | QFETCH(DataContainer, inputData); | |
|
171 | 171 | QFETCH(std::vector<int>, sortPermutation); |
|
172 |
QFETCH( |
|
|
172 | QFETCH(DataContainer, expectedData); | |
|
173 | 173 | |
|
174 | 174 | ArrayData<1> arrayData{inputData}; |
|
175 | 175 | auto sortedArrayData = arrayData.sort(sortPermutation); |
|
176 | 176 | QVERIFY(sortedArrayData != nullptr); |
|
177 | 177 | verifyArrayData(*sortedArrayData, expectedData); |
|
178 | 178 | } |
|
179 | 179 | |
|
180 | 180 | QTEST_MAIN(TestOneDimArrayData) |
|
181 | 181 | #include "TestOneDimArrayData.moc" |
@@ -1,239 +1,240 | |||
|
1 | 1 | #include "Data/ArrayData.h" |
|
2 | 2 | #include <QObject> |
|
3 | 3 | #include <QtTest> |
|
4 | 4 | |
|
5 |
using Container = |
|
|
6 | using InputData = QPair<QVector<double>, int>; | |
|
5 | using DataContainer = std::vector<double>; | |
|
6 | using Container = std::vector<DataContainer>; | |
|
7 | using InputData = QPair<DataContainer, int>; | |
|
7 | 8 | |
|
8 | 9 | namespace { |
|
9 | 10 | |
|
10 | 11 | InputData flatten(const Container &container) |
|
11 | 12 | { |
|
12 |
if (container. |
|
|
13 | if (container.empty()) { | |
|
13 | 14 | return {}; |
|
14 | 15 | } |
|
15 | 16 | |
|
16 | 17 | // We assume here that each component of the container have the same size |
|
17 | 18 | auto containerSize = container.size(); |
|
18 |
auto componentSize = container.f |
|
|
19 | auto componentSize = container.front().size(); | |
|
19 | 20 | |
|
20 |
auto result = |
|
|
21 | auto result = DataContainer{}; | |
|
21 | 22 | result.reserve(componentSize * containerSize); |
|
22 | 23 | |
|
23 | 24 | for (auto i = 0; i < componentSize; ++i) { |
|
24 | 25 | for (auto j = 0; j < containerSize; ++j) { |
|
25 |
result. |
|
|
26 | result.push_back(container.at(j).at(i)); | |
|
26 | 27 | } |
|
27 | 28 | } |
|
28 | 29 | |
|
29 | return {result, containerSize}; | |
|
30 | return {result, static_cast<int>(containerSize)}; | |
|
30 | 31 | } |
|
31 | 32 | |
|
32 | 33 | void verifyArrayData(const ArrayData<2> &arrayData, const Container &expectedData) |
|
33 | 34 | { |
|
34 | 35 | auto verifyComponent = [&arrayData](const auto &componentData, const auto &equalFun) { |
|
35 | 36 | QVERIFY(std::equal(arrayData.cbegin(), arrayData.cend(), componentData.cbegin(), |
|
36 | 37 | componentData.cend(), |
|
37 | 38 | [&equalFun](const auto &dataSeriesIt, const auto &expectedValue) { |
|
38 | 39 | return equalFun(dataSeriesIt, expectedValue); |
|
39 | 40 | })); |
|
40 | 41 | }; |
|
41 | 42 | |
|
42 | 43 | for (auto i = 0; i < expectedData.size(); ++i) { |
|
43 | 44 | verifyComponent(expectedData.at(i), [i](const auto &seriesIt, const auto &value) { |
|
44 | 45 | return seriesIt.at(i) == value; |
|
45 | 46 | }); |
|
46 | 47 | } |
|
47 | 48 | } |
|
48 | 49 | |
|
49 | 50 | } // namespace |
|
50 | 51 | |
|
51 | 52 | class TestTwoDimArrayData : public QObject { |
|
52 | 53 | Q_OBJECT |
|
53 | 54 | private slots: |
|
54 | 55 | /// Tests @sa ArrayData ctor |
|
55 | 56 | void testCtor_data(); |
|
56 | 57 | void testCtor(); |
|
57 | 58 | |
|
58 | 59 | /// Tests @sa ArrayData::add() |
|
59 | 60 | void testAdd_data(); |
|
60 | 61 | void testAdd(); |
|
61 | 62 | |
|
62 | 63 | /// Tests @sa ArrayData::clear() |
|
63 | 64 | void testClear_data(); |
|
64 | 65 | void testClear(); |
|
65 | 66 | |
|
66 | 67 | /// Tests @sa ArrayData::size() |
|
67 | 68 | void testSize_data(); |
|
68 | 69 | void testSize(); |
|
69 | 70 | |
|
70 | 71 | /// Tests @sa ArrayData::sort() |
|
71 | 72 | void testSort_data(); |
|
72 | 73 | void testSort(); |
|
73 | 74 | }; |
|
74 | 75 | |
|
75 | 76 | void TestTwoDimArrayData::testCtor_data() |
|
76 | 77 | { |
|
77 | 78 | // Test structure |
|
78 | 79 | QTest::addColumn<InputData>("inputData"); // array data's input |
|
79 | 80 | QTest::addColumn<bool>("success"); // array data has been successfully constructed |
|
80 | 81 | QTest::addColumn<Container>("expectedData"); // expected array data (when success) |
|
81 | 82 | |
|
82 | 83 | // Test cases |
|
83 | 84 | QTest::newRow("validInput") << flatten(Container{{1., 2., 3., 4., 5.}, |
|
84 | 85 | {6., 7., 8., 9., 10.}, |
|
85 | 86 | {11., 12., 13., 14., 15.}}) |
|
86 | 87 | << true << Container{{1., 2., 3., 4., 5.}, |
|
87 | 88 | {6., 7., 8., 9., 10.}, |
|
88 | 89 | {11., 12., 13., 14., 15.}}; |
|
89 | 90 | QTest::newRow("invalidInput (invalid data size") |
|
90 | 91 | << InputData{{1., 2., 3., 4., 5., 6., 7.}, 3} << false << Container{{}, {}, {}}; |
|
91 | 92 | QTest::newRow("invalidInput (less than two components") |
|
92 | 93 | << flatten(Container{{1., 2., 3., 4., 5.}}) << false << Container{{}, {}, {}}; |
|
93 | 94 | } |
|
94 | 95 | |
|
95 | 96 | void TestTwoDimArrayData::testCtor() |
|
96 | 97 | { |
|
97 | 98 | QFETCH(InputData, inputData); |
|
98 | 99 | QFETCH(bool, success); |
|
99 | 100 | |
|
100 | 101 | if (success) { |
|
101 | 102 | QFETCH(Container, expectedData); |
|
102 | 103 | |
|
103 | 104 | ArrayData<2> arrayData{inputData.first, inputData.second}; |
|
104 | 105 | verifyArrayData(arrayData, expectedData); |
|
105 | 106 | } |
|
106 | 107 | else { |
|
107 | 108 | QVERIFY_EXCEPTION_THROWN(ArrayData<2>(inputData.first, inputData.second), |
|
108 | 109 | std::invalid_argument); |
|
109 | 110 | } |
|
110 | 111 | } |
|
111 | 112 | |
|
112 | 113 | void TestTwoDimArrayData::testAdd_data() |
|
113 | 114 | { |
|
114 | 115 | // Test structure |
|
115 | 116 | QTest::addColumn<InputData>("inputData"); // array's data input |
|
116 | 117 | QTest::addColumn<InputData>("otherData"); // array data's input to merge with |
|
117 | 118 | QTest::addColumn<bool>("prepend"); // prepend or append merge |
|
118 | 119 | QTest::addColumn<Container>("expectedData"); // expected data after merge |
|
119 | 120 | |
|
120 | 121 | // Test cases |
|
121 | 122 | auto inputData = flatten( |
|
122 | 123 | Container{{1., 2., 3., 4., 5.}, {11., 12., 13., 14., 15.}, {21., 22., 23., 24., 25.}}); |
|
123 | 124 | |
|
124 | 125 | auto vectorContainer = flatten(Container{{6., 7., 8.}, {16., 17., 18.}, {26., 27., 28}}); |
|
125 | 126 | auto tensorContainer = flatten(Container{{6., 7., 8.}, |
|
126 | 127 | {16., 17., 18.}, |
|
127 | 128 | {26., 27., 28}, |
|
128 | 129 | {36., 37., 38.}, |
|
129 | 130 | {46., 47., 48.}, |
|
130 | 131 | {56., 57., 58}}); |
|
131 | 132 | |
|
132 | 133 | QTest::newRow("appendMerge") << inputData << vectorContainer << false |
|
133 | 134 | << Container{{1., 2., 3., 4., 5., 6., 7., 8.}, |
|
134 | 135 | {11., 12., 13., 14., 15., 16., 17., 18.}, |
|
135 | 136 | {21., 22., 23., 24., 25., 26., 27., 28}}; |
|
136 | 137 | QTest::newRow("prependMerge") << inputData << vectorContainer << true |
|
137 | 138 | << Container{{6., 7., 8., 1., 2., 3., 4., 5.}, |
|
138 | 139 | {16., 17., 18., 11., 12., 13., 14., 15.}, |
|
139 | 140 | {26., 27., 28, 21., 22., 23., 24., 25.}}; |
|
140 | 141 | QTest::newRow("invalidMerge") << inputData << tensorContainer << false |
|
141 | 142 | << Container{{1., 2., 3., 4., 5.}, |
|
142 | 143 | {11., 12., 13., 14., 15.}, |
|
143 | 144 | {21., 22., 23., 24., 25.}}; |
|
144 | 145 | } |
|
145 | 146 | |
|
146 | 147 | void TestTwoDimArrayData::testAdd() |
|
147 | 148 | { |
|
148 | 149 | QFETCH(InputData, inputData); |
|
149 | 150 | QFETCH(InputData, otherData); |
|
150 | 151 | QFETCH(bool, prepend); |
|
151 | 152 | QFETCH(Container, expectedData); |
|
152 | 153 | |
|
153 | 154 | ArrayData<2> arrayData{inputData.first, inputData.second}; |
|
154 | 155 | ArrayData<2> other{otherData.first, otherData.second}; |
|
155 | 156 | |
|
156 | 157 | arrayData.add(other, prepend); |
|
157 | 158 | |
|
158 | 159 | verifyArrayData(arrayData, expectedData); |
|
159 | 160 | } |
|
160 | 161 | |
|
161 | 162 | void TestTwoDimArrayData::testClear_data() |
|
162 | 163 | { |
|
163 | 164 | // Test structure |
|
164 | 165 | QTest::addColumn<InputData>("inputData"); // array data's input |
|
165 | 166 | |
|
166 | 167 | // Test cases |
|
167 | 168 | QTest::newRow("data1") << flatten( |
|
168 | 169 | Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}); |
|
169 | 170 | } |
|
170 | 171 | |
|
171 | 172 | void TestTwoDimArrayData::testClear() |
|
172 | 173 | { |
|
173 | 174 | QFETCH(InputData, inputData); |
|
174 | 175 | |
|
175 | 176 | ArrayData<2> arrayData{inputData.first, inputData.second}; |
|
176 | 177 | arrayData.clear(); |
|
177 | 178 | |
|
178 |
auto emptyData = Container(inputData.second, |
|
|
179 | auto emptyData = Container(inputData.second, DataContainer{}); | |
|
179 | 180 | verifyArrayData(arrayData, emptyData); |
|
180 | 181 | } |
|
181 | 182 | |
|
182 | 183 | void TestTwoDimArrayData::testSize_data() |
|
183 | 184 | { |
|
184 | 185 | // Test structure |
|
185 | 186 | QTest::addColumn<InputData>("inputData"); // array data's input |
|
186 | 187 | QTest::addColumn<int>("expectedSize"); // expected array data size |
|
187 | 188 | |
|
188 | 189 | // Test cases |
|
189 | 190 | QTest::newRow("data1") << flatten(Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}}) << 5; |
|
190 | 191 | QTest::newRow("data2") << flatten(Container{{1., 2., 3., 4., 5.}, |
|
191 | 192 | {6., 7., 8., 9., 10.}, |
|
192 | 193 | {11., 12., 13., 14., 15.}}) |
|
193 | 194 | << 5; |
|
194 | 195 | } |
|
195 | 196 | |
|
196 | 197 | void TestTwoDimArrayData::testSize() |
|
197 | 198 | { |
|
198 | 199 | QFETCH(InputData, inputData); |
|
199 | 200 | QFETCH(int, expectedSize); |
|
200 | 201 | |
|
201 | 202 | ArrayData<2> arrayData{inputData.first, inputData.second}; |
|
202 | 203 | QVERIFY(arrayData.size() == expectedSize); |
|
203 | 204 | } |
|
204 | 205 | |
|
205 | 206 | void TestTwoDimArrayData::testSort_data() |
|
206 | 207 | { |
|
207 | 208 | // Test structure |
|
208 | 209 | QTest::addColumn<InputData>("inputData"); // array data's input |
|
209 | 210 | QTest::addColumn<std::vector<int> >("sortPermutation"); // permutation used to sort data |
|
210 | 211 | QTest::addColumn<Container>("expectedData"); // expected data after sorting |
|
211 | 212 | |
|
212 | 213 | // Test cases |
|
213 | 214 | QTest::newRow("data1") |
|
214 | 215 | << flatten( |
|
215 | 216 | Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}) |
|
216 | 217 | << std::vector<int>{0, 2, 3, 1, 4} |
|
217 | 218 | << Container{{1., 3., 4., 2., 5.}, {6., 8., 9., 7., 10.}, {11., 13., 14., 12., 15.}}; |
|
218 | 219 | QTest::newRow("data2") |
|
219 | 220 | << flatten( |
|
220 | 221 | Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}) |
|
221 | 222 | << std::vector<int>{2, 4, 3, 0, 1} |
|
222 | 223 | << Container{{3., 5., 4., 1., 2.}, {8., 10., 9., 6., 7.}, {13., 15., 14., 11., 12.}}; |
|
223 | 224 | } |
|
224 | 225 | |
|
225 | 226 | void TestTwoDimArrayData::testSort() |
|
226 | 227 | { |
|
227 | 228 | QFETCH(InputData, inputData); |
|
228 | 229 | QFETCH(std::vector<int>, sortPermutation); |
|
229 | 230 | QFETCH(Container, expectedData); |
|
230 | 231 | |
|
231 | 232 | ArrayData<2> arrayData{inputData.first, inputData.second}; |
|
232 | 233 | auto sortedArrayData = arrayData.sort(sortPermutation); |
|
233 | 234 | QVERIFY(sortedArrayData != nullptr); |
|
234 | 235 | |
|
235 | 236 | verifyArrayData(*sortedArrayData, expectedData); |
|
236 | 237 | } |
|
237 | 238 | |
|
238 | 239 | QTEST_MAIN(TestTwoDimArrayData) |
|
239 | 240 | #include "TestTwoDimArrayData.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now