106 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 11// <flat_set>12 13// friend void swap(flat_set& x, flat_set& y) noexcept14 15#include <flat_set>16#include <cassert>17#include <deque>18#include <functional>19#include <vector>20 21#include "MinSequenceContainer.h"22#include "MoveOnly.h"23#include "min_allocator.h"24#include "test_macros.h"25#include "../helpers.h"26 27// test noexcept28 29template <class T>30concept NoExceptAdlSwap = requires(T t1, T t2) {31 { swap(t1, t2) } noexcept;32};33 34static_assert(NoExceptAdlSwap<std::flat_set<int>>);35 36#ifndef TEST_HAS_NO_EXCEPTIONS37static_assert(NoExceptAdlSwap<std::flat_set<int, std::less<int>, ThrowOnMoveContainer<int>>>);38#endif39 40template <class KeyContainer>41constexpr void test_one() {42 using Key = typename KeyContainer::value_type;43 using M = std::flat_set<Key, std::less<Key>, KeyContainer>;44 45 {46 M m1;47 M m2;48 M m1_save = m1;49 M m2_save = m2;50 swap(m1, m2);51 assert(m1 == m2_save);52 assert(m2 == m1_save);53 }54 {55 int ar2[] = {5, 6, 7, 8, 9, 10, 11, 12};56 M m1;57 M m2(ar2, ar2 + sizeof(ar2) / sizeof(ar2[0]));58 M m1_save = m1;59 M m2_save = m2;60 swap(m1, m2);61 assert(m1 == m2_save);62 assert(m2 == m1_save);63 }64 {65 int ar1[] = {1, 2, 3, 4};66 M m1(ar1, ar1 + sizeof(ar1) / sizeof(ar1[0]));67 M m2;68 M m1_save = m1;69 M m2_save = m2;70 swap(m1, m2);71 assert(m1 == m2_save);72 assert(m2 == m1_save);73 }74 {75 int ar1[] = {1, 2, 3, 4};76 int ar2[] = {5, 6, 7, 8, 9, 10, 11, 12};77 M m1(ar1, ar1 + sizeof(ar1) / sizeof(ar1[0]));78 M m2(ar2, ar2 + sizeof(ar2) / sizeof(ar2[0]));79 M m1_save = m1;80 M m2_save = m2;81 swap(m1, m2);82 assert(m1 == m2_save);83 assert(m2 == m1_save);84 }85}86 87constexpr bool test() {88 test_one<std::vector<int>>();89#ifndef __cpp_lib_constexpr_deque90 if (!TEST_IS_CONSTANT_EVALUATED)91#endif92 test_one<std::deque<int>>();93 test_one<MinSequenceContainer<int>>();94 95 return true;96}97 98int main(int, char**) {99 test();100#if TEST_STD_VER >= 26101 static_assert(test());102#endif103 104 return 0;105}106