]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/backward/hash_map/25896.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / backward / hash_map / 25896.cc
1 // { dg-options "-Wno-deprecated" }
2
3 // Copyright (C) 2009-2024 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 3, 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 COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // This is a copy of tr1/6_containers/unordered_map/erase/1.cc, using
21 // hash_map instead of unordered_map.
22
23 #include <hash_map>
24 #include <string>
25 #include <testsuite_hooks.h>
26
27 namespace __gnu_cxx
28 {
29 using std::string;
30
31 inline size_t hash_string(const char* s)
32 {
33 unsigned long h;
34 for (h=0; *s; ++s) {
35 h = 5*h + *s;
36 }
37 return size_t(h);
38 }
39
40 template<class T> struct hash<T *>
41 {
42 size_t operator()(const T *const & s) const
43 { return reinterpret_cast<size_t>(s); }
44 };
45
46 template<> struct hash<string>
47 {
48 size_t operator()(const string &s) const { return hash_string(s.c_str()); }
49 };
50
51 template<> struct hash<const string>
52 {
53 size_t operator()(const string &s) const { return hash_string(s.c_str()); }
54 };
55 }
56
57 void test01()
58 {
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;
63
64 Map m1;
65
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 );
77
78 VERIFY( m1.erase("eeilo") == 1 );
79 VERIFY( m1.size() == 9 );
80 iterator it1 = m1.find("eeilo");
81 VERIFY( it1 == m1.end() );
82
83 VERIFY( m1.erase("tillsammans") == 1 );
84 VERIFY( m1.size() == 8 );
85 iterator it2 = m1.find("tillsammans");
86 VERIFY( it2 == m1.end() );
87
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() );
95
96 VERIFY( !m1.erase("abra") );
97 VERIFY( m1.size() == 7 );
98
99 VERIFY( !m1.erase("eeilo") );
100 VERIFY( m1.size() == 7 );
101
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() );
106
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() );
111
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() );
116
117 VERIFY( m1.erase(it5->first) == 1 );
118 VERIFY( m1.size() == 5 );
119 it5 = m1.find("umbra/penumbra");
120 VERIFY( it5 == m1.end() );
121
122 VERIFY( m1.erase(it6->first) == 1 );
123 VERIFY( m1.size() == 4 );
124 it6 = m1.find("one line behind");
125 VERIFY( it6 == m1.end() );
126
127 iterator it7 = m1.begin();
128 iterator it8 = it7;
129 ++it8;
130 iterator it9 = it8;
131 ++it9;
132
133 VERIFY( m1.erase(it8->first) == 1 );
134 VERIFY( m1.size() == 3 );
135 VERIFY( ++it7 == it9 );
136
137 iterator it10 = it9;
138 ++it10;
139 iterator it11 = it10;
140
141 VERIFY( m1.erase(it9->first) == 1 );
142 VERIFY( m1.size() == 2 );
143 VERIFY( ++it10 == m1.end() );
144
145 m1.erase(m1.begin());
146 VERIFY( m1.size() == 1 );
147 VERIFY( m1.begin() == it11 );
148
149 VERIFY( m1.erase(m1.begin()->first) == 1 );
150 VERIFY( m1.size() == 0 );
151 VERIFY( m1.begin() == m1.end() );
152 }
153
154 int main()
155 {
156 test01();
157 return 0;
158 }