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-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10
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>
36void 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
52int main(int, char**) {
53 test<std::vector<int>, std::vector<int>>();
54 test<std::deque<int>, std::vector<int>>();
55 test<MinSequenceContainer<int>, MinSequenceContainer<int>>();
56 test<std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>();
57 {
58 // extracted object maintains invariant if one of underlying container does not clear after move
59 using M = std::flat_map<int, int, std::less<>, std::vector<int>, CopyOnlyVector<int>>;
60 M m = M({1, 2, 3}, {1, 2, 3});
61 std::same_as<M::containers> auto containers = std::move(m).extract();
62 assert(containers.keys.size() == 3);
63 assert(containers.values.size() == 3);
64 check_invariant(m);
65 LIBCPP_ASSERT(m.empty());
66 LIBCPP_ASSERT(m.keys().size() == 0);
67 LIBCPP_ASSERT(m.values().size() == 0);
68 }
69
70 {
71#ifndef TEST_HAS_NO_EXCEPTIONS
72 using KeyContainer = std::vector<int>;
73 using ValueContainer = ThrowOnMoveContainer<int>;
74 using M = std::flat_map<int, int, std::ranges::less, KeyContainer, ValueContainer>;
75
76 M m;
77 m.emplace(1, 1);
78 m.emplace(2, 2);
79 try {
80 auto c = std::move(m).extract();
81 assert(false);
82 } catch (int) {
83 check_invariant(m);
84 // In libc++, we try to erase the key after value emplacement failure.
85 // and after erasure failure, we clear the flat_map
86 LIBCPP_ASSERT(m.size() == 0);
87 }
88#endif
89 }
90 return 0;
91}
92

source code of libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/extract.pass.cpp