maximum_gap.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2015, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Licensed under the Boost Software License version 1.0.
  7. // http://www.boost.org/users/license.html
  8. #ifndef BOOST_TEST_MODULE
  9. #define BOOST_TEST_MODULE test_maximum_gap
  10. #endif
  11. #include <boost/test/included/unit_test.hpp>
  12. #include <cstddef>
  13. #include <iostream>
  14. #include <sstream>
  15. #include <vector>
  16. #include <boost/geometry/algorithms/detail/max_interval_gap.hpp>
  17. namespace bg = boost::geometry;
  18. class uint_interval
  19. {
  20. public:
  21. typedef unsigned value_type;
  22. typedef int difference_type;
  23. uint_interval(unsigned left, unsigned right)
  24. : m_left(left)
  25. , m_right(right)
  26. {}
  27. template <std::size_t Index>
  28. value_type get() const
  29. {
  30. return (Index == 0) ? m_left : m_right;
  31. }
  32. difference_type length() const
  33. {
  34. return static_cast<int>(m_right) - static_cast<int>(m_left);
  35. }
  36. private:
  37. unsigned m_left, m_right;
  38. };
  39. struct uint_intervals
  40. : public std::vector<uint_interval>
  41. {
  42. uint_intervals()
  43. {}
  44. uint_intervals(unsigned left, unsigned right)
  45. {
  46. this->push_back(uint_interval(left, right));
  47. }
  48. uint_intervals & operator()(unsigned left, unsigned right)
  49. {
  50. this->push_back(uint_interval(left, right));
  51. return *this;
  52. }
  53. };
  54. std::ostream& operator<<(std::ostream& os, uint_interval const& interval)
  55. {
  56. os << "[" << interval.get<0>() << ", " << interval.get<1>() << "]";
  57. return os;
  58. }
  59. template <typename RangeOfIntervals>
  60. inline void test_one(std::string const& case_id,
  61. RangeOfIntervals const& intervals,
  62. typename boost::range_value
  63. <
  64. RangeOfIntervals
  65. >::type::difference_type expected_gap)
  66. {
  67. typedef typename boost::range_value<RangeOfIntervals>::type interval_type;
  68. typedef typename interval_type::difference_type gap_type;
  69. gap_type gap = bg::maximum_gap(intervals);
  70. std::ostringstream stream;
  71. for (typename boost::range_const_iterator<RangeOfIntervals>::type
  72. it = boost::const_begin(intervals);
  73. it != boost::const_end(intervals);
  74. ++it)
  75. {
  76. stream << " " << *it;
  77. }
  78. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  79. std::cout << "intervals:" << stream.str() << std::endl;
  80. std::cout << "gap found? " << ((gap > 0) ? "yes" : "no") << std::endl;
  81. std::cout << "max gap length: " << gap << std::endl;
  82. std::cout << std::endl << std::endl;
  83. #endif
  84. BOOST_CHECK_MESSAGE(gap == expected_gap,
  85. case_id << "; intervals: "
  86. << stream.str()
  87. << "; expected: " << expected_gap
  88. << ", detected: " << gap);
  89. }
  90. BOOST_AUTO_TEST_CASE( test_maximum_gap )
  91. {
  92. uint_intervals intervals;
  93. intervals = uint_intervals(3,4)(1,10)(5,11)(20,35)(12,14)(36,40)(39,41)(35,36)(37,37)(50,50)(50,51);
  94. test_one("case_01", intervals, 9);
  95. intervals = uint_intervals(3,4)(1,10)(5,11)(20,35)(52,60)(12,14)(36,40)(39,41)(35,36)(37,37)(55,56);
  96. test_one("case_02", intervals, 11);
  97. intervals = uint_intervals(3,4);
  98. test_one("case_03", intervals, 0);
  99. intervals = uint_intervals(3,4)(15,15);
  100. test_one("case_04", intervals, 11);
  101. intervals = uint_intervals(3,14)(5,5)(5,6);
  102. test_one("case_05", intervals, 0);
  103. intervals = uint_intervals(3,10)(15,15)(15,18)(15,16);
  104. test_one("case_06", intervals, 5);
  105. intervals = uint_intervals(38,41)(3,10)(15,15)(15,18)(15,16)(20,30)(22,30)(23,30);
  106. test_one("case_07", intervals, 8);
  107. }