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