find_if_not_test1.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/find_if_not.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. BOOST_CXX14_CONSTEXPR bool is_true ( int v ) { return true; }
  18. BOOST_CXX14_CONSTEXPR bool is_false ( int v ) { return false; }
  19. BOOST_CXX14_CONSTEXPR bool is_not_three ( int v ) { return v != 3; }
  20. BOOST_CXX14_CONSTEXPR bool check_constexpr() {
  21. int in_data[] = {1, 2, 3, 4, 5};
  22. bool res = true;
  23. const int* from = in_data;
  24. const int* to = in_data + 5;
  25. const int* start = ba::find_if_not (from, to, is_false); // stops on first
  26. res = (res && start == from);
  27. const int* end = ba::find_if_not(from, to, is_true); // stops on the end
  28. res = (res && end == to);
  29. const int* three = ba::find_if_not(from, to, is_not_three); // stops on third element
  30. res = (res && three == in_data + 2);
  31. return res;
  32. }
  33. template <typename Container>
  34. typename Container::iterator offset_to_iter ( Container &v, int offset ) {
  35. typename Container::iterator retval;
  36. if ( offset >= 0 ) {
  37. retval = v.begin ();
  38. std::advance ( retval, offset );
  39. }
  40. else {
  41. retval = v.end ();
  42. std::advance ( retval, offset + 1 );
  43. }
  44. return retval;
  45. }
  46. template <typename Container, typename Predicate>
  47. void test_sequence ( Container &v, Predicate comp, int expected ) {
  48. typename Container::iterator res, exp;
  49. res = ba::find_if_not ( v.begin (), v.end (), comp );
  50. exp = offset_to_iter ( v, expected );
  51. std::cout << "Expected(1): " << std::distance ( v.begin (), exp )
  52. << ", got: " << std::distance ( v.begin (), res ) << std::endl;
  53. BOOST_CHECK ( exp == res );
  54. }
  55. template <typename T>
  56. struct less_than {
  57. public:
  58. less_than ( T foo ) : val ( foo ) {}
  59. less_than ( const less_than &rhs ) : val ( rhs.val ) {}
  60. bool operator () ( const T &v ) const { return v < val; }
  61. private:
  62. less_than ();
  63. less_than operator = ( const less_than &rhs );
  64. T val;
  65. };
  66. void test_sequence1 () {
  67. std::vector<int> v;
  68. v.clear ();
  69. for ( int i = 5; i < 15; ++i )
  70. v.push_back ( i );
  71. test_sequence ( v, less_than<int>(3), 0 ); // no elements
  72. test_sequence ( v, less_than<int>(6), 1 ); // only the first element
  73. test_sequence ( v, less_than<int>(10), 5 );
  74. test_sequence ( v, less_than<int>(99), -1 ); // all elements satisfy
  75. // With bidirectional iterators.
  76. std::list<int> l;
  77. for ( int i = 5; i < 15; ++i )
  78. l.push_back ( i );
  79. test_sequence ( l, less_than<int>(3), 0 ); // no elements
  80. test_sequence ( l, less_than<int>(6), 1 ); // only the first element
  81. test_sequence ( l, less_than<int>(10), 5 );
  82. test_sequence ( l, less_than<int>(99), -1 ); // all elements satisfy
  83. }
  84. BOOST_AUTO_TEST_CASE( test_main )
  85. {
  86. test_sequence1 ();
  87. }