diff --git a/core/include/Data/DataSeries.h b/core/include/Data/DataSeries.h index b2ddacb..13adea2 100644 --- a/core/include/Data/DataSeries.h +++ b/core/include/Data/DataSeries.h @@ -191,6 +191,14 @@ public: std::make_unique >(*this, false)}}; } + /// @sa IDataSeries::minData() + DataSeriesIterator minData(double minXAxisData) const override + { + return std::lower_bound( + cbegin(), cend(), minXAxisData, + [](const auto &itValue, const auto &value) { return itValue.x() < value; }); + } + std::pair subData(double min, double max) const override { if (min > max) { diff --git a/core/include/Data/IDataSeries.h b/core/include/Data/IDataSeries.h index 7574cab..9c91fbb 100644 --- a/core/include/Data/IDataSeries.h +++ b/core/include/Data/IDataSeries.h @@ -70,6 +70,10 @@ public: virtual DataSeriesIterator cbegin() const = 0; virtual DataSeriesIterator cend() const = 0; + /// @return the iterator to the first entry of the data series whose x-axis data is greater than + /// or equal to the value passed in parameter, or the end iterator if there is no matching value + virtual DataSeriesIterator minData(double minXAxisData) const = 0; + virtual std::pair subData(double min, double max) const = 0; diff --git a/core/tests/Data/TestDataSeries.cpp b/core/tests/Data/TestDataSeries.cpp index a8f6525..c669727 100644 --- a/core/tests/Data/TestDataSeries.cpp +++ b/core/tests/Data/TestDataSeries.cpp @@ -24,6 +24,12 @@ private slots: void testMerge(); /// Input test data + /// @sa testMinData() + void testMinData_data(); + + /// Tests get min data of a data series + void testMinData(); + /// @sa testSubdata() void testSubdata_data(); @@ -167,6 +173,59 @@ void TestDataSeries::testMerge() seriesValuesData.cbegin())); } +void TestDataSeries::testMinData_data() +{ + // ////////////// // + // Test structure // + // ////////////// // + + // Data series to get min data + QTest::addColumn >("dataSeries"); + + // Min data + QTest::addColumn("min"); + + // Expected results + QTest::addColumn( + "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator) + QTest::addColumn( + "expectedMin"); // Expected value when method doesn't return end iterator + + // ////////// // + // Test cases // + // ////////// // + + QTest::newRow("minData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) + << 0. << true << 1.; + QTest::newRow("minData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) + << 1. << true << 1.; + QTest::newRow("minData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) + << 1.1 << true << 2.; + QTest::newRow("minData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) + << 5. << true << 5.; + QTest::newRow("minData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) + << 5.1 << false << std::numeric_limits::quiet_NaN(); + QTest::newRow("minData6") << createSeries({}, {}) << 1.1 << false + << std::numeric_limits::quiet_NaN(); +} + +void TestDataSeries::testMinData() +{ + QFETCH(std::shared_ptr, dataSeries); + QFETCH(double, min); + + QFETCH(bool, expectedOK); + QFETCH(double, expectedMin); + + auto it = dataSeries->minData(min); + + QCOMPARE(expectedOK, it != dataSeries->cend()); + + // If the method doesn't return a end iterator, checks with expected value + if (expectedOK) { + QCOMPARE(expectedMin, it->x()); + } +} void TestDataSeries::testSubdata_data() { // ////////////// //