1// { dg-options "-Wno-deprecated" } 2 3// Copyright (C) 2009-2015 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 27namespace __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 57void test01() 58{ 59 bool test __attribute__((unused)) = true; 60 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; 65 66 Map m1; 67 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 ); 79 80 VERIFY( m1.erase("eeilo") == 1 ); 81 VERIFY( m1.size() == 9 ); 82 iterator it1 = m1.find("eeilo"); 83 VERIFY( it1 == m1.end() ); 84 85 VERIFY( m1.erase("tillsammans") == 1 ); 86 VERIFY( m1.size() == 8 ); 87 iterator it2 = m1.find("tillsammans"); 88 VERIFY( it2 == m1.end() ); 89 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() ); 97 98 VERIFY( !m1.erase("abra") ); 99 VERIFY( m1.size() == 7 ); 100 101 VERIFY( !m1.erase("eeilo") ); 102 VERIFY( m1.size() == 7 ); 103 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() ); 108 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() ); 113 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() ); 118 119 VERIFY( m1.erase(it5->first) == 1 ); 120 VERIFY( m1.size() == 5 ); 121 it5 = m1.find("umbra/penumbra"); 122 VERIFY( it5 == m1.end() ); 123 124 VERIFY( m1.erase(it6->first) == 1 ); 125 VERIFY( m1.size() == 4 ); 126 it6 = m1.find("one line behind"); 127 VERIFY( it6 == m1.end() ); 128 129 iterator it7 = m1.begin(); 130 iterator it8 = it7; 131 ++it8; 132 iterator it9 = it8; 133 ++it9; 134 135 VERIFY( m1.erase(it8->first) == 1 ); 136 VERIFY( m1.size() == 3 ); 137 VERIFY( ++it7 == it9 ); 138 139 iterator it10 = it9; 140 ++it10; 141 iterator it11 = it10; 142 143 VERIFY( m1.erase(it9->first) == 1 ); 144 VERIFY( m1.size() == 2 ); 145 VERIFY( ++it10 == m1.end() ); 146 147 m1.erase(m1.begin()); 148 VERIFY( m1.size() == 1 ); 149 VERIFY( m1.begin() == it11 ); 150 151 VERIFY( m1.erase(m1.begin()->first) == 1 ); 152 VERIFY( m1.size() == 0 ); 153 VERIFY( m1.begin() == m1.end() ); 154} 155 156int main() 157{ 158 test01(); 159 return 0; 160} 161