1// { dg-options "-std=gnu++0x" }
2
3// 2010-02-10  Paolo Carlini  <paolo.carlini@oracle.com>
4//
5// Copyright (C) 2010 Free Software Foundation, Inc.
6//
7// This file is part of the GNU ISO C++ Library.  This library is free
8// software; you can redistribute it and/or modify it under the
9// terms of the GNU General Public License as published by the
10// Free Software Foundation; either version 3, or (at your option)
11// any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License along
19// with this library; see the file COPYING3.  If not see
20// <http://www.gnu.org/licenses/>.
21
22#include <unordered_map>
23#include <string>
24#include <testsuite_hooks.h>
25
26void test01()
27{
28  bool test __attribute__((unused)) = true;
29
30  typedef std::unordered_map<std::string, int> Map;
31  typedef Map::iterator       iterator;
32  typedef Map::const_iterator const_iterator;
33  typedef Map::value_type     value_type;
34
35  Map m1;
36
37  m1.insert(value_type("because to why", 1));
38  m1.insert(value_type("the stockholm syndrome", 2));
39  m1.insert(value_type("a cereous night", 3));
40  m1.insert(value_type("eeilo", 4));
41  m1.insert(value_type("protean", 5));
42  m1.insert(value_type("the way you are when", 6));
43  m1.insert(value_type("tillsammans", 7));
44  m1.insert(value_type("umbra/penumbra", 8));
45  m1.insert(value_type("belonging (no longer mix)", 9));
46  m1.insert(value_type("one line behind", 10));
47  VERIFY( m1.size() == 10 );
48
49  VERIFY( m1.erase("eeilo") == 1 );
50  VERIFY( m1.size() == 9 );
51  iterator it1 = m1.find("eeilo");
52  VERIFY( it1 == m1.end() );
53
54  VERIFY( m1.erase("tillsammans") == 1 );
55  VERIFY( m1.size() == 8 );
56  iterator it2 = m1.find("tillsammans");
57  VERIFY( it2 == m1.end() );
58
59  // Must work (see DR 526)
60  iterator it3 = m1.find("belonging (no longer mix)");
61  VERIFY( it3 != m1.end() );
62  VERIFY( m1.erase(it3->first) == 1 );
63  VERIFY( m1.size() == 7 );
64  it3 = m1.find("belonging (no longer mix)");
65  VERIFY( it3 == m1.end() );
66
67  VERIFY( !m1.erase("abra") );
68  VERIFY( m1.size() == 7 );
69
70  VERIFY( !m1.erase("eeilo") );
71  VERIFY( m1.size() == 7 );
72
73  VERIFY( m1.erase("because to why") == 1 );
74  VERIFY( m1.size() == 6 );
75  iterator it4 = m1.find("because to why");
76  VERIFY( it4 == m1.end() );
77
78  iterator it5 = m1.find("umbra/penumbra");
79  iterator it6 = m1.find("one line behind");
80  VERIFY( it5 != m1.end() );
81  VERIFY( it6 != m1.end() );
82
83  VERIFY( m1.find("the stockholm syndrome") != m1.end() );
84  VERIFY( m1.find("a cereous night") != m1.end() );
85  VERIFY( m1.find("the way you are when") != m1.end() );
86  VERIFY( m1.find("a cereous night") != m1.end() );
87
88  VERIFY( m1.erase(it5->first) == 1 );
89  VERIFY( m1.size() == 5 );
90  it5 = m1.find("umbra/penumbra");
91  VERIFY( it5 == m1.end() );
92
93  VERIFY( m1.erase(it6->first) == 1 );
94  VERIFY( m1.size() == 4 );
95  it6 = m1.find("one line behind");
96  VERIFY( it6 == m1.end() );
97
98  iterator it7 = m1.begin();
99  iterator it8 = it7;
100  ++it8;
101  iterator it9 = it8;
102  ++it9;
103
104  VERIFY( m1.erase(it8->first) == 1 );
105  VERIFY( m1.size() == 3 );
106  VERIFY( ++it7 == it9 );
107
108  iterator it10 = it9;
109  ++it10;
110  iterator it11 = it10;
111
112  VERIFY( m1.erase(it9->first) == 1 );
113  VERIFY( m1.size() == 2 );
114  VERIFY( ++it10 == m1.end() );
115
116  VERIFY( m1.erase(m1.begin()) != m1.end() );
117  VERIFY( m1.size() == 1 );
118  VERIFY( m1.begin() == it11 );
119
120  VERIFY( m1.erase(m1.begin()->first) == 1 );
121  VERIFY( m1.size() == 0 );
122  VERIFY( m1.begin() == m1.end() );
123}
124
125int main()
126{
127  test01();
128  return 0;
129}
130