1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14
10// UNSUPPORTED: no-exceptions
11// `check_assertion.h` requires Unix headers and regex support.
12// UNSUPPORTED: !has-unix-headers, no-localization
13
14// UNSUPPORTED: libcpp-has-no-incomplete-pstl
15
16// check that std::for_each(ExecutionPolicy) and std::for_each_n(ExecutionPolicy) terminate on user-thrown exceptions
17
18#include <algorithm>
19
20#include "check_assertion.h"
21#include "test_execution_policies.h"
22#include "test_iterators.h"
23
24int main(int, char**) {
25 test_execution_policies([](auto&& policy) {
26 int a[] = {1, 2};
27 // std::for_each
28 EXPECT_STD_TERMINATE([&] { std::for_each(policy, std::begin(arr&: a), std::end(arr&: a), [](int) { throw int{}; }); });
29 EXPECT_STD_TERMINATE([&] {
30 try {
31 (void)std::for_each(
32 policy,
33 util::throw_on_move_iterator(std::begin(a), 1),
34 util::throw_on_move_iterator(std::end(a), 1),
35 [](int) {});
36 } catch (const util::iterator_error&) {
37 assert(false);
38 }
39 std::terminate(); // make the test pass in case the algorithm didn't move the iterator
40 });
41
42 // std::for_each_n
43 EXPECT_STD_TERMINATE([&] { std::for_each_n(policy, std::data(array&: a), std::size(a), [](int) { throw int{}; }); });
44 EXPECT_STD_TERMINATE([&] {
45 try {
46 (void)std::for_each_n(policy, util::throw_on_move_iterator(std::begin(a), 1), std::size(a), [](int) {});
47 } catch (const util::iterator_error&) {
48 assert(false);
49 }
50 std::terminate(); // make the test pass in case the algorithm didn't move the iterator
51 });
52 });
53}
54

source code of libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/pstl.exception_handling.pass.cpp