112 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_map>12 13// friend void swap(flat_map& x, flat_map& y) noexcept14 15#include <flat_map>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_map<int, int>>);35 36#ifndef TEST_HAS_NO_EXCEPTIONS37static_assert(38 NoExceptAdlSwap<std::flat_map<int, int, std::less<int>, ThrowOnMoveContainer<int>, ThrowOnMoveContainer<int>>>);39#endif40 41template <class KeyContainer, class ValueContainer>42constexpr void test() {43 using Key = typename KeyContainer::value_type;44 using Value = typename ValueContainer::value_type;45 using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;46 using V = std::pair<const Key, Value>;47 48 {49 M m1;50 M m2;51 M m1_save = m1;52 M m2_save = m2;53 swap(m1, m2);54 assert(m1 == m2_save);55 assert(m2 == m1_save);56 }57 {58 V ar2[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)};59 M m1;60 M m2(ar2, ar2 + sizeof(ar2) / sizeof(ar2[0]));61 M m1_save = m1;62 M m2_save = m2;63 swap(m1, m2);64 assert(m1 == m2_save);65 assert(m2 == m1_save);66 }67 {68 V ar1[] = {V(1, 1), V(2, 2), V(3, 3), V(4, 4)};69 M m1(ar1, ar1 + sizeof(ar1) / sizeof(ar1[0]));70 M m2;71 M m1_save = m1;72 M m2_save = m2;73 swap(m1, m2);74 assert(m1 == m2_save);75 assert(m2 == m1_save);76 }77 {78 V ar1[] = {V(1, 1), V(2, 2), V(3, 3), V(4, 4)};79 V ar2[] = {V(5, 5), V(6, 6), V(7, 7), V(8, 8), V(9, 9), V(10, 10), V(11, 11), V(12, 12)};80 M m1(ar1, ar1 + sizeof(ar1) / sizeof(ar1[0]));81 M m2(ar2, ar2 + sizeof(ar2) / sizeof(ar2[0]));82 M m1_save = m1;83 M m2_save = m2;84 swap(m1, m2);85 assert(m1 == m2_save);86 assert(m2 == m1_save);87 }88}89 90constexpr bool test() {91 test<std::vector<int>, std::vector<double>>();92#ifndef __cpp_lib_constexpr_deque93 if (!TEST_IS_CONSTANT_EVALUATED)94#endif95 {96 test<std::deque<int>, std::vector<double>>();97 }98 test<MinSequenceContainer<int>, MinSequenceContainer<double>>();99 test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();100 101 return true;102}103 104int main(int, char**) {105 test();106#if TEST_STD_VER >= 26107 static_assert(test());108#endif109 110 return 0;111}112