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::find(ExecutionPolicy), std::find_if(ExecutionPolicy) and std::find_if_not(ExecutionPolicy) terminate
17// on user-thrown exceptions
18
19#include <algorithm>
20
21#include "check_assertion.h"
22#include "test_execution_policies.h"
23#include "test_iterators.h"
24
25struct ThrowOnCompare {};
26
27#ifndef TEST_HAS_NO_EXCEPTIONS
28bool operator==(ThrowOnCompare, ThrowOnCompare) { throw int{}; }
29#endif
30
31int main(int, char**) {
32 test_execution_policies([](auto&& policy) {
33 // std::find
34 EXPECT_STD_TERMINATE([&] {
35 ThrowOnCompare a[2] = {};
36 (void)std::find(policy, std::begin(arr&: a), std::end(arr&: a), ThrowOnCompare{});
37 });
38 EXPECT_STD_TERMINATE([&] {
39 try {
40 int a[] = {1, 2};
41 (void)std::find(
42 policy, util::throw_on_move_iterator(std::begin(a), 1), util::throw_on_move_iterator(std::end(a), 1), 0);
43 } catch (const util::iterator_error&) {
44 assert(false);
45 }
46 std::terminate(); // make the test pass in case the algorithm didn't move the iterator
47 });
48
49 // std::find_if
50 EXPECT_STD_TERMINATE([&] {
51 int a[] = {1, 2};
52 (void)std::find_if(policy, std::begin(arr&: a), std::end(arr&: a), [](int) -> bool { throw int{}; });
53 });
54 EXPECT_STD_TERMINATE([&] {
55 try {
56 int a[] = {1, 2};
57 (void)std::find_if(
58 policy,
59 util::throw_on_move_iterator(std::begin(a), 1),
60 util::throw_on_move_iterator(std::end(a), 1),
61 [](int) { return true; });
62 } catch (const util::iterator_error&) {
63 assert(false);
64 }
65 std::terminate(); // make the test pass in case the algorithm didn't move the iterator
66 });
67
68 // std::find_if_not
69 EXPECT_STD_TERMINATE([&] {
70 int a[] = {1, 2};
71 (void)std::find_if_not(policy, std::begin(arr&: a), std::end(arr&: a), [](int) -> bool { throw int{}; });
72 });
73 EXPECT_STD_TERMINATE([&] {
74 try {
75 int a[] = {1, 2};
76 (void)std::find_if_not(
77 policy,
78 util::throw_on_move_iterator(std::begin(a), 1),
79 util::throw_on_move_iterator(std::end(a), 1),
80 [](int) { return true; });
81 } catch (const util::iterator_error&) {
82 assert(false);
83 }
84 std::terminate(); // make the test pass in case the algorithm didn't move the iterator
85 });
86 });
87}
88

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