| 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, c++17, c++20 |
| 10 | // `check_assertion.h` requires Unix headers and regex support. |
| 11 | // REQUIRES: has-unix-headers |
| 12 | // UNSUPPORTED: no-localization |
| 13 | // UNSUPPORTED: no-exceptions |
| 14 | |
| 15 | // <flat_set> |
| 16 | |
| 17 | // void swap(flat_multiset& y) noexcept; |
| 18 | // friend void swap(flat_multiset& x, flat_multiset& y) noexcept |
| 19 | |
| 20 | // Test that std::terminate is called if any exception is thrown during swap |
| 21 | |
| 22 | #include <flat_set> |
| 23 | #include <cassert> |
| 24 | #include <deque> |
| 25 | #include <functional> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "test_macros.h" |
| 29 | #include "../helpers.h" |
| 30 | #include "check_assertion.h" |
| 31 | |
| 32 | template <class F> |
| 33 | void test_swap_exception_guarantee([[maybe_unused]] F&& swap_function) { |
| 34 | { |
| 35 | // key swap throws |
| 36 | using KeyContainer = ThrowOnMoveContainer<int>; |
| 37 | using M = std::flat_multiset<int, TransparentComparator, KeyContainer>; |
| 38 | |
| 39 | M m1, m2; |
| 40 | m1.emplace(1); |
| 41 | m1.emplace(1); |
| 42 | m2.emplace(1); |
| 43 | m2.emplace(4); |
| 44 | // swap is noexcept |
| 45 | EXPECT_STD_TERMINATE([&] { swap_function(m1, m2); }); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | int main(int, char**) { |
| 50 | { |
| 51 | auto swap_func = [](auto& m1, auto& m2) { swap(m1, m2); }; |
| 52 | test_swap_exception_guarantee(swap_function&: swap_func); |
| 53 | } |
| 54 | |
| 55 | { |
| 56 | auto swap_func = [](auto& m1, auto& m2) { m1.swap(m2); }; |
| 57 | test_swap_exception_guarantee(swap_function&: swap_func); |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |