gather_test1.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/gather.hpp>
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <string>
  13. #include <vector>
  14. #include <list>
  15. #include "iterator_test.hpp"
  16. namespace ba = boost::algorithm;
  17. template <typename Container>
  18. void print ( const char *prompt, const Container &c ) {
  19. std::cout << prompt << " { ";
  20. std::copy ( c.begin (), c.end (), std::ostream_iterator<typename Container::value_type>(std::cout, " "));
  21. std::cout << std::endl;
  22. }
  23. template <typename Iterator, typename Predicate>
  24. void test_iterators ( Iterator first, Iterator last, Predicate comp, std::size_t offset ) {
  25. // Create the pivot point
  26. Iterator off = first;
  27. std::advance(off, offset);
  28. // Gather the elements
  29. std::pair<Iterator, Iterator> res = ba::gather ( first, last, off, comp );
  30. // We should now have three sequences, any of which may be empty:
  31. // * [begin .. result.first) - items that do not satisfy the predicate
  32. // * [result.first .. result.second) - items that do satisfy the predicate
  33. // * [result.second .. end) - items that do not satisfy the predicate
  34. Iterator iter = first;
  35. for ( ; iter != res.first; ++iter )
  36. BOOST_CHECK ( !comp ( *iter ));
  37. for ( ; iter != res.second; ++iter)
  38. BOOST_CHECK ( comp ( *iter ));
  39. for ( ; iter != last; ++iter )
  40. BOOST_CHECK ( !comp ( *iter ));
  41. }
  42. template <typename Container, typename Predicate>
  43. void test_iterator_types ( const Container &c, Predicate comp, std::size_t offset ) {
  44. typedef std::vector<typename Container::value_type> vec;
  45. typedef bidirectional_iterator<typename vec::iterator> BDI;
  46. typedef random_access_iterator<typename vec::iterator> RAI;
  47. vec v;
  48. v.assign ( c.begin (), c.end ());
  49. test_iterators ( BDI ( v.begin ()), BDI ( v.end ()), comp, offset );
  50. v.assign ( c.begin (), c.end ());
  51. test_iterators ( RAI ( v.begin ()), RAI ( v.end ()), comp, offset );
  52. }
  53. template <typename T>
  54. struct less_than {
  55. public:
  56. // typedef T argument_type;
  57. // typedef bool result_type;
  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. bool is_even ( int i ) { return i % 2 == 0; }
  67. bool is_ten ( int i ) { return i == 10; }
  68. void test_sequence1 () {
  69. std::vector<int> v;
  70. for ( int i = 5; i < 15; ++i )
  71. v.push_back ( i );
  72. test_iterator_types ( v, less_than<int>(10), 0 ); // at beginning
  73. test_iterator_types ( v, less_than<int>(10), 5 );
  74. test_iterator_types ( v, less_than<int>(10), v.size () - 1 ); // at end
  75. test_iterator_types ( v, is_even, 0 );
  76. test_iterator_types ( v, is_even, 5 );
  77. test_iterator_types ( v, is_even, v.size () - 1 );
  78. // Exactly one element in the sequence matches
  79. test_iterator_types ( v, is_ten, 0 );
  80. test_iterator_types ( v, is_ten, 5 );
  81. test_iterator_types ( v, is_ten, v.size () - 1 );
  82. // Everything in the sequence matches
  83. test_iterator_types ( v, less_than<int>(99), 0 );
  84. test_iterator_types ( v, less_than<int>(99), 5 );
  85. test_iterator_types ( v, less_than<int>(99), v.size () - 1 );
  86. // Nothing in the sequence matches
  87. test_iterator_types ( v, less_than<int>(0), 0 );
  88. test_iterator_types ( v, less_than<int>(0), 5 );
  89. test_iterator_types ( v, less_than<int>(0), v.size () - 1 );
  90. // All the elements in the sequence are the same
  91. v.clear ();
  92. for ( int i = 0; i < 11; ++i )
  93. v.push_back ( 10 );
  94. // Everything in the sequence matches
  95. test_iterator_types ( v, is_ten, 0 );
  96. test_iterator_types ( v, is_ten, 5 );
  97. test_iterator_types ( v, is_ten, v.size () - 1 );
  98. // Nothing in the sequence matches
  99. test_iterator_types ( v, less_than<int>(5), 0 );
  100. test_iterator_types ( v, less_than<int>(5), 5 );
  101. test_iterator_types ( v, less_than<int>(5), v.size () - 1 );
  102. }
  103. BOOST_AUTO_TEST_CASE( test_main )
  104. {
  105. test_sequence1 ();
  106. }