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