partition_point_test1.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright (c) Marshall Clow 2011-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. For more information, see http://www.boost.org
  6. */
  7. #include <iostream>
  8. #include <boost/config.hpp>
  9. #include <boost/algorithm/cxx11/partition_point.hpp>
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <string>
  13. #include <vector>
  14. #include <list>
  15. namespace ba = boost::algorithm;
  16. // namespace ba = boost;
  17. template <typename Container>
  18. typename Container::iterator offset_to_iter ( Container &v, int offset ) {
  19. typename Container::iterator retval;
  20. if ( offset >= 0 ) {
  21. retval = v.begin ();
  22. std::advance ( retval, offset );
  23. }
  24. else {
  25. retval = v.end ();
  26. std::advance ( retval, offset + 1 );
  27. }
  28. return retval;
  29. }
  30. template <typename Container, typename Predicate>
  31. void test_sequence ( Container &v, Predicate comp, int expected ) {
  32. typename Container::iterator res, exp;
  33. res = ba::partition_point ( v.begin (), v.end (), comp );
  34. exp = offset_to_iter ( v, expected );
  35. BOOST_CHECK ( exp == res );
  36. // Duplicate the last element; this checks for any even/odd problems
  37. v.push_back ( * v.rbegin ());
  38. res = ba::partition_point ( v.begin (), v.end (), comp );
  39. exp = offset_to_iter ( v, expected );
  40. BOOST_CHECK ( exp == res );
  41. // Range based test
  42. res = ba::partition_point ( v, comp );
  43. exp = offset_to_iter ( v, expected );
  44. BOOST_CHECK ( exp == res );
  45. }
  46. template <typename T>
  47. struct less_than {
  48. public:
  49. less_than ( T foo ) : val ( foo ) {}
  50. less_than ( const less_than &rhs ) : val ( rhs.val ) {}
  51. bool operator () ( const T &v ) const { return v < val; }
  52. private:
  53. less_than ();
  54. less_than operator = ( const less_than &rhs );
  55. T val;
  56. };
  57. void test_sequence1 () {
  58. std::vector<int> v;
  59. v.clear ();
  60. for ( int i = 5; i < 15; ++i )
  61. v.push_back ( i );
  62. test_sequence ( v, less_than<int>(3), 0 ); // no elements
  63. test_sequence ( v, less_than<int>(6), 1 ); // only the first element
  64. test_sequence ( v, less_than<int>(10), 5 );
  65. test_sequence ( v, less_than<int>(99), -1 ); // all elements satisfy
  66. // With bidirectional iterators.
  67. std::list<int> l;
  68. for ( int i = 5; i < 15; ++i )
  69. l.push_back ( i );
  70. test_sequence ( l, less_than<int>(3), 0 ); // no elements
  71. test_sequence ( l, less_than<int>(6), 1 ); // only the first element
  72. test_sequence ( l, less_than<int>(10), 5 );
  73. test_sequence ( l, less_than<int>(99), -1 ); // all elements satisfy
  74. }
  75. BOOST_AUTO_TEST_CASE( test_main )
  76. {
  77. test_sequence1 ();
  78. }