| 1 | // 2007-02-22 Paolo Carlini <[email protected]> |
| 2 | // |
| 3 | // Copyright (C) 2007-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 | // 6.3.4.4 Class template unordered_map |
| 21 | |
| 22 | #include <tr1/unordered_map> |
| 23 | #include <string> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | // In the occasion of libstdc++/25896 |
| 27 | void test01() |
| 28 | { |
| 29 | typedef std::tr1::unordered_map<std::string, int> Map; |
| 30 | typedef Map::iterator iterator; |
| 31 | typedef Map::const_iterator const_iterator; |
| 32 | typedef Map::value_type value_type; |
| 33 | |
| 34 | Map m1; |
| 35 | |
| 36 | m1.insert(value_type("because to why", 1)); |
| 37 | m1.insert(value_type("the stockholm syndrome", 2)); |
| 38 | m1.insert(value_type("a cereous night", 3)); |
| 39 | m1.insert(value_type("eeilo", 4)); |
| 40 | m1.insert(value_type("protean", 5)); |
| 41 | m1.insert(value_type("the way you are when", 6)); |
| 42 | m1.insert(value_type("tillsammans", 7)); |
| 43 | m1.insert(value_type("umbra/penumbra", 8)); |
| 44 | m1.insert(value_type("belonging (no longer mix)", 9)); |
| 45 | m1.insert(value_type("one line behind", 10)); |
| 46 | VERIFY( m1.size() == 10 ); |
| 47 | |
| 48 | VERIFY( m1.erase("eeilo") == 1 ); |
| 49 | VERIFY( m1.size() == 9 ); |
| 50 | iterator it1 = m1.find("eeilo"); |
| 51 | VERIFY( it1 == m1.end() ); |
| 52 | |
| 53 | VERIFY( m1.erase("tillsammans") == 1 ); |
| 54 | VERIFY( m1.size() == 8 ); |
| 55 | iterator it2 = m1.find("tillsammans"); |
| 56 | VERIFY( it2 == m1.end() ); |
| 57 | |
| 58 | // Must work (see DR 526) |
| 59 | iterator it3 = m1.find("belonging (no longer mix)"); |
| 60 | VERIFY( it3 != m1.end() ); |
| 61 | VERIFY( m1.erase(it3->first) == 1 ); |
| 62 | VERIFY( m1.size() == 7 ); |
| 63 | it3 = m1.find("belonging (no longer mix)"); |
| 64 | VERIFY( it3 == m1.end() ); |
| 65 | |
| 66 | VERIFY( !m1.erase("abra") ); |
| 67 | VERIFY( m1.size() == 7 ); |
| 68 | |
| 69 | VERIFY( !m1.erase("eeilo") ); |
| 70 | VERIFY( m1.size() == 7 ); |
| 71 | |
| 72 | VERIFY( m1.erase("because to why") == 1 ); |
| 73 | VERIFY( m1.size() == 6 ); |
| 74 | iterator it4 = m1.find("because to why"); |
| 75 | VERIFY( it4 == m1.end() ); |
| 76 | |
| 77 | iterator it5 = m1.find("umbra/penumbra"); |
| 78 | iterator it6 = m1.find("one line behind"); |
| 79 | VERIFY( it5 != m1.end() ); |
| 80 | VERIFY( it6 != m1.end() ); |
| 81 | |
| 82 | VERIFY( m1.find("the stockholm syndrome") != m1.end() ); |
| 83 | VERIFY( m1.find("a cereous night") != m1.end() ); |
| 84 | VERIFY( m1.find("the way you are when") != m1.end() ); |
| 85 | VERIFY( m1.find("a cereous night") != m1.end() ); |
| 86 | |
| 87 | VERIFY( m1.erase(it5->first) == 1 ); |
| 88 | VERIFY( m1.size() == 5 ); |
| 89 | it5 = m1.find("umbra/penumbra"); |
| 90 | VERIFY( it5 == m1.end() ); |
| 91 | |
| 92 | VERIFY( m1.erase(it6->first) == 1 ); |
| 93 | VERIFY( m1.size() == 4 ); |
| 94 | it6 = m1.find("one line behind"); |
| 95 | VERIFY( it6 == m1.end() ); |
| 96 | |
| 97 | iterator it7 = m1.begin(); |
| 98 | iterator it8 = it7; |
| 99 | ++it8; |
| 100 | iterator it9 = it8; |
| 101 | ++it9; |
| 102 | |
| 103 | VERIFY( m1.erase(it8->first) == 1 ); |
| 104 | VERIFY( m1.size() == 3 ); |
| 105 | VERIFY( ++it7 == it9 ); |
| 106 | |
| 107 | iterator it10 = it9; |
| 108 | ++it10; |
| 109 | iterator it11 = it10; |
| 110 | |
| 111 | VERIFY( m1.erase(it9->first) == 1 ); |
| 112 | VERIFY( m1.size() == 2 ); |
| 113 | VERIFY( ++it10 == m1.end() ); |
| 114 | |
| 115 | VERIFY( m1.erase(m1.begin()) != m1.end() ); |
| 116 | VERIFY( m1.size() == 1 ); |
| 117 | VERIFY( m1.begin() == it11 ); |
| 118 | |
| 119 | VERIFY( m1.erase(m1.begin()->first) == 1 ); |
| 120 | VERIFY( m1.size() == 0 ); |
| 121 | VERIFY( m1.begin() == m1.end() ); |
| 122 | } |
| 123 | |
| 124 | int main() |
| 125 | { |
| 126 | test01(); |
| 127 | return 0; |
| 128 | } |