##// END OF EJS Templates
Merge branch 'feature/UpdateArrayDataStruct' into develop
Merge branch 'feature/UpdateArrayDataStruct' into develop

File last commit:

r604:da7f81670868
r605:340220774853 merge
Show More
TestTwoDimArrayData.cpp
239 lines | 8.4 KiB | text/x-c | CppLexer
/ core / tests / Data / TestTwoDimArrayData.cpp
Alexandre Leroux
Inits unit tests for 1-dim and 2-dim array data
r473 #include "Data/ArrayData.h"
#include <QObject>
#include <QtTest>
Alexandre Leroux
Updates unit tests
r604 using Container = QVector<QVector<double> >;
using InputData = QPair<QVector<double>, int>;
namespace {
InputData flatten(const Container &container)
{
if (container.isEmpty()) {
return {};
}
// We assume here that each component of the container have the same size
auto containerSize = container.size();
auto componentSize = container.first().size();
auto result = QVector<double>{};
result.reserve(componentSize * containerSize);
for (auto i = 0; i < componentSize; ++i) {
for (auto j = 0; j < containerSize; ++j) {
result.append(container.at(j).at(i));
}
}
return {result, containerSize};
}
void verifyArrayData(const ArrayData<2> &arrayData, const Container &expectedData)
{
auto verifyComponent = [&arrayData](const auto &componentData, const auto &equalFun) {
QVERIFY(std::equal(arrayData.cbegin(), arrayData.cend(), componentData.cbegin(),
componentData.cend(),
[&equalFun](const auto &dataSeriesIt, const auto &expectedValue) {
return equalFun(dataSeriesIt, expectedValue);
}));
};
for (auto i = 0; i < expectedData.size(); ++i) {
verifyComponent(expectedData.at(i), [i](const auto &seriesIt, const auto &value) {
return seriesIt.at(i) == value;
});
}
}
} // namespace
Alexandre Leroux
Inits unit tests for 1-dim and 2-dim array data
r473
class TestTwoDimArrayData : public QObject {
Q_OBJECT
private slots:
Alexandre Leroux
Tests 2-dim ctor
r479 /// Tests @sa ArrayData ctor
void testCtor_data();
void testCtor();
Alexandre Leroux
Tests add() method
r475 /// Tests @sa ArrayData::add()
void testAdd_data();
void testAdd();
Alexandre Leroux
Tests clear() and size() methods
r476 /// Tests @sa ArrayData::clear()
void testClear_data();
void testClear();
/// Tests @sa ArrayData::size()
void testSize_data();
void testSize();
Alexandre Leroux
Tests sort() method
r477 /// Tests @sa ArrayData::sort()
void testSort_data();
void testSort();
Alexandre Leroux
Inits unit tests for 1-dim and 2-dim array data
r473 };
Alexandre Leroux
Tests 2-dim ctor
r479 void TestTwoDimArrayData::testCtor_data()
{
// Test structure
Alexandre Leroux
Updates unit tests
r604 QTest::addColumn<InputData>("inputData"); // array data's input
QTest::addColumn<bool>("success"); // array data has been successfully constructed
QTest::addColumn<Container>("expectedData"); // expected array data (when success)
Alexandre Leroux
Tests 2-dim ctor
r479
// Test cases
Alexandre Leroux
Updates unit tests
r604 QTest::newRow("validInput") << flatten(Container{{1., 2., 3., 4., 5.},
{6., 7., 8., 9., 10.},
{11., 12., 13., 14., 15.}})
<< true << Container{{1., 2., 3., 4., 5.},
{6., 7., 8., 9., 10.},
{11., 12., 13., 14., 15.}};
QTest::newRow("invalidInput (invalid data size")
<< InputData{{1., 2., 3., 4., 5., 6., 7.}, 3} << false << Container{{}, {}, {}};
QTest::newRow("invalidInput (less than two components")
<< flatten(Container{{1., 2., 3., 4., 5.}}) << false << Container{{}, {}, {}};
Alexandre Leroux
Tests 2-dim ctor
r479 }
void TestTwoDimArrayData::testCtor()
{
Alexandre Leroux
Updates unit tests
r604 QFETCH(InputData, inputData);
Alexandre Leroux
Tests 2-dim ctor
r479 QFETCH(bool, success);
if (success) {
Alexandre Leroux
Updates unit tests
r604 QFETCH(Container, expectedData);
Alexandre Leroux
Tests 2-dim ctor
r479
Alexandre Leroux
Updates unit tests
r604 ArrayData<2> arrayData{inputData.first, inputData.second};
verifyArrayData(arrayData, expectedData);
Alexandre Leroux
Tests 2-dim ctor
r479 }
else {
Alexandre Leroux
Updates unit tests
r604 QVERIFY_EXCEPTION_THROWN(ArrayData<2>(inputData.first, inputData.second),
std::invalid_argument);
Alexandre Leroux
Tests 2-dim ctor
r479 }
}
Alexandre Leroux
Tests add() method
r475 void TestTwoDimArrayData::testAdd_data()
{
// Test structure
Alexandre Leroux
Updates unit tests
r604 QTest::addColumn<InputData>("inputData"); // array's data input
QTest::addColumn<InputData>("otherData"); // array data's input to merge with
QTest::addColumn<bool>("prepend"); // prepend or append merge
QTest::addColumn<Container>("expectedData"); // expected data after merge
Alexandre Leroux
Tests add() method
r475
// Test cases
Alexandre Leroux
Updates unit tests
r604 auto inputData = flatten(
Container{{1., 2., 3., 4., 5.}, {11., 12., 13., 14., 15.}, {21., 22., 23., 24., 25.}});
Alexandre Leroux
Tests add() method
r475
Alexandre Leroux
Updates unit tests
r604 auto vectorContainer = flatten(Container{{6., 7., 8.}, {16., 17., 18.}, {26., 27., 28}});
auto tensorContainer = flatten(Container{{6., 7., 8.},
{16., 17., 18.},
{26., 27., 28},
{36., 37., 38.},
{46., 47., 48.},
{56., 57., 58}});
Alexandre Leroux
Tests add() method
r475
QTest::newRow("appendMerge") << inputData << vectorContainer << false
Alexandre Leroux
Updates unit tests
r604 << Container{{1., 2., 3., 4., 5., 6., 7., 8.},
{11., 12., 13., 14., 15., 16., 17., 18.},
{21., 22., 23., 24., 25., 26., 27., 28}};
Alexandre Leroux
Tests add() method
r475 QTest::newRow("prependMerge") << inputData << vectorContainer << true
Alexandre Leroux
Updates unit tests
r604 << Container{{6., 7., 8., 1., 2., 3., 4., 5.},
{16., 17., 18., 11., 12., 13., 14., 15.},
{26., 27., 28, 21., 22., 23., 24., 25.}};
QTest::newRow("invalidMerge") << inputData << tensorContainer << false
<< Container{{1., 2., 3., 4., 5.},
{11., 12., 13., 14., 15.},
{21., 22., 23., 24., 25.}};
Alexandre Leroux
Tests add() method
r475 }
void TestTwoDimArrayData::testAdd()
{
Alexandre Leroux
Updates unit tests
r604 QFETCH(InputData, inputData);
QFETCH(InputData, otherData);
Alexandre Leroux
Tests add() method
r475 QFETCH(bool, prepend);
Alexandre Leroux
Updates unit tests
r604 QFETCH(Container, expectedData);
Alexandre Leroux
Tests add() method
r475
Alexandre Leroux
Updates unit tests
r604 ArrayData<2> arrayData{inputData.first, inputData.second};
ArrayData<2> other{otherData.first, otherData.second};
Alexandre Leroux
Tests add() method
r475
arrayData.add(other, prepend);
Alexandre Leroux
Updates unit tests
r604 verifyArrayData(arrayData, expectedData);
Alexandre Leroux
Tests add() method
r475 }
Alexandre Leroux
Tests clear() and size() methods
r476 void TestTwoDimArrayData::testClear_data()
{
// Test structure
Alexandre Leroux
Updates unit tests
r604 QTest::addColumn<InputData>("inputData"); // array data's input
Alexandre Leroux
Tests clear() and size() methods
r476
// Test cases
Alexandre Leroux
Updates unit tests
r604 QTest::newRow("data1") << flatten(
Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}});
Alexandre Leroux
Tests clear() and size() methods
r476 }
void TestTwoDimArrayData::testClear()
{
Alexandre Leroux
Updates unit tests
r604 QFETCH(InputData, inputData);
Alexandre Leroux
Tests clear() and size() methods
r476
Alexandre Leroux
Updates unit tests
r604 ArrayData<2> arrayData{inputData.first, inputData.second};
Alexandre Leroux
Tests clear() and size() methods
r476 arrayData.clear();
Alexandre Leroux
Updates unit tests
r604 auto emptyData = Container(inputData.second, QVector<double>{});
verifyArrayData(arrayData, emptyData);
Alexandre Leroux
Tests clear() and size() methods
r476 }
void TestTwoDimArrayData::testSize_data()
{
// Test structure
Alexandre Leroux
Updates unit tests
r604 QTest::addColumn<InputData>("inputData"); // array data's input
QTest::addColumn<int>("expectedSize"); // expected array data size
Alexandre Leroux
Tests clear() and size() methods
r476
// Test cases
Alexandre Leroux
Updates unit tests
r604 QTest::newRow("data1") << flatten(Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}}) << 5;
QTest::newRow("data2") << flatten(Container{{1., 2., 3., 4., 5.},
{6., 7., 8., 9., 10.},
{11., 12., 13., 14., 15.}})
Alexandre Leroux
Tests clear() and size() methods
r476 << 5;
}
void TestTwoDimArrayData::testSize()
{
Alexandre Leroux
Updates unit tests
r604 QFETCH(InputData, inputData);
Alexandre Leroux
Tests clear() and size() methods
r476 QFETCH(int, expectedSize);
Alexandre Leroux
Updates unit tests
r604 ArrayData<2> arrayData{inputData.first, inputData.second};
Alexandre Leroux
Tests clear() and size() methods
r476 QVERIFY(arrayData.size() == expectedSize);
}
Alexandre Leroux
Tests sort() method
r477 void TestTwoDimArrayData::testSort_data()
{
// Test structure
Alexandre Leroux
Updates unit tests
r604 QTest::addColumn<InputData>("inputData"); // array data's input
Alexandre Leroux
Tests sort() method
r477 QTest::addColumn<std::vector<int> >("sortPermutation"); // permutation used to sort data
Alexandre Leroux
Updates unit tests
r604 QTest::addColumn<Container>("expectedData"); // expected data after sorting
Alexandre Leroux
Tests sort() method
r477
// Test cases
QTest::newRow("data1")
Alexandre Leroux
Updates unit tests
r604 << flatten(
Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}})
Alexandre Leroux
Tests sort() method
r477 << std::vector<int>{0, 2, 3, 1, 4}
Alexandre Leroux
Updates unit tests
r604 << Container{{1., 3., 4., 2., 5.}, {6., 8., 9., 7., 10.}, {11., 13., 14., 12., 15.}};
Alexandre Leroux
Tests sort() method
r477 QTest::newRow("data2")
Alexandre Leroux
Updates unit tests
r604 << flatten(
Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}})
Alexandre Leroux
Tests sort() method
r477 << std::vector<int>{2, 4, 3, 0, 1}
Alexandre Leroux
Updates unit tests
r604 << Container{{3., 5., 4., 1., 2.}, {8., 10., 9., 6., 7.}, {13., 15., 14., 11., 12.}};
Alexandre Leroux
Tests sort() method
r477 }
void TestTwoDimArrayData::testSort()
{
Alexandre Leroux
Updates unit tests
r604 QFETCH(InputData, inputData);
Alexandre Leroux
Tests sort() method
r477 QFETCH(std::vector<int>, sortPermutation);
Alexandre Leroux
Updates unit tests
r604 QFETCH(Container, expectedData);
Alexandre Leroux
Tests sort() method
r477
Alexandre Leroux
Updates unit tests
r604 ArrayData<2> arrayData{inputData.first, inputData.second};
Alexandre Leroux
Tests sort() method
r477 auto sortedArrayData = arrayData.sort(sortPermutation);
Alexandre Leroux
Fix compilation on Linux
r496 QVERIFY(sortedArrayData != nullptr);
Alexandre Leroux
Tests sort() method
r477
Alexandre Leroux
Updates unit tests
r604 verifyArrayData(*sortedArrayData, expectedData);
Alexandre Leroux
Tests sort() method
r477 }
Alexandre Leroux
Inits unit tests for 1-dim and 2-dim array data
r473 QTEST_MAIN(TestTwoDimArrayData)
#include "TestTwoDimArrayData.moc"