62 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010// `check_assertion.h` requires Unix headers and regex support.11// REQUIRES: has-unix-headers12// UNSUPPORTED: no-localization13// UNSUPPORTED: no-exceptions14 15// <flat_set>16 17// void swap(flat_set& y) noexcept;18// friend void swap(flat_set& x, flat_set& y) noexcept19 20// Test that std::terminate is called if any exception is thrown during swap21 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 32template <class F>33void test_swap_exception_guarantee([[maybe_unused]] F&& swap_function) {34 {35 // key swap throws36 using KeyContainer = ThrowOnMoveContainer<int>;37 using M = std::flat_set<int, TransparentComparator, KeyContainer>;38 39 M m1, m2;40 m1.emplace(1);41 m1.emplace(2);42 m2.emplace(3);43 m2.emplace(4);44 // swap is noexcept45 EXPECT_STD_TERMINATE([&] { swap_function(m1, m2); });46 }47}48 49int main(int, char**) {50 {51 auto swap_func = [](auto& m1, auto& m2) { swap(m1, m2); };52 test_swap_exception_guarantee(swap_func);53 }54 55 {56 auto swap_func = [](auto& m1, auto& m2) { m1.swap(m2); };57 test_swap_exception_guarantee(swap_func);58 }59 60 return 0;61}62