##// END OF EJS Templates
Merge branch 'feature/ArrayDataTests' into develop
Alexandre Leroux -
r521:09ae4b54cb70 merge
parent child
Show More
@@ -0,0 +1,199
1 #include "Data/ArrayData.h"
2 #include <QObject>
3 #include <QtTest>
4
5 class TestOneDimArrayData : public QObject {
6 Q_OBJECT
7 private slots:
8 /// Tests @sa ArrayData::data()
9 void testData_data();
10 void testData();
11
12 /// Tests @sa ArrayData::data(int componentIndex)
13 void testDataByComponentIndex_data();
14 void testDataByComponentIndex();
15
16 /// Tests @sa ArrayData::add()
17 void testAdd_data();
18 void testAdd();
19
20 /// Tests @sa ArrayData::at(int index)
21 void testAt_data();
22 void testAt();
23
24 /// Tests @sa ArrayData::clear()
25 void testClear_data();
26 void testClear();
27
28 /// Tests @sa ArrayData::size()
29 void testSize_data();
30 void testSize();
31
32 /// Tests @sa ArrayData::sort()
33 void testSort_data();
34 void testSort();
35 };
36
37 void TestOneDimArrayData::testData_data()
38 {
39 // Test structure
40 QTest::addColumn<QVector<double> >("inputData"); // array's data input
41 QTest::addColumn<QVector<double> >("expectedData"); // expected data
42
43 // Test cases
44 QTest::newRow("data1") << QVector<double>{1., 2., 3., 4., 5.}
45 << QVector<double>{1., 2., 3., 4., 5.};
46 }
47
48 void TestOneDimArrayData::testData()
49 {
50 QFETCH(QVector<double>, inputData);
51 QFETCH(QVector<double>, expectedData);
52
53 ArrayData<1> arrayData{inputData};
54 QVERIFY(arrayData.data() == 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")
68 << QVector<double>{1., 2., 3., 4., 5.} << -1 << 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 }
81
82 void TestOneDimArrayData::testAdd_data()
83 {
84 // Test structure
85 QTest::addColumn<QVector<double> >("inputData"); // array's data input
86 QTest::addColumn<QVector<double> >("otherData"); // array data's input to merge with
87 QTest::addColumn<bool>("prepend"); // prepend or append merge
88 QTest::addColumn<QVector<double> >("expectedData"); // expected data after merge
89
90 // Test cases
91 QTest::newRow("appendMerge") << QVector<double>{1., 2., 3., 4., 5.}
92 << QVector<double>{6., 7., 8.} << false
93 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8.};
94 QTest::newRow("prependMerge") << QVector<double>{1., 2., 3., 4., 5.}
95 << QVector<double>{6., 7., 8.} << true
96 << QVector<double>{6., 7., 8., 1., 2., 3., 4., 5.};
97 }
98
99 void TestOneDimArrayData::testAdd()
100 {
101 QFETCH(QVector<double>, inputData);
102 QFETCH(QVector<double>, otherData);
103 QFETCH(bool, prepend);
104 QFETCH(QVector<double>, expectedData);
105
106 ArrayData<1> arrayData{inputData};
107 ArrayData<1> other{otherData};
108
109 arrayData.add(other, prepend);
110 QVERIFY(arrayData.data() == expectedData);
111 }
112
113 void TestOneDimArrayData::testAt_data()
114 {
115 // Test structure
116 QTest::addColumn<QVector<double> >("inputData"); // array data's input
117 QTest::addColumn<int>("index"); // index to retrieve data
118 QTest::addColumn<double>("expectedData"); // expected data at index
119
120 // Test cases
121 QTest::newRow("data1") << QVector<double>{1., 2., 3., 4., 5.} << 0 << 1.;
122 QTest::newRow("data2") << QVector<double>{1., 2., 3., 4., 5.} << 3 << 4.;
123 }
124
125 void TestOneDimArrayData::testAt()
126 {
127 QFETCH(QVector<double>, inputData);
128 QFETCH(int, index);
129 QFETCH(double, expectedData);
130
131 ArrayData<1> arrayData{inputData};
132 QVERIFY(arrayData.at(index) == expectedData);
133 }
134
135 void TestOneDimArrayData::testClear_data()
136 {
137 // Test structure
138 QTest::addColumn<QVector<double> >("inputData"); // array data's input
139
140 // Test cases
141 QTest::newRow("data1") << QVector<double>{1., 2., 3., 4., 5.};
142 }
143
144 void TestOneDimArrayData::testClear()
145 {
146 QFETCH(QVector<double>, inputData);
147
148 ArrayData<1> arrayData{inputData};
149 arrayData.clear();
150 QVERIFY(arrayData.data() == QVector<double>{});
151 }
152
153 void TestOneDimArrayData::testSize_data()
154 {
155 // Test structure
156 QTest::addColumn<QVector<double> >("inputData"); // array data's input
157 QTest::addColumn<int>("expectedSize"); // expected array data size
158
159 // Test cases
160 QTest::newRow("data1") << QVector<double>{1., 2., 3., 4., 5.} << 5;
161 }
162
163 void TestOneDimArrayData::testSize()
164 {
165 QFETCH(QVector<double>, inputData);
166 QFETCH(int, expectedSize);
167
168 ArrayData<1> arrayData{inputData};
169 QVERIFY(arrayData.size() == expectedSize);
170 }
171
172 void TestOneDimArrayData::testSort_data()
173 {
174 // Test structure
175 QTest::addColumn<QVector<double> >("inputData"); // array data's input
176 QTest::addColumn<std::vector<int> >("sortPermutation"); // permutation used to sort data
177 QTest::addColumn<QVector<double> >("expectedData"); // expected data after sorting
178
179 // Test cases
180 QTest::newRow("data1") << QVector<double>{1., 2., 3., 4., 5.} << std::vector<int>{0, 2, 3, 1, 4}
181 << QVector<double>{1., 3., 4., 2., 5.};
182 QTest::newRow("data2") << QVector<double>{1., 2., 3., 4., 5.} << std::vector<int>{4, 1, 2, 3, 0}
183 << QVector<double>{5., 2., 3., 4., 1.};
184 }
185
186 void TestOneDimArrayData::testSort()
187 {
188 QFETCH(QVector<double>, inputData);
189 QFETCH(std::vector<int>, sortPermutation);
190 QFETCH(QVector<double>, expectedData);
191
192 ArrayData<1> arrayData{inputData};
193 auto sortedArrayData = arrayData.sort(sortPermutation);
194 QVERIFY(sortedArrayData);
195 QVERIFY(sortedArrayData->data() == expectedData);
196 }
197
198 QTEST_MAIN(TestOneDimArrayData)
199 #include "TestOneDimArrayData.moc"
@@ -0,0 +1,224
1 #include "Data/ArrayData.h"
2 #include <QObject>
3 #include <QtTest>
4
5 using DataContainer = QVector<QVector<double> >;
6
7 class TestTwoDimArrayData : public QObject {
8 Q_OBJECT
9 private slots:
10 /// Tests @sa ArrayData::data(int componentIndex)
11 void testDataByComponentIndex_data();
12 void testDataByComponentIndex();
13
14 /// Tests @sa ArrayData ctor
15 void testCtor_data();
16 void testCtor();
17
18 /// Tests @sa ArrayData::add()
19 void testAdd_data();
20 void testAdd();
21
22 /// Tests @sa ArrayData::clear()
23 void testClear_data();
24 void testClear();
25
26 /// Tests @sa ArrayData::size()
27 void testSize_data();
28 void testSize();
29
30 /// Tests @sa ArrayData::sort()
31 void testSort_data();
32 void testSort();
33 };
34
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()
64 {
65 // Test structure
66 QTest::addColumn<DataContainer>("inputData"); // array data's input
67 QTest::addColumn<bool>("success"); // array data has been successfully constructed
68 QTest::addColumn<DataContainer>("expectedData"); // expected array data (when success)
69
70 // Test cases
71 QTest::newRow("validInput")
72 << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}
73 << true
74 << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}};
75 QTest::newRow("malformedInput (components of the array data haven't the same size")
76 << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8.}, {11., 12.}} << true
77 << DataContainer{{}, {}, {}};
78 QTest::newRow("invalidInput (less than tow components")
79 << DataContainer{{1., 2., 3., 4., 5.}} << false << DataContainer{{}, {}, {}};
80 }
81
82 void TestTwoDimArrayData::testCtor()
83 {
84 QFETCH(DataContainer, inputData);
85 QFETCH(bool, success);
86
87 if (success) {
88 QFETCH(DataContainer, expectedData);
89
90 ArrayData<2> arrayData{inputData};
91
92 for (auto i = 0; i < expectedData.size(); ++i) {
93 QVERIFY(arrayData.data(i) == expectedData.at(i));
94 }
95 }
96 else {
97 QVERIFY_EXCEPTION_THROWN(ArrayData<2> arrayData{inputData}, std::invalid_argument);
98 }
99 }
100
101 void TestTwoDimArrayData::testAdd_data()
102 {
103 // Test structure
104 QTest::addColumn<DataContainer>("inputData"); // array's data input
105 QTest::addColumn<DataContainer>("otherData"); // array data's input to merge with
106 QTest::addColumn<bool>("prepend"); // prepend or append merge
107 QTest::addColumn<DataContainer>("expectedData"); // expected data after merge
108
109 // Test cases
110 auto inputData
111 = DataContainer{{1., 2., 3., 4., 5.}, {11., 12., 13., 14., 15.}, {21., 22., 23., 24., 25.}};
112
113 auto vectorContainer = DataContainer{{6., 7., 8.}, {16., 17., 18.}, {26., 27., 28}};
114 auto tensorContainer = DataContainer{{6., 7., 8.}, {16., 17., 18.}, {26., 27., 28},
115 {36., 37., 38.}, {46., 47., 48.}, {56., 57., 58}};
116
117 QTest::newRow("appendMerge") << inputData << vectorContainer << false
118 << DataContainer{{1., 2., 3., 4., 5., 6., 7., 8.},
119 {11., 12., 13., 14., 15., 16., 17., 18.},
120 {21., 22., 23., 24., 25., 26., 27., 28}};
121 QTest::newRow("prependMerge") << inputData << vectorContainer << true
122 << DataContainer{{6., 7., 8., 1., 2., 3., 4., 5.},
123 {16., 17., 18., 11., 12., 13., 14., 15.},
124 {26., 27., 28, 21., 22., 23., 24., 25.}};
125 QTest::newRow("invalidMerge") << inputData << tensorContainer << false << inputData;
126 }
127
128 void TestTwoDimArrayData::testAdd()
129 {
130 QFETCH(DataContainer, inputData);
131 QFETCH(DataContainer, otherData);
132 QFETCH(bool, prepend);
133 QFETCH(DataContainer, expectedData);
134
135 ArrayData<2> arrayData{inputData};
136 ArrayData<2> other{otherData};
137
138 arrayData.add(other, prepend);
139
140 for (auto i = 0; i < expectedData.size(); ++i) {
141 QVERIFY(arrayData.data(i) == expectedData.at(i));
142 }
143 }
144
145 void TestTwoDimArrayData::testClear_data()
146 {
147 // Test structure
148 QTest::addColumn<DataContainer>("inputData"); // array data's input
149
150 // Test cases
151 QTest::newRow("data1") << DataContainer{
152 {1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}};
153 }
154
155 void TestTwoDimArrayData::testClear()
156 {
157 QFETCH(DataContainer, inputData);
158
159 ArrayData<2> arrayData{inputData};
160 arrayData.clear();
161
162 for (auto i = 0; i < inputData.size(); ++i) {
163 QVERIFY(arrayData.data(i) == QVector<double>{});
164 }
165 }
166
167 void TestTwoDimArrayData::testSize_data()
168 {
169 // Test structure
170 QTest::addColumn<QVector<QVector<double> > >("inputData"); // array data's input
171 QTest::addColumn<int>("expectedSize"); // expected array data size
172
173 // Test cases
174 QTest::newRow("data1") << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}} << 5;
175 QTest::newRow("data2") << DataContainer{{1., 2., 3., 4., 5.},
176 {6., 7., 8., 9., 10.},
177 {11., 12., 13., 14., 15.}}
178 << 5;
179 }
180
181 void TestTwoDimArrayData::testSize()
182 {
183 QFETCH(DataContainer, inputData);
184 QFETCH(int, expectedSize);
185
186 ArrayData<2> arrayData{inputData};
187 QVERIFY(arrayData.size() == expectedSize);
188 }
189
190 void TestTwoDimArrayData::testSort_data()
191 {
192 // Test structure
193 QTest::addColumn<DataContainer>("inputData"); // array data's input
194 QTest::addColumn<std::vector<int> >("sortPermutation"); // permutation used to sort data
195 QTest::addColumn<DataContainer>("expectedData"); // expected data after sorting
196
197 // Test cases
198 QTest::newRow("data1")
199 << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}
200 << 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.}};
202 QTest::newRow("data2")
203 << DataContainer{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}
204 << 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.}};
206 }
207
208 void TestTwoDimArrayData::testSort()
209 {
210 QFETCH(DataContainer, inputData);
211 QFETCH(std::vector<int>, sortPermutation);
212 QFETCH(DataContainer, expectedData);
213
214 ArrayData<2> arrayData{inputData};
215 auto sortedArrayData = arrayData.sort(sortPermutation);
216 QVERIFY(sortedArrayData);
217
218 for (auto i = 0; i < expectedData.size(); ++i) {
219 QVERIFY(sortedArrayData->data(i) == expectedData.at(i));
220 }
221 }
222
223 QTEST_MAIN(TestTwoDimArrayData)
224 #include "TestTwoDimArrayData.moc"
General Comments 0
You need to be logged in to leave comments. Login now