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// containers extract() &&;14 15#include <algorithm>16#include <concepts>17#include <deque>18#include <flat_map>19#include <functional>20#include <vector>21 22#include "MinSequenceContainer.h"23#include "../helpers.h"24#include "test_macros.h"25#include "min_allocator.h"26 27template <class T>28concept CanExtract = requires(T&& t) { std::forward<T>(t).extract(); };29 30static_assert(CanExtract<std::flat_map<int, int>&&>);31static_assert(!CanExtract<std::flat_map<int, int>&>);32static_assert(!CanExtract<std::flat_map<int, int> const&>);33static_assert(!CanExtract<std::flat_map<int, int> const&&>);34 35template <class KeyContainer, class ValueContainer>36constexpr void test() {37 using M = std::flat_map<int, int, std::less<int>, KeyContainer, ValueContainer>;38 M m = M({1, 2, 3}, {4, 5, 6});39 40 std::same_as<typename M::containers> auto containers = std::move(m).extract();41 42 auto expected_keys = {1, 2, 3};43 auto expected_values = {4, 5, 6};44 assert(std::ranges::equal(containers.keys, expected_keys));45 assert(std::ranges::equal(containers.values, expected_values));46 check_invariant(m);47 LIBCPP_ASSERT(m.empty());48 LIBCPP_ASSERT(m.keys().size() == 0);49 LIBCPP_ASSERT(m.values().size() == 0);50}51 52constexpr bool test() {53 test<std::vector<int>, std::vector<int>>();54#ifndef __cpp_lib_constexpr_deque55 if (!TEST_IS_CONSTANT_EVALUATED)56#endif57 {58 test<std::deque<int>, std::vector<int>>();59 }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_map<int, int, std::less<>, std::vector<int>, CopyOnlyVector<int>>;65 M m = M({1, 2, 3}, {1, 2, 3});66 std::same_as<M::containers> auto containers = std::move(m).extract();67 assert(containers.keys.size() == 3);68 assert(containers.values.size() == 3);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_map<int, int, std::ranges::less, KeyContainer, ValueContainer>;80 81 M m;82 m.emplace(1, 1);83 m.emplace(2, 2);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_map91 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