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