Fabcoin Core  0.16.2
P2P Digital Currency
RangeMask.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include <libdevcore/RangeMask.h>
24 
25 using namespace std;
26 using namespace dev;
27 using namespace boost::unit_test;
28 
29 namespace dev
30 {
31 namespace test
32 {
33 
34 BOOST_FIXTURE_TEST_SUITE(RangeMaskTest, TestOutputHelper)
35 
37 {
38  using RM = RangeMask<unsigned>;
39  using Range = pair<unsigned, unsigned>;
40  for (RM r: {RM(), RM(1, 10), RM(Range(2, 10))})
41  {
42  BOOST_CHECK(r.empty());
43  BOOST_CHECK(!r.contains(0));
44  BOOST_CHECK(!r.contains(1));
45  BOOST_CHECK_EQUAL(0, r.size());
46  }
47  BOOST_CHECK(RM().full());
48  BOOST_CHECK(!RM(1, 10).full());
49  BOOST_CHECK(!RM(Range(2, 10)).full());
50 }
51 
52 BOOST_AUTO_TEST_CASE(simple_unions)
53 {
54  using RM = RangeMask<unsigned>;
55  using Range = pair<unsigned, unsigned>;
56  RM m(Range(0, 2000));
57  m.unionWith(Range(1, 2));
58  BOOST_CHECK_EQUAL(m.size(), 1);
59  m.unionWith(Range(50, 250));
60  BOOST_CHECK_EQUAL(m.size(), 201);
61  m.unionWith(Range(10, 16));
62  BOOST_CHECK_EQUAL(m.size(), 207);
63  BOOST_CHECK(m.contains(1));
64  BOOST_CHECK(m.contains(11));
65  BOOST_CHECK(m.contains(51));
66  BOOST_CHECK(m.contains(200));
67  BOOST_CHECK(!m.contains(2));
68  BOOST_CHECK(!m.contains(7));
69  BOOST_CHECK(!m.contains(17));
70  BOOST_CHECK(!m.contains(258));
71 }
72 
74 {
75  using RM = RangeMask<unsigned>;
76  using Range = pair<unsigned, unsigned>;
77  RM m(Range(0, 2000));
78  m.unionWith(Range(3, 6));
79  BOOST_CHECK_EQUAL(m.size(), 3);
80  m.unionWith(Range(50, 50));
81  BOOST_CHECK_EQUAL(m.size(), 3);
82  m.unionWith(Range(0, 0));
83  BOOST_CHECK_EQUAL(m.size(), 3);
84  m.unionWith(Range(1, 1));
85  BOOST_CHECK_EQUAL(m.size(), 3);
86  m.unionWith(Range(2, 2));
87  BOOST_CHECK_EQUAL(m.size(), 3);
88  m.unionWith(Range(3, 3));
89  BOOST_CHECK_EQUAL(m.size(), 3);
90 }
91 
92 BOOST_AUTO_TEST_CASE(overlapping_unions)
93 {
94  using RM = RangeMask<unsigned>;
95  using Range = pair<unsigned, unsigned>;
96  RM m(Range(0, 2000));
97  m.unionWith(Range(10, 20));
98  BOOST_CHECK_EQUAL(10, m.size());
99  m.unionWith(Range(30, 40));
100  BOOST_CHECK_EQUAL(20, m.size());
101  m.unionWith(Range(15, 30));
102  BOOST_CHECK_EQUAL(40 - 10, m.size());
103  m.unionWith(Range(50, 60));
104  m.unionWith(Range(45, 55));
105  // [40, 45) still missing here
106  BOOST_CHECK_EQUAL(60 - 10 - 5, m.size());
107  m.unionWith(Range(15, 56));
108  BOOST_CHECK_EQUAL(60 - 10, m.size());
109  m.unionWith(Range(15, 65));
110  BOOST_CHECK_EQUAL(65 - 10, m.size());
111  m.unionWith(Range(5, 70));
112  BOOST_CHECK_EQUAL(70 - 5, m.size());
113 }
114 
116 {
117  using RM = RangeMask<unsigned>;
118  using Range = pair<unsigned, unsigned>;
119  RM m(Range(0, 2000));
120  m.unionWith(7).unionWith(9);
121  m = ~m;
122  m.unionWith(7).unionWith(9);
123  m = ~m;
124  BOOST_CHECK(m.empty());
125 
126  m += Range(0, 10);
127  m += Range(1000, 2000);
128  m.invert();
129  BOOST_CHECK_EQUAL(m.size(), 1000 - 10);
130 }
131 
133 {
134  using RM = RangeMask<unsigned>;
135  using Range = pair<unsigned, unsigned>;
136  RM m(Range(0, 2000));
137  m.unionWith(Range(7, 9));
138  m.unionWith(11);
139  m.unionWith(Range(200, 205));
140 
141  vector<unsigned> elements;
142  copy(m.begin(), m.end(), back_inserter(elements));
143  BOOST_CHECK(elements == (vector<unsigned>{7, 8, 11, 200, 201, 202, 203, 204}));
144 }
145 
147 
148 }
149 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::hash for asio::adress
Definition: Common.h:323
BOOST_AUTO_TEST_CASE(iterator)
Definition: RangeMask.cpp:132
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
Set of elements of a certain "ground range" representable by unions of ranges inside this ground rang...
Definition: RangeMask.h:46
Helper functions to work with json::spirit and test files.
#define BOOST_CHECK(expr)
Definition: object.cpp:17