]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | // Copyright (C) 2016-2021 Free Software Foundation, Inc. |
2dbe56bd JW |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
6458742a | 18 | // { dg-do run { target c++17 } } |
2dbe56bd JW |
19 | |
20 | #include <unordered_map> | |
21 | #include <testsuite_hooks.h> | |
22 | ||
23 | using test_type = std::unordered_map<int, int>; | |
24 | ||
25 | void | |
26 | test01() | |
27 | { | |
2dbe56bd JW |
28 | test_type c{ {1, 10}, {2, 20}, {3, 30} }; |
29 | test_type::node_type node; | |
30 | test_type::insert_return_type ins; | |
31 | test_type::iterator pos; | |
32 | ||
33 | node = c.extract(0); | |
34 | VERIFY( !node ); | |
35 | VERIFY( node.empty() ); | |
36 | VERIFY( c.size() == 3 ); | |
37 | ||
38 | ins = c.insert(std::move(node)); | |
39 | VERIFY( !node ); | |
40 | VERIFY( node.empty() ); | |
41 | VERIFY( c.size() == 3 ); | |
42 | VERIFY( !ins.inserted ); | |
43 | VERIFY( !ins.node ); | |
44 | VERIFY( ins.position == c.end() ); | |
45 | ||
46 | node = c.extract(1); | |
47 | VERIFY( (bool)node ); | |
48 | VERIFY( !node.empty() ); | |
49 | VERIFY( c.size() == 2 ); | |
50 | VERIFY( node.get_allocator() == c.get_allocator() ); | |
51 | VERIFY( node.key() == 1 ); | |
52 | VERIFY( node.mapped() == 10 ); | |
53 | ||
54 | node.key() = 4; | |
55 | node.mapped() = 40; | |
56 | VERIFY( node.key() == 4 ); | |
57 | VERIFY( node.mapped() == 40 ); | |
58 | ||
59 | ins = c.insert(std::move(node)); | |
60 | VERIFY( !node ); | |
61 | VERIFY( node.empty() ); | |
62 | VERIFY( c.size() == 3 ); | |
63 | VERIFY( ins.inserted ); | |
64 | VERIFY( !ins.node ); | |
65 | VERIFY( ins.position != c.end() ); | |
66 | VERIFY( ins.position->first == 4 ); | |
67 | VERIFY( ins.position->second == 40 ); | |
68 | VERIFY( c.count(1) == 0 ); | |
69 | VERIFY( c.count(4) == 1 ); | |
70 | ||
71 | pos = c.insert(c.begin(), std::move(node)); | |
72 | VERIFY( !node ); | |
73 | VERIFY( node.empty() ); | |
74 | VERIFY( c.size() == 3 ); | |
75 | VERIFY( pos == c.end() ); | |
76 | ||
77 | pos = c.insert(c.begin(), c.extract(2)); | |
78 | VERIFY( c.size() == 3 ); | |
79 | VERIFY( pos != c.end() ); | |
80 | VERIFY( pos->first == 2 ); | |
81 | VERIFY( pos->second == 20 ); | |
82 | ||
83 | test_type c2 = c; | |
84 | node = c2.extract(3); | |
85 | ins = c.insert(std::move(node)); | |
86 | VERIFY( node.empty() ); | |
87 | VERIFY( ins.position != c.end() ); | |
88 | VERIFY( !ins.inserted ); | |
89 | VERIFY( !ins.node.empty() ); | |
90 | VERIFY( ins.node.key() == 3 ); | |
91 | VERIFY( ins.node.mapped() == 30 ); | |
92 | auto hasher = c.hash_function(); | |
93 | VERIFY( hasher(ins.position->first) == hasher(ins.node.key()) ); | |
94 | auto eq = c.key_eq(); | |
95 | VERIFY( eq(ins.position->first, ins.node.key()) ); | |
96 | } | |
97 | ||
98 | void | |
99 | test02() | |
100 | { | |
2dbe56bd JW |
101 | test_type c{ {1, 10}, {2, 20}, {3, 30} }; |
102 | test_type::node_type node; | |
103 | test_type::insert_return_type ins; | |
104 | ||
105 | const int key = c.begin()->first; | |
106 | node = c.extract(c.begin()); | |
107 | VERIFY( (bool)node ); | |
108 | VERIFY( !node.empty() ); | |
109 | VERIFY( c.size() == 2 ); | |
110 | VERIFY( node.get_allocator() == c.get_allocator() ); | |
111 | VERIFY( node.key() == key ); | |
112 | VERIFY( node.mapped() == (key * 10) ); | |
113 | ||
114 | ins = c.insert(std::move(node)); | |
115 | VERIFY( node.empty() ); | |
116 | VERIFY( c.size() == 3 ); | |
117 | VERIFY( ins.inserted ); | |
118 | VERIFY( !ins.node ); | |
119 | VERIFY( ins.position != c.end() ); | |
120 | VERIFY( ins.position->first == key ); | |
121 | VERIFY( ins.position->second == (key * 10) ); | |
122 | } | |
123 | ||
124 | void | |
125 | test03() | |
126 | { | |
127 | struct hash : std::hash<int> { }; | |
128 | struct equal : std::equal_to<int> { }; | |
129 | using std::is_same_v; | |
130 | using compat_type1 = std::unordered_map<int, int, hash, equal>; | |
131 | static_assert( is_same_v<test_type::node_type, compat_type1::node_type> ); | |
132 | using compat_type2 = std::unordered_multimap<int, int>; | |
133 | static_assert( is_same_v<test_type::node_type, compat_type2::node_type> ); | |
134 | using compat_type3 = std::unordered_multimap<int, int, hash, equal>; | |
135 | static_assert( is_same_v<test_type::node_type, compat_type3::node_type> ); | |
136 | } | |
137 | ||
85c05b5e JW |
138 | void |
139 | test04() | |
140 | { | |
141 | // Check order of members in insert_return_type | |
142 | auto [pos, ins, node] = test_type::insert_return_type{}; | |
143 | using std::is_same_v; | |
144 | static_assert( is_same_v<test_type::iterator, decltype(pos)> ); | |
145 | static_assert( is_same_v<bool, decltype(ins)> ); | |
146 | static_assert( is_same_v<test_type::node_type, decltype(node)> ); | |
147 | } | |
148 | ||
2dbe56bd JW |
149 | int |
150 | main() | |
151 | { | |
152 | test01(); | |
153 | test02(); | |
154 | test03(); | |
155 | } |