Skip to content

Commit

Permalink
[dataAccess] start > end leads to an invalied range, or even an excep…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
jgrewe committed Jul 28, 2020
1 parent 7d55c58 commit ec46176
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
15 changes: 8 additions & 7 deletions src/Dimensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,16 @@ boost::optional<std::pair<ndsize_t, ndsize_t>> SampledDimension::indexOf(const d

boost::optional<std::pair<ndsize_t, ndsize_t>> SampledDimension::indexOf(double start, double end, const double sampling_interval,
const double offset, const RangeMatch match) const {
boost::optional<std::pair<ndsize_t, ndsize_t>> 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<ndsize_t> si = getSampledIndex(start, offset, sampling_interval, PositionMatch::GreaterOrEqual);
boost::optional<ndsize_t> ei = getSampledIndex(end, offset, sampling_interval, pos_match);

boost::optional<std::pair<ndsize_t, ndsize_t>> indices;
if (si && ei && *si <= *ei) {
indices = std::pair<ndsize_t, ndsize_t>(*si, *ei);
}
Expand Down Expand Up @@ -405,7 +406,6 @@ boost::optional<ndsize_t> getSetIndex(const double position, std::vector<std::st
}
}
return index;

}


Expand All @@ -419,11 +419,12 @@ boost::optional<std::pair<ndsize_t, ndsize_t>> SetDimension::indexOf(double star
if (set_labels.size() == 0) {
set_labels = labels();
}

boost::optional<std::pair<ndsize_t, ndsize_t>> 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<std::pair<ndsize_t, ndsize_t>> index;

boost::optional<ndsize_t> si = getSetIndex(start, set_labels, PositionMatch::GreaterOrEqual);
boost::optional<ndsize_t> ei = getSetIndex(end, set_labels, end_match);
Expand Down
6 changes: 3 additions & 3 deletions test/BaseTestDataAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ void BaseTestDataAccess::testPositionToIndexSampledDimension() {
CPPUNIT_ASSERT_THROW(util::positionToIndex(1.0, unit, sampledDim), nix::IncompatibleDimensions);
sampledDim.unit(unit);

vector<optional<pair<ndsize_t, ndsize_t>>> ranges = util::positionToIndex({0.0, 2.0, 10.0}, {10.0, 2.0, 5.0}, {unit, unit, unit}, RangeMatch::Inclusive, sampledDim);
vector<optional<pair<ndsize_t, ndsize_t>>> 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]);
Expand All @@ -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<pair<ndsize_t, ndsize_t>> ranges = util::positionToIndex({0.0}, {0.0}, {unit}, sampledDim);
CPPUNIT_ASSERT(ranges[0].first == 0 && ranges[0].second == 0);
}
Expand Down
32 changes: 16 additions & 16 deletions test/BaseTestDimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ndsize_t, ndsize_t> 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);
Expand All @@ -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<std::pair<ndsize_t, ndsize_t>> ranges = sd.indexOf({1.0, 20.0, 40.0}, {10.9, 12., 1.});
std::vector<std::pair<ndsize_t, ndsize_t>> 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.);
Expand Down Expand Up @@ -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);
Expand All @@ -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<boost::optional<std::pair<ndsize_t, ndsize_t>>> ranges = sd.indexOf({1.0, 20.0, 40.0, 5.0}, {10.9, 12.0, 1.0, 5.0}, RangeMatch::Inclusive);
std::vector<boost::optional<std::pair<ndsize_t, ndsize_t>>> 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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit ec46176

Please sign in to comment.