| 1 | // { dg-options "-Wno-deprecated" } |
| 2 | // |
| 3 | // Copyright (C) 2009-2025 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_set/erase/1.cc, using |
| 21 | // hash_set instead of unordered_set. |
| 22 | |
| 23 | #include <hash_set> |
| 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_set<std::string> Set; |
| 60 | typedef Set::iterator iterator; |
| 61 | typedef Set::const_iterator const_iterator; |
| 62 | |
| 63 | Set s1; |
| 64 | |
| 65 | s1.insert("because to why"); |
| 66 | s1.insert("the stockholm syndrome"); |
| 67 | s1.insert("a cereous night"); |
| 68 | s1.insert("eeilo"); |
| 69 | s1.insert("protean"); |
| 70 | s1.insert("the way you are when"); |
| 71 | s1.insert("tillsammans"); |
| 72 | s1.insert("umbra/penumbra"); |
| 73 | s1.insert("belonging (no longer mix)"); |
| 74 | s1.insert("one line behind"); |
| 75 | VERIFY( s1.size() == 10 ); |
| 76 | |
| 77 | VERIFY( s1.erase("eeilo") == 1 ); |
| 78 | VERIFY( s1.size() == 9 ); |
| 79 | iterator it1 = s1.find("eeilo"); |
| 80 | VERIFY( it1 == s1.end() ); |
| 81 | |
| 82 | VERIFY( s1.erase("tillsammans") == 1 ); |
| 83 | VERIFY( s1.size() == 8 ); |
| 84 | iterator it2 = s1.find("tillsammans"); |
| 85 | VERIFY( it2 == s1.end() ); |
| 86 | |
| 87 | // Must work (see DR 526) |
| 88 | iterator it3 = s1.find("belonging (no longer mix)"); |
| 89 | VERIFY( it3 != s1.end() ); |
| 90 | VERIFY( s1.erase(*it3) == 1 ); |
| 91 | VERIFY( s1.size() == 7 ); |
| 92 | it3 = s1.find("belonging (no longer mix)"); |
| 93 | VERIFY( it3 == s1.end() ); |
| 94 | |
| 95 | VERIFY( !s1.erase("abra") ); |
| 96 | VERIFY( s1.size() == 7 ); |
| 97 | |
| 98 | VERIFY( !s1.erase("eeilo") ); |
| 99 | VERIFY( s1.size() == 7 ); |
| 100 | |
| 101 | VERIFY( s1.erase("because to why") == 1 ); |
| 102 | VERIFY( s1.size() == 6 ); |
| 103 | iterator it4 = s1.find("because to why"); |
| 104 | VERIFY( it4 == s1.end() ); |
| 105 | |
| 106 | iterator it5 = s1.find("umbra/penumbra"); |
| 107 | iterator it6 = s1.find("one line behind"); |
| 108 | VERIFY( it5 != s1.end() ); |
| 109 | VERIFY( it6 != s1.end() ); |
| 110 | |
| 111 | VERIFY( s1.find("the stockholm syndrome") != s1.end() ); |
| 112 | VERIFY( s1.find("a cereous night") != s1.end() ); |
| 113 | VERIFY( s1.find("the way you are when") != s1.end() ); |
| 114 | VERIFY( s1.find("a cereous night") != s1.end() ); |
| 115 | |
| 116 | VERIFY( s1.erase(*it5) == 1 ); |
| 117 | VERIFY( s1.size() == 5 ); |
| 118 | it5 = s1.find("umbra/penumbra"); |
| 119 | VERIFY( it5 == s1.end() ); |
| 120 | |
| 121 | VERIFY( s1.erase(*it6) == 1 ); |
| 122 | VERIFY( s1.size() == 4 ); |
| 123 | it6 = s1.find("one line behind"); |
| 124 | VERIFY( it6 == s1.end() ); |
| 125 | |
| 126 | iterator it7 = s1.begin(); |
| 127 | iterator it8 = it7; |
| 128 | ++it8; |
| 129 | iterator it9 = it8; |
| 130 | ++it9; |
| 131 | |
| 132 | VERIFY( s1.erase(*it8) == 1 ); |
| 133 | VERIFY( s1.size() == 3 ); |
| 134 | VERIFY( ++it7 == it9 ); |
| 135 | |
| 136 | iterator it10 = it9; |
| 137 | ++it10; |
| 138 | iterator it11 = it10; |
| 139 | |
| 140 | VERIFY( s1.erase(*it9) == 1 ); |
| 141 | VERIFY( s1.size() == 2 ); |
| 142 | VERIFY( ++it10 == s1.end() ); |
| 143 | |
| 144 | s1.erase(s1.begin()); |
| 145 | VERIFY( s1.size() == 1 ); |
| 146 | VERIFY( s1.begin() == it11 ); |
| 147 | |
| 148 | VERIFY( s1.erase(*s1.begin()) == 1 ); |
| 149 | VERIFY( s1.size() == 0 ); |
| 150 | VERIFY( s1.begin() == s1.end() ); |
| 151 | } |
| 152 | |
| 153 | int main() |
| 154 | { |
| 155 | test01(); |
| 156 | return 0; |
| 157 | } |