diff options
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 | 21 | ||||
-rw-r--r-- | libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp | 18 |
2 files changed, 37 insertions, 2 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 index 9b391f01cea..b2db22b1e82 100644 --- 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 @@ -11,14 +11,29 @@ // 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); +// constexpr InputIterator // constexpr after C++17 +// for_each_n(InputIterator first, Size n, Function f); #include <algorithm> #include <cassert> +#include "test_macros.h" #include "test_iterators.h" +#if TEST_STD_VER > 17 +TEST_CONSTEXPR bool test_constexpr() { + int ia[] = {1, 3, 6, 7}; + int expected[] = {3, 5, 8, 9}; + const size_t N = 4; + + auto it = std::for_each_n(std::begin(ia), N, [](int &a) { a += 2; }); + return it == (std::begin(ia) + N) + && std::equal(std::begin(ia), std::end(ia), std::begin(expected)) + ; + } +#endif + struct for_each_test { for_each_test(int c) : count(c) {} @@ -58,4 +73,8 @@ int main() for (unsigned i = 0; i < 1; ++i) assert(ia[i] == static_cast<int>(i+2)); } + +#if TEST_STD_VER > 17 + static_assert(test_constexpr()); +#endif } diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp index d6fdd18968d..a334c609333 100644 --- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp +++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp @@ -11,14 +11,26 @@ // template<InputIterator Iter, Callable<auto, Iter::reference> Function> // requires CopyConstructible<Function> -// Function +// constexpr Function // constexpr after C++17 // for_each(Iter first, Iter last, Function f); #include <algorithm> #include <cassert> +#include "test_macros.h" #include "test_iterators.h" +#if TEST_STD_VER > 17 +TEST_CONSTEXPR bool test_constexpr() { + int ia[] = {1, 3, 6, 7}; + int expected[] = {3, 5, 8, 9}; + + std::for_each(std::begin(ia), std::end(ia), [](int &a) { a += 2; }); + return std::equal(std::begin(ia), std::end(ia), std::begin(expected)) + ; + } +#endif + struct for_each_test { for_each_test(int c) : count(c) {} @@ -36,4 +48,8 @@ int main() assert(f.count == s); for (unsigned i = 0; i < s; ++i) assert(ia[i] == static_cast<int>(i+1)); + +#if TEST_STD_VER > 17 + static_assert(test_constexpr()); +#endif } |