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