diff options
author | Marshall Clow <mclow.lists@gmail.com> | 2017-05-25 02:29:54 +0000 |
---|---|---|
committer | Marshall Clow <mclow.lists@gmail.com> | 2017-05-25 02:29:54 +0000 |
commit | d5c65ffa8dd419b618b1c28602637d3e816d8a94 (patch) | |
tree | 229d2b9b70ebc54ccec1f113574b1f916faa085c /libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach | |
parent | d2a7e8538b9c6de35759c28c3e3df8419680d08e (diff) | |
download | bcm5719-llvm-d5c65ffa8dd419b618b1c28602637d3e816d8a94.tar.gz bcm5719-llvm-d5c65ffa8dd419b618b1c28602637d3e816d8a94.zip |
Add non-parallel version of for_each_n (+tests) from the Parallelism TS
llvm-svn: 303833
Diffstat (limited to 'libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach')
-rw-r--r-- | libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp new file mode 100644 index 00000000000..fd24edb4306 --- /dev/null +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <algorithm> +// UNSUPPORTED: c++98, c++03, c++11, c++14 + +// template<class InputIterator, class Size, class Function> +// InputIterator for_each_n(InputIterator first, Size n, Function f); + + +#include <algorithm> +#include <cassert> + +#include "test_iterators.h" + +struct for_each_test +{ + for_each_test(int c) : count(c) {} + int count; + void operator()(int& i) {++i; ++count;} +}; + +int main() +{ + typedef input_iterator<int*> Iter; + int ia[] = {0, 1, 2, 3, 4, 5}; + const unsigned s = sizeof(ia)/sizeof(ia[0]); + + { + auto f = for_each_test(0); + Iter it = std::for_each_n(Iter(ia), 0, std::ref(f)); + assert(it == Iter(ia)); + assert(f.count == 0); + } + + { + auto f = for_each_test(0); + Iter it = std::for_each_n(Iter(ia), s, std::ref(f)); + + assert(it == Iter(ia+s)); + assert(f.count == s); + for (unsigned i = 0; i < s; ++i) + assert(ia[i] == static_cast<int>(i+1)); + } + + { + auto f = for_each_test(0); + Iter it = std::for_each_n(Iter(ia), 1, std::ref(f)); + + assert(it == Iter(ia+1)); + assert(f.count == 1); + for (unsigned i = 0; i < 1; ++i) + assert(ia[i] == static_cast<int>(i+2)); + } +} |