1// { dg-options "-std=gnu++11" }
2
3// 2010-02-10  Paolo Carlini  <paolo.carlini@oracle.com>
4//
5// Copyright (C) 2010-2015 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
26namespace
27{
28  std::size_t
29  get_nb_bucket_elems(const std::unordered_map<std::string, int>& us)
30  {
31    std::size_t nb = 0;
32    for (std::size_t b = 0; b != us.bucket_count(); ++b)
33      nb += us.bucket_size(b);
34    return nb;
35  }
36}
37
38void test01()
39{
40  bool test __attribute__((unused)) = true;
41
42  typedef std::unordered_map<std::string, int> Map;
43  typedef Map::iterator       iterator;
44  typedef Map::const_iterator const_iterator;
45  typedef Map::value_type     value_type;
46
47  Map m1;
48
49  m1.insert(value_type("because to why", 1));
50  m1.insert(value_type("the stockholm syndrome", 2));
51  m1.insert(value_type("a cereous night", 3));
52  m1.insert(value_type("eeilo", 4));
53  m1.insert(value_type("protean", 5));
54  m1.insert(value_type("the way you are when", 6));
55  m1.insert(value_type("tillsammans", 7));
56  m1.insert(value_type("umbra/penumbra", 8));
57  m1.insert(value_type("belonging (no longer mix)", 9));
58  m1.insert(value_type("one line behind", 10));
59  VERIFY( m1.size() == 10 );
60  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
61  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
62
63  VERIFY( m1.erase("eeilo") == 1 );
64  VERIFY( m1.size() == 9 );
65  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
66  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
67  iterator it1 = m1.find("eeilo");
68  VERIFY( it1 == m1.end() );
69
70  VERIFY( m1.erase("tillsammans") == 1 );
71  VERIFY( m1.size() == 8 );
72  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
73  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
74  iterator it2 = m1.find("tillsammans");
75  VERIFY( it2 == m1.end() );
76
77  // Must work (see DR 526)
78  iterator it3 = m1.find("belonging (no longer mix)");
79  VERIFY( it3 != m1.end() );
80  VERIFY( m1.erase(it3->first) == 1 );
81  VERIFY( m1.size() == 7 );
82  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
83  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
84  it3 = m1.find("belonging (no longer mix)");
85  VERIFY( it3 == m1.end() );
86
87  VERIFY( !m1.erase("abra") );
88  VERIFY( m1.size() == 7 );
89  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
90  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
91
92  VERIFY( !m1.erase("eeilo") );
93  VERIFY( m1.size() == 7 );
94
95  VERIFY( m1.erase("because to why") == 1 );
96  VERIFY( m1.size() == 6 );
97  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
98  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
99  iterator it4 = m1.find("because to why");
100  VERIFY( it4 == m1.end() );
101
102  iterator it5 = m1.find("umbra/penumbra");
103  iterator it6 = m1.find("one line behind");
104  VERIFY( it5 != m1.end() );
105  VERIFY( it6 != m1.end() );
106
107  VERIFY( m1.find("the stockholm syndrome") != m1.end() );
108  VERIFY( m1.find("a cereous night") != m1.end() );
109  VERIFY( m1.find("the way you are when") != m1.end() );
110  VERIFY( m1.find("a cereous night") != m1.end() );
111
112  VERIFY( m1.erase(it5->first) == 1 );
113  VERIFY( m1.size() == 5 );
114  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
115  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
116  it5 = m1.find("umbra/penumbra");
117  VERIFY( it5 == m1.end() );
118
119  VERIFY( m1.erase(it6->first) == 1 );
120  VERIFY( m1.size() == 4 );
121  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
122  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
123  it6 = m1.find("one line behind");
124  VERIFY( it6 == m1.end() );
125
126  iterator it7 = m1.begin();
127  iterator it8 = it7;
128  ++it8;
129  iterator it9 = it8;
130  ++it9;
131
132  VERIFY( m1.erase(it8->first) == 1 );
133  VERIFY( m1.size() == 3 );
134  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
135  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
136  VERIFY( ++it7 == it9 );
137
138  iterator it10 = it9;
139  ++it10;
140  iterator it11 = it10;
141
142  VERIFY( m1.erase(it9->first) == 1 );
143  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
144  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
145  VERIFY( m1.size() == 2 );
146  VERIFY( ++it10 == m1.end() );
147
148  VERIFY( m1.erase(m1.begin()) != m1.end() );
149  VERIFY( m1.size() == 1 );
150  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
151  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
152  VERIFY( m1.begin() == it11 );
153
154  VERIFY( m1.erase(m1.begin()->first) == 1 );
155  VERIFY( m1.size() == 0 );
156  VERIFY( get_nb_bucket_elems(m1) == m1.size() );
157  VERIFY( distance(m1.begin(), m1.end()) - m1.size() == 0 );
158  VERIFY( m1.begin() == m1.end() );
159}
160
161int main()
162{
163  test01();
164  return 0;
165}
166