From 9d4ace4d73d078215e2804aad6dcdcea38a83105 2017-11-23 16:38:09 From: Alexandre Leroux Date: 2017-11-23 16:38:09 Subject: [PATCH] Unit tests for fillDataHoles() method --- diff --git a/core/meson.build b/core/meson.build index 2a527f6..11a1fb6 100644 --- a/core/meson.build +++ b/core/meson.build @@ -27,6 +27,7 @@ core_sources = [ 'src/Data/ArrayDataIterator.cpp', 'src/Data/VectorSeries.cpp', 'src/Data/OptionalAxis.cpp', + 'src/Data/DataSeriesUtils.cpp', 'src/DataSource/DataSourceController.cpp', 'src/DataSource/DataSourceItem.cpp', 'src/DataSource/DataSourceItemAction.cpp', diff --git a/core/tests/Data/TestDataSeriesUtils.cpp b/core/tests/Data/TestDataSeriesUtils.cpp new file mode 100644 index 0000000..3e4bd90 --- /dev/null +++ b/core/tests/Data/TestDataSeriesUtils.cpp @@ -0,0 +1,87 @@ +#include + +#include +#include + +class TestDataSeriesUtils : public QObject { + Q_OBJECT + +private slots: + /// Tests @sa DataSeriesUtils::fillDataHoles() method + void testFillDataHoles_data(); + void testFillDataHoles(); +}; + +void TestDataSeriesUtils::testFillDataHoles_data() +{ + QTest::addColumn >("xAxisData"); + QTest::addColumn >("valuesData"); + QTest::addColumn("resolution"); + QTest::addColumn("fillValue"); + QTest::addColumn >( + "expectedXAxisData"); // expected x-axis data after filling holes + QTest::addColumn >( + "expectedValuesData"); // expected values data after filling holes + + auto nan = std::numeric_limits::quiet_NaN(); + + QTest::newRow("fillDataHoles (basic case)") + << std::vector{0., 1., 5., 7., 14.} << std::vector{0., 1., 2., 3., 4.} << 2. + << nan << std::vector{0., 1., 3., 5., 7., 9., 11., 13., 14.} + << std::vector{0., 1., nan, 2., 3., nan, nan, nan, 4.}; + + QTest::newRow("fillDataHoles (change nb components)") + << std::vector{0., 1., 5., 7., 14.} + << std::vector{0., 1., 2., 3., 4., 5., 6., 7., 8., 9.} << 2. << nan + << std::vector{0., 1., 3., 5., 7., 9., 11., 13., 14.} + << std::vector{0., 1., 2., 3., nan, nan, 4., 5., 6., + 7., nan, nan, nan, nan, nan, nan, 8., 9.}; + + QTest::newRow("fillDataHoles (change resolution)") + << std::vector{0., 1., 5., 7., 14.} << std::vector{0., 1., 2., 3., 4.} + << 1.5 << nan << std::vector{0., 1., 2.5, 4., 5., 6.5, 7., 8.5, 10., 11.5, 13., 14.} + << std::vector{0., 1., nan, nan, 2., nan, 3., nan, nan, nan, nan, 4.}; + + QTest::newRow("fillDataHoles (with no data (no changes made))") + << std::vector{} << std::vector{} << 2. << nan << std::vector{} + << std::vector{}; + + QTest::newRow("fillDataHoles (with no resolution (no changes made))") + << std::vector{0., 1., 5., 7., 14.} << std::vector{0., 1., 2., 3., 4.} << 0. + << nan << std::vector{0., 1., 5., 7., 14.} + << std::vector{0., 1., 2., 3., 4.}; + + QTest::newRow("fillDataHoles (change fill value)") + << std::vector{0., 1., 5., 7., 14.} << std::vector{0., 1., 2., 3., 4.} << 2. + << -1. << std::vector{0., 1., 3., 5., 7., 9., 11., 13., 14.} + << std::vector{0., 1., -1., 2., 3., -1., -1., -1., 4.}; +} + +void TestDataSeriesUtils::testFillDataHoles() +{ + QFETCH(std::vector, xAxisData); + QFETCH(std::vector, valuesData); + QFETCH(double, resolution); + QFETCH(double, fillValue); + + QFETCH(std::vector, expectedXAxisData); + QFETCH(std::vector, expectedValuesData); + + // Executes method (xAxisData and valuesData are modified) + DataSeriesUtils::fillDataHoles(xAxisData, valuesData, resolution, fillValue); + + // Checks results + auto equal = [](const auto &data, const auto &expectedData) { + // Compares with NaN values + return std::equal(data.begin(), data.end(), expectedData.begin(), expectedData.end(), + [](const auto &val, const auto &expectedVal) { + return (std::isnan(val) && std::isnan(expectedVal)) + || val == expectedVal; + }); + }; + QVERIFY(equal(xAxisData, expectedXAxisData)); + QVERIFY(equal(valuesData, expectedValuesData)); +} + +QTEST_MAIN(TestDataSeriesUtils) +#include "TestDataSeriesUtils.moc" diff --git a/core/tests/meson.build b/core/tests/meson.build index b41a34f..8df1c87 100644 --- a/core/tests/meson.build +++ b/core/tests/meson.build @@ -8,6 +8,7 @@ tests = [ [['Data/TestOneDimArrayData.cpp'],'test_1d','One Dim Array test'], [['Data/TestOptionalAxis.cpp'],'test_optional_axis','OptionalAxis test'], [['Data/TestTwoDimArrayData.cpp'],'test_2d','Two Dim Array test'], + [['Data/TestDataSeriesUtils.cpp'],'test_dataseries_util','Data series utils test'], [['DataSource/TestDataSourceController.cpp'],'test_data_source','DataSourceController test'], [['Variable/TestVariableCacheController.cpp'],'test_variable_cache','VariableCacheController test'], [['Variable/TestVariable.cpp'],'test_variable','Variable test'],