iota_test1.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/iota.hpp>
  9. #define BOOST_TEST_MAIN
  10. #include <boost/test/unit_test.hpp>
  11. #include <iostream>
  12. #include <string>
  13. #include <vector>
  14. #include <list>
  15. // Test to make sure a sequence is "correctly formed"; i.e, ascending by one
  16. template <typename Iterator, typename T>
  17. BOOST_CXX14_CONSTEXPR bool test_iota_results ( Iterator first, Iterator last, T initial_value ) {
  18. if ( first == last ) return true;
  19. if ( initial_value != *first ) return false;
  20. Iterator prev = first;
  21. while ( ++first != last ) {
  22. if (( *first - *prev ) != 1 )
  23. return false;
  24. prev = first;
  25. }
  26. return true;
  27. }
  28. template <typename Range, typename T>
  29. BOOST_CXX14_CONSTEXPR bool test_iota_results ( const Range &r, T initial_value ) {
  30. return test_iota_results (boost::begin (r), boost::end (r), initial_value );
  31. }
  32. void test_ints () {
  33. std::vector<int> v;
  34. std::list<int> l;
  35. v.clear (); v.resize ( 10 );
  36. boost::algorithm::iota ( v.begin (), v.end (), 23 );
  37. BOOST_CHECK ( test_iota_results ( v.begin (), v.end (), 23 ));
  38. v.clear (); v.resize ( 19 );
  39. boost::algorithm::iota ( v, 18 );
  40. BOOST_CHECK ( test_iota_results ( v, 18 ));
  41. v.clear ();
  42. boost::algorithm::iota_n ( std::back_inserter(v), 99, 20 );
  43. BOOST_CHECK ( test_iota_results ( v, 99 ));
  44. v.clear ();
  45. boost::algorithm::iota_n ( std::back_inserter(v), 99, 0 );
  46. BOOST_CHECK ( v.size() == 0 );
  47. /*
  48. l.clear (); l.reserve ( 5 );
  49. boost::algorithm::iota ( l.begin (), l.end (), 123 );
  50. BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 123 ));
  51. l.clear (); l.reserve ( 9 );
  52. boost::algorithm::iota ( l.begin (), l.end (), 87 );
  53. BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 87 ));
  54. */
  55. l.clear ();
  56. boost::algorithm::iota_n ( std::back_inserter(l), 99, 20 );
  57. BOOST_CHECK ( test_iota_results ( l, 99 ));
  58. l.clear ();
  59. boost::algorithm::iota_n ( std::front_inserter(l), 123, 20 );
  60. BOOST_CHECK ( test_iota_results ( l.rbegin (), l.rend (), 123 ));
  61. }
  62. BOOST_CXX14_CONSTEXPR inline bool test_constexpr_iota() {
  63. bool res = true;
  64. int data[] = {0, 0, 0};
  65. boost::algorithm::iota(data, data, 1); // fill none
  66. res = (res && data[0] == 0);
  67. boost::algorithm::iota(data, data + 3, 1); // fill all
  68. res = (res && test_iota_results(data, data + 3, 1));
  69. return res;
  70. }
  71. BOOST_CXX14_CONSTEXPR inline bool test_constexpr_iota_n() {
  72. bool res = true;
  73. int data[] = {0, 0, 0};
  74. boost::algorithm::iota_n(data, 1, 0); // fill none
  75. res = (res && data[0] == 0);
  76. boost::algorithm::iota_n(data, 1, 3); // fill all
  77. res = (res && test_iota_results(data, 1));
  78. return res;
  79. }
  80. BOOST_AUTO_TEST_CASE( test_main )
  81. {
  82. test_ints ();
  83. BOOST_CXX14_CONSTEXPR bool constexpr_iota_res = test_constexpr_iota ();
  84. BOOST_CHECK(constexpr_iota_res);
  85. BOOST_CXX14_CONSTEXPR bool constexpr_iota_n_res = test_constexpr_iota_n ();
  86. BOOST_CHECK(constexpr_iota_n_res);
  87. }