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// class flat_multimap
18
19// void swap(flat_multimap& y) noexcept;
20// friend void swap(flat_multimap& x, flat_multimap& y) noexcept
21
22// Test that std::terminate is called if any exception is thrown during swap
23
24#include <flat_map>
25#include <cassert>
26#include <deque>
27#include <functional>
28#include <vector>
29
30#include "test_macros.h"
31#include "../helpers.h"
32#include "check_assertion.h"
33
34template <class F>
35void test_swap_exception_guarantee([[maybe_unused]] F&& swap_function) {
36 {
37 // key swap throws
38 using KeyContainer = ThrowOnMoveContainer<int>;
39 using ValueContainer = std::vector<int>;
40 using M = std::flat_multimap<int, int, TransparentComparator, KeyContainer, ValueContainer>;
41
42 M m1, m2;
43 m1.emplace(1, 1);
44 m1.emplace(1, 2);
45 m2.emplace(3, 3);
46 m2.emplace(3, 4);
47 // swap is noexcept
48 EXPECT_STD_TERMINATE([&] { swap_function(m1, m2); });
49 }
50
51 {
52 // value swap throws
53 using KeyContainer = std::vector<int>;
54 using ValueContainer = ThrowOnMoveContainer<int>;
55 using M = std::flat_multimap<int, int, TransparentComparator, KeyContainer, ValueContainer>;
56
57 M m1, m2;
58 m1.emplace(1, 1);
59 m1.emplace(1, 2);
60 m2.emplace(3, 3);
61 m2.emplace(3, 4);
62
63 // swap is noexcept
64 EXPECT_STD_TERMINATE([&] { swap_function(m1, m2); });
65 }
66}
67
68int main(int, char**) {
69 {
70 auto swap_func = [](auto& m1, auto& m2) { swap(m1, m2); };
71 test_swap_exception_guarantee(swap_function&: swap_func);
72 }
73
74 {
75 auto swap_func = [](auto& m1, auto& m2) { m1.swap(m2); };
76 test_swap_exception_guarantee(swap_function&: swap_func);
77 }
78
79 return 0;
80}
81

source code of libcxx/test/std/containers/container.adaptors/flat.multimap/flat.multimap.modifiers/swap_exception.pass.cpp