is_partitioned_test1.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/is_partitioned.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 T>
  18. struct less_than {
  19. public:
  20. BOOST_CXX14_CONSTEXPR less_than ( T foo ) : val ( foo ) {}
  21. BOOST_CXX14_CONSTEXPR less_than ( const less_than &rhs ) : val ( rhs.val ) {}
  22. BOOST_CXX14_CONSTEXPR bool operator () ( const T &v ) const { return v < val; }
  23. private:
  24. less_than ();
  25. less_than operator = ( const less_than &rhs );
  26. T val;
  27. };
  28. BOOST_CXX14_CONSTEXPR bool test_constexpr() {
  29. int v[] = { 4, 5, 6, 7, 8, 9, 10 };
  30. bool res = true;
  31. res = ( res && ba::is_partitioned ( v, less_than<int>(3))); // no elements
  32. res = ( res && ba::is_partitioned ( v, less_than<int>(5))); // only the first element
  33. res = ( res && ba::is_partitioned ( v, less_than<int>(8))); // in the middle somewhere
  34. res = ( res && ba::is_partitioned ( v, less_than<int>(99))); // all elements
  35. return res;
  36. }
  37. void test_sequence1 () {
  38. std::vector<int> v;
  39. v.clear ();
  40. for ( int i = 5; i < 15; ++i )
  41. v.push_back ( i );
  42. BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(3))); // no elements
  43. BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(6))); // only the first element
  44. BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(10))); // in the middle somewhere
  45. BOOST_CHECK ( ba::is_partitioned ( v, less_than<int>(99))); // all elements satisfy
  46. // With bidirectional iterators.
  47. std::list<int> l;
  48. for ( int i = 5; i < 15; ++i )
  49. l.push_back ( i );
  50. BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(3))); // no elements
  51. BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(6))); // only the first element
  52. BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(10))); // in the middle somewhere
  53. BOOST_CHECK ( ba::is_partitioned ( l.begin (), l.end (), less_than<int>(99))); // all elements satisfy
  54. }
  55. BOOST_AUTO_TEST_CASE( test_main )
  56. {
  57. test_sequence1 ();
  58. BOOST_CXX14_CONSTEXPR bool constexpr_res = test_constexpr ();
  59. BOOST_CHECK ( constexpr_res );
  60. }