copy_n_test1.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <boost/config.hpp>
  8. #include <boost/algorithm/cxx11/copy_n.hpp>
  9. #include <boost/algorithm/cxx14/equal.hpp>
  10. #include <boost/algorithm/cxx11/all_of.hpp>
  11. #include "iterator_test.hpp"
  12. #define BOOST_TEST_MAIN
  13. #include <boost/test/unit_test.hpp>
  14. #include <string>
  15. #include <iostream>
  16. #include <vector>
  17. #include <list>
  18. namespace ba = boost::algorithm;
  19. // namespace ba = boost;
  20. BOOST_CXX14_CONSTEXPR bool is_zero( int v ) { return v == 0; }
  21. template <typename Container>
  22. void test_sequence ( Container const &c ) {
  23. typedef typename Container::value_type value_type;
  24. std::vector<value_type> v;
  25. // Copy zero elements
  26. v.clear ();
  27. ba::copy_n ( c.begin (), 0, back_inserter ( v ));
  28. BOOST_CHECK ( v.size () == 0 );
  29. ba::copy_n ( c.begin (), 0U, back_inserter ( v ));
  30. BOOST_CHECK ( v.size () == 0 );
  31. if ( c.size () > 0 ) {
  32. // Just one element
  33. v.clear ();
  34. ba::copy_n ( c.begin (), 1, back_inserter ( v ));
  35. BOOST_CHECK ( v.size () == 1 );
  36. BOOST_CHECK ( v[0] == *c.begin ());
  37. v.clear ();
  38. ba::copy_n ( c.begin (), 1U, back_inserter ( v ));
  39. BOOST_CHECK ( v.size () == 1 );
  40. BOOST_CHECK ( v[0] == *c.begin ());
  41. // Half the elements
  42. v.clear ();
  43. ba::copy_n ( c.begin (), c.size () / 2, back_inserter ( v ));
  44. BOOST_CHECK ( v.size () == c.size () / 2);
  45. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  46. // Half the elements + 1
  47. v.clear ();
  48. ba::copy_n ( c.begin (), c.size () / 2 + 1, back_inserter ( v ));
  49. BOOST_CHECK ( v.size () == c.size () / 2 + 1 );
  50. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  51. // All the elements
  52. v.clear ();
  53. ba::copy_n ( c.begin (), c.size (), back_inserter ( v ));
  54. BOOST_CHECK ( v.size () == c.size ());
  55. BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
  56. }
  57. }
  58. BOOST_CXX14_CONSTEXPR inline bool test_constexpr() {
  59. const size_t sz = 64;
  60. int in_data[sz] = {0};
  61. bool res = true;
  62. const int* from = in_data;
  63. const int* to = in_data + sz;
  64. int out_data[sz] = {0};
  65. int* out = out_data;
  66. out = ba::copy_n ( from, 0, out ); // Copy none
  67. res = (res && out == out_data && ba::all_of(out, out + sz, is_zero));
  68. out = ba::copy_n ( from, sz, out ); // Copy all
  69. res = (res && out == out_data + sz
  70. && ba::equal( input_iterator<const int *>(out_data), input_iterator<const int *>(out_data + sz),
  71. input_iterator<const int *>(from), input_iterator<const int *>(to)));
  72. return res;
  73. }
  74. void test_sequence1 () {
  75. std::vector<int> v;
  76. for ( int i = 5; i < 15; ++i )
  77. v.push_back ( i );
  78. test_sequence ( v );
  79. BOOST_CXX14_CONSTEXPR bool constexpr_res = test_constexpr();
  80. BOOST_CHECK(constexpr_res);
  81. std::list<int> l;
  82. for ( int i = 25; i > 15; --i )
  83. l.push_back ( i );
  84. test_sequence ( l );
  85. }
  86. BOOST_AUTO_TEST_CASE( test_main )
  87. {
  88. test_sequence1 ();
  89. }