From 5783f0996876739fe04caf22291315cb1480dee4 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Wed, 24 Jul 2013 12:11:40 -0700 Subject: [PATCH] Add prevMissing unit test to RangeSet --- .../containers/ripple_RangeSet.cpp | 42 ++++++++++++++++++- .../containers/ripple_RangeSet.h | 4 ++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/modules/ripple_basics/containers/ripple_RangeSet.cpp b/modules/ripple_basics/containers/ripple_RangeSet.cpp index 878984c0d..edd9314f3 100644 --- a/modules/ripple_basics/containers/ripple_RangeSet.cpp +++ b/modules/ripple_basics/containers/ripple_RangeSet.cpp @@ -251,7 +251,23 @@ public: { } - void runTest () + RangeSet createPredefinedSet () + { + 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; + } + + void testMembership () { beginTest ("membership"); @@ -268,6 +284,30 @@ public: expect (!r1.hasValue (5)); expect (r2.hasValue (9)); + } + + void testPrevMissing () + { + beginTest ("prevMissing"); + + RangeSet const set = createPredefinedSet (); + + for (int i = 0; i < 100; ++i) + { + int const oneBelowRange = (10*(i/10))-1; + + int const expectedPrevMissing = + ((i % 10) > 6) ? (i-1) : oneBelowRange; + + expect (set.prevMissing (i) == expectedPrevMissing); + } + } + + void runTest () + { + testMembership (); + + testPrevMissing (); // TODO: Traverse functions must be tested } diff --git a/modules/ripple_basics/containers/ripple_RangeSet.h b/modules/ripple_basics/containers/ripple_RangeSet.h index bc41095d6..09d1ccf4f 100644 --- a/modules/ripple_basics/containers/ripple_RangeSet.h +++ b/modules/ripple_basics/containers/ripple_RangeSet.h @@ -28,8 +28,12 @@ public: // largest number not in the set that is less than the given number uint32 prevMissing (uint32) const; + // Add an item to the set void setValue (uint32); + + // Add the closed interval to the set void setRange (uint32, uint32); + void clearValue (uint32); std::string toString () const;