filtered.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2009. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/adaptor/filtered.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <boost/range/algorithm_ext.hpp>
  16. #include <algorithm>
  17. #include <list>
  18. #include <set>
  19. #include <string>
  20. #include <vector>
  21. #include <sstream>
  22. namespace boost
  23. {
  24. namespace
  25. {
  26. struct always_false_pred
  27. {
  28. template< class T1 >
  29. bool operator()(T1) const { return false; }
  30. };
  31. struct always_true_pred
  32. {
  33. template< class T1 >
  34. bool operator()(T1) const { return true; }
  35. };
  36. struct is_even
  37. {
  38. template< class IntegerT >
  39. bool operator()( IntegerT x ) const { return x % 2 == 0; }
  40. };
  41. struct is_odd
  42. {
  43. template< class IntegerT >
  44. bool operator()( IntegerT x ) const { return x % 2 != 0; }
  45. };
  46. struct lambda_init
  47. {
  48. };
  49. struct lambda
  50. {
  51. lambda(const lambda_init& init) {}
  52. lambda(const lambda& rhs) {}
  53. template< class T1 >
  54. bool operator()(T1) const { return false; }
  55. private:
  56. lambda() {}
  57. lambda& operator=(const lambda& rhs) { return *this; }
  58. };
  59. template< class Container, class Pred >
  60. void filtered_test_impl( Container& c, Pred pred )
  61. {
  62. using namespace boost::adaptors;
  63. typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t;
  64. // This is my preferred syntax using the | operator.
  65. std::vector< value_t > test_result1;
  66. boost::push_back(test_result1, c | filtered(pred));
  67. // This is an alternative syntax preferred by some.
  68. std::vector< value_t > test_result2;
  69. boost::push_back(test_result2, adaptors::filter(c, pred));
  70. // Calculate the reference result.
  71. std::vector< value_t > reference_result;
  72. typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
  73. for (iter_t it = c.begin(); it != c.end(); ++it)
  74. {
  75. if (pred(*it))
  76. reference_result.push_back(*it);
  77. }
  78. BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
  79. reference_result.end(),
  80. test_result1.begin(),
  81. test_result1.end() );
  82. BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(),
  83. reference_result.end(),
  84. test_result2.begin(),
  85. test_result2.end() );
  86. }
  87. template< class Rng >
  88. void check_copy_assign(Rng r)
  89. {
  90. Rng r2 = r;
  91. r2 = r;
  92. }
  93. template< class Container, class Pred >
  94. void filtered_range_copy_assign(Container& c, Pred pred)
  95. {
  96. using namespace boost::adaptors;
  97. check_copy_assign(c | filtered(pred));
  98. check_copy_assign(adaptors::filter(c, pred));
  99. }
  100. template< class Container, class Pred, class PredInit >
  101. void filtered_test_impl()
  102. {
  103. using namespace boost::assign;
  104. Container c;
  105. PredInit init;
  106. Pred pred(init);
  107. // test empty container
  108. filtered_test_impl(c, pred);
  109. // test one element
  110. c += 1;
  111. filtered_test_impl(c, pred);
  112. // test many elements
  113. c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9;
  114. filtered_test_impl(c, pred);
  115. // test the range and iterator are copy assignable
  116. filtered_range_copy_assign(c, pred);
  117. }
  118. template< class Container >
  119. void filtered_test_all_predicates()
  120. {
  121. filtered_test_impl< Container, always_false_pred, always_false_pred >();
  122. filtered_test_impl< Container, always_true_pred, always_true_pred >();
  123. filtered_test_impl< Container, is_odd, is_odd >();
  124. filtered_test_impl< Container, is_even, is_even >();
  125. filtered_test_impl< Container, lambda, lambda_init >();
  126. }
  127. void ticket_10988_single_pass()
  128. {
  129. std::vector<int> v;
  130. std::string str("0 1 2 3 4 5");
  131. std::istringstream in(str);
  132. boost::push_back(v,
  133. boost::make_iterator_range(
  134. std::istream_iterator<int>(in),
  135. std::istream_iterator<int>())
  136. | boost::adaptors::filtered(is_even()));
  137. std::vector<int> reference;
  138. for (int i = 0; i < 6; i += 2)
  139. {
  140. reference.push_back(i);
  141. }
  142. BOOST_CHECK_EQUAL_COLLECTIONS(
  143. reference.begin(), reference.end(),
  144. v.begin(), v.end());
  145. }
  146. void filtered_test()
  147. {
  148. filtered_test_all_predicates< std::vector< int > >();
  149. filtered_test_all_predicates< std::list< int > >();
  150. filtered_test_all_predicates< std::set< int > >();
  151. filtered_test_all_predicates< std::multiset< int > >();
  152. ticket_10988_single_pass();
  153. }
  154. }
  155. }
  156. boost::unit_test::test_suite*
  157. init_unit_test_suite(int argc, char* argv[])
  158. {
  159. boost::unit_test::test_suite* test
  160. = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.filtered" );
  161. test->add( BOOST_TEST_CASE( &boost::filtered_test ) );
  162. return test;
  163. }