From e1033620c9c8f6d8bf45befd7ddf2e2e81c3ca76 Mon Sep 17 00:00:00 2001 From: David Schwartz Date: Thu, 25 Jul 2013 11:38:05 -0700 Subject: [PATCH 1/3] Fix prevMissing. --- .../containers/ripple_RangeSet.cpp | 22 ++++++++++++++----- src/cpp/ripple/LedgerMaster.cpp | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/modules/ripple_basics/containers/ripple_RangeSet.cpp b/modules/ripple_basics/containers/ripple_RangeSet.cpp index 30b0fcafbd..2482d5b5c7 100644 --- a/modules/ripple_basics/containers/ripple_RangeSet.cpp +++ b/modules/ripple_basics/containers/ripple_RangeSet.cpp @@ -66,7 +66,6 @@ uint32 RangeSet::getPrev (uint32 v) const return it.second; if (contains (it, v + 1)) - return v - 1; } return RangeSetAbsent; } @@ -74,15 +73,26 @@ uint32 RangeSet::getPrev (uint32 v) const uint32 RangeSet::prevMissing (uint32 v) const { // largest number not in the set that is less than the given number - BOOST_FOREACH (const value_type & it, mRanges) + uint32 result = RangeSetAbsent; + + BOOST_REVERSE_FOREACH (const value_type & it, mRanges) { if (contains (it, v)) - return it.first - 1; + { + if (it.first == 0) + result = RangeSetAbsent; + else + result = it.first - 1; + } - if (it.first > v) - return v + 1; + if (v > it.second) + { + if (v == it.second + 1) + result = it.first - 1; + result = v - 1; + } } - return RangeSetAbsent; + return result; } void RangeSet::setValue (uint32 v) diff --git a/src/cpp/ripple/LedgerMaster.cpp b/src/cpp/ripple/LedgerMaster.cpp index 7cfff58c15..359fa246f2 100644 --- a/src/cpp/ripple/LedgerMaster.cpp +++ b/src/cpp/ripple/LedgerMaster.cpp @@ -225,7 +225,7 @@ bool LedgerMaster::getValidatedRange (uint32& minVal, uint32& maxVal) minVal = mCompleteLedgers.prevMissing (maxVal); if (minVal == RangeSet::RangeSetAbsent) - minVal = 0; + minVal = maxVal; else ++minVal; From 05ee52a70dda4b182ec236fba1cfbad7a41c1368 Mon Sep 17 00:00:00 2001 From: David Schwartz Date: Thu, 25 Jul 2013 11:56:39 -0700 Subject: [PATCH 2/3] Clean fix. --- .../containers/ripple_RangeSet.cpp | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/modules/ripple_basics/containers/ripple_RangeSet.cpp b/modules/ripple_basics/containers/ripple_RangeSet.cpp index 2482d5b5c7..be3d29733b 100644 --- a/modules/ripple_basics/containers/ripple_RangeSet.cpp +++ b/modules/ripple_basics/containers/ripple_RangeSet.cpp @@ -73,26 +73,23 @@ uint32 RangeSet::getPrev (uint32 v) const uint32 RangeSet::prevMissing (uint32 v) const { // largest number not in the set that is less than the given number - uint32 result = RangeSetAbsent; - BOOST_REVERSE_FOREACH (const value_type & it, mRanges) + // Nothing before zero + if (v == 0) + return RangeSetAbsent; + + BOOST_FOREACH (const value_type & it, mRanges) { - if (contains (it, v)) - { + if (contains (it, v - 1)) + { // We have (v-1) in the set if (it.first == 0) - result = RangeSetAbsent; - else - result = it.first - 1; - } - - if (v > it.second) - { - if (v == it.second + 1) - result = it.first - 1; - result = v - 1; + return RangeSetAbent; + return it.first - 1; } } - return result; + + // We don't have (v-1), so v-1 is it + return v - 1; } void RangeSet::setValue (uint32 v) From 1e3ee9bbf8d6839a6219d87b6693c756b7b510ed Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 25 Jul 2013 07:00:35 -0700 Subject: [PATCH 3/3] Fix compile error, add unit test for prevMissing --- .../containers/ripple_RangeSet.cpp | 41 +++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/modules/ripple_basics/containers/ripple_RangeSet.cpp b/modules/ripple_basics/containers/ripple_RangeSet.cpp index be3d29733b..8c5a8b460b 100644 --- a/modules/ripple_basics/containers/ripple_RangeSet.cpp +++ b/modules/ripple_basics/containers/ripple_RangeSet.cpp @@ -66,6 +66,7 @@ uint32 RangeSet::getPrev (uint32 v) const return it.second; if (contains (it, v + 1)) + return v - 1; } return RangeSetAbsent; } @@ -83,7 +84,7 @@ uint32 RangeSet::prevMissing (uint32 v) const if (contains (it, v - 1)) { // We have (v-1) in the set if (it.first == 0) - return RangeSetAbent; + return RangeSetAbsent; return it.first - 1; } } @@ -189,6 +190,23 @@ void RangeSet::simplify () } } +static RangeSet createPredefinedRangeSet () +{ + RangeSet set; + + // Set will include: + // [ 0, 5] + // [10,15] + // [20,25] + // etc... + + for (int i = 0; i < 10; ++i) + set.setRange (10 * i, 10 * i + 5); + + return set; +} + + BOOST_AUTO_TEST_SUITE (RangeSet_suite) BOOST_AUTO_TEST_CASE (RangeSet_test) @@ -214,6 +232,23 @@ BOOST_AUTO_TEST_CASE (RangeSet_test) WriteLog (lsTRACE, RangeSet) << "RangeSet test complete"; } -BOOST_AUTO_TEST_SUITE_END () +BOOST_AUTO_TEST_CASE (RangeSetPrevMissing_test) +{ + RangeSet const set = createPredefinedRangeSet (); -// vim:ts=4 + for (int i = 0; i < 100; ++i) + { + int const oneBelowRange = (10*(i/10))-1; + + int const expectedPrevMissing = + ((i % 10) > 6) ? (i-1) : oneBelowRange; + + bool const pass = set.prevMissing (i) == expectedPrevMissing; + + BOOST_REQUIRE (pass); + + assert (pass); + } +} + +BOOST_AUTO_TEST_SUITE_END ()