rippled
Loading...
Searching...
No Matches
RangeSet.cpp
1#include <xrpl/basics/RangeSet.h>
2
3#include <doctest/doctest.h>
4
5#include <cstdint>
6#include <optional>
7
8using namespace ripple;
9
10TEST_SUITE_BEGIN("RangeSet");
11
12TEST_CASE("prevMissing")
13{
14 // Set will include:
15 // [ 0, 5]
16 // [10,15]
17 // [20,25]
18 // etc...
19
21 for (std::uint32_t i = 0; i < 10; ++i)
22 set.insert(range(10 * i, 10 * i + 5));
23
24 for (std::uint32_t i = 1; i < 100; ++i)
25 {
27 // no prev missing in domain for i <= 6
28 if (i > 6)
29 {
30 std::uint32_t const oneBelowRange = (10 * (i / 10)) - 1;
31
32 expected = ((i % 10) > 6) ? (i - 1) : oneBelowRange;
33 }
34 CHECK(prevMissing(set, i) == expected);
35 }
36}
37
38TEST_CASE("toString")
39{
41 CHECK(to_string(set) == "empty");
42
43 set.insert(1);
44 CHECK(to_string(set) == "1");
45
46 set.insert(range(4u, 6u));
47 CHECK(to_string(set) == "1,4-6");
48
49 set.insert(2);
50 CHECK(to_string(set) == "1-2,4-6");
51
52 set.erase(range(4u, 5u));
53 CHECK(to_string(set) == "1-2,6");
54}
55
56TEST_CASE("fromString")
57{
59
60 CHECK(!from_string(set, ""));
61 CHECK(boost::icl::length(set) == 0);
62
63 CHECK(!from_string(set, "#"));
64 CHECK(boost::icl::length(set) == 0);
65
66 CHECK(!from_string(set, ","));
67 CHECK(boost::icl::length(set) == 0);
68
69 CHECK(!from_string(set, ",-"));
70 CHECK(boost::icl::length(set) == 0);
71
72 CHECK(!from_string(set, "1,,2"));
73 CHECK(boost::icl::length(set) == 0);
74
75 CHECK(from_string(set, "1"));
76 CHECK(boost::icl::length(set) == 1);
77 CHECK(boost::icl::first(set) == 1);
78
79 CHECK(from_string(set, "1,1"));
80 CHECK(boost::icl::length(set) == 1);
81 CHECK(boost::icl::first(set) == 1);
82
83 CHECK(from_string(set, "1-1"));
84 CHECK(boost::icl::length(set) == 1);
85 CHECK(boost::icl::first(set) == 1);
86
87 CHECK(from_string(set, "1,4-6"));
88 CHECK(boost::icl::length(set) == 4);
89 CHECK(boost::icl::first(set) == 1);
90 CHECK(!boost::icl::contains(set, 2));
91 CHECK(!boost::icl::contains(set, 3));
92 CHECK(boost::icl::contains(set, 4));
93 CHECK(boost::icl::contains(set, 5));
94 CHECK(boost::icl::last(set) == 6);
95
96 CHECK(from_string(set, "1-2,4-6"));
97 CHECK(boost::icl::length(set) == 5);
98 CHECK(boost::icl::first(set) == 1);
99 CHECK(boost::icl::contains(set, 2));
100 CHECK(boost::icl::contains(set, 4));
101 CHECK(boost::icl::last(set) == 6);
102
103 CHECK(from_string(set, "1-2,6"));
104 CHECK(boost::icl::length(set) == 3);
105 CHECK(boost::icl::first(set) == 1);
106 CHECK(boost::icl::contains(set, 2));
107 CHECK(boost::icl::last(set) == 6);
108}
109
110TEST_SUITE_END();
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::optional< T > prevMissing(RangeSet< T > const &rs, T t, T minVal=0)
Find the largest value not in the set that is less than a given value.
Definition RangeSet.h:164
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
bool from_string(RangeSet< T > &rs, std::string const &s)
Convert the given styled string to a RangeSet.
Definition RangeSet.h:105
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition RangeSet.h:51
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:35