1 // { dg-options "-Wno-deprecated" }
3 // Copyright (C) 2009-2021 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // This is a copy of tr1/6_containers/unordered_map/erase/1.cc, using
21 // hash_map instead of unordered_map.
25 #include <testsuite_hooks.h>
31 inline size_t hash_string(const char* s
)
40 template<class T
> struct hash
<T
*>
42 size_t operator()(const T
*const & s
) const
43 { return reinterpret_cast<size_t>(s
); }
46 template<> struct hash
<string
>
48 size_t operator()(const string
&s
) const { return hash_string(s
.c_str()); }
51 template<> struct hash
<const string
>
53 size_t operator()(const string
&s
) const { return hash_string(s
.c_str()); }
59 typedef __gnu_cxx::hash_map
<std::string
, int> Map
;
60 typedef Map::iterator iterator
;
61 typedef Map::const_iterator const_iterator
;
62 typedef Map::value_type value_type
;
66 m1
.insert(value_type("because to why", 1));
67 m1
.insert(value_type("the stockholm syndrome", 2));
68 m1
.insert(value_type("a cereous night", 3));
69 m1
.insert(value_type("eeilo", 4));
70 m1
.insert(value_type("protean", 5));
71 m1
.insert(value_type("the way you are when", 6));
72 m1
.insert(value_type("tillsammans", 7));
73 m1
.insert(value_type("umbra/penumbra", 8));
74 m1
.insert(value_type("belonging (no longer mix)", 9));
75 m1
.insert(value_type("one line behind", 10));
76 VERIFY( m1
.size() == 10 );
78 VERIFY( m1
.erase("eeilo") == 1 );
79 VERIFY( m1
.size() == 9 );
80 iterator it1
= m1
.find("eeilo");
81 VERIFY( it1
== m1
.end() );
83 VERIFY( m1
.erase("tillsammans") == 1 );
84 VERIFY( m1
.size() == 8 );
85 iterator it2
= m1
.find("tillsammans");
86 VERIFY( it2
== m1
.end() );
88 // Must work (see DR 526)
89 iterator it3
= m1
.find("belonging (no longer mix)");
90 VERIFY( it3
!= m1
.end() );
91 VERIFY( m1
.erase(it3
->first
) == 1 );
92 VERIFY( m1
.size() == 7 );
93 it3
= m1
.find("belonging (no longer mix)");
94 VERIFY( it3
== m1
.end() );
96 VERIFY( !m1
.erase("abra") );
97 VERIFY( m1
.size() == 7 );
99 VERIFY( !m1
.erase("eeilo") );
100 VERIFY( m1
.size() == 7 );
102 VERIFY( m1
.erase("because to why") == 1 );
103 VERIFY( m1
.size() == 6 );
104 iterator it4
= m1
.find("because to why");
105 VERIFY( it4
== m1
.end() );
107 iterator it5
= m1
.find("umbra/penumbra");
108 iterator it6
= m1
.find("one line behind");
109 VERIFY( it5
!= m1
.end() );
110 VERIFY( it6
!= m1
.end() );
112 VERIFY( m1
.find("the stockholm syndrome") != m1
.end() );
113 VERIFY( m1
.find("a cereous night") != m1
.end() );
114 VERIFY( m1
.find("the way you are when") != m1
.end() );
115 VERIFY( m1
.find("a cereous night") != m1
.end() );
117 VERIFY( m1
.erase(it5
->first
) == 1 );
118 VERIFY( m1
.size() == 5 );
119 it5
= m1
.find("umbra/penumbra");
120 VERIFY( it5
== m1
.end() );
122 VERIFY( m1
.erase(it6
->first
) == 1 );
123 VERIFY( m1
.size() == 4 );
124 it6
= m1
.find("one line behind");
125 VERIFY( it6
== m1
.end() );
127 iterator it7
= m1
.begin();
133 VERIFY( m1
.erase(it8
->first
) == 1 );
134 VERIFY( m1
.size() == 3 );
135 VERIFY( ++it7
== it9
);
139 iterator it11
= it10
;
141 VERIFY( m1
.erase(it9
->first
) == 1 );
142 VERIFY( m1
.size() == 2 );
143 VERIFY( ++it10
== m1
.end() );
145 m1
.erase(m1
.begin());
146 VERIFY( m1
.size() == 1 );
147 VERIFY( m1
.begin() == it11
);
149 VERIFY( m1
.erase(m1
.begin()->first
) == 1 );
150 VERIFY( m1
.size() == 0 );
151 VERIFY( m1
.begin() == m1
.end() );