for_each_n_test.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Copyright (c) Marshall Clow 2013.
  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/cxx17/for_each_n.hpp>
  9. #include "iterator_test.hpp"
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. namespace ba = boost::algorithm;
  13. struct for_each_test
  14. {
  15. for_each_test() {}
  16. static int count;
  17. void operator()(int& i) {++i; ++count;}
  18. };
  19. int for_each_test::count = 0;
  20. void test_for_each_n ()
  21. {
  22. typedef input_iterator<int*> Iter;
  23. int ia[] = {0, 1, 2, 3, 4, 5};
  24. const unsigned s = sizeof(ia)/sizeof(ia[0]);
  25. {
  26. for_each_test::count = 0;
  27. Iter it = ba::for_each_n(Iter(ia), 0, for_each_test());
  28. BOOST_CHECK(it == Iter(ia));
  29. BOOST_CHECK(for_each_test::count == 0);
  30. }
  31. {
  32. for_each_test::count = 0;
  33. Iter it = ba::for_each_n(Iter(ia), s, for_each_test());
  34. BOOST_CHECK(it == Iter(ia+s));
  35. BOOST_CHECK(for_each_test::count == s);
  36. for (unsigned i = 0; i < s; ++i)
  37. BOOST_CHECK(ia[i] == static_cast<int>(i+1));
  38. }
  39. {
  40. for_each_test::count = 0;
  41. Iter it = ba::for_each_n(Iter(ia), 1, for_each_test());
  42. BOOST_CHECK(it == Iter(ia+1));
  43. BOOST_CHECK(for_each_test::count == 1);
  44. for (unsigned i = 0; i < 1; ++i)
  45. BOOST_CHECK(ia[i] == static_cast<int>(i+2));
  46. }
  47. }
  48. BOOST_AUTO_TEST_CASE( test_main )
  49. {
  50. test_for_each_n ();
  51. }