]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_map/erase/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_map / erase / 1.cc
1 // { dg-do run { target c++11 } }
2
3 // 2010-02-10 Paolo Carlini <paolo.carlini@oracle.com>
4 //
5 // Copyright (C) 2010-2023 Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
21
22 #include <unordered_map>
23 #include <string>
24 #include <testsuite_hooks.h>
25
26 namespace
27 {
28 std::size_t
29 get_nb_bucket_elems(const std::unordered_map<std::string, int>& us)
30 {
31 std::size_t nb = 0;
32 for (std::size_t b = 0; b != us.bucket_count(); ++b)
33 nb += us.bucket_size(b);
34 return nb;
35 }
36 }
37
38 void test01()
39 {
40 typedef std::unordered_map<std::string, int> Map;
41 typedef Map::iterator iterator;
42 typedef Map::const_iterator const_iterator;
43 typedef Map::value_type value_type;
44
45 Map m1;
46
47 m1.insert(value_type("because to why", 1));
48 m1.insert(value_type("the stockholm syndrome", 2));
49 m1.insert(value_type("a cereous night", 3));
50 m1.insert(value_type("eeilo", 4));
51 m1.insert(value_type("protean", 5));
52 m1.insert(value_type("the way you are when", 6));
53 m1.insert(value_type("tillsammans", 7));
54 m1.insert(value_type("umbra/penumbra", 8));
55 m1.insert(value_type("belonging (no longer mix)", 9));
56 m1.insert(value_type("one line behind", 10));
57 VERIFY( m1.size() == 10 );
58 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
59 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
60
61 VERIFY( m1.erase("eeilo") == 1 );
62 VERIFY( m1.size() == 9 );
63 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
64 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
65 iterator it1 = m1.find("eeilo");
66 VERIFY( it1 == m1.end() );
67
68 VERIFY( m1.erase("tillsammans") == 1 );
69 VERIFY( m1.size() == 8 );
70 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
71 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
72 iterator it2 = m1.find("tillsammans");
73 VERIFY( it2 == m1.end() );
74
75 // Must work (see DR 526)
76 iterator it3 = m1.find("belonging (no longer mix)");
77 VERIFY( it3 != m1.end() );
78 VERIFY( m1.erase(it3->first) == 1 );
79 VERIFY( m1.size() == 7 );
80 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
81 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
82 it3 = m1.find("belonging (no longer mix)");
83 VERIFY( it3 == m1.end() );
84
85 VERIFY( !m1.erase("abra") );
86 VERIFY( m1.size() == 7 );
87 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
88 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
89
90 VERIFY( !m1.erase("eeilo") );
91 VERIFY( m1.size() == 7 );
92
93 VERIFY( m1.erase("because to why") == 1 );
94 VERIFY( m1.size() == 6 );
95 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
96 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
97 iterator it4 = m1.find("because to why");
98 VERIFY( it4 == m1.end() );
99
100 iterator it5 = m1.find("umbra/penumbra");
101 iterator it6 = m1.find("one line behind");
102 VERIFY( it5 != m1.end() );
103 VERIFY( it6 != m1.end() );
104
105 VERIFY( m1.find("the stockholm syndrome") != m1.end() );
106 VERIFY( m1.find("a cereous night") != m1.end() );
107 VERIFY( m1.find("the way you are when") != m1.end() );
108 VERIFY( m1.find("a cereous night") != m1.end() );
109
110 VERIFY( m1.erase(it5->first) == 1 );
111 VERIFY( m1.size() == 5 );
112 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
113 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
114 it5 = m1.find("umbra/penumbra");
115 VERIFY( it5 == m1.end() );
116
117 VERIFY( m1.erase(it6->first) == 1 );
118 VERIFY( m1.size() == 4 );
119 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
120 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
121 it6 = m1.find("one line behind");
122 VERIFY( it6 == m1.end() );
123
124 iterator it7 = m1.begin();
125 iterator it8 = it7;
126 ++it8;
127 iterator it9 = it8;
128 ++it9;
129
130 VERIFY( m1.erase(it8->first) == 1 );
131 VERIFY( m1.size() == 3 );
132 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
133 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
134 VERIFY( ++it7 == it9 );
135
136 iterator it10 = it9;
137 ++it10;
138 iterator it11 = it10;
139
140 VERIFY( m1.erase(it9->first) == 1 );
141 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
142 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
143 VERIFY( m1.size() == 2 );
144 VERIFY( ++it10 == m1.end() );
145
146 VERIFY( m1.erase(m1.begin()) != m1.end() );
147 VERIFY( m1.size() == 1 );
148 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
149 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
150 VERIFY( m1.begin() == it11 );
151
152 VERIFY( m1.erase(m1.begin()->first) == 1 );
153 VERIFY( m1.size() == 0 );
154 VERIFY( get_nb_bucket_elems(m1) == m1.size() );
155 VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
156 VERIFY( m1.begin() == m1.end() );
157 }
158
159 int main()
160 {
161 test01();
162 return 0;
163 }