]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_multiset/modifiers/extract.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_multiset / modifiers / extract.cc
1 // Copyright (C) 2016-2024 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-do run { target c++17 } }
19
20 #include <unordered_set>
21 #include <testsuite_hooks.h>
22
23 using test_type = std::unordered_multiset<int>;
24
25 void
26 test01()
27 {
28 test_type c{ 1, 1, 2, 2, 3, 3 };
29 test_type::node_type node;
30 test_type::iterator pos;
31
32 node = c.extract(0);
33 VERIFY( !node );
34 VERIFY( node.empty() );
35 VERIFY( c.size() == 6 );
36
37 pos = c.insert(std::move(node));
38 VERIFY( !node );
39 VERIFY( node.empty() );
40 VERIFY( c.size() == 6 );
41 VERIFY( pos == c.end() );
42
43 node = c.extract(1);
44 VERIFY( (bool)node );
45 VERIFY( !node.empty() );
46 VERIFY( c.size() == 5 );
47 VERIFY( node.get_allocator() == c.get_allocator() );
48 VERIFY( node.value() == 1 );
49
50 node.value() = 4;
51 VERIFY( node.value() == 4 );
52
53 pos = c.insert(std::move(node));
54 VERIFY( !node );
55 VERIFY( node.empty() );
56 VERIFY( c.size() == 6 );
57 VERIFY( pos != c.end() );
58 VERIFY( *pos == 4 );
59 VERIFY( c.count(1) == 1 );
60 VERIFY( c.count(4) == 1 );
61
62 pos = c.insert(c.begin(), std::move(node));
63 VERIFY( !node );
64 VERIFY( node.empty() );
65 VERIFY( c.size() == 6 );
66 VERIFY( pos == c.end() );
67
68 node = c.extract(1);
69 pos = c.insert(c.begin(), std::move(node));
70 VERIFY( !node );
71 VERIFY( c.size() == 6 );
72 VERIFY( pos != c.end() );
73 VERIFY( *pos == 1 );
74
75 test_type c2 = c;
76 node = c2.extract(1);
77 pos = c.insert(std::move(node));
78 VERIFY( pos != c.end() );
79 VERIFY( node.empty() );
80 VERIFY( *pos == 1 );
81 }
82
83 void
84 test02()
85 {
86 test_type c{ 1, 1, 2, 2, 3, 3 };
87 test_type::node_type node;
88 test_type::iterator pos;
89
90 const int val = *c.begin();
91 node = c.extract(c.begin());
92 VERIFY( !node.empty() );
93 VERIFY( c.size() == 5 );
94 VERIFY( node.get_allocator() == c.get_allocator() );
95 VERIFY( node.value() == val );
96
97 pos = c.insert(std::move(node));
98 VERIFY( node.empty() );
99 VERIFY( c.size() == 6 );
100 VERIFY( pos != c.end() );
101 VERIFY( *pos == val );
102 }
103
104 void
105 test03()
106 {
107 struct hash : std::hash<int> { };
108 struct equal : std::equal_to<int> { };
109 using std::is_same_v;
110 using compat_type1 = std::unordered_multiset<int, hash, equal>;
111 static_assert( is_same_v<test_type::node_type, compat_type1::node_type> );
112 using compat_type2 = std::unordered_set<int>;
113 static_assert( is_same_v<test_type::node_type, compat_type2::node_type> );
114 using compat_type3 = std::unordered_set<int, hash, equal>;
115 static_assert( is_same_v<test_type::node_type, compat_type3::node_type> );
116 }
117
118 int
119 main()
120 {
121 test01();
122 test02();
123 test03();
124 }