copy_if_test1.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. Copyright (c) Marshall Clow 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 <boost/config.hpp>
  8. #include <boost/algorithm/cxx11/copy_if.hpp>
  9. #include "iterator_test.hpp"
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <algorithm>
  13. #include <string>
  14. #include <iostream>
  15. #include <vector>
  16. #include <list>
  17. #include <boost/algorithm/cxx11/all_of.hpp>
  18. #include <boost/algorithm/cxx14/equal.hpp>
  19. #include <boost/algorithm/cxx11/none_of.hpp>
  20. namespace ba = boost::algorithm;
  21. // namespace ba = boost;
  22. BOOST_CXX14_CONSTEXPR bool is_true ( int v ) { return true; }
  23. BOOST_CXX14_CONSTEXPR bool is_false ( int v ) { return false; }
  24. BOOST_CXX14_CONSTEXPR bool is_even ( int v ) { return v % 2 == 0; }
  25. BOOST_CXX14_CONSTEXPR bool is_odd ( int v ) { return v % 2 == 1; }
  26. BOOST_CXX14_CONSTEXPR bool is_zero ( int v ) { return v == 0; }
  27. template <typename Container>
  28. void test_copy_if ( Container const &c ) {
  29. typedef typename Container::value_type value_type;
  30. std::vector<value_type> v;
  31. // None of the elements
  32. v.clear ();
  33. ba::copy_if ( c.begin (), c.end (), back_inserter ( v ), is_false);
  34. BOOST_CHECK ( v.size () == 0 );
  35. v.clear ();
  36. ba::copy_if ( c, back_inserter ( v ), is_false);
  37. BOOST_CHECK ( v.size () == 0 );
  38. // All the elements
  39. v.clear ();
  40. ba::copy_if ( c.begin (), c.end (), back_inserter ( v ), is_true);
  41. BOOST_CHECK ( v.size () == c.size ());
  42. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  43. v.clear ();
  44. ba::copy_if ( c, back_inserter ( v ), is_true);
  45. BOOST_CHECK ( v.size () == c.size ());
  46. BOOST_CHECK ( v.size () == c.size ());
  47. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  48. // Some of the elements
  49. v.clear ();
  50. ba::copy_if ( c.begin (), c.end (), back_inserter ( v ), is_even );
  51. BOOST_CHECK ( v.size () == (size_t) std::count_if ( c.begin (), c.end (), is_even ));
  52. BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
  53. v.clear ();
  54. ba::copy_if ( c, back_inserter ( v ), is_even );
  55. BOOST_CHECK ( v.size () == (size_t) std::count_if ( c.begin (), c.end (), is_even ));
  56. BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
  57. }
  58. template <typename Container>
  59. void test_copy_while ( Container const &c ) {
  60. typedef typename Container::value_type value_type;
  61. typename Container::const_iterator it;
  62. std::vector<value_type> v;
  63. // None of the elements
  64. v.clear ();
  65. ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_false);
  66. BOOST_CHECK ( v.size () == 0 );
  67. v.clear ();
  68. ba::copy_while ( c, back_inserter ( v ), is_false);
  69. BOOST_CHECK ( v.size () == 0 );
  70. // All the elements
  71. v.clear ();
  72. ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_true);
  73. BOOST_CHECK ( v.size () == c.size ());
  74. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  75. v.clear ();
  76. ba::copy_while ( c, back_inserter ( v ), is_true);
  77. BOOST_CHECK ( v.size () == c.size ());
  78. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  79. // Some of the elements
  80. v.clear ();
  81. it = ba::copy_while ( c.begin (), c.end (), back_inserter ( v ), is_even ).first;
  82. BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
  83. BOOST_CHECK ( it == c.end () || !is_even ( *it ));
  84. BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
  85. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  86. v.clear ();
  87. it = ba::copy_while ( c, back_inserter ( v ), is_even ).first;
  88. BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
  89. BOOST_CHECK ( it == c.end () || !is_even ( *it ));
  90. BOOST_CHECK ( ba::all_of ( v.begin (), v.end (), is_even ));
  91. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  92. }
  93. template <typename Container>
  94. void test_copy_until ( Container const &c ) {
  95. typedef typename Container::value_type value_type;
  96. typename Container::const_iterator it;
  97. std::vector<value_type> v;
  98. // None of the elements
  99. v.clear ();
  100. ba::copy_until ( c.begin (), c.end (), back_inserter ( v ), is_true);
  101. BOOST_CHECK ( v.size () == 0 );
  102. v.clear ();
  103. ba::copy_until ( c, back_inserter ( v ), is_true);
  104. BOOST_CHECK ( v.size () == 0 );
  105. // All the elements
  106. v.clear ();
  107. ba::copy_until ( c.begin (), c.end (), back_inserter ( v ), is_false);
  108. BOOST_CHECK ( v.size () == c.size ());
  109. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  110. v.clear ();
  111. ba::copy_until ( c, back_inserter ( v ), is_false);
  112. BOOST_CHECK ( v.size () == c.size ());
  113. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  114. // Some of the elements
  115. v.clear ();
  116. it = ba::copy_until ( c.begin (), c.end (), back_inserter ( v ), is_even ).first;
  117. BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
  118. BOOST_CHECK ( it == c.end () || is_even ( *it ));
  119. BOOST_CHECK ( ba::none_of ( v.begin (), v.end (), is_even ));
  120. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  121. v.clear ();
  122. it = ba::copy_until ( c, back_inserter ( v ), is_even ).first;
  123. BOOST_CHECK ( v.size () == (size_t) std::distance ( c.begin (), it ));
  124. BOOST_CHECK ( it == c.end () || is_even ( *it ));
  125. BOOST_CHECK ( ba::none_of ( v.begin (), v.end (), is_even ));
  126. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  127. }
  128. BOOST_CXX14_CONSTEXPR inline bool constexpr_test_copy_if() {
  129. const int sz = 64;
  130. int in_data[sz] = {0};
  131. bool res = true;
  132. const int* from = in_data;
  133. const int* to = in_data + sz;
  134. int out_data[sz] = {0};
  135. int* out = out_data;
  136. out = ba::copy_if ( from, to, out, is_false ); // copy none
  137. res = (res && out == out_data);
  138. out = ba::copy_if ( from, to, out, is_true ); // copy all
  139. res = (res && out == out_data + sz
  140. && ba::equal( input_iterator<const int *>(out_data), input_iterator<const int *>(out_data + sz),
  141. input_iterator<const int *>(from), input_iterator<const int *>(to)));
  142. return res;
  143. }
  144. BOOST_CXX14_CONSTEXPR inline bool constexpr_test_copy_while() {
  145. const int sz = 64;
  146. int in_data[sz] = {0};
  147. bool res = true;
  148. const int* from = in_data;
  149. const int* to = in_data + sz;
  150. int out_data[sz] = {0};
  151. int* out = out_data;
  152. out = ba::copy_while ( from, to, out, is_false ).second; // copy none
  153. res = (res && out == out_data && ba::all_of(out, out + sz, is_zero));
  154. out = ba::copy_while ( from, to, out, is_true ).second; // copy all
  155. res = (res && out == out_data + sz
  156. && ba::equal( input_iterator<const int *>(out_data), input_iterator<const int *>(out_data + sz),
  157. input_iterator<const int *>(from), input_iterator<const int *>(to)));
  158. return res;
  159. }
  160. BOOST_CXX14_CONSTEXPR inline bool constexpr_test_copy_until() {
  161. const int sz = 64;
  162. int in_data[sz] = {0};
  163. bool res = true;
  164. const int* from = in_data;
  165. const int* to = in_data + sz;
  166. int out_data[sz] = {0};
  167. int* out = out_data;
  168. out = ba::copy_until ( from, to, out, is_true ).second; // copy none
  169. res = (res && out == out_data && ba::all_of(out, out + sz, is_zero));
  170. out = ba::copy_until ( from, to, out, is_false ).second; // copy all
  171. res = (res && out == out_data + sz
  172. && ba::equal( input_iterator<const int *>(out_data), input_iterator<const int *>(out_data + sz),
  173. input_iterator<const int *>(from), input_iterator<const int *>(to)));
  174. return res;
  175. }
  176. void test_sequence1 () {
  177. std::vector<int> v;
  178. for ( int i = 5; i < 15; ++i )
  179. v.push_back ( i );
  180. test_copy_if ( v );
  181. test_copy_while ( v );
  182. test_copy_until ( v );
  183. BOOST_CXX14_CONSTEXPR bool constexpr_res_if = constexpr_test_copy_if();
  184. BOOST_CHECK ( constexpr_res_if );
  185. BOOST_CXX14_CONSTEXPR bool constexpr_res_while = constexpr_test_copy_while();
  186. BOOST_CHECK ( constexpr_res_while );
  187. BOOST_CXX14_CONSTEXPR bool constexpr_res_until = constexpr_test_copy_until();
  188. BOOST_CHECK ( constexpr_res_until );
  189. std::list<int> l;
  190. for ( int i = 25; i > 15; --i )
  191. l.push_back ( i );
  192. test_copy_if ( l );
  193. test_copy_while ( l );
  194. test_copy_until ( l );
  195. }
  196. BOOST_AUTO_TEST_CASE( test_main )
  197. {
  198. test_sequence1 ();
  199. }