| 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_map> |
| 16 | |
| 17 | // void swap(flat_map& y) noexcept; |
| 18 | // friend void swap(flat_map& x, flat_map& y) noexcept |
| 19 | |
| 20 | // Test that std::terminate is called if any exception is thrown during swap |
| 21 | |
| 22 | #include <flat_map> |
| 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 ValueContainer = std::vector<int>; |
| 38 | using M = std::flat_map<int, int, TransparentComparator, KeyContainer, ValueContainer>; |
| 39 | |
| 40 | M m1, m2; |
| 41 | m1.emplace(1, 1); |
| 42 | m1.emplace(2, 2); |
| 43 | m2.emplace(3, 3); |
| 44 | m2.emplace(4, 4); |
| 45 | // swap is noexcept |
| 46 | EXPECT_STD_TERMINATE([&] { swap_function(m1, m2); }); |
| 47 | } |
| 48 | |
| 49 | { |
| 50 | // value swap throws |
| 51 | using KeyContainer = std::vector<int>; |
| 52 | using ValueContainer = ThrowOnMoveContainer<int>; |
| 53 | using M = std::flat_map<int, int, TransparentComparator, KeyContainer, ValueContainer>; |
| 54 | |
| 55 | M m1, m2; |
| 56 | m1.emplace(1, 1); |
| 57 | m1.emplace(2, 2); |
| 58 | m2.emplace(3, 3); |
| 59 | m2.emplace(4, 4); |
| 60 | |
| 61 | // swap is noexcept |
| 62 | EXPECT_STD_TERMINATE([&] { swap_function(m1, m2); }); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | int main(int, char**) { |
| 67 | { |
| 68 | auto swap_func = [](auto& m1, auto& m2) { swap(m1, m2); }; |
| 69 | test_swap_exception_guarantee(swap_function&: swap_func); |
| 70 | } |
| 71 | |
| 72 | { |
| 73 | auto swap_func = [](auto& m1, auto& m2) { m1.swap(m2); }; |
| 74 | test_swap_exception_guarantee(swap_function&: swap_func); |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |