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