partition_copy_test1.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_copy.hpp>
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/algorithm/cxx11/all_of.hpp>
  13. #include <boost/algorithm/cxx11/none_of.hpp>
  14. #include <string>
  15. #include <vector>
  16. #include <list>
  17. namespace ba = boost::algorithm;
  18. // namespace ba = boost;
  19. template <typename Container, typename Predicate>
  20. void test_sequence ( const Container &c, Predicate comp ) {
  21. std::vector<typename Container::value_type> v1, v2;
  22. v1.clear (); v2.clear ();
  23. ba::partition_copy ( c.begin (), c.end (),
  24. std::back_inserter (v1), std::back_inserter (v2), comp );
  25. // std::cout << "Sizes(1): " << c.size () << " -> { " << v1.size () << ", " << v2.size () << " }" << std::endl;
  26. BOOST_CHECK ( v1.size () + v2.size () == c.size ());
  27. BOOST_CHECK ( ba::all_of ( v1.begin (), v1.end (), comp ));
  28. BOOST_CHECK ( ba::none_of ( v2.begin (), v2.end (), comp ));
  29. v1.clear (); v2.clear ();
  30. ba::partition_copy ( c, std::back_inserter (v1), std::back_inserter ( v2 ), comp );
  31. // std::cout << "Sizes(2): " << c.size () << " -> { " << v1.size () << ", " << v2.size () << " }" << std::endl;
  32. BOOST_CHECK ( v1.size () + v2.size () == c.size ());
  33. BOOST_CHECK ( ba::all_of ( v1, comp ));
  34. BOOST_CHECK ( ba::none_of ( v2, comp ));
  35. }
  36. template <typename T>
  37. struct less_than {
  38. public:
  39. BOOST_CXX14_CONSTEXPR less_than ( T foo ) : val ( foo ) {}
  40. BOOST_CXX14_CONSTEXPR less_than ( const less_than &rhs ) : val ( rhs.val ) {}
  41. BOOST_CXX14_CONSTEXPR bool operator () ( const T &v ) const { return v < val; }
  42. private:
  43. less_than ();
  44. less_than operator = ( const less_than &rhs );
  45. T val;
  46. };
  47. bool is_even ( int v ) { return v % 2 == 0; }
  48. void test_sequence1 () {
  49. std::vector<int> v;
  50. v.clear ();
  51. for ( int i = 5; i < 15; ++i )
  52. v.push_back ( i );
  53. test_sequence ( v, less_than<int>(3)); // no elements
  54. test_sequence ( v, less_than<int>(6)); // only the first element
  55. test_sequence ( v, less_than<int>(10));
  56. test_sequence ( v, less_than<int>(99)); // all elements satisfy
  57. // With bidirectional iterators.
  58. std::list<int> l;
  59. for ( int i = 5; i < 16; ++i )
  60. l.push_back ( i );
  61. test_sequence ( l, less_than<int>(3)); // no elements
  62. test_sequence ( l, less_than<int>(6)); // only the first element
  63. test_sequence ( l, less_than<int>(10));
  64. test_sequence ( l, less_than<int>(99)); // all elements satisfy
  65. }
  66. BOOST_CXX14_CONSTEXPR bool test_constexpr () {
  67. int in[] = {1, 1, 2};
  68. int out_true[3] = {0};
  69. int out_false[3] = {0};
  70. bool res = true;
  71. ba::partition_copy( in, in + 3, out_true, out_false, less_than<int>(2) );
  72. res = (res && ba::all_of(out_true, out_true + 2, less_than<int>(2)) );
  73. res = (res && ba::none_of(out_false, out_false + 1, less_than<int>(2)) );
  74. // clear elements
  75. out_true [0] = 0;
  76. out_true [1] = 0;
  77. out_false[0] = 0;
  78. ba::partition_copy( in, out_true, out_false, less_than<int>(2));
  79. res = ( res && ba::all_of(out_true, out_true + 2, less_than<int>(2)));
  80. res = ( res && ba::none_of(out_false, out_false + 1, less_than<int>(2)));
  81. return res;
  82. }
  83. BOOST_AUTO_TEST_CASE( test_main )
  84. {
  85. test_sequence1 ();
  86. BOOST_CXX14_CONSTEXPR bool constexpr_res = test_constexpr ();
  87. BOOST_CHECK ( constexpr_res );
  88. }