From c613c4935e083f9ca12ce6fe08ec49059cf0c440 2017-08-23 12:12:57 From: Alexandre Leroux Date: 2017-08-23 12:12:57 Subject: [PATCH] Shows min/max x-axis data in Variable widget (2) Creates max() method that returns the iterator on the last value that is <= than the value passed in parameter + implements unit tests --- diff --git a/core/include/Data/DataSeries.h b/core/include/Data/DataSeries.h index 13adea2..da79366 100644 --- a/core/include/Data/DataSeries.h +++ b/core/include/Data/DataSeries.h @@ -199,6 +199,17 @@ public: [](const auto &itValue, const auto &value) { return itValue.x() < value; }); } + /// @sa IDataSeries::maxData() + DataSeriesIterator maxData(double maxXAxisData) const override + { + // Gets the first element that greater than max value + auto it = std::upper_bound( + cbegin(), cend(), maxXAxisData, + [](const auto &value, const auto &itValue) { return value < itValue.x(); }); + + return it == cbegin() ? cend() : --it; + } + 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 9c91fbb..a9059c4 100644 --- a/core/include/Data/IDataSeries.h +++ b/core/include/Data/IDataSeries.h @@ -74,6 +74,10 @@ public: /// 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; + /// @return the iterator to the last entry of the data series whose x-axis data is less than or + /// equal to the value passed in parameter, or the end iterator if there is no matching value + virtual DataSeriesIterator maxData(double maxXAxisData) 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 c669727..d6c75b8 100644 --- a/core/tests/Data/TestDataSeries.cpp +++ b/core/tests/Data/TestDataSeries.cpp @@ -30,6 +30,14 @@ private slots: /// Tests get min data of a data series void testMinData(); + /// Input test data + /// @sa testMaxData() + void testMaxData_data(); + + /// Tests get max data of a data series + void testMaxData(); + + /// Input test data /// @sa testSubdata() void testSubdata_data(); @@ -226,6 +234,61 @@ void TestDataSeries::testMinData() QCOMPARE(expectedMin, it->x()); } } + +void TestDataSeries::testMaxData_data() +{ + // ////////////// // + // Test structure // + // ////////////// // + + // Data series to get max data + QTest::addColumn >("dataSeries"); + + // Max data + QTest::addColumn("max"); + + // Expected results + QTest::addColumn( + "expectedOK"); // if true, expects to have a result (i.e. the iterator != end iterator) + QTest::addColumn( + "expectedMax"); // Expected value when method doesn't return end iterator + + // ////////// // + // Test cases // + // ////////// // + + QTest::newRow("maxData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) + << 6. << true << 5.; + QTest::newRow("maxData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) + << 5. << true << 5.; + QTest::newRow("maxData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) + << 4.9 << true << 4.; + QTest::newRow("maxData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) + << 1.1 << true << 1.; + QTest::newRow("maxData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) + << 1. << true << 1.; + QTest::newRow("maxData6") << createSeries({}, {}) << 1.1 << false + << std::numeric_limits::quiet_NaN(); +} + +void TestDataSeries::testMaxData() +{ + QFETCH(std::shared_ptr, dataSeries); + QFETCH(double, max); + + QFETCH(bool, expectedOK); + QFETCH(double, expectedMax); + + auto it = dataSeries->maxData(max); + + QCOMPARE(expectedOK, it != dataSeries->cend()); + + // If the method doesn't return a end iterator, checks with expected value + if (expectedOK) { + QCOMPARE(expectedMax, it->x()); + } +} + void TestDataSeries::testSubdata_data() { // ////////////// //