RangeMask.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file RangeMask.cpp
  15. * @author Christian <c@ethdev.com>
  16. * @date 2015
  17. */
  18. #include <libdevcore/RangeMask.h>
  19. #include <test/TestHelper.h>
  20. using namespace std;
  21. using namespace dev;
  22. using namespace boost::unit_test;
  23. namespace dev
  24. {
  25. namespace test
  26. {
  27. BOOST_FIXTURE_TEST_SUITE(RangeMaskTest, TestOutputHelper)
  28. BOOST_AUTO_TEST_CASE(constructor)
  29. {
  30. using RM = RangeMask<unsigned>;
  31. using Range = pair<unsigned, unsigned>;
  32. for (RM r: {RM(), RM(1, 10), RM(Range(2, 10))})
  33. {
  34. BOOST_CHECK(r.empty());
  35. BOOST_CHECK(!r.contains(0));
  36. BOOST_CHECK(!r.contains(1));
  37. BOOST_CHECK_EQUAL(0, r.size());
  38. }
  39. BOOST_CHECK(RM().full());
  40. BOOST_CHECK(!RM(1, 10).full());
  41. BOOST_CHECK(!RM(Range(2, 10)).full());
  42. }
  43. BOOST_AUTO_TEST_CASE(simple_unions)
  44. {
  45. using RM = RangeMask<unsigned>;
  46. using Range = pair<unsigned, unsigned>;
  47. RM m(Range(0, 2000));
  48. m.unionWith(Range(1, 2));
  49. BOOST_CHECK_EQUAL(m.size(), 1);
  50. m.unionWith(Range(50, 250));
  51. BOOST_CHECK_EQUAL(m.size(), 201);
  52. m.unionWith(Range(10, 16));
  53. BOOST_CHECK_EQUAL(m.size(), 207);
  54. BOOST_CHECK(m.contains(1));
  55. BOOST_CHECK(m.contains(11));
  56. BOOST_CHECK(m.contains(51));
  57. BOOST_CHECK(m.contains(200));
  58. BOOST_CHECK(!m.contains(2));
  59. BOOST_CHECK(!m.contains(7));
  60. BOOST_CHECK(!m.contains(17));
  61. BOOST_CHECK(!m.contains(258));
  62. }
  63. BOOST_AUTO_TEST_CASE(empty_union)
  64. {
  65. using RM = RangeMask<unsigned>;
  66. using Range = pair<unsigned, unsigned>;
  67. RM m(Range(0, 2000));
  68. m.unionWith(Range(3, 6));
  69. BOOST_CHECK_EQUAL(m.size(), 3);
  70. m.unionWith(Range(50, 50));
  71. BOOST_CHECK_EQUAL(m.size(), 3);
  72. m.unionWith(Range(0, 0));
  73. BOOST_CHECK_EQUAL(m.size(), 3);
  74. m.unionWith(Range(1, 1));
  75. BOOST_CHECK_EQUAL(m.size(), 3);
  76. m.unionWith(Range(2, 2));
  77. BOOST_CHECK_EQUAL(m.size(), 3);
  78. m.unionWith(Range(3, 3));
  79. BOOST_CHECK_EQUAL(m.size(), 3);
  80. }
  81. BOOST_AUTO_TEST_CASE(overlapping_unions)
  82. {
  83. using RM = RangeMask<unsigned>;
  84. using Range = pair<unsigned, unsigned>;
  85. RM m(Range(0, 2000));
  86. m.unionWith(Range(10, 20));
  87. BOOST_CHECK_EQUAL(10, m.size());
  88. m.unionWith(Range(30, 40));
  89. BOOST_CHECK_EQUAL(20, m.size());
  90. m.unionWith(Range(15, 30));
  91. BOOST_CHECK_EQUAL(40 - 10, m.size());
  92. m.unionWith(Range(50, 60));
  93. m.unionWith(Range(45, 55));
  94. // [40, 45) still missing here
  95. BOOST_CHECK_EQUAL(60 - 10 - 5, m.size());
  96. m.unionWith(Range(15, 56));
  97. BOOST_CHECK_EQUAL(60 - 10, m.size());
  98. m.unionWith(Range(15, 65));
  99. BOOST_CHECK_EQUAL(65 - 10, m.size());
  100. m.unionWith(Range(5, 70));
  101. BOOST_CHECK_EQUAL(70 - 5, m.size());
  102. }
  103. BOOST_AUTO_TEST_CASE(complement)
  104. {
  105. using RM = RangeMask<unsigned>;
  106. using Range = pair<unsigned, unsigned>;
  107. RM m(Range(0, 2000));
  108. m.unionWith(7).unionWith(9);
  109. m = ~m;
  110. m.unionWith(7).unionWith(9);
  111. m = ~m;
  112. BOOST_CHECK(m.empty());
  113. m += Range(0, 10);
  114. m += Range(1000, 2000);
  115. m.invert();
  116. BOOST_CHECK_EQUAL(m.size(), 1000 - 10);
  117. }
  118. BOOST_AUTO_TEST_CASE(iterator)
  119. {
  120. using RM = RangeMask<unsigned>;
  121. using Range = pair<unsigned, unsigned>;
  122. RM m(Range(0, 2000));
  123. m.unionWith(Range(7, 9));
  124. m.unionWith(11);
  125. m.unionWith(Range(200, 205));
  126. vector<unsigned> elements;
  127. copy(m.begin(), m.end(), back_inserter(elements));
  128. BOOST_CHECK(elements == (vector<unsigned>{7, 8, 11, 200, 201, 202, 203, 204}));
  129. }
  130. BOOST_AUTO_TEST_SUITE_END()
  131. }
  132. }