rippled
Loading...
Searching...
No Matches
RangeSet_test.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/RangeSet.h>
21#include <xrpl/beast/unit_test.h>
22
23namespace ripple {
25{
26public:
27 void
29 {
30 testcase("prevMissing");
31
32 // Set will include:
33 // [ 0, 5]
34 // [10,15]
35 // [20,25]
36 // etc...
37
39 for (std::uint32_t i = 0; i < 10; ++i)
40 set.insert(range(10 * i, 10 * i + 5));
41
42 for (std::uint32_t i = 1; i < 100; ++i)
43 {
45 // no prev missing in domain for i <= 6
46 if (i > 6)
47 {
48 std::uint32_t const oneBelowRange = (10 * (i / 10)) - 1;
49
50 expected = ((i % 10) > 6) ? (i - 1) : oneBelowRange;
51 }
52 BEAST_EXPECT(prevMissing(set, i) == expected);
53 }
54 }
55
56 void
58 {
59 testcase("toString");
60
62 BEAST_EXPECT(to_string(set) == "empty");
63
64 set.insert(1);
65 BEAST_EXPECT(to_string(set) == "1");
66
67 set.insert(range(4u, 6u));
68 BEAST_EXPECT(to_string(set) == "1,4-6");
69
70 set.insert(2);
71 BEAST_EXPECT(to_string(set) == "1-2,4-6");
72
73 set.erase(range(4u, 5u));
74 BEAST_EXPECT(to_string(set) == "1-2,6");
75 }
76
77 void
79 {
80 testcase("fromString");
81
83
84 BEAST_EXPECT(!from_string(set, ""));
85 BEAST_EXPECT(boost::icl::length(set) == 0);
86
87 BEAST_EXPECT(!from_string(set, "#"));
88 BEAST_EXPECT(boost::icl::length(set) == 0);
89
90 BEAST_EXPECT(!from_string(set, ","));
91 BEAST_EXPECT(boost::icl::length(set) == 0);
92
93 BEAST_EXPECT(!from_string(set, ",-"));
94 BEAST_EXPECT(boost::icl::length(set) == 0);
95
96 BEAST_EXPECT(!from_string(set, "1,,2"));
97 BEAST_EXPECT(boost::icl::length(set) == 0);
98
99 BEAST_EXPECT(from_string(set, "1"));
100 BEAST_EXPECT(boost::icl::length(set) == 1);
101 BEAST_EXPECT(boost::icl::first(set) == 1);
102
103 BEAST_EXPECT(from_string(set, "1,1"));
104 BEAST_EXPECT(boost::icl::length(set) == 1);
105 BEAST_EXPECT(boost::icl::first(set) == 1);
106
107 BEAST_EXPECT(from_string(set, "1-1"));
108 BEAST_EXPECT(boost::icl::length(set) == 1);
109 BEAST_EXPECT(boost::icl::first(set) == 1);
110
111 BEAST_EXPECT(from_string(set, "1,4-6"));
112 BEAST_EXPECT(boost::icl::length(set) == 4);
113 BEAST_EXPECT(boost::icl::first(set) == 1);
114 BEAST_EXPECT(!boost::icl::contains(set, 2));
115 BEAST_EXPECT(!boost::icl::contains(set, 3));
116 BEAST_EXPECT(boost::icl::contains(set, 4));
117 BEAST_EXPECT(boost::icl::contains(set, 5));
118 BEAST_EXPECT(boost::icl::last(set) == 6);
119
120 BEAST_EXPECT(from_string(set, "1-2,4-6"));
121 BEAST_EXPECT(boost::icl::length(set) == 5);
122 BEAST_EXPECT(boost::icl::first(set) == 1);
123 BEAST_EXPECT(boost::icl::contains(set, 2));
124 BEAST_EXPECT(boost::icl::contains(set, 4));
125 BEAST_EXPECT(boost::icl::last(set) == 6);
126
127 BEAST_EXPECT(from_string(set, "1-2,6"));
128 BEAST_EXPECT(boost::icl::length(set) == 3);
129 BEAST_EXPECT(boost::icl::first(set) == 1);
130 BEAST_EXPECT(boost::icl::contains(set, 2));
131 BEAST_EXPECT(boost::icl::last(set) == 6);
132 }
133 void
134 run() override
135 {
137 testToString();
139 }
140};
141
142BEAST_DEFINE_TESTSUITE(RangeSet, ripple_basics, ripple);
143
144} // namespace ripple
A testsuite class.
Definition: suite.h:53
testcase_t testcase
Memberspace for declaring test cases.
Definition: suite.h:153
void run() override
Runs the suite.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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:183
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,...
Definition: BasicConfig.h:316
bool from_string(RangeSet< T > &rs, std::string const &s)
Convert the given styled string to a RangeSet.
Definition: RangeSet.h:124
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition: RangeSet.h:70
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:54