From 53e4211b306c7f80e866171aa0fdc64fba691736 Mon Sep 17 00:00:00 2001 From: Jan Grewe Date: Tue, 9 Jun 2020 20:16:14 +0200 Subject: [PATCH] [dataAccess] start > end leads to an invalied range, or even an exception --- src/Dimensions.cpp | 15 ++++++++------- test/BaseTestDataAccess.cpp | 6 +++--- test/BaseTestDimension.cpp | 32 ++++++++++++++++---------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/Dimensions.cpp b/src/Dimensions.cpp index 9be96c840..06f601e7b 100644 --- a/src/Dimensions.cpp +++ b/src/Dimensions.cpp @@ -239,15 +239,16 @@ boost::optional> SampledDimension::indexOf(const d boost::optional> SampledDimension::indexOf(double start, double end, const double sampling_interval, const double offset, const RangeMatch match) const { + boost::optional> indices; + PositionMatch pos_match = match == RangeMatch::Inclusive ? PositionMatch::LessOrEqual : PositionMatch::Less; + if (start > end) { - std::swap(start, end); + return indices; } - PositionMatch pos_match = match == RangeMatch::Inclusive ? PositionMatch::LessOrEqual : PositionMatch::Less; boost::optional si = getSampledIndex(start, offset, sampling_interval, PositionMatch::GreaterOrEqual); boost::optional ei = getSampledIndex(end, offset, sampling_interval, pos_match); - boost::optional> indices; if (si && ei && *si <= *ei) { indices = std::pair(*si, *ei); } @@ -405,7 +406,6 @@ boost::optional getSetIndex(const double position, std::vector> SetDimension::indexOf(double star if (set_labels.size() == 0) { set_labels = labels(); } + + boost::optional> index; + PositionMatch end_match = match == RangeMatch::Inclusive ? PositionMatch::LessOrEqual : PositionMatch::Less; if (start > end) { - std::swap(start, end); + return index; } - PositionMatch end_match = match == RangeMatch::Inclusive ? PositionMatch::LessOrEqual : PositionMatch::Less; - boost::optional> index; boost::optional si = getSetIndex(start, set_labels, PositionMatch::GreaterOrEqual); boost::optional ei = getSetIndex(end, set_labels, end_match); diff --git a/test/BaseTestDataAccess.cpp b/test/BaseTestDataAccess.cpp index 8e2783951..fe8a6ae1d 100644 --- a/test/BaseTestDataAccess.cpp +++ b/test/BaseTestDataAccess.cpp @@ -115,13 +115,13 @@ void BaseTestDataAccess::testPositionToIndexSampledDimension() { CPPUNIT_ASSERT_THROW(util::positionToIndex(1.0, unit, sampledDim), nix::IncompatibleDimensions); sampledDim.unit(unit); - vector>> ranges = util::positionToIndex({0.0, 2.0, 10.0}, {10.0, 2.0, 5.0}, {unit, unit, unit}, RangeMatch::Inclusive, sampledDim); + vector>> ranges = util::positionToIndex({0.0, 2.0, 5.0}, {10.0, 2.0, 10.0}, {unit, unit, unit}, RangeMatch::Inclusive, sampledDim); CPPUNIT_ASSERT(ranges.size() == 3); CPPUNIT_ASSERT(ranges[0] && (*ranges[0]).first == 0 && (*ranges[0]).second == 10); CPPUNIT_ASSERT(ranges[1] && (*ranges[1]).first == 2 && (*ranges[1]).second == 2); CPPUNIT_ASSERT(ranges[2] && (*ranges[2]).first == 5 && (*ranges[2]).second == 10); - ranges = util::positionToIndex({0.0, 2.0, 10.0}, {10.0, 2.0, 5.0}, {unit, unit, unit}, RangeMatch::Exclusive, sampledDim); + ranges = util::positionToIndex({0.0, 2.0, 5.0}, {10.0, 2.0, 10.0}, {unit, unit, unit}, RangeMatch::Exclusive, sampledDim); CPPUNIT_ASSERT(ranges.size() == 3); CPPUNIT_ASSERT(ranges[0] && (*ranges[0]).first == 0 && (*ranges[0]).second == 9); CPPUNIT_ASSERT(!ranges[1]); @@ -139,7 +139,7 @@ void BaseTestDataAccess::testPositionToIndexSampledDimensionOld() { CPPUNIT_ASSERT(util::positionToIndex(0.005, scaled_unit, sampledDim) == 5); CPPUNIT_ASSERT_THROW(util::positionToIndex({},{1.0}, {unit}, sampledDim), std::runtime_error); - CPPUNIT_ASSERT(util::positionToIndex({0.0, 1.0}, {3.5, 0.5}, {unit, unit}, sampledDim).size() == 2); + CPPUNIT_ASSERT(util::positionToIndex({0.0, 0.5}, {3.5, 1.0}, {unit, unit}, sampledDim).size() == 2); vector> ranges = util::positionToIndex({0.0}, {0.0}, {unit}, sampledDim); CPPUNIT_ASSERT(ranges[0].first == 0 && ranges[0].second == 0); } diff --git a/test/BaseTestDimension.cpp b/test/BaseTestDimension.cpp index b9ff7b4c7..de68c972d 100644 --- a/test/BaseTestDimension.cpp +++ b/test/BaseTestDimension.cpp @@ -191,16 +191,16 @@ void BaseTestDimension::testSampledDimIndexOfOld() { CPPUNIT_ASSERT_NO_THROW(sd.indexOf(-0.25, 1.01)); CPPUNIT_ASSERT_NO_THROW(sd.indexOf(0.0, 1.01)); CPPUNIT_ASSERT_NO_THROW(sd.indexOf(1.0, 1.01)); - CPPUNIT_ASSERT_NO_THROW(sd.indexOf(1.01, 0.0)); - CPPUNIT_ASSERT_NO_THROW(sd.indexOf(3.5, 1.5)); + CPPUNIT_ASSERT_THROW(sd.indexOf(1.01, 0.0), nix::OutOfBounds); + CPPUNIT_ASSERT_THROW(sd.indexOf(3.5, 1.5), nix::OutOfBounds); CPPUNIT_ASSERT_THROW(sd.indexOf(-1.0, -.05), nix::OutOfBounds); // because end pos is invalid std::pair range = sd.indexOf(-1.0, 5.0); CPPUNIT_ASSERT(range.first == 0 && range.second == 4); range = sd.indexOf(1.0, 5.0); CPPUNIT_ASSERT(range.first == 0 && range.second == 4); - range = sd.indexOf(5.0, 1.0); - CPPUNIT_ASSERT(range.first == 0 && range.second == 4); + //range = sd.indexOf(5.0, 1.0); + //CPPUNIT_ASSERT(range.first == 0 && range.second == 4); range = sd.indexOf(1.5, 3.2); CPPUNIT_ASSERT(range.first == 1 && range.second == 2); // less or equal for end of range range = sd.indexOf(1.5, 3.7); @@ -210,11 +210,11 @@ void BaseTestDimension::testSampledDimIndexOfOld() { // test vectors of ranges, offset = 1, sampling interval = 1 CPPUNIT_ASSERT_THROW(sd.indexOf({0.0, 20.0, 40.0}, {10.9}), std::runtime_error); - CPPUNIT_ASSERT_NO_THROW(sd.indexOf({0.0, 20.0, 40.0}, {10.9, 12., 1.})); - CPPUNIT_ASSERT_NO_THROW(sd.indexOf({1.0, 20.0, 40.0}, {10.9, 12., 1.})); - CPPUNIT_ASSERT(sd.indexOf({1.0, 20.0, 40.0}, {10.9, 12., 1.}).size() == 3); + CPPUNIT_ASSERT_NO_THROW(sd.indexOf({0.0, 12.0, 1.0}, {10.9, 20., 40.})); + CPPUNIT_ASSERT_NO_THROW(sd.indexOf({1.0, 12.0, 1.0}, {10.9, 20., 40.})); + CPPUNIT_ASSERT(sd.indexOf({1.0, 12.0, 1.0}, {10.9, 20., 40.}).size() == 3); - std::vector> ranges = sd.indexOf({1.0, 20.0, 40.0}, {10.9, 12., 1.}); + std::vector> ranges = sd.indexOf({1.0, 12.0, 1.0}, {10.9, 20., 40.}); CPPUNIT_ASSERT(ranges.size() == 3); CPPUNIT_ASSERT(ranges[0].first == 0 && ranges[0].second == 9); CPPUNIT_ASSERT(sd.positionAt(ranges[0].first) == 1.0 && sd.positionAt(ranges[0].second) == 10.); @@ -310,9 +310,9 @@ void BaseTestDimension::testSampledDimIndexOf() { range = sd.indexOf(-1.5, 2.0, RangeMatch::Exclusive); CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 2); range = sd.indexOf(2., -2.0, RangeMatch::Inclusive); - CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 3); + CPPUNIT_ASSERT(!range); range = sd.indexOf(2., -2.0, RangeMatch::Exclusive); - CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 2); + CPPUNIT_ASSERT(!range); range = sd.indexOf(2.0, 2.0, RangeMatch::Inclusive); CPPUNIT_ASSERT(range && (*range).first == 3 && (*range).second == 3); range = sd.indexOf(2., 2.0, RangeMatch::Exclusive); @@ -321,7 +321,7 @@ void BaseTestDimension::testSampledDimIndexOf() { // test vector of ranges, offset = -1, sampling interval = 1 CPPUNIT_ASSERT_THROW(sd.indexOf({1.0, 20.0, 40.0}, {10.9, 12.}, RangeMatch::Exclusive), std::runtime_error); - std::vector>> ranges = sd.indexOf({1.0, 20.0, 40.0, 5.0}, {10.9, 12.0, 1.0, 5.0}, RangeMatch::Inclusive); + std::vector>> ranges = sd.indexOf({1.0, 12.0, 1.0, 5.0}, {10.9, 20.0, 40.0, 5.0}, RangeMatch::Inclusive); CPPUNIT_ASSERT(ranges.size() == 4); CPPUNIT_ASSERT(ranges[0] && (*ranges[0]).first == 2 && (*ranges[0]).second == 11 && sd.positionAt((*ranges[0]).first) >= 1.0 && sd.positionAt((*ranges[0]).second) <= 10.9); @@ -332,7 +332,7 @@ void BaseTestDimension::testSampledDimIndexOf() { CPPUNIT_ASSERT(ranges[3] && (*ranges[3]).first == 6 && (*ranges[3]).second == 6 && sd.positionAt((*ranges[3]).first) == 5 && sd.positionAt((*ranges[3]).second) == 5); - ranges = sd.indexOf({1.0, 20.0, 40.0, 5.0}, {10.9, 12.0, 1.0, 5.0}, RangeMatch::Exclusive); + ranges = sd.indexOf({1.0, 12.0, 1.0, 5.0}, {10.9, 20.0, 40.0, 5.0}, RangeMatch::Exclusive); CPPUNIT_ASSERT(ranges.size() == 4); CPPUNIT_ASSERT(ranges[0] && (*ranges[0]).first == 2 && (*ranges[0]).second == 11 && sd.positionAt((*ranges[0]).first) >= 1.0 && sd.positionAt((*ranges[0]).second) <= 10.9); @@ -574,9 +574,9 @@ void BaseTestDimension::testSetDimIndexOf() { range = sd.indexOf(0.0, 3.0, RangeMatch::Exclusive); CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 2); range = sd.indexOf(3.0, 0.0, RangeMatch::Inclusive); - CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 3); + CPPUNIT_ASSERT(!range); range = sd.indexOf(3.0, 0.0, RangeMatch::Exclusive); - CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 2); + CPPUNIT_ASSERT(!range); sd.labels(labels); range = sd.indexOf(0.0, 0.0, RangeMatch::Inclusive); @@ -588,9 +588,9 @@ void BaseTestDimension::testSetDimIndexOf() { range = sd.indexOf(0.0, 3.0, RangeMatch::Exclusive); CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 2); range = sd.indexOf(3.0, 0.0, RangeMatch::Inclusive); - CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 3); + CPPUNIT_ASSERT(!range); range = sd.indexOf(3.0, 0.0, RangeMatch::Exclusive); - CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 2); + CPPUNIT_ASSERT(!range); range = sd.indexOf(0.0, 7.0, RangeMatch::Inclusive); CPPUNIT_ASSERT(range && (*range).first == 0 && (*range).second == 4); range = sd.indexOf(3.0, 7.0, RangeMatch::Exclusive);