rippled
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 <ripple/basics/RangeSet.h>
21 #include <ripple/beast/unit_test.h>
22 
23 namespace ripple {
24 class RangeSet_test : public beast::unit_test::suite
25 {
26 public:
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  {
136  testPrevMissing();
137  testToString();
138  testFromString();
139  }
140 };
141 
142 BEAST_DEFINE_TESTSUITE(RangeSet, ripple_basics, ripple);
143 
144 } // namespace ripple
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::from_string
bool from_string(RangeSet< T > &rs, std::string const &s)
Convert the given styled string to a RangeSet.
Definition: RangeSet.h:123
ripple::RangeSet_test
Definition: RangeSet_test.cpp:24
ripple::set
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:313
ripple::RangeSet_test::testFromString
void testFromString()
Definition: RangeSet_test.cpp:78
std::uint32_t
ripple::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:53
ripple::prevMissing
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:182
ripple::RangeSet_test::run
void run() override
Definition: RangeSet_test.cpp:134
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RangeSet_test::testPrevMissing
void testPrevMissing()
Definition: RangeSet_test.cpp:28
std::optional< std::uint32_t >
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::RangeSet_test::testToString
void testToString()
Definition: RangeSet_test.cpp:57
ripple::RangeSet
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition: RangeSet.h:69