]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/backward/hash_map/25896.cc
hashtable.h (erase): Correctly handle erasing a reference to an entry in the hash...
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / backward / hash_map / 25896.cc
CommitLineData
9767a048
ILT
1// { dg-options "-Wno-deprecated" }
2
3// Copyright (C) 2009 Free Software Foundation, Inc.
4//
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 2, or (at your option)
9// any later version.
10//
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.
15//
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING. If not, write to the Free
18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19// USA.
20
21// This is a copy of tr1/6_containers/unordered_map/erase/1.cc, using
22// hash_map instead of unordered_map.
23
24#include <hash_map>
25#include <string>
26#include <testsuite_hooks.h>
27
28namespace __gnu_cxx
29{
30 using std::string;
31
32 inline size_t hash_string(const char* s)
33 {
34 unsigned long h;
35 for (h=0; *s; ++s) {
36 h = 5*h + *s;
37 }
38 return size_t(h);
39 }
40
41 template<class T> struct hash<T *>
42 {
43 size_t operator()(const T *const & s) const
44 { return reinterpret_cast<size_t>(s); }
45 };
46
47 template<> struct hash<string>
48 {
49 size_t operator()(const string &s) const { return hash_string(s.c_str()); }
50 };
51
52 template<> struct hash<const string>
53 {
54 size_t operator()(const string &s) const { return hash_string(s.c_str()); }
55 };
56}
57
58void test01()
59{
60 bool test __attribute__((unused)) = true;
61
62 typedef __gnu_cxx::hash_map<std::string, int> Map;
63 typedef Map::iterator iterator;
64 typedef Map::const_iterator const_iterator;
65 typedef Map::value_type value_type;
66
67 Map m1;
68
69 m1.insert(value_type("because to why", 1));
70 m1.insert(value_type("the stockholm syndrome", 2));
71 m1.insert(value_type("a cereous night", 3));
72 m1.insert(value_type("eeilo", 4));
73 m1.insert(value_type("protean", 5));
74 m1.insert(value_type("the way you are when", 6));
75 m1.insert(value_type("tillsammans", 7));
76 m1.insert(value_type("umbra/penumbra", 8));
77 m1.insert(value_type("belonging (no longer mix)", 9));
78 m1.insert(value_type("one line behind", 10));
79 VERIFY( m1.size() == 10 );
80
81 VERIFY( m1.erase("eeilo") == 1 );
82 VERIFY( m1.size() == 9 );
83 iterator it1 = m1.find("eeilo");
84 VERIFY( it1 == m1.end() );
85
86 VERIFY( m1.erase("tillsammans") == 1 );
87 VERIFY( m1.size() == 8 );
88 iterator it2 = m1.find("tillsammans");
89 VERIFY( it2 == m1.end() );
90
91 // Must work (see DR 526)
92 iterator it3 = m1.find("belonging (no longer mix)");
93 VERIFY( it3 != m1.end() );
94 VERIFY( m1.erase(it3->first) == 1 );
95 VERIFY( m1.size() == 7 );
96 it3 = m1.find("belonging (no longer mix)");
97 VERIFY( it3 == m1.end() );
98
99 VERIFY( !m1.erase("abra") );
100 VERIFY( m1.size() == 7 );
101
102 VERIFY( !m1.erase("eeilo") );
103 VERIFY( m1.size() == 7 );
104
105 VERIFY( m1.erase("because to why") == 1 );
106 VERIFY( m1.size() == 6 );
107 iterator it4 = m1.find("because to why");
108 VERIFY( it4 == m1.end() );
109
110 iterator it5 = m1.find("umbra/penumbra");
111 iterator it6 = m1.find("one line behind");
112 VERIFY( it5 != m1.end() );
113 VERIFY( it6 != m1.end() );
114
115 VERIFY( m1.find("the stockholm syndrome") != m1.end() );
116 VERIFY( m1.find("a cereous night") != m1.end() );
117 VERIFY( m1.find("the way you are when") != m1.end() );
118 VERIFY( m1.find("a cereous night") != m1.end() );
119
120 VERIFY( m1.erase(it5->first) == 1 );
121 VERIFY( m1.size() == 5 );
122 it5 = m1.find("umbra/penumbra");
123 VERIFY( it5 == m1.end() );
124
125 VERIFY( m1.erase(it6->first) == 1 );
126 VERIFY( m1.size() == 4 );
127 it6 = m1.find("one line behind");
128 VERIFY( it6 == m1.end() );
129
130 iterator it7 = m1.begin();
131 iterator it8 = it7;
132 ++it8;
133 iterator it9 = it8;
134 ++it9;
135
136 VERIFY( m1.erase(it8->first) == 1 );
137 VERIFY( m1.size() == 3 );
138 VERIFY( ++it7 == it9 );
139
140 iterator it10 = it9;
141 ++it10;
142 iterator it11 = it10;
143
144 VERIFY( m1.erase(it9->first) == 1 );
145 VERIFY( m1.size() == 2 );
146 VERIFY( ++it10 == m1.end() );
147
148 m1.erase(m1.begin());
149 VERIFY( m1.size() == 1 );
150 VERIFY( m1.begin() == it11 );
151
152 VERIFY( m1.erase(m1.begin()->first) == 1 );
153 VERIFY( m1.size() == 0 );
154 VERIFY( m1.begin() == m1.end() );
155}
156
157int main()
158{
159 test01();
160 return 0;
161}