brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 771bcd3 Raw
110 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// void swap(flat_map& y) noexcept;14 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 NoExceptMemberSwap = requires(T t1, T t2) {31  { t1.swap(t2) } noexcept;32};33 34static_assert(NoExceptMemberSwap<std::flat_map<int, int>>);35#ifndef TEST_HAS_NO_EXCEPTIONS36static_assert(37    NoExceptMemberSwap<std::flat_map<int, int, std::less<int>, ThrowOnMoveContainer<int>, ThrowOnMoveContainer<int>>>);38#endif39 40template <class KeyContainer, class ValueContainer>41constexpr void test() {42  using Key   = typename KeyContainer::value_type;43  using Value = typename ValueContainer::value_type;44  using M     = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;45  using V     = std::pair<const Key, Value>;46  {47    M m1;48    M m2;49    M m1_save = m1;50    M m2_save = m2;51    m1.swap(m2);52    assert(m1 == m2_save);53    assert(m2 == m1_save);54  }55  {56    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)};57    M m1;58    M m2(ar2, ar2 + sizeof(ar2) / sizeof(ar2[0]));59    M m1_save = m1;60    M m2_save = m2;61    m1.swap(m2);62    assert(m1 == m2_save);63    assert(m2 == m1_save);64  }65  {66    V ar1[] = {V(1, 1), V(2, 2), V(3, 3), V(4, 4)};67    M m1(ar1, ar1 + sizeof(ar1) / sizeof(ar1[0]));68    M m2;69    M m1_save = m1;70    M m2_save = m2;71    m1.swap(m2);72    assert(m1 == m2_save);73    assert(m2 == m1_save);74  }75  {76    V ar1[] = {V(1, 1), V(2, 2), V(3, 3), V(4, 4)};77    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)};78    M m1(ar1, ar1 + sizeof(ar1) / sizeof(ar1[0]));79    M m2(ar2, ar2 + sizeof(ar2) / sizeof(ar2[0]));80    M m1_save = m1;81    M m2_save = m2;82    m1.swap(m2);83    assert(m1 == m2_save);84    assert(m2 == m1_save);85  }86}87 88constexpr bool test() {89  test<std::vector<int>, std::vector<double>>();90#ifndef __cpp_lib_constexpr_deque91  if (!TEST_IS_CONSTANT_EVALUATED)92#endif93  {94    test<std::deque<int>, std::vector<double>>();95  }96  test<MinSequenceContainer<int>, MinSequenceContainer<double>>();97  test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();98 99  return true;100}101 102int main(int, char**) {103  test();104#if TEST_STD_VER >= 26105  static_assert(test());106#endif107 108  return 0;109}110