1 // { dg-options "-Wno-deprecated" }
3 // Copyright (C) 2009-2013 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 bool test
__attribute__((unused
)) = true;
61 typedef __gnu_cxx::hash_map
<std::string
, int> Map
;
62 typedef Map::iterator iterator
;
63 typedef Map::const_iterator const_iterator
;
64 typedef Map::value_type value_type
;
68 m1
.insert(value_type("because to why", 1));
69 m1
.insert(value_type("the stockholm syndrome", 2));
70 m1
.insert(value_type("a cereous night", 3));
71 m1
.insert(value_type("eeilo", 4));
72 m1
.insert(value_type("protean", 5));
73 m1
.insert(value_type("the way you are when", 6));
74 m1
.insert(value_type("tillsammans", 7));
75 m1
.insert(value_type("umbra/penumbra", 8));
76 m1
.insert(value_type("belonging (no longer mix)", 9));
77 m1
.insert(value_type("one line behind", 10));
78 VERIFY( m1
.size() == 10 );
80 VERIFY( m1
.erase("eeilo") == 1 );
81 VERIFY( m1
.size() == 9 );
82 iterator it1
= m1
.find("eeilo");
83 VERIFY( it1
== m1
.end() );
85 VERIFY( m1
.erase("tillsammans") == 1 );
86 VERIFY( m1
.size() == 8 );
87 iterator it2
= m1
.find("tillsammans");
88 VERIFY( it2
== m1
.end() );
90 // Must work (see DR 526)
91 iterator it3
= m1
.find("belonging (no longer mix)");
92 VERIFY( it3
!= m1
.end() );
93 VERIFY( m1
.erase(it3
->first
) == 1 );
94 VERIFY( m1
.size() == 7 );
95 it3
= m1
.find("belonging (no longer mix)");
96 VERIFY( it3
== m1
.end() );
98 VERIFY( !m1
.erase("abra") );
99 VERIFY( m1
.size() == 7 );
101 VERIFY( !m1
.erase("eeilo") );
102 VERIFY( m1
.size() == 7 );
104 VERIFY( m1
.erase("because to why") == 1 );
105 VERIFY( m1
.size() == 6 );
106 iterator it4
= m1
.find("because to why");
107 VERIFY( it4
== m1
.end() );
109 iterator it5
= m1
.find("umbra/penumbra");
110 iterator it6
= m1
.find("one line behind");
111 VERIFY( it5
!= m1
.end() );
112 VERIFY( it6
!= m1
.end() );
114 VERIFY( m1
.find("the stockholm syndrome") != m1
.end() );
115 VERIFY( m1
.find("a cereous night") != m1
.end() );
116 VERIFY( m1
.find("the way you are when") != m1
.end() );
117 VERIFY( m1
.find("a cereous night") != m1
.end() );
119 VERIFY( m1
.erase(it5
->first
) == 1 );
120 VERIFY( m1
.size() == 5 );
121 it5
= m1
.find("umbra/penumbra");
122 VERIFY( it5
== m1
.end() );
124 VERIFY( m1
.erase(it6
->first
) == 1 );
125 VERIFY( m1
.size() == 4 );
126 it6
= m1
.find("one line behind");
127 VERIFY( it6
== m1
.end() );
129 iterator it7
= m1
.begin();
135 VERIFY( m1
.erase(it8
->first
) == 1 );
136 VERIFY( m1
.size() == 3 );
137 VERIFY( ++it7
== it9
);
141 iterator it11
= it10
;
143 VERIFY( m1
.erase(it9
->first
) == 1 );
144 VERIFY( m1
.size() == 2 );
145 VERIFY( ++it10
== m1
.end() );
147 m1
.erase(m1
.begin());
148 VERIFY( m1
.size() == 1 );
149 VERIFY( m1
.begin() == it11
);
151 VERIFY( m1
.erase(m1
.begin()->first
) == 1 );
152 VERIFY( m1
.size() == 0 );
153 VERIFY( m1
.begin() == m1
.end() );