##// END OF EJS Templates
Fixed untimely update of the range to be displayed in the variable widget
Fixed untimely update of the range to be displayed in the variable widget

File last commit:

r647:da7f81670868
r654:aff19a50babf
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
r514 #include "Data/ArrayData.h"
#include <QObject>
#include <QtTest>
Alexandre Leroux
Updates unit tests
r647 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
r514
class TestTwoDimArrayData : public QObject {
Q_OBJECT
private slots:
Alexandre Leroux
Tests 2-dim ctor
r520 /// Tests @sa ArrayData ctor
void testCtor_data();
void testCtor();
Alexandre Leroux
Tests add() method
r516 /// Tests @sa ArrayData::add()
void testAdd_data();
void testAdd();
Alexandre Leroux
Tests clear() and size() methods
r517 /// Tests @sa ArrayData::clear()
void testClear_data();
void testClear();
/// Tests @sa ArrayData::size()
void testSize_data();
void testSize();
Alexandre Leroux
Tests sort() method
r518 /// Tests @sa ArrayData::sort()
void testSort_data();
void testSort();
Alexandre Leroux
Inits unit tests for 1-dim and 2-dim array data
r514 };
Alexandre Leroux
Tests 2-dim ctor
r520 void TestTwoDimArrayData::testCtor_data()
{
// Test structure
Alexandre Leroux
Updates unit tests
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
Tests 2-dim ctor
r520
// Test cases
Alexandre Leroux
Updates unit tests
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.}};
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
r520 }
void TestTwoDimArrayData::testCtor()
{
Alexandre Leroux
Updates unit tests
r647 QFETCH(InputData, inputData);
Alexandre Leroux
Tests 2-dim ctor
r520 QFETCH(bool, success);
if (success) {
Alexandre Leroux
Updates unit tests
r647 QFETCH(Container, expectedData);
Alexandre Leroux
Tests 2-dim ctor
r520
Alexandre Leroux
Updates unit tests
r647 ArrayData<2> arrayData{inputData.first, inputData.second};
verifyArrayData(arrayData, expectedData);
Alexandre Leroux
Tests 2-dim ctor
r520 }
else {
Alexandre Leroux
Updates unit tests
r647 QVERIFY_EXCEPTION_THROWN(ArrayData<2>(inputData.first, inputData.second),
std::invalid_argument);
Alexandre Leroux
Tests 2-dim ctor
r520 }
}
Alexandre Leroux
Tests add() method
r516 void TestTwoDimArrayData::testAdd_data()
{
// Test structure
Alexandre Leroux
Updates unit tests
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
Tests add() method
r516
// Test cases
Alexandre Leroux
Updates unit tests
r647 auto inputData = flatten(
Container{{1., 2., 3., 4., 5.}, {11., 12., 13., 14., 15.}, {21., 22., 23., 24., 25.}});
Alexandre Leroux
Tests add() method
r516
Alexandre Leroux
Updates unit tests
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
Tests add() method
r516
QTest::newRow("appendMerge") << inputData << vectorContainer << false
Alexandre Leroux
Updates unit tests
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
Tests add() method
r516 QTest::newRow("prependMerge") << inputData << vectorContainer << true
Alexandre Leroux
Updates unit tests
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
Tests add() method
r516 }
void TestTwoDimArrayData::testAdd()
{
Alexandre Leroux
Updates unit tests
r647 QFETCH(InputData, inputData);
QFETCH(InputData, otherData);
Alexandre Leroux
Tests add() method
r516 QFETCH(bool, prepend);
Alexandre Leroux
Updates unit tests
r647 QFETCH(Container, expectedData);
Alexandre Leroux
Tests add() method
r516
Alexandre Leroux
Updates unit tests
r647 ArrayData<2> arrayData{inputData.first, inputData.second};
ArrayData<2> other{otherData.first, otherData.second};
Alexandre Leroux
Tests add() method
r516
arrayData.add(other, prepend);
Alexandre Leroux
Updates unit tests
r647 verifyArrayData(arrayData, expectedData);
Alexandre Leroux
Tests add() method
r516 }
Alexandre Leroux
Tests clear() and size() methods
r517 void TestTwoDimArrayData::testClear_data()
{
// Test structure
Alexandre Leroux
Updates unit tests
r647 QTest::addColumn<InputData>("inputData"); // array data's input
Alexandre Leroux
Tests clear() and size() methods
r517
// Test cases
Alexandre Leroux
Updates unit tests
r647 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
r517 }
void TestTwoDimArrayData::testClear()
{
Alexandre Leroux
Updates unit tests
r647 QFETCH(InputData, inputData);
Alexandre Leroux
Tests clear() and size() methods
r517
Alexandre Leroux
Updates unit tests
r647 ArrayData<2> arrayData{inputData.first, inputData.second};
Alexandre Leroux
Tests clear() and size() methods
r517 arrayData.clear();
Alexandre Leroux
Updates unit tests
r647 auto emptyData = Container(inputData.second, QVector<double>{});
verifyArrayData(arrayData, emptyData);
Alexandre Leroux
Tests clear() and size() methods
r517 }
void TestTwoDimArrayData::testSize_data()
{
// Test structure
Alexandre Leroux
Updates unit tests
r647 QTest::addColumn<InputData>("inputData"); // array data's input
QTest::addColumn<int>("expectedSize"); // expected array data size
Alexandre Leroux
Tests clear() and size() methods
r517
// Test cases
Alexandre Leroux
Updates unit tests
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
Tests clear() and size() methods
r517 << 5;
}
void TestTwoDimArrayData::testSize()
{
Alexandre Leroux
Updates unit tests
r647 QFETCH(InputData, inputData);
Alexandre Leroux
Tests clear() and size() methods
r517 QFETCH(int, expectedSize);
Alexandre Leroux
Updates unit tests
r647 ArrayData<2> arrayData{inputData.first, inputData.second};
Alexandre Leroux
Tests clear() and size() methods
r517 QVERIFY(arrayData.size() == expectedSize);
}
Alexandre Leroux
Tests sort() method
r518 void TestTwoDimArrayData::testSort_data()
{
// Test structure
Alexandre Leroux
Updates unit tests
r647 QTest::addColumn<InputData>("inputData"); // array data's input
Alexandre Leroux
Tests sort() method
r518 QTest::addColumn<std::vector<int> >("sortPermutation"); // permutation used to sort data
Alexandre Leroux
Updates unit tests
r647 QTest::addColumn<Container>("expectedData"); // expected data after sorting
Alexandre Leroux
Tests sort() method
r518
// Test cases
QTest::newRow("data1")
Alexandre Leroux
Updates unit tests
r647 << flatten(
Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}})
Alexandre Leroux
Tests sort() method
r518 << std::vector<int>{0, 2, 3, 1, 4}
Alexandre Leroux
Updates unit tests
r647 << Container{{1., 3., 4., 2., 5.}, {6., 8., 9., 7., 10.}, {11., 13., 14., 12., 15.}};
Alexandre Leroux
Tests sort() method
r518 QTest::newRow("data2")
Alexandre Leroux
Updates unit tests
r647 << flatten(
Container{{1., 2., 3., 4., 5.}, {6., 7., 8., 9., 10.}, {11., 12., 13., 14., 15.}})
Alexandre Leroux
Tests sort() method
r518 << std::vector<int>{2, 4, 3, 0, 1}
Alexandre Leroux
Updates unit tests
r647 << Container{{3., 5., 4., 1., 2.}, {8., 10., 9., 6., 7.}, {13., 15., 14., 11., 12.}};
Alexandre Leroux
Tests sort() method
r518 }
void TestTwoDimArrayData::testSort()
{
Alexandre Leroux
Updates unit tests
r647 QFETCH(InputData, inputData);
Alexandre Leroux
Tests sort() method
r518 QFETCH(std::vector<int>, sortPermutation);
Alexandre Leroux
Updates unit tests
r647 QFETCH(Container, expectedData);
Alexandre Leroux
Tests sort() method
r518
Alexandre Leroux
Updates unit tests
r647 ArrayData<2> arrayData{inputData.first, inputData.second};
Alexandre Leroux
Tests sort() method
r518 auto sortedArrayData = arrayData.sort(sortPermutation);
Alexandre Leroux
Fix compilation on Linux
r524 QVERIFY(sortedArrayData != nullptr);
Alexandre Leroux
Tests sort() method
r518
Alexandre Leroux
Updates unit tests
r647 verifyArrayData(*sortedArrayData, expectedData);
Alexandre Leroux
Tests sort() method
r518 }
Alexandre Leroux
Inits unit tests for 1-dim and 2-dim array data
r514 QTEST_MAIN(TestTwoDimArrayData)
#include "TestTwoDimArrayData.moc"