TestTwoDimArrayData.cpp
240 lines
| 8.5 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r514 | #include "Data/ArrayData.h" | ||
#include <QObject> | ||||
#include <QtTest> | ||||
Alexandre Leroux
|
r695 | using DataContainer = std::vector<double>; | ||
using Container = std::vector<DataContainer>; | ||||
using InputData = QPair<DataContainer, int>; | ||||
Alexandre Leroux
|
r647 | |||
namespace { | ||||
InputData flatten(const Container &container) | ||||
{ | ||||
Alexandre Leroux
|
r695 | if (container.empty()) { | ||
Alexandre Leroux
|
r647 | return {}; | ||
} | ||||
// We assume here that each component of the container have the same size | ||||
auto containerSize = container.size(); | ||||
Alexandre Leroux
|
r695 | auto componentSize = container.front().size(); | ||
Alexandre Leroux
|
r647 | |||
Alexandre Leroux
|
r695 | auto result = DataContainer{}; | ||
Alexandre Leroux
|
r647 | result.reserve(componentSize * containerSize); | ||
Alexandre Leroux
|
r961 | for (auto i = 0u; i < componentSize; ++i) { | ||
for (auto j = 0u; j < containerSize; ++j) { | ||||
Alexandre Leroux
|
r695 | result.push_back(container.at(j).at(i)); | ||
Alexandre Leroux
|
r647 | } | ||
} | ||||
Alexandre Leroux
|
r695 | return {result, static_cast<int>(containerSize)}; | ||
Alexandre Leroux
|
r647 | } | ||
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); | ||||
})); | ||||
}; | ||||
Alexandre Leroux
|
r961 | for (auto i = 0u; i < expectedData.size(); ++i) { | ||
Alexandre Leroux
|
r647 | verifyComponent(expectedData.at(i), [i](const auto &seriesIt, const auto &value) { | ||
return seriesIt.at(i) == value; | ||||
}); | ||||
} | ||||
} | ||||
} // namespace | ||||
Alexandre Leroux
|
r514 | |||
class TestTwoDimArrayData : public QObject { | ||||
Q_OBJECT | ||||
private slots: | ||||
Alexandre Leroux
|
r520 | /// Tests @sa ArrayData ctor | ||
void testCtor_data(); | ||||
void testCtor(); | ||||
Alexandre Leroux
|
r516 | /// Tests @sa ArrayData::add() | ||
void testAdd_data(); | ||||
void testAdd(); | ||||
Alexandre Leroux
|
r517 | /// Tests @sa ArrayData::clear() | ||
void testClear_data(); | ||||
void testClear(); | ||||
/// Tests @sa ArrayData::size() | ||||
void testSize_data(); | ||||
void testSize(); | ||||
Alexandre Leroux
|
r518 | /// Tests @sa ArrayData::sort() | ||
void testSort_data(); | ||||
void testSort(); | ||||
Alexandre Leroux
|
r514 | }; | ||
Alexandre Leroux
|
r520 | void TestTwoDimArrayData::testCtor_data() | ||
{ | ||||
// Test structure | ||||
Alexandre Leroux
|
r647 | 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
|
r520 | |||
// Test cases | ||||
Alexandre Leroux
|
r647 | 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.}}; | ||||
r750 | QTest::newRow("invalidInput (invalid data size") << InputData{{1., 2., 3., 4., 5., 6., 7.}, 3} | |||
<< false << Container{{}, {}, {}}; | ||||
Alexandre Leroux
|
r647 | QTest::newRow("invalidInput (less than two components") | ||
<< flatten(Container{{1., 2., 3., 4., 5.}}) << false << Container{{}, {}, {}}; | ||||
Alexandre Leroux
|
r520 | } | ||
void TestTwoDimArrayData::testCtor() | ||||
{ | ||||
Alexandre Leroux
|
r647 | QFETCH(InputData, inputData); | ||
Alexandre Leroux
|
r520 | QFETCH(bool, success); | ||
if (success) { | ||||
Alexandre Leroux
|
r647 | QFETCH(Container, expectedData); | ||
Alexandre Leroux
|
r520 | |||
Alexandre Leroux
|
r647 | ArrayData<2> arrayData{inputData.first, inputData.second}; | ||
verifyArrayData(arrayData, expectedData); | ||||
Alexandre Leroux
|
r520 | } | ||
else { | ||||
Alexandre Leroux
|
r647 | QVERIFY_EXCEPTION_THROWN(ArrayData<2>(inputData.first, inputData.second), | ||
std::invalid_argument); | ||||
Alexandre Leroux
|
r520 | } | ||
} | ||||
Alexandre Leroux
|
r516 | void TestTwoDimArrayData::testAdd_data() | ||
{ | ||||
// Test structure | ||||
Alexandre Leroux
|
r647 | 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
|
r516 | |||
// Test cases | ||||
Alexandre Leroux
|
r647 | auto inputData = flatten( | ||
Container{{1., 2., 3., 4., 5.}, {11., 12., 13., 14., 15.}, {21., 22., 23., 24., 25.}}); | ||||
Alexandre Leroux
|
r516 | |||
Alexandre Leroux
|
r647 | 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
|
r516 | |||
QTest::newRow("appendMerge") << inputData << vectorContainer << false | ||||
Alexandre Leroux
|
r647 | << 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
|
r516 | QTest::newRow("prependMerge") << inputData << vectorContainer << true | ||
Alexandre Leroux
|
r647 | << 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
|
r516 | } | ||
void TestTwoDimArrayData::testAdd() | ||||
{ | ||||
Alexandre Leroux
|
r647 | QFETCH(InputData, inputData); | ||
QFETCH(InputData, otherData); | ||||
Alexandre Leroux
|
r516 | QFETCH(bool, prepend); | ||
Alexandre Leroux
|
r647 | QFETCH(Container, expectedData); | ||
Alexandre Leroux
|
r516 | |||
Alexandre Leroux
|
r647 | ArrayData<2> arrayData{inputData.first, inputData.second}; | ||
ArrayData<2> other{otherData.first, otherData.second}; | ||||
Alexandre Leroux
|
r516 | |||
arrayData.add(other, prepend); | ||||
Alexandre Leroux
|
r647 | verifyArrayData(arrayData, expectedData); | ||
Alexandre Leroux
|
r516 | } | ||
Alexandre Leroux
|
r517 | void TestTwoDimArrayData::testClear_data() | ||
{ | ||||
// Test structure | ||||
Alexandre Leroux
|
r647 | QTest::addColumn<InputData>("inputData"); // array data's input | ||
Alexandre Leroux
|
r517 | |||
// Test cases | ||||
Alexandre Leroux
|
r647 | QTest::newRow("data1") << flatten( | ||
Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}); | ||||
Alexandre Leroux
|
r517 | } | ||
void TestTwoDimArrayData::testClear() | ||||
{ | ||||
Alexandre Leroux
|
r647 | QFETCH(InputData, inputData); | ||
Alexandre Leroux
|
r517 | |||
Alexandre Leroux
|
r647 | ArrayData<2> arrayData{inputData.first, inputData.second}; | ||
Alexandre Leroux
|
r517 | arrayData.clear(); | ||
Alexandre Leroux
|
r695 | auto emptyData = Container(inputData.second, DataContainer{}); | ||
Alexandre Leroux
|
r647 | verifyArrayData(arrayData, emptyData); | ||
Alexandre Leroux
|
r517 | } | ||
void TestTwoDimArrayData::testSize_data() | ||||
{ | ||||
// Test structure | ||||
Alexandre Leroux
|
r647 | QTest::addColumn<InputData>("inputData"); // array data's input | ||
QTest::addColumn<int>("expectedSize"); // expected array data size | ||||
Alexandre Leroux
|
r517 | |||
// Test cases | ||||
Alexandre Leroux
|
r647 | 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
|
r517 | << 5; | ||
} | ||||
void TestTwoDimArrayData::testSize() | ||||
{ | ||||
Alexandre Leroux
|
r647 | QFETCH(InputData, inputData); | ||
Alexandre Leroux
|
r517 | QFETCH(int, expectedSize); | ||
Alexandre Leroux
|
r647 | ArrayData<2> arrayData{inputData.first, inputData.second}; | ||
Alexandre Leroux
|
r517 | QVERIFY(arrayData.size() == expectedSize); | ||
} | ||||
Alexandre Leroux
|
r518 | void TestTwoDimArrayData::testSort_data() | ||
{ | ||||
// Test structure | ||||
Alexandre Leroux
|
r647 | QTest::addColumn<InputData>("inputData"); // array data's input | ||
Alexandre Leroux
|
r518 | QTest::addColumn<std::vector<int> >("sortPermutation"); // permutation used to sort data | ||
Alexandre Leroux
|
r647 | QTest::addColumn<Container>("expectedData"); // expected data after sorting | ||
Alexandre Leroux
|
r518 | |||
// Test cases | ||||
QTest::newRow("data1") | ||||
Alexandre Leroux
|
r647 | << flatten( | ||
Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}) | ||||
Alexandre Leroux
|
r518 | << std::vector<int>{0, 2, 3, 1, 4} | ||
Alexandre Leroux
|
r647 | << Container{{1., 3., 4., 2., 5.}, {6., 8., 9., 7., 10.}, {11., 13., 14., 12., 15.}}; | ||
Alexandre Leroux
|
r518 | QTest::newRow("data2") | ||
Alexandre Leroux
|
r647 | << flatten( | ||
Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}}) | ||||
Alexandre Leroux
|
r518 | << std::vector<int>{2, 4, 3, 0, 1} | ||
Alexandre Leroux
|
r647 | << Container{{3., 5., 4., 1., 2.}, {8., 10., 9., 6., 7.}, {13., 15., 14., 11., 12.}}; | ||
Alexandre Leroux
|
r518 | } | ||
void TestTwoDimArrayData::testSort() | ||||
{ | ||||
Alexandre Leroux
|
r647 | QFETCH(InputData, inputData); | ||
Alexandre Leroux
|
r518 | QFETCH(std::vector<int>, sortPermutation); | ||
Alexandre Leroux
|
r647 | QFETCH(Container, expectedData); | ||
Alexandre Leroux
|
r518 | |||
Alexandre Leroux
|
r647 | ArrayData<2> arrayData{inputData.first, inputData.second}; | ||
Alexandre Leroux
|
r518 | auto sortedArrayData = arrayData.sort(sortPermutation); | ||
Alexandre Leroux
|
r524 | QVERIFY(sortedArrayData != nullptr); | ||
Alexandre Leroux
|
r518 | |||
Alexandre Leroux
|
r647 | verifyArrayData(*sortedArrayData, expectedData); | ||
Alexandre Leroux
|
r518 | } | ||
Alexandre Leroux
|
r514 | QTEST_MAIN(TestTwoDimArrayData) | ||
#include "TestTwoDimArrayData.moc" | ||||