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  set.clear();
100  BEAST_EXPECT(from_string(set, "1"));
101  BEAST_EXPECT(boost::icl::length(set) == 1);
102  BEAST_EXPECT(boost::icl::first(set) == 1);
103 
104  set.clear();
105  BEAST_EXPECT(from_string(set, "1,1"));
106  BEAST_EXPECT(boost::icl::length(set) == 1);
107  BEAST_EXPECT(boost::icl::first(set) == 1);
108 
109  set.clear();
110  BEAST_EXPECT(from_string(set, "1-1"));
111  BEAST_EXPECT(boost::icl::length(set) == 1);
112  BEAST_EXPECT(boost::icl::first(set) == 1);
113 
114  set.clear();
115  BEAST_EXPECT(from_string(set, "1,4-6"));
116  BEAST_EXPECT(boost::icl::length(set) == 4);
117  BEAST_EXPECT(boost::icl::first(set) == 1);
118  BEAST_EXPECT(!boost::icl::contains(set, 2));
119  BEAST_EXPECT(!boost::icl::contains(set, 3));
120  BEAST_EXPECT(boost::icl::contains(set, 4));
121  BEAST_EXPECT(boost::icl::contains(set, 5));
122  BEAST_EXPECT(boost::icl::last(set) == 6);
123 
124  set.clear();
125  BEAST_EXPECT(from_string(set, "1-2,4-6"));
126  BEAST_EXPECT(boost::icl::length(set) == 5);
127  BEAST_EXPECT(boost::icl::first(set) == 1);
128  BEAST_EXPECT(boost::icl::contains(set, 2));
129  BEAST_EXPECT(boost::icl::contains(set, 4));
130  BEAST_EXPECT(boost::icl::last(set) == 6);
131 
132  set.clear();
133  BEAST_EXPECT(from_string(set, "1-2,6"));
134  BEAST_EXPECT(boost::icl::length(set) == 3);
135  BEAST_EXPECT(boost::icl::first(set) == 1);
136  BEAST_EXPECT(boost::icl::contains(set, 2));
137  BEAST_EXPECT(boost::icl::last(set) == 6);
138  }
139  void
140  run() override
141  {
142  testPrevMissing();
143  testToString();
144  testFromString();
145  }
146 };
147 
148 BEAST_DEFINE_TESTSUITE(RangeSet, ripple_basics, ripple);
149 
150 } // 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:126
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:276
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:184
ripple::RangeSet_test::run
void run() override
Definition: RangeSet_test.cpp:140
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:40
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