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
10
11// <unordered_map>
12
13// class unordered_map
14
15// node_type extract(key_type const&);
16
17#include <iterator>
18#include <unordered_map>
19
20#include "test_macros.h"
21#include "min_allocator.h"
22#include "Counter.h"
23
24template <class Container, class KeyTypeIter>
25void test(Container& c, KeyTypeIter first, KeyTypeIter last)
26{
27 std::size_t sz = c.size();
28 assert((std::size_t)std::distance(first, last) == sz);
29
30 for (KeyTypeIter copy = first; copy != last; ++copy)
31 {
32 typename Container::node_type t = c.extract(*copy);
33 assert(!t.empty());
34 --sz;
35 assert(t.key() == *copy);
36 t.key() = *first; // We should be able to mutate key.
37 assert(t.key() == *first);
38 assert(t.get_allocator() == c.get_allocator());
39 assert(sz == c.size());
40 }
41
42 assert(c.size() == 0);
43
44 for (KeyTypeIter copy = first; copy != last; ++copy)
45 {
46 typename Container::node_type t = c.extract(*copy);
47 assert(t.empty());
48 }
49}
50
51int main(int, char**)
52{
53 {
54 std::unordered_map<int, int> m = {{1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}};
55 int keys[] = {1, 2, 3, 4, 5, 6};
56 test(c&: m, first: std::begin(arr&: keys), last: std::end(arr&: keys));
57 }
58
59 {
60 std::unordered_map<Counter<int>, Counter<int>> m =
61 {{1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}};
62 {
63 Counter<int> keys[] = {1, 2, 3, 4, 5, 6};
64 assert(Counter_base::gConstructed == 12+6);
65 test(m, std::begin(keys), std::end(keys));
66 }
67 assert(Counter_base::gConstructed == 0);
68 }
69
70 {
71 using min_alloc_map =
72 std::unordered_map<int, int, std::hash<int>, std::equal_to<int>,
73 min_allocator<std::pair<const int, int>>>;
74 min_alloc_map m = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}};
75 int keys[] = {1, 2, 3, 4, 5, 6};
76 test(m, std::begin(arr&: keys), std::end(arr&: keys));
77 }
78
79 return 0;
80}
81

source code of libcxx/test/std/containers/unord/unord.map/unord.map.modifiers/extract_key.pass.cpp