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::fill(ExecutionPolicy) and std::fill_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
24#ifndef TEST_HAS_NO_EXCEPTIONS
25struct ThrowOnCopy {
26 ThrowOnCopy& operator=(const ThrowOnCopy&) { throw int{}; }
27};
28#endif
29
30int main(int, char**) {
31 ThrowOnCopy a[2]{};
32 int b[2]{};
33
34 test_execution_policies([&](auto&& policy) {
35 // std::fill
36 EXPECT_STD_TERMINATE([&] { (void)std::fill(policy, std::begin(arr&: a), std::end(arr&: a), ThrowOnCopy{}); });
37 EXPECT_STD_TERMINATE([&] {
38 try {
39 (void)std::fill(
40 policy, util::throw_on_move_iterator(std::begin(b), 1), util::throw_on_move_iterator(std::end(b), 1), 0);
41 } catch (const util::iterator_error&) {
42 assert(false);
43 }
44 std::terminate(); // make the test pass in case the algorithm didn't move the iterator
45 });
46
47 // std::fill_n
48 EXPECT_STD_TERMINATE([&] { (void)std::fill_n(policy, std::begin(arr&: a), std::size(a), ThrowOnCopy{}); });
49 EXPECT_STD_TERMINATE([&] {
50 try {
51 (void)std::fill_n(policy, util::throw_on_move_iterator(std::begin(b), 1), std::size(b), 0);
52 } catch (const util::iterator_error&) {
53 assert(false);
54 }
55 std::terminate(); // make the test pass in case the algorithm didn't move the iterator
56 });
57 });
58}
59

source code of libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/pstl.exception_handling.pass.cpp