Fix order and types of members in C++17 insert_return_type structs
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_set / modifiers / extract.cc
blob5be81951407eeeb3dd5450f034c6cfac095124bc
1 // Copyright (C) 2016-2017 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.
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.
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/>.
18 // { dg-options "-std=gnu++17" }
19 // { dg-skip-if "" { *-*-* } { "-D_GLIBCXX_PROFILE" } }
21 #include <unordered_set>
22 #include <testsuite_hooks.h>
24 using test_type = std::unordered_set<int>;
26 void
27 test01()
29 test_type c{ 1, 2, 3 };
30 test_type::node_type node;
31 test_type::insert_return_type ins;
32 test_type::iterator pos;
34 node = c.extract(0);
35 VERIFY( !node );
36 VERIFY( node.empty() );
37 VERIFY( c.size() == 3 );
39 ins = c.insert(std::move(node));
40 VERIFY( !node );
41 VERIFY( node.empty() );
42 VERIFY( c.size() == 3 );
43 VERIFY( !ins.inserted );
44 VERIFY( !ins.node );
45 VERIFY( ins.position == c.end() );
47 node = c.extract(1);
48 VERIFY( (bool)node );
49 VERIFY( !node.empty() );
50 VERIFY( c.size() == 2 );
51 VERIFY( node.get_allocator() == c.get_allocator() );
52 VERIFY( node.value() == 1 );
54 node.value() = 4;
55 VERIFY( node.value() == 4 );
57 ins = c.insert(std::move(node));
58 VERIFY( !node );
59 VERIFY( node.empty() );
60 VERIFY( c.size() == 3 );
61 VERIFY( ins.inserted );
62 VERIFY( !ins.node );
63 VERIFY( ins.position != c.end() );
64 VERIFY( *ins.position == 4 );
65 VERIFY( c.count(1) == 0 );
66 VERIFY( c.count(4) == 1 );
68 pos = c.insert(c.begin(), std::move(node));
69 VERIFY( !node );
70 VERIFY( node.empty() );
71 VERIFY( c.size() == 3 );
72 VERIFY( pos == c.end() );
74 pos = c.insert(c.begin(), c.extract(2));
75 VERIFY( c.size() == 3 );
76 VERIFY( pos != c.end() );
77 VERIFY( *pos == 2 );
79 test_type c2 = c;
80 node = c2.extract(3);
81 ins = c.insert(std::move(node));
82 VERIFY( node.empty() );
83 VERIFY( ins.position != c.end() );
84 VERIFY( !ins.inserted );
85 VERIFY( !ins.node.empty() );
86 VERIFY( ins.node.value() == 3 );
87 auto hasher = c.hash_function();
88 VERIFY( hasher(*ins.position) == hasher(ins.node.value()) );
89 auto eq = c.key_eq();
90 VERIFY( eq(*ins.position, ins.node.value()) );
93 void
94 test02()
96 test_type c{ 1, 2, 3 };
97 test_type::node_type node;
98 test_type::insert_return_type ins;
100 const int val = *c.begin();
101 node = c.extract(c.begin());
102 VERIFY( (bool)node );
103 VERIFY( !node.empty() );
104 VERIFY( c.size() == 2 );
105 VERIFY( node.get_allocator() == c.get_allocator() );
106 VERIFY( node.value() == val );
108 ins = c.insert(std::move(node));
109 VERIFY( node.empty() );
110 VERIFY( c.size() == 3 );
111 VERIFY( ins.inserted );
112 VERIFY( !ins.node );
113 VERIFY( ins.position != c.end() );
114 VERIFY( *ins.position == val );
117 void
118 test03()
120 struct hash : std::hash<int> { };
121 struct equal : std::equal_to<int> { };
122 using std::is_same_v;
123 using compat_type1 = std::unordered_set<int, hash, equal>;
124 static_assert( is_same_v<test_type::node_type, compat_type1::node_type> );
125 using compat_type2 = std::unordered_multiset<int>;
126 static_assert( is_same_v<test_type::node_type, compat_type2::node_type> );
127 using compat_type3 = std::unordered_multiset<int, hash, equal>;
128 static_assert( is_same_v<test_type::node_type, compat_type3::node_type> );
131 void
132 test04()
134 // Check order of members in insert_return_type
135 auto [pos, ins, node] = test_type::insert_return_type{};
136 using std::is_same_v;
137 static_assert( is_same_v<test_type::iterator, decltype(pos)> );
138 static_assert( is_same_v<bool, decltype(ins)> );
139 static_assert( is_same_v<test_type::node_type, decltype(node)> );
143 main()
145 test01();
146 test02();
147 test03();