rippled
Loading...
Searching...
No Matches
RangeSet.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012 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
22#include <doctest/doctest.h>
23
24#include <cstdint>
25#include <optional>
26
27using namespace ripple;
28
29TEST_SUITE_BEGIN("RangeSet");
30
31TEST_CASE("prevMissing")
32{
33 // Set will include:
34 // [ 0, 5]
35 // [10,15]
36 // [20,25]
37 // etc...
38
40 for (std::uint32_t i = 0; i < 10; ++i)
41 set.insert(range(10 * i, 10 * i + 5));
42
43 for (std::uint32_t i = 1; i < 100; ++i)
44 {
46 // no prev missing in domain for i <= 6
47 if (i > 6)
48 {
49 std::uint32_t const oneBelowRange = (10 * (i / 10)) - 1;
50
51 expected = ((i % 10) > 6) ? (i - 1) : oneBelowRange;
52 }
53 CHECK(prevMissing(set, i) == expected);
54 }
55}
56
57TEST_CASE("toString")
58{
60 CHECK(to_string(set) == "empty");
61
62 set.insert(1);
63 CHECK(to_string(set) == "1");
64
65 set.insert(range(4u, 6u));
66 CHECK(to_string(set) == "1,4-6");
67
68 set.insert(2);
69 CHECK(to_string(set) == "1-2,4-6");
70
71 set.erase(range(4u, 5u));
72 CHECK(to_string(set) == "1-2,6");
73}
74
75TEST_CASE("fromString")
76{
78
79 CHECK(!from_string(set, ""));
80 CHECK(boost::icl::length(set) == 0);
81
82 CHECK(!from_string(set, "#"));
83 CHECK(boost::icl::length(set) == 0);
84
85 CHECK(!from_string(set, ","));
86 CHECK(boost::icl::length(set) == 0);
87
88 CHECK(!from_string(set, ",-"));
89 CHECK(boost::icl::length(set) == 0);
90
91 CHECK(!from_string(set, "1,,2"));
92 CHECK(boost::icl::length(set) == 0);
93
94 CHECK(from_string(set, "1"));
95 CHECK(boost::icl::length(set) == 1);
96 CHECK(boost::icl::first(set) == 1);
97
98 CHECK(from_string(set, "1,1"));
99 CHECK(boost::icl::length(set) == 1);
100 CHECK(boost::icl::first(set) == 1);
101
102 CHECK(from_string(set, "1-1"));
103 CHECK(boost::icl::length(set) == 1);
104 CHECK(boost::icl::first(set) == 1);
105
106 CHECK(from_string(set, "1,4-6"));
107 CHECK(boost::icl::length(set) == 4);
108 CHECK(boost::icl::first(set) == 1);
109 CHECK(!boost::icl::contains(set, 2));
110 CHECK(!boost::icl::contains(set, 3));
111 CHECK(boost::icl::contains(set, 4));
112 CHECK(boost::icl::contains(set, 5));
113 CHECK(boost::icl::last(set) == 6);
114
115 CHECK(from_string(set, "1-2,4-6"));
116 CHECK(boost::icl::length(set) == 5);
117 CHECK(boost::icl::first(set) == 1);
118 CHECK(boost::icl::contains(set, 2));
119 CHECK(boost::icl::contains(set, 4));
120 CHECK(boost::icl::last(set) == 6);
121
122 CHECK(from_string(set, "1-2,6"));
123 CHECK(boost::icl::length(set) == 3);
124 CHECK(boost::icl::first(set) == 1);
125 CHECK(boost::icl::contains(set, 2));
126 CHECK(boost::icl::last(set) == 6);
127}
128
129TEST_SUITE_END();
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:315
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:630
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:54