1/*
2 Copyright (c) Marshall Clow 2013.
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 For more information, see http://www.boost.org
8*/
9
10#include <boost/config.hpp>
11#include <boost/algorithm/cxx17/for_each_n.hpp>
12
13#include "iterator_test.hpp"
14
15#define BOOST_TEST_MAIN
16#include <boost/test/unit_test.hpp>
17
18namespace ba = boost::algorithm;
19
20struct for_each_test
21{
22 for_each_test() {}
23 static int count;
24 void operator()(int& i) {++i; ++count;}
25};
26
27int for_each_test::count = 0;
28
29void test_for_each_n ()
30{
31 typedef input_iterator<int*> Iter;
32 int ia[] = {0, 1, 2, 3, 4, 5};
33 const unsigned s = sizeof(ia)/sizeof(ia[0]);
34
35 {
36 for_each_test::count = 0;
37 Iter it = ba::for_each_n(first: Iter(ia), n: 0, f: for_each_test());
38 BOOST_CHECK(it == Iter(ia));
39 BOOST_CHECK(for_each_test::count == 0);
40 }
41
42 {
43 for_each_test::count = 0;
44 Iter it = ba::for_each_n(first: Iter(ia), n: s, f: for_each_test());
45
46 BOOST_CHECK(it == Iter(ia+s));
47 BOOST_CHECK(for_each_test::count == s);
48 for (unsigned i = 0; i < s; ++i)
49 BOOST_CHECK(ia[i] == static_cast<int>(i+1));
50 }
51
52 {
53 for_each_test::count = 0;
54 Iter it = ba::for_each_n(first: Iter(ia), n: 1, f: for_each_test());
55
56 BOOST_CHECK(it == Iter(ia+1));
57 BOOST_CHECK(for_each_test::count == 1);
58 for (unsigned i = 0; i < 1; ++i)
59 BOOST_CHECK(ia[i] == static_cast<int>(i+2));
60 }
61}
62
63BOOST_AUTO_TEST_CASE( test_main )
64{
65 test_for_each_n ();
66}
67

source code of boost/libs/algorithm/test/for_each_n_test.cpp