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 {
25 class RangeSet_test : public beast::unit_test::suite
26 {
27 public:
28  void
30  {
31  testcase("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  {
45  boost::optional<std::uint32_t> expected;
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  BEAST_EXPECT(prevMissing(set, i) == expected);
54  }
55  }
56 
57  void
59  {
60  testcase("toString");
61 
63  BEAST_EXPECT(to_string(set) == "empty");
64 
65  set.insert(1);
66  BEAST_EXPECT(to_string(set) == "1");
67 
68  set.insert(range(4u, 6u));
69  BEAST_EXPECT(to_string(set) == "1,4-6");
70 
71  set.insert(2);
72  BEAST_EXPECT(to_string(set) == "1-2,4-6");
73 
74  set.erase(range(4u, 5u));
75  BEAST_EXPECT(to_string(set) == "1-2,6");
76  }
77 
78  void
80  {
81  testcase("fromString");
82 
84 
85  BEAST_EXPECT(!from_string(set, ""));
86  BEAST_EXPECT(boost::icl::length(set) == 0);
87 
88  BEAST_EXPECT(!from_string(set, "#"));
89  BEAST_EXPECT(boost::icl::length(set) == 0);
90 
91  BEAST_EXPECT(!from_string(set, ","));
92  BEAST_EXPECT(boost::icl::length(set) == 0);
93 
94  BEAST_EXPECT(!from_string(set, ",-"));
95  BEAST_EXPECT(boost::icl::length(set) == 0);
96 
97  BEAST_EXPECT(!from_string(set, "1,,2"));
98  BEAST_EXPECT(boost::icl::length(set) == 0);
99 
100  set.clear();
101  BEAST_EXPECT(from_string(set, "1"));
102  BEAST_EXPECT(boost::icl::length(set) == 1);
103  BEAST_EXPECT(boost::icl::first(set) == 1);
104 
105  set.clear();
106  BEAST_EXPECT(from_string(set, "1,1"));
107  BEAST_EXPECT(boost::icl::length(set) == 1);
108  BEAST_EXPECT(boost::icl::first(set) == 1);
109 
110  set.clear();
111  BEAST_EXPECT(from_string(set, "1-1"));
112  BEAST_EXPECT(boost::icl::length(set) == 1);
113  BEAST_EXPECT(boost::icl::first(set) == 1);
114 
115  set.clear();
116  BEAST_EXPECT(from_string(set, "1,4-6"));
117  BEAST_EXPECT(boost::icl::length(set) == 4);
118  BEAST_EXPECT(boost::icl::first(set) == 1);
119  BEAST_EXPECT(!boost::icl::contains(set, 2));
120  BEAST_EXPECT(!boost::icl::contains(set, 3));
121  BEAST_EXPECT(boost::icl::contains(set, 4));
122  BEAST_EXPECT(boost::icl::contains(set, 5));
123  BEAST_EXPECT(boost::icl::last(set) == 6);
124 
125  set.clear();
126  BEAST_EXPECT(from_string(set, "1-2,4-6"));
127  BEAST_EXPECT(boost::icl::length(set) == 5);
128  BEAST_EXPECT(boost::icl::first(set) == 1);
129  BEAST_EXPECT(boost::icl::contains(set, 2));
130  BEAST_EXPECT(boost::icl::contains(set, 4));
131  BEAST_EXPECT(boost::icl::last(set) == 6);
132 
133  set.clear();
134  BEAST_EXPECT(from_string(set, "1-2,6"));
135  BEAST_EXPECT(boost::icl::length(set) == 3);
136  BEAST_EXPECT(boost::icl::first(set) == 1);
137  BEAST_EXPECT(boost::icl::contains(set, 2));
138  BEAST_EXPECT(boost::icl::last(set) == 6);
139  }
140  void
141  run() override
142  {
143  testPrevMissing();
144  testToString();
145  testFromString();
146  }
147 };
148 
149 BEAST_DEFINE_TESTSUITE(RangeSet, ripple_basics, ripple);
150 
151 } // 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::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:41
ripple::RangeSet_test
Definition: RangeSet_test.cpp:25
ripple::prevMissing
boost::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:186
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:271
ripple::RangeSet_test::testFromString
void testFromString()
Definition: RangeSet_test.cpp:79
std::uint32_t
ripple::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:54
ripple::RangeSet_test::run
void run() override
Definition: RangeSet_test.cpp:141
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:29
ripple::RangeSet_test::testToString
void testToString()
Definition: RangeSet_test.cpp:58
ripple::RangeSet
boost::icl::interval_set< T, std::less, ClosedInterval< T > > RangeSet
A set of closed intervals over the domain T.
Definition: RangeSet.h:70