]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/extract.cc
Implement C++17 node extraction and insertion (P0083R5)
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_map / modifiers / extract.cc
CommitLineData
2dbe56bd
JW
1// Copyright (C) 2016 Free Software Foundation, Inc.
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
18// { dg-options "-std=gnu++17" }
19
20#include <unordered_map>
21#include <testsuite_hooks.h>
22
23using test_type = std::unordered_map<int, int>;
24
25void
26test01()
27{
28 bool test __attribute__((unused)) = true;
29
30 test_type c{ {1, 10}, {2, 20}, {3, 30} };
31 test_type::node_type node;
32 test_type::insert_return_type ins;
33 test_type::iterator pos;
34
35 node = c.extract(0);
36 VERIFY( !node );
37 VERIFY( node.empty() );
38 VERIFY( c.size() == 3 );
39
40 ins = c.insert(std::move(node));
41 VERIFY( !node );
42 VERIFY( node.empty() );
43 VERIFY( c.size() == 3 );
44 VERIFY( !ins.inserted );
45 VERIFY( !ins.node );
46 VERIFY( ins.position == c.end() );
47
48 node = c.extract(1);
49 VERIFY( (bool)node );
50 VERIFY( !node.empty() );
51 VERIFY( c.size() == 2 );
52 VERIFY( node.get_allocator() == c.get_allocator() );
53 VERIFY( node.key() == 1 );
54 VERIFY( node.mapped() == 10 );
55
56 node.key() = 4;
57 node.mapped() = 40;
58 VERIFY( node.key() == 4 );
59 VERIFY( node.mapped() == 40 );
60
61 ins = c.insert(std::move(node));
62 VERIFY( !node );
63 VERIFY( node.empty() );
64 VERIFY( c.size() == 3 );
65 VERIFY( ins.inserted );
66 VERIFY( !ins.node );
67 VERIFY( ins.position != c.end() );
68 VERIFY( ins.position->first == 4 );
69 VERIFY( ins.position->second == 40 );
70 VERIFY( c.count(1) == 0 );
71 VERIFY( c.count(4) == 1 );
72
73 pos = c.insert(c.begin(), std::move(node));
74 VERIFY( !node );
75 VERIFY( node.empty() );
76 VERIFY( c.size() == 3 );
77 VERIFY( pos == c.end() );
78
79 pos = c.insert(c.begin(), c.extract(2));
80 VERIFY( c.size() == 3 );
81 VERIFY( pos != c.end() );
82 VERIFY( pos->first == 2 );
83 VERIFY( pos->second == 20 );
84
85 test_type c2 = c;
86 node = c2.extract(3);
87 ins = c.insert(std::move(node));
88 VERIFY( node.empty() );
89 VERIFY( ins.position != c.end() );
90 VERIFY( !ins.inserted );
91 VERIFY( !ins.node.empty() );
92 VERIFY( ins.node.key() == 3 );
93 VERIFY( ins.node.mapped() == 30 );
94 auto hasher = c.hash_function();
95 VERIFY( hasher(ins.position->first) == hasher(ins.node.key()) );
96 auto eq = c.key_eq();
97 VERIFY( eq(ins.position->first, ins.node.key()) );
98}
99
100void
101test02()
102{
103 bool test __attribute__((unused)) = true;
104
105 test_type c{ {1, 10}, {2, 20}, {3, 30} };
106 test_type::node_type node;
107 test_type::insert_return_type ins;
108
109 const int key = c.begin()->first;
110 node = c.extract(c.begin());
111 VERIFY( (bool)node );
112 VERIFY( !node.empty() );
113 VERIFY( c.size() == 2 );
114 VERIFY( node.get_allocator() == c.get_allocator() );
115 VERIFY( node.key() == key );
116 VERIFY( node.mapped() == (key * 10) );
117
118 ins = c.insert(std::move(node));
119 VERIFY( node.empty() );
120 VERIFY( c.size() == 3 );
121 VERIFY( ins.inserted );
122 VERIFY( !ins.node );
123 VERIFY( ins.position != c.end() );
124 VERIFY( ins.position->first == key );
125 VERIFY( ins.position->second == (key * 10) );
126}
127
128void
129test03()
130{
131 struct hash : std::hash<int> { };
132 struct equal : std::equal_to<int> { };
133 using std::is_same_v;
134 using compat_type1 = std::unordered_map<int, int, hash, equal>;
135 static_assert( is_same_v<test_type::node_type, compat_type1::node_type> );
136 using compat_type2 = std::unordered_multimap<int, int>;
137 static_assert( is_same_v<test_type::node_type, compat_type2::node_type> );
138 using compat_type3 = std::unordered_multimap<int, int, hash, equal>;
139 static_assert( is_same_v<test_type::node_type, compat_type3::node_type> );
140}
141
142int
143main()
144{
145 test01();
146 test02();
147 test03();
148}