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