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