Reverting merge from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / backward / hash_set / 25896.cc
blob34f567fc60ffc60394385281bb9a45d17ac0a166
1 // { dg-options "-Wno-deprecated" }
2 //
3 // Copyright (C) 2009-2013 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.
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_set/erase/1.cc, using
21 // hash_set instead of unordered_set.
23 #include <hash_set>
24 #include <string>
25 #include <testsuite_hooks.h>
27 namespace __gnu_cxx
29 using std::string;
31 inline size_t hash_string(const char* s)
33 unsigned long h;
34 for (h=0; *s; ++s) {
35 h = 5*h + *s;
37 return size_t(h);
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()); }
57 void test01()
59 bool test __attribute__((unused)) = true;
61 typedef __gnu_cxx::hash_set<std::string> Set;
62 typedef Set::iterator iterator;
63 typedef Set::const_iterator const_iterator;
65 Set s1;
67 s1.insert("because to why");
68 s1.insert("the stockholm syndrome");
69 s1.insert("a cereous night");
70 s1.insert("eeilo");
71 s1.insert("protean");
72 s1.insert("the way you are when");
73 s1.insert("tillsammans");
74 s1.insert("umbra/penumbra");
75 s1.insert("belonging (no longer mix)");
76 s1.insert("one line behind");
77 VERIFY( s1.size() == 10 );
79 VERIFY( s1.erase("eeilo") == 1 );
80 VERIFY( s1.size() == 9 );
81 iterator it1 = s1.find("eeilo");
82 VERIFY( it1 == s1.end() );
84 VERIFY( s1.erase("tillsammans") == 1 );
85 VERIFY( s1.size() == 8 );
86 iterator it2 = s1.find("tillsammans");
87 VERIFY( it2 == s1.end() );
89 // Must work (see DR 526)
90 iterator it3 = s1.find("belonging (no longer mix)");
91 VERIFY( it3 != s1.end() );
92 VERIFY( s1.erase(*it3) == 1 );
93 VERIFY( s1.size() == 7 );
94 it3 = s1.find("belonging (no longer mix)");
95 VERIFY( it3 == s1.end() );
97 VERIFY( !s1.erase("abra") );
98 VERIFY( s1.size() == 7 );
100 VERIFY( !s1.erase("eeilo") );
101 VERIFY( s1.size() == 7 );
103 VERIFY( s1.erase("because to why") == 1 );
104 VERIFY( s1.size() == 6 );
105 iterator it4 = s1.find("because to why");
106 VERIFY( it4 == s1.end() );
108 iterator it5 = s1.find("umbra/penumbra");
109 iterator it6 = s1.find("one line behind");
110 VERIFY( it5 != s1.end() );
111 VERIFY( it6 != s1.end() );
113 VERIFY( s1.find("the stockholm syndrome") != s1.end() );
114 VERIFY( s1.find("a cereous night") != s1.end() );
115 VERIFY( s1.find("the way you are when") != s1.end() );
116 VERIFY( s1.find("a cereous night") != s1.end() );
118 VERIFY( s1.erase(*it5) == 1 );
119 VERIFY( s1.size() == 5 );
120 it5 = s1.find("umbra/penumbra");
121 VERIFY( it5 == s1.end() );
123 VERIFY( s1.erase(*it6) == 1 );
124 VERIFY( s1.size() == 4 );
125 it6 = s1.find("one line behind");
126 VERIFY( it6 == s1.end() );
128 iterator it7 = s1.begin();
129 iterator it8 = it7;
130 ++it8;
131 iterator it9 = it8;
132 ++it9;
134 VERIFY( s1.erase(*it8) == 1 );
135 VERIFY( s1.size() == 3 );
136 VERIFY( ++it7 == it9 );
138 iterator it10 = it9;
139 ++it10;
140 iterator it11 = it10;
142 VERIFY( s1.erase(*it9) == 1 );
143 VERIFY( s1.size() == 2 );
144 VERIFY( ++it10 == s1.end() );
146 s1.erase(s1.begin());
147 VERIFY( s1.size() == 1 );
148 VERIFY( s1.begin() == it11 );
150 VERIFY( s1.erase(*s1.begin()) == 1 );
151 VERIFY( s1.size() == 0 );
152 VERIFY( s1.begin() == s1.end() );
155 int main()
157 test01();
158 return 0;