##// END OF EJS Templates
Updates unit tests
Alexandre Leroux -
r604:da7f81670868
parent child
Show More
@@ -10,6 +10,20
10 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
10 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
11 Q_DECLARE_METATYPE(std::shared_ptr<VectorSeries>)
11 Q_DECLARE_METATYPE(std::shared_ptr<VectorSeries>)
12
12
13 namespace {
14
15 void validateRange(DataSeriesIterator first, DataSeriesIterator last, const QVector<double> &xData,
16 const QVector<double> &valuesData)
17 {
18 QVERIFY(std::equal(first, last, xData.cbegin(), xData.cend(),
19 [](const auto &it, const auto &expectedX) { return it.x() == expectedX; }));
20 QVERIFY(std::equal(
21 first, last, valuesData.cbegin(), valuesData.cend(),
22 [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; }));
23 }
24
25 } // namespace
26
13 class TestDataSeries : public QObject {
27 class TestDataSeries : public QObject {
14 Q_OBJECT
28 Q_OBJECT
15 private:
29 private:
@@ -164,13 +178,7 void TestDataSeries::testCtor()
164 QFETCH(QVector<double>, expectedXAxisData);
178 QFETCH(QVector<double>, expectedXAxisData);
165 QFETCH(QVector<double>, expectedValuesData);
179 QFETCH(QVector<double>, expectedValuesData);
166
180
167 auto seriesXAxisData = series->xAxisData()->data();
181 validateRange(series->cbegin(), series->cend(), expectedXAxisData, expectedValuesData);
168 auto seriesValuesData = series->valuesData()->data();
169
170 QVERIFY(
171 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
172 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
173 seriesValuesData.cbegin()));
174 }
182 }
175
183
176 namespace {
184 namespace {
@@ -250,13 +258,7 void TestDataSeries::testMerge()
250 QFETCH(QVector<double>, expectedXAxisData);
258 QFETCH(QVector<double>, expectedXAxisData);
251 QFETCH(QVector<double>, expectedValuesData);
259 QFETCH(QVector<double>, expectedValuesData);
252
260
253 auto seriesXAxisData = dataSeries->xAxisData()->data();
261 validateRange(dataSeries->cbegin(), dataSeries->cend(), expectedXAxisData, expectedValuesData);
254 auto seriesValuesData = dataSeries->valuesData()->data();
255
256 QVERIFY(
257 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
258 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
259 seriesValuesData.cbegin()));
260 }
262 }
261
263
262 void TestDataSeries::testMinXAxisData_data()
264 void TestDataSeries::testMinXAxisData_data()
@@ -438,12 +440,7 void TestDataSeries::testXAxisRange()
438 QFETCH(QVector<double>, expectedValuesData);
440 QFETCH(QVector<double>, expectedValuesData);
439
441
440 auto bounds = dataSeries->xAxisRange(min, max);
442 auto bounds = dataSeries->xAxisRange(min, max);
441 QVERIFY(std::equal(bounds.first, bounds.second, expectedXAxisData.cbegin(),
443 validateRange(bounds.first, bounds.second, expectedXAxisData, expectedValuesData);
442 expectedXAxisData.cend(),
443 [](const auto &it, const auto &expectedX) { return it.x() == expectedX; }));
444 QVERIFY(std::equal(
445 bounds.first, bounds.second, expectedValuesData.cbegin(), expectedValuesData.cend(),
446 [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; }));
447 }
444 }
448
445
449 void TestDataSeries::testValuesBoundsScalar_data()
446 void TestDataSeries::testValuesBoundsScalar_data()
@@ -2,6 +2,17
2 #include <QObject>
2 #include <QObject>
3 #include <QtTest>
3 #include <QtTest>
4
4
5 namespace {
6
7 void verifyArrayData(const ArrayData<1> &arrayData, const QVector<double> &expectedData)
8 {
9 QVERIFY(std::equal(
10 arrayData.cbegin(), arrayData.cend(), expectedData.cbegin(), expectedData.cend(),
11 [](const auto &it, const auto &expectedData) { return it.at(0) == expectedData; }));
12 }
13
14 } // namespace
15
5 class TestOneDimArrayData : public QObject {
16 class TestOneDimArrayData : public QObject {
6 Q_OBJECT
17 Q_OBJECT
7 private slots:
18 private slots:
@@ -9,10 +20,6 private slots:
9 void testData_data();
20 void testData_data();
10 void testData();
21 void testData();
11
22
12 /// Tests @sa ArrayData::data(int componentIndex)
13 void testDataByComponentIndex_data();
14 void testDataByComponentIndex();
15
16 /// Tests @sa ArrayData::add()
23 /// Tests @sa ArrayData::add()
17 void testAdd_data();
24 void testAdd_data();
18 void testAdd();
25 void testAdd();
@@ -51,32 +58,7 void TestOneDimArrayData::testData()
51 QFETCH(QVector<double>, expectedData);
58 QFETCH(QVector<double>, expectedData);
52
59
53 ArrayData<1> arrayData{inputData};
60 ArrayData<1> arrayData{inputData};
54 QVERIFY(arrayData.data() == expectedData);
61 verifyArrayData(arrayData, expectedData);
55 }
56
57 void TestOneDimArrayData::testDataByComponentIndex_data()
58 {
59 // Test structure
60 QTest::addColumn<QVector<double> >("inputData"); // array data's input
61 QTest::addColumn<int>("componentIndex"); // component index to test
62 QTest::addColumn<QVector<double> >("expectedData"); // expected data
63
64 // Test cases
65 QTest::newRow("validIndex") << QVector<double>{1., 2., 3., 4., 5.} << 0
66 << QVector<double>{1., 2., 3., 4., 5.};
67 QTest::newRow("invalidIndex1") << QVector<double>{1., 2., 3., 4., 5.} << -1
68 << QVector<double>{};
69 QTest::newRow("invalidIndex2") << QVector<double>{1., 2., 3., 4., 5.} << 1 << QVector<double>{};
70 }
71
72 void TestOneDimArrayData::testDataByComponentIndex()
73 {
74 QFETCH(QVector<double>, inputData);
75 QFETCH(int, componentIndex);
76 QFETCH(QVector<double>, expectedData);
77
78 ArrayData<1> arrayData{inputData};
79 QVERIFY(arrayData.data(componentIndex) == expectedData);
80 }
62 }
81
63
82 void TestOneDimArrayData::testAdd_data()
64 void TestOneDimArrayData::testAdd_data()
@@ -107,7 +89,7 void TestOneDimArrayData::testAdd()
107 ArrayData<1> other{otherData};
89 ArrayData<1> other{otherData};
108
90
109 arrayData.add(other, prepend);
91 arrayData.add(other, prepend);
110 QVERIFY(arrayData.data() == expectedData);
92 verifyArrayData(arrayData, expectedData);
111 }
93 }
112
94
113 void TestOneDimArrayData::testAt_data()
95 void TestOneDimArrayData::testAt_data()
@@ -147,7 +129,7 void TestOneDimArrayData::testClear()
147
129
148 ArrayData<1> arrayData{inputData};
130 ArrayData<1> arrayData{inputData};
149 arrayData.clear();
131 arrayData.clear();
150 QVERIFY(arrayData.data() == QVector<double>{});
132 verifyArrayData(arrayData, QVector<double>{});
151 }
133 }
152
134
153 void TestOneDimArrayData::testSize_data()
135 void TestOneDimArrayData::testSize_data()
@@ -192,7 +174,7 void TestOneDimArrayData::testSort()
192 ArrayData<1> arrayData{inputData};
174 ArrayData<1> arrayData{inputData};
193 auto sortedArrayData = arrayData.sort(sortPermutation);
175 auto sortedArrayData = arrayData.sort(sortPermutation);
194 QVERIFY(sortedArrayData != nullptr);
176 QVERIFY(sortedArrayData != nullptr);
195 QVERIFY(sortedArrayData->data() == expectedData);
177 verifyArrayData(*sortedArrayData, expectedData);
196 }
178 }
197
179
198 QTEST_MAIN(TestOneDimArrayData)
180 QTEST_MAIN(TestOneDimArrayData)
@@ -2,15 +2,55
2 #include <QObject>
2 #include <QObject>
3 #include <QtTest>
3 #include <QtTest>
4
4
5 using DataContainer = QVector<QVector<double> >;
5 using Container = QVector<QVector<double> >;
6 using InputData = QPair<QVector<double>, int>;
7
8 namespace {
9
10 InputData flatten(const Container &container)
11 {
12 if (container.isEmpty()) {
13 return {};
14 }
15
16 // We assume here that each component of the container have the same size
17 auto containerSize = container.size();
18 auto componentSize = container.first().size();
19
20 auto result = QVector<double>{};
21 result.reserve(componentSize * containerSize);
22
23 for (auto i = 0; i < componentSize; ++i) {
24 for (auto j = 0; j < containerSize; ++j) {
25 result.append(container.at(j).at(i));
26 }
27 }
28
29 return {result, containerSize};
30 }
31
32 void verifyArrayData(const ArrayData<2> &arrayData, const Container &expectedData)
33 {
34 auto verifyComponent = [&arrayData](const auto &componentData, const auto &equalFun) {
35 QVERIFY(std::equal(arrayData.cbegin(), arrayData.cend(), componentData.cbegin(),
36 componentData.cend(),
37 [&equalFun](const auto &dataSeriesIt, const auto &expectedValue) {
38 return equalFun(dataSeriesIt, expectedValue);
39 }));
40 };
41
42 for (auto i = 0; i < expectedData.size(); ++i) {
43 verifyComponent(expectedData.at(i), [i](const auto &seriesIt, const auto &value) {
44 return seriesIt.at(i) == value;
45 });
46 }
47 }
48
49 } // namespace
6
50
7 class TestTwoDimArrayData : public QObject {
51 class TestTwoDimArrayData : public QObject {
8 Q_OBJECT
52 Q_OBJECT
9 private slots:
53 private slots:
10 /// Tests @sa ArrayData::data(int componentIndex)
11 void testDataByComponentIndex_data();
12 void testDataByComponentIndex();
13
14 /// Tests @sa ArrayData ctor
54 /// Tests @sa ArrayData ctor
15 void testCtor_data();
55 void testCtor_data();
16 void testCtor();
56 void testCtor();
@@ -32,192 +72,167 private slots:
32 void testSort();
72 void testSort();
33 };
73 };
34
74
35 void TestTwoDimArrayData::testDataByComponentIndex_data()
36 {
37 // Test structure
38 QTest::addColumn<DataContainer>("inputData"); // array data's input
39 QTest::addColumn<int>("componentIndex"); // component index to test
40 QTest::addColumn<QVector<double> >("expectedData"); // expected data
41
42 // Test cases
43 auto inputData
44 = DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}};
45
46 QTest::newRow("validIndex1") << inputData << 0 << QVector<double>{1., 2., 3., 4., 5.};
47 QTest::newRow("validIndex2") << inputData << 1 << QVector<double>{6., 7., 8., 9., 10.};
48 QTest::newRow("validIndex3") << inputData << 2 << QVector<double>{11., 12., 13., 14., 15.};
49 QTest::newRow("invalidIndex1") << inputData << -1 << QVector<double>{};
50 QTest::newRow("invalidIndex2") << inputData << 3 << QVector<double>{};
51 }
52
53 void TestTwoDimArrayData::testDataByComponentIndex()
54 {
55 QFETCH(DataContainer, inputData);
56 QFETCH(int, componentIndex);
57 QFETCH(QVector<double>, expectedData);
58
59 ArrayData<2> arrayData{inputData};
60 QVERIFY(arrayData.data(componentIndex) == expectedData);
61 }
62
63 void TestTwoDimArrayData::testCtor_data()
75 void TestTwoDimArrayData::testCtor_data()
64 {
76 {
65 // Test structure
77 // Test structure
66 QTest::addColumn<DataContainer>("inputData"); // array data's input
78 QTest::addColumn<InputData>("inputData"); // array data's input
67 QTest::addColumn<bool>("success"); // array data has been successfully constructed
79 QTest::addColumn<bool>("success"); // array data has been successfully constructed
68 QTest::addColumn<DataContainer>("expectedData"); // expected array data (when success)
80 QTest::addColumn<Container>("expectedData"); // expected array data (when success)
69
81
70 // Test cases
82 // Test cases
71 QTest::newRow("validInput")
83 QTest::newRow("validInput") << flatten(Container{{1., 2., 3., 4., 5.},
72 << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}
84 {6., 7., 8., 9., 10.},
73 << true
85 {11., 12., 13., 14., 15.}})
74 << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}};
86 << true << Container{{1., 2., 3., 4., 5.},
75 QTest::newRow("malformedInput (components of the array data haven't the same size")
87 {6., 7., 8., 9., 10.},
76 << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8.}, {11., 12.}} << true
88 {11., 12., 13., 14., 15.}};
77 << DataContainer{{}, {}, {}};
89 QTest::newRow("invalidInput (invalid data size")
78 QTest::newRow("invalidInput (less than tow components") << DataContainer{{1., 2., 3., 4., 5.}}
90 << InputData{{1., 2., 3., 4., 5., 6., 7.}, 3} << false << Container{{}, {}, {}};
79 << false << DataContainer{{}, {}, {}};
91 QTest::newRow("invalidInput (less than two components")
92 << flatten(Container{{1., 2., 3., 4., 5.}}) << false << Container{{}, {}, {}};
80 }
93 }
81
94
82 void TestTwoDimArrayData::testCtor()
95 void TestTwoDimArrayData::testCtor()
83 {
96 {
84 QFETCH(DataContainer, inputData);
97 QFETCH(InputData, inputData);
85 QFETCH(bool, success);
98 QFETCH(bool, success);
86
99
87 if (success) {
100 if (success) {
88 QFETCH(DataContainer, expectedData);
101 QFETCH(Container, expectedData);
89
90 ArrayData<2> arrayData{inputData};
91
102
92 for (auto i = 0; i < expectedData.size(); ++i) {
103 ArrayData<2> arrayData{inputData.first, inputData.second};
93 QVERIFY(arrayData.data(i) == expectedData.at(i));
104 verifyArrayData(arrayData, expectedData);
94 }
95 }
105 }
96 else {
106 else {
97 QVERIFY_EXCEPTION_THROWN(ArrayData<2> arrayData{inputData}, std::invalid_argument);
107 QVERIFY_EXCEPTION_THROWN(ArrayData<2>(inputData.first, inputData.second),
108 std::invalid_argument);
98 }
109 }
99 }
110 }
100
111
101 void TestTwoDimArrayData::testAdd_data()
112 void TestTwoDimArrayData::testAdd_data()
102 {
113 {
103 // Test structure
114 // Test structure
104 QTest::addColumn<DataContainer>("inputData"); // array's data input
115 QTest::addColumn<InputData>("inputData"); // array's data input
105 QTest::addColumn<DataContainer>("otherData"); // array data's input to merge with
116 QTest::addColumn<InputData>("otherData"); // array data's input to merge with
106 QTest::addColumn<bool>("prepend"); // prepend or append merge
117 QTest::addColumn<bool>("prepend"); // prepend or append merge
107 QTest::addColumn<DataContainer>("expectedData"); // expected data after merge
118 QTest::addColumn<Container>("expectedData"); // expected data after merge
108
119
109 // Test cases
120 // Test cases
110 auto inputData
121 auto inputData = flatten(
111 = DataContainer{{1., 2., 3., 4., 5.}, {11., 12., 13., 14., 15.}, {21., 22., 23., 24., 25.}};
122 Container{{1., 2., 3., 4., 5.}, {11., 12., 13., 14., 15.}, {21., 22., 23., 24., 25.}});
112
123
113 auto vectorContainer = DataContainer{{6., 7., 8.}, {16., 17., 18.}, {26., 27., 28}};
124 auto vectorContainer = flatten(Container{{6., 7., 8.}, {16., 17., 18.}, {26., 27., 28}});
114 auto tensorContainer = DataContainer{{6., 7., 8.}, {16., 17., 18.}, {26., 27., 28},
125 auto tensorContainer = flatten(Container{{6., 7., 8.},
115 {36., 37., 38.}, {46., 47., 48.}, {56., 57., 58}};
126 {16., 17., 18.},
127 {26., 27., 28},
128 {36., 37., 38.},
129 {46., 47., 48.},
130 {56., 57., 58}});
116
131
117 QTest::newRow("appendMerge") << inputData << vectorContainer << false
132 QTest::newRow("appendMerge") << inputData << vectorContainer << false
118 << DataContainer{{1., 2., 3., 4., 5., 6., 7., 8.},
133 << Container{{1., 2., 3., 4., 5., 6., 7., 8.},
119 {11., 12., 13., 14., 15., 16., 17., 18.},
134 {11., 12., 13., 14., 15., 16., 17., 18.},
120 {21., 22., 23., 24., 25., 26., 27., 28}};
135 {21., 22., 23., 24., 25., 26., 27., 28}};
121 QTest::newRow("prependMerge") << inputData << vectorContainer << true
136 QTest::newRow("prependMerge") << inputData << vectorContainer << true
122 << DataContainer{{6., 7., 8., 1., 2., 3., 4., 5.},
137 << Container{{6., 7., 8., 1., 2., 3., 4., 5.},
123 {16., 17., 18., 11., 12., 13., 14., 15.},
138 {16., 17., 18., 11., 12., 13., 14., 15.},
124 {26., 27., 28, 21., 22., 23., 24., 25.}};
139 {26., 27., 28, 21., 22., 23., 24., 25.}};
125 QTest::newRow("invalidMerge") << inputData << tensorContainer << false << inputData;
140 QTest::newRow("invalidMerge") << inputData << tensorContainer << false
141 << Container{{1., 2., 3., 4., 5.},
142 {11., 12., 13., 14., 15.},
143 {21., 22., 23., 24., 25.}};
126 }
144 }
127
145
128 void TestTwoDimArrayData::testAdd()
146 void TestTwoDimArrayData::testAdd()
129 {
147 {
130 QFETCH(DataContainer, inputData);
148 QFETCH(InputData, inputData);
131 QFETCH(DataContainer, otherData);
149 QFETCH(InputData, otherData);
132 QFETCH(bool, prepend);
150 QFETCH(bool, prepend);
133 QFETCH(DataContainer, expectedData);
151 QFETCH(Container, expectedData);
134
152
135 ArrayData<2> arrayData{inputData};
153 ArrayData<2> arrayData{inputData.first, inputData.second};
136 ArrayData<2> other{otherData};
154 ArrayData<2> other{otherData.first, otherData.second};
137
155
138 arrayData.add(other, prepend);
156 arrayData.add(other, prepend);
139
157
140 for (auto i = 0; i < expectedData.size(); ++i) {
158 verifyArrayData(arrayData, expectedData);
141 QVERIFY(arrayData.data(i) == expectedData.at(i));
142 }
143 }
159 }
144
160
145 void TestTwoDimArrayData::testClear_data()
161 void TestTwoDimArrayData::testClear_data()
146 {
162 {
147 // Test structure
163 // Test structure
148 QTest::addColumn<DataContainer>("inputData"); // array data's input
164 QTest::addColumn<InputData>("inputData"); // array data's input
149
165
150 // Test cases
166 // Test cases
151 QTest::newRow("data1") << DataContainer{
167 QTest::newRow("data1") << flatten(
152 {1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}};
168 Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}});
153 }
169 }
154
170
155 void TestTwoDimArrayData::testClear()
171 void TestTwoDimArrayData::testClear()
156 {
172 {
157 QFETCH(DataContainer, inputData);
173 QFETCH(InputData, inputData);
158
174
159 ArrayData<2> arrayData{inputData};
175 ArrayData<2> arrayData{inputData.first, inputData.second};
160 arrayData.clear();
176 arrayData.clear();
161
177
162 for (auto i = 0; i < inputData.size(); ++i) {
178 auto emptyData = Container(inputData.second, QVector<double>{});
163 QVERIFY(arrayData.data(i) == QVector<double>{});
179 verifyArrayData(arrayData, emptyData);
164 }
165 }
180 }
166
181
167 void TestTwoDimArrayData::testSize_data()
182 void TestTwoDimArrayData::testSize_data()
168 {
183 {
169 // Test structure
184 // Test structure
170 QTest::addColumn<QVector<QVector<double> > >("inputData"); // array data's input
185 QTest::addColumn<InputData>("inputData"); // array data's input
171 QTest::addColumn<int>("expectedSize"); // expected array data size
186 QTest::addColumn<int>("expectedSize"); // expected array data size
172
187
173 // Test cases
188 // Test cases
174 QTest::newRow("data1") << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}} << 5;
189 QTest::newRow("data1") << flatten(Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}}) << 5;
175 QTest::newRow("data2") << DataContainer{{1., 2., 3., 4., 5.},
190 QTest::newRow("data2") << flatten(Container{{1., 2., 3., 4., 5.},
176 {6., 7., 8., 9., 10.},
191 {6., 7., 8., 9., 10.},
177 {11., 12., 13., 14., 15.}}
192 {11., 12., 13., 14., 15.}})
178 << 5;
193 << 5;
179 }
194 }
180
195
181 void TestTwoDimArrayData::testSize()
196 void TestTwoDimArrayData::testSize()
182 {
197 {
183 QFETCH(DataContainer, inputData);
198 QFETCH(InputData, inputData);
184 QFETCH(int, expectedSize);
199 QFETCH(int, expectedSize);
185
200
186 ArrayData<2> arrayData{inputData};
201 ArrayData<2> arrayData{inputData.first, inputData.second};
187 QVERIFY(arrayData.size() == expectedSize);
202 QVERIFY(arrayData.size() == expectedSize);
188 }
203 }
189
204
190 void TestTwoDimArrayData::testSort_data()
205 void TestTwoDimArrayData::testSort_data()
191 {
206 {
192 // Test structure
207 // Test structure
193 QTest::addColumn<DataContainer>("inputData"); // array data's input
208 QTest::addColumn<InputData>("inputData"); // array data's input
194 QTest::addColumn<std::vector<int> >("sortPermutation"); // permutation used to sort data
209 QTest::addColumn<std::vector<int> >("sortPermutation"); // permutation used to sort data
195 QTest::addColumn<DataContainer>("expectedData"); // expected data after sorting
210 QTest::addColumn<Container>("expectedData"); // expected data after sorting
196
211
197 // Test cases
212 // Test cases
198 QTest::newRow("data1")
213 QTest::newRow("data1")
199 << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}
214 << flatten(
215 Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}})
200 << std::vector<int>{0, 2, 3, 1, 4}
216 << std::vector<int>{0, 2, 3, 1, 4}
201 << DataContainer{{1., 3., 4., 2., 5.}, {6., 8., 9., 7., 10.}, {11., 13., 14., 12., 15.}};
217 << Container{{1., 3., 4., 2., 5.}, {6., 8., 9., 7., 10.}, {11., 13., 14., 12., 15.}};
202 QTest::newRow("data2")
218 QTest::newRow("data2")
203 << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}
219 << flatten(
220 Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}})
204 << std::vector<int>{2, 4, 3, 0, 1}
221 << std::vector<int>{2, 4, 3, 0, 1}
205 << DataContainer{{3., 5., 4., 1., 2.}, {8., 10., 9., 6., 7.}, {13., 15., 14., 11., 12.}};
222 << Container{{3., 5., 4., 1., 2.}, {8., 10., 9., 6., 7.}, {13., 15., 14., 11., 12.}};
206 }
223 }
207
224
208 void TestTwoDimArrayData::testSort()
225 void TestTwoDimArrayData::testSort()
209 {
226 {
210 QFETCH(DataContainer, inputData);
227 QFETCH(InputData, inputData);
211 QFETCH(std::vector<int>, sortPermutation);
228 QFETCH(std::vector<int>, sortPermutation);
212 QFETCH(DataContainer, expectedData);
229 QFETCH(Container, expectedData);
213
230
214 ArrayData<2> arrayData{inputData};
231 ArrayData<2> arrayData{inputData.first, inputData.second};
215 auto sortedArrayData = arrayData.sort(sortPermutation);
232 auto sortedArrayData = arrayData.sort(sortPermutation);
216 QVERIFY(sortedArrayData != nullptr);
233 QVERIFY(sortedArrayData != nullptr);
217
234
218 for (auto i = 0; i < expectedData.size(); ++i) {
235 verifyArrayData(*sortedArrayData, expectedData);
219 QVERIFY(sortedArrayData->data(i) == expectedData.at(i));
220 }
221 }
236 }
222
237
223 QTEST_MAIN(TestTwoDimArrayData)
238 QTEST_MAIN(TestTwoDimArrayData)
@@ -17,51 +17,6 QDateTime dateTime(int year, int month, int day, int hours, int minutes, int sec
17 return QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC};
17 return QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC};
18 }
18 }
19
19
20 /// Compares two vectors that can potentially contain NaN values
21 bool compareVectors(const QVector<double> &v1, const QVector<double> &v2)
22 {
23 if (v1.size() != v2.size()) {
24 return false;
25 }
26
27 auto result = true;
28 auto v2It = v2.cbegin();
29 for (auto v1It = v1.cbegin(), v1End = v1.cend(); v1It != v1End && result; ++v1It, ++v2It) {
30 auto v1Value = *v1It;
31 auto v2Value = *v2It;
32
33 // If v1 is NaN, v2 has to be NaN too
34 result = std::isnan(v1Value) ? std::isnan(v2Value) : (v1Value == v2Value);
35 }
36
37 return result;
38 }
39
40 bool compareVectors(const QVector<QVector<double> > &v1, const QVector<QVector<double> > &v2)
41 {
42 if (v1.size() != v2.size()) {
43 return false;
44 }
45
46 auto result = true;
47 for (auto i = 0; i < v1.size() && result; ++i) {
48 result &= compareVectors(v1.at(i), v2.at(i));
49 }
50
51 return result;
52 }
53
54 QVector<QVector<double> > valuesData(const ArrayData<1> &arrayData)
55 {
56 return QVector<QVector<double> >{arrayData.data()};
57 }
58
59 QVector<QVector<double> > valuesData(const ArrayData<2> &arrayData)
60 {
61 return arrayData.data();
62 }
63
64
65 QString inputFilePath(const QString &inputFileName)
20 QString inputFilePath(const QString &inputFileName)
66 {
21 {
67 return QFileInfo{TESTS_RESOURCES_PATH, inputFileName}.absoluteFilePath();
22 return QFileInfo{TESTS_RESOURCES_PATH, inputFileName}.absoluteFilePath();
@@ -106,10 +61,26 struct ExpectedResults {
106 QVERIFY(dataSeries->xAxisUnit() == m_XAxisUnit);
61 QVERIFY(dataSeries->xAxisUnit() == m_XAxisUnit);
107 QVERIFY(dataSeries->valuesUnit() == m_ValuesUnit);
62 QVERIFY(dataSeries->valuesUnit() == m_ValuesUnit);
108
63
109 // Checks values : as the vectors can potentially contain NaN values, we must use a
64 auto verifyRange = [dataSeries](const auto &expectedData, const auto &equalFun) {
110 // custom vector comparison method
65 QVERIFY(std::equal(dataSeries->cbegin(), dataSeries->cend(), expectedData.cbegin(),
111 QVERIFY(compareVectors(dataSeries->xAxisData()->data(), m_XAxisData));
66 expectedData.cend(),
112 QVERIFY(compareVectors(valuesData(*dataSeries->valuesData()), m_ValuesData));
67 [&equalFun](const auto &dataSeriesIt, const auto &expectedX) {
68 return equalFun(dataSeriesIt, expectedX);
69 }));
70 };
71
72 // Checks x-axis data
73 verifyRange(m_XAxisData, [](const auto &seriesIt, const auto &value) {
74 return seriesIt.x() == value;
75 });
76
77 // Checks values data of each component
78 for (auto i = 0; i < m_ValuesData.size(); ++i) {
79 verifyRange(m_ValuesData.at(i), [i](const auto &seriesIt, const auto &value) {
80 auto itValue = seriesIt.value(i);
81 return (std::isnan(itValue) && std::isnan(value)) || seriesIt.value(i) == value;
82 });
83 }
113 }
84 }
114 else {
85 else {
115 QVERIFY(results == nullptr);
86 QVERIFY(results == nullptr);
General Comments 0
You need to be logged in to leave comments. Login now