Fix order and types of members in C++17 insert_return_type structs
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / set / modifiers / extract.cc
blob3fbc6b9a798baabd998c076d574a5a34985e372e
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 <set>
22 #include <algorithm>
23 #include <testsuite_hooks.h>
25 using test_type = std::set<int>;
27 void
28 test01()
30 test_type c{ 1, 2, 3 };
31 test_type::node_type node;
32 test_type::insert_return_type ins;
33 test_type::iterator pos;
35 node = c.extract(0);
36 VERIFY( !node );
37 VERIFY( node.empty() );
38 VERIFY( c.size() == 3 );
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() );
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.value() == 1 );
55 node.value() = 4;
56 VERIFY( node.value() == 4 );
58 ins = c.insert(std::move(node));
59 VERIFY( !node );
60 VERIFY( node.empty() );
61 VERIFY( c.size() == 3 );
62 VERIFY( ins.inserted );
63 VERIFY( !ins.node );
64 VERIFY( ins.position != c.end() );
65 VERIFY( *ins.position == 4 );
66 VERIFY( c.count(1) == 0 );
67 VERIFY( c.count(4) == 1 );
68 VERIFY( std::is_sorted(c.begin(), c.end()) );
70 pos = c.insert(c.begin(), std::move(node));
71 VERIFY( !node );
72 VERIFY( node.empty() );
73 VERIFY( c.size() == 3 );
74 VERIFY( pos == c.end() );
76 node = c.extract(2);
77 pos = c.insert(c.begin(), std::move(node));
78 VERIFY( c.size() == 3 );
79 VERIFY( pos != c.end() );
80 VERIFY( *pos == 2 );
82 test_type c2 = c;
83 node = c2.extract(3);
84 ins = c.insert(std::move(node));
85 VERIFY( node.empty() );
86 VERIFY( ins.position != c.end() );
87 VERIFY( !ins.inserted );
88 VERIFY( !ins.node.empty() );
89 VERIFY( ins.node.value() == 3 );
90 VERIFY( *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 node = c.extract(c.begin());
101 VERIFY( (bool)node );
102 VERIFY( !node.empty() );
103 VERIFY( c.size() == 2 );
104 VERIFY( node.get_allocator() == c.get_allocator() );
105 VERIFY( node.value() == 1 );
107 ins = c.insert(std::move(node));
108 VERIFY( node.empty() );
109 VERIFY( c.size() == 3 );
110 VERIFY( ins.inserted );
111 VERIFY( !ins.node );
112 VERIFY( ins.position != c.end() );
113 VERIFY( *ins.position == 1 );
116 void
117 test03()
119 struct less : std::less<int> { };
120 using std::is_same_v;
121 using compat_type1 = std::set<int, less>;
122 static_assert( is_same_v<test_type::node_type, compat_type1::node_type> );
123 using compat_type2 = std::multiset<int>;
124 static_assert( is_same_v<test_type::node_type, compat_type2::node_type> );
125 using compat_type3 = std::multiset<int, less>;
126 static_assert( is_same_v<test_type::node_type, compat_type3::node_type> );
129 void
130 test04()
132 // Check order of members in insert_return_type
133 auto [pos, ins, node] = test_type::insert_return_type{};
134 using std::is_same_v;
135 static_assert( is_same_v<test_type::iterator, decltype(pos)> );
136 static_assert( is_same_v<bool, decltype(ins)> );
137 static_assert( is_same_v<test_type::node_type, decltype(node)> );
141 main()
143 test01();
144 test02();
145 test03();