find_first_of.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/algorithm/find_first_of.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include "../test_driver/range_return_test_driver.hpp"
  16. #include <algorithm>
  17. #include <functional>
  18. #include <vector>
  19. #include <set>
  20. #include <list>
  21. namespace boost_range_test_algorithm_find_first_of
  22. {
  23. template<class Container2>
  24. class find_first_of_test_policy
  25. {
  26. typedef Container2 container2_t;
  27. public:
  28. explicit find_first_of_test_policy(const Container2& cont)
  29. : m_cont(cont)
  30. {
  31. }
  32. container2_t& cont() { return m_cont; }
  33. template<class Container>
  34. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  35. test_iter(Container& cont)
  36. {
  37. typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iter_t;
  38. iter_t result = boost::find_first_of(cont, m_cont);
  39. BOOST_CHECK( result == boost::find_first_of(boost::make_iterator_range(cont), m_cont) );
  40. BOOST_CHECK( result == boost::find_first_of(cont, boost::make_iterator_range(m_cont)) );
  41. BOOST_CHECK( result == boost::find_first_of(boost::make_iterator_range(cont), boost::make_iterator_range(m_cont)) );
  42. return result;
  43. }
  44. template<boost::range_return_value return_type>
  45. struct test_range
  46. {
  47. template<class Container, class Policy>
  48. BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type
  49. operator()(Policy& policy, Container& cont)
  50. {
  51. typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type result_t;
  52. result_t result = boost::find_first_of<return_type>(cont, policy.cont());
  53. BOOST_CHECK( result == boost::find_first_of<return_type>(boost::make_iterator_range(cont), policy.cont()) );
  54. BOOST_CHECK( result == boost::find_first_of<return_type>(cont, boost::make_iterator_range(policy.cont())) );
  55. BOOST_CHECK( result == boost::find_first_of<return_type>(boost::make_iterator_range(cont), boost::make_iterator_range(policy.cont())) );
  56. return result;
  57. }
  58. };
  59. template<class Container>
  60. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  61. reference(Container& cont)
  62. {
  63. return std::find_first_of(cont.begin(), cont.end(),
  64. m_cont.begin(), m_cont.end());
  65. }
  66. private:
  67. Container2 m_cont;
  68. };
  69. template<class Container2, class BinaryPredicate>
  70. class find_first_of_pred_test_policy
  71. {
  72. typedef Container2 container2_t;
  73. public:
  74. explicit find_first_of_pred_test_policy(const Container2& cont)
  75. : m_cont(cont)
  76. {
  77. }
  78. container2_t& cont() { return m_cont; }
  79. BinaryPredicate& pred() { return m_pred; }
  80. template<class Container>
  81. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  82. test_iter(Container& cont)
  83. {
  84. typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type iter_t;
  85. iter_t result = boost::find_first_of(cont, m_cont, m_pred);
  86. BOOST_CHECK( result == boost::find_first_of(boost::make_iterator_range(cont), m_cont, m_pred) );
  87. BOOST_CHECK( result == boost::find_first_of(cont, boost::make_iterator_range(m_cont), m_pred) );
  88. BOOST_CHECK( result == boost::find_first_of(boost::make_iterator_range(cont), boost::make_iterator_range(m_cont), m_pred) );
  89. return result;
  90. }
  91. template<boost::range_return_value return_type>
  92. struct test_range
  93. {
  94. template<class Container, class Policy>
  95. BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type
  96. operator()(Policy& policy, Container& cont)
  97. {
  98. typedef BOOST_DEDUCED_TYPENAME boost::range_return<Container,return_type>::type result_t;
  99. result_t result = boost::find_first_of<return_type>(cont, policy.cont(), policy.pred());
  100. BOOST_CHECK( result == boost::find_first_of<return_type>(boost::make_iterator_range(cont), policy.cont(), policy.pred()) );
  101. BOOST_CHECK( result == boost::find_first_of<return_type>(cont, boost::make_iterator_range(policy.cont()), policy.pred()) );
  102. BOOST_CHECK( result == boost::find_first_of<return_type>(boost::make_iterator_range(cont), boost::make_iterator_range(policy.cont()), policy.pred()) );
  103. return result;
  104. }
  105. };
  106. template<class Container>
  107. BOOST_DEDUCED_TYPENAME boost::range_iterator<Container>::type
  108. reference(Container& cont)
  109. {
  110. return std::find_first_of(cont.begin(), cont.end(),
  111. m_cont.begin(), m_cont.end(),
  112. m_pred);
  113. }
  114. private:
  115. Container2 m_cont;
  116. BinaryPredicate m_pred;
  117. };
  118. template<class Container1, class Container2>
  119. void run_tests(Container1& cont1, Container2& cont2)
  120. {
  121. boost::range_test::range_return_test_driver test_driver;
  122. test_driver(cont1, find_first_of_test_policy<Container2>(cont2));
  123. test_driver(cont1, find_first_of_pred_test_policy<Container2, std::less<int> >(cont2));
  124. test_driver(cont2, find_first_of_pred_test_policy<Container2, std::greater<int> >(cont2));
  125. }
  126. template<class Container1, class Container2>
  127. void test_find_first_of_impl()
  128. {
  129. using namespace boost::assign;
  130. typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container1>::type container1_t;
  131. typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container2>::type container2_t;
  132. container1_t mcont1;
  133. Container1& cont1 = mcont1;
  134. container2_t mcont2;
  135. Container2& cont2 = mcont2;
  136. run_tests(cont1, cont2);
  137. mcont1 += 1;
  138. run_tests(cont1, cont2);
  139. mcont2 += 1;
  140. run_tests(cont1, cont2);
  141. mcont1 += 2,3,4,5,6,7,8,9;
  142. mcont2 += 2,3,4;
  143. run_tests(cont1, cont2);
  144. mcont2.clear();
  145. mcont2 += 7,8,9;
  146. run_tests(cont1, cont2);
  147. }
  148. void test_find_first_of()
  149. {
  150. test_find_first_of_impl< std::vector<int>, std::vector<int> >();
  151. test_find_first_of_impl< std::list<int>, std::list<int> >();
  152. test_find_first_of_impl< std::deque<int>, std::deque<int> >();
  153. test_find_first_of_impl< const std::vector<int>, const std::vector<int> >();
  154. test_find_first_of_impl< const std::list<int>, const std::list<int> >();
  155. test_find_first_of_impl< const std::deque<int>, const std::deque<int> >();
  156. test_find_first_of_impl< const std::vector<int>, const std::list<int> >();
  157. test_find_first_of_impl< const std::list<int>, const std::vector<int> >();
  158. test_find_first_of_impl< const std::vector<int>, std::list<int> >();
  159. test_find_first_of_impl< const std::list<int>, std::vector<int> >();
  160. test_find_first_of_impl< std::vector<int>, std::list<int> >();
  161. test_find_first_of_impl< std::list<int>, std::vector<int> >();
  162. }
  163. }
  164. boost::unit_test::test_suite*
  165. init_unit_test_suite(int argc, char* argv[])
  166. {
  167. boost::unit_test::test_suite* test
  168. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.find_first_of" );
  169. test->add( BOOST_TEST_CASE( &boost_range_test_algorithm_find_first_of::test_find_first_of ) );
  170. return test;
  171. }