brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · bc1bdcb Raw
107 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// class flat_multimap14 15// containers extract() &&;16 17#include <algorithm>18#include <concepts>19#include <deque>20#include <flat_map>21#include <functional>22#include <vector>23 24#include "MinSequenceContainer.h"25#include "../helpers.h"26#include "test_macros.h"27#include "min_allocator.h"28 29template <class T>30concept CanExtract = requires(T&& t) { std::forward<T>(t).extract(); };31 32static_assert(CanExtract<std::flat_multimap<int, int>&&>);33static_assert(!CanExtract<std::flat_multimap<int, int>&>);34static_assert(!CanExtract<std::flat_multimap<int, int> const&>);35static_assert(!CanExtract<std::flat_multimap<int, int> const&&>);36 37template <class KeyContainer, class ValueContainer>38constexpr void test() {39  using M = std::flat_multimap<int, int, std::less<int>, KeyContainer, ValueContainer>;40  M m     = M({1, 2, 2, 2, 3, 3}, {4, 5, 6, 7, 8, 9});41 42  std::same_as<typename M::containers> auto containers = std::move(m).extract();43 44  auto expected_keys = {1, 2, 2, 2, 3, 3};45  assert(std::ranges::equal(containers.keys, expected_keys));46  check_possible_values(47      containers.values, std::vector<std::vector<int>>{{4}, {5, 6, 7}, {5, 6, 7}, {5, 6, 7}, {8, 9}, {8, 9}});48  check_invariant(m);49  LIBCPP_ASSERT(m.empty());50  LIBCPP_ASSERT(m.keys().size() == 0);51  LIBCPP_ASSERT(m.values().size() == 0);52}53 54constexpr bool test() {55  test<std::vector<int>, std::vector<int>>();56#ifndef __cpp_lib_constexpr_deque57  if (!TEST_IS_CONSTANT_EVALUATED)58#endif59    test<std::deque<int>, std::vector<int>>();60  test<MinSequenceContainer<int>, MinSequenceContainer<int>>();61  test<std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>();62  {63    // extracted object maintains invariant if one of underlying container does not clear after move64    using M = std::flat_multimap<int, int, std::less<>, std::vector<int>, CopyOnlyVector<int>>;65    M m     = M({1, 2, 2, 2, 3, 3}, {1, 2, 3, 4, 5, 6});66    std::same_as<M::containers> auto containers = std::move(m).extract();67    assert(containers.keys.size() == 6);68    assert(containers.values.size() == 6);69    check_invariant(m);70    LIBCPP_ASSERT(m.empty());71    LIBCPP_ASSERT(m.keys().size() == 0);72    LIBCPP_ASSERT(m.values().size() == 0);73  }74 75  if (!TEST_IS_CONSTANT_EVALUATED) {76#ifndef TEST_HAS_NO_EXCEPTIONS77    using KeyContainer   = std::vector<int>;78    using ValueContainer = ThrowOnMoveContainer<int>;79    using M              = std::flat_multimap<int, int, std::ranges::less, KeyContainer, ValueContainer>;80 81    M m;82    m.emplace(1, 1);83    m.emplace(1, 1);84    try {85      auto c = std::move(m).extract();86      assert(false);87    } catch (int) {88      check_invariant(m);89      // In libc++, we try to erase the key after value emplacement failure.90      // and after erasure failure, we clear the flat_multimap91      LIBCPP_ASSERT(m.size() == 0);92    }93#endif94  }95 96  return true;97}98 99int main(int, char**) {100  test();101#if TEST_STD_VER >= 26102  static_assert(test());103#endif104 105  return 0;106}107