OSDN Git Service

e48e31149e3ef3cc769ccb133c1f96fe6d071183
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 23_containers / unordered_multiset / erase / 1.cc
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_set>
23 #include <string>
24 #include <testsuite_hooks.h>
25
26 namespace
27 {
28   std::size_t
29   get_nb_bucket_elems(const std::unordered_multiset<std::string>& 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
38 void test01()
39 {
40   bool test __attribute__((unused)) = true;
41   
42   typedef std::unordered_multiset<std::string> Mset;
43   typedef Mset::iterator       iterator;
44   typedef Mset::const_iterator const_iterator;
45
46   Mset ms1;
47   
48   ms1.insert("because to why");
49   ms1.insert("the stockholm syndrome");
50   ms1.insert("a cereous night");
51   ms1.insert("eeilo");
52   ms1.insert("protean");
53   ms1.insert("the way you are when");
54   ms1.insert("tillsammans");
55   ms1.insert("umbra/penumbra");
56   ms1.insert("belonging (no longer mix)");
57   ms1.insert("one line behind");
58   ms1.insert("because to why");
59   VERIFY( ms1.size() == 11 );
60   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
61
62   VERIFY( ms1.erase("eeilo") == 1 );
63   VERIFY( ms1.size() == 10 );
64   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
65   iterator it1 = ms1.find("eeilo");
66   VERIFY( it1 == ms1.end() );
67
68   VERIFY( ms1.erase("tillsammans") == 1 );
69   VERIFY( ms1.size() == 9 );
70   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
71   iterator it2 = ms1.find("tillsammans");
72   VERIFY( it2 == ms1.end() );
73
74   // Must work (see DR 526)
75   iterator it3 = ms1.find("belonging (no longer mix)");
76   VERIFY( it3 != ms1.end() );
77   VERIFY( ms1.erase(*it3) == 1 );
78   VERIFY( ms1.size() == 8 );
79   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
80   it3 = ms1.find("belonging (no longer mix)");
81   VERIFY( it3 == ms1.end() );
82
83   VERIFY( !ms1.erase("abra") );
84   VERIFY( ms1.size() == 8 );
85   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
86
87   VERIFY( !ms1.erase("eeilo") );
88   VERIFY( ms1.size() == 8 );
89
90   VERIFY( ms1.erase("because to why") == 2 );
91   VERIFY( ms1.size() == 6 );
92   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
93   iterator it4 = ms1.find("because to why");
94   VERIFY( it4 == ms1.end() );
95
96   iterator it5 = ms1.find("umbra/penumbra");
97   iterator it6 = ms1.find("one line behind");
98   VERIFY( it5 != ms1.end() );
99   VERIFY( it6 != ms1.end() );
100
101   VERIFY( ms1.find("the stockholm syndrome") != ms1.end() );
102   VERIFY( ms1.find("a cereous night") != ms1.end() );
103   VERIFY( ms1.find("the way you are when") != ms1.end() );
104   VERIFY( ms1.find("a cereous night") != ms1.end() );
105
106   VERIFY( ms1.erase(*it5) == 1 );
107   VERIFY( ms1.size() == 5 );
108   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
109   it5 = ms1.find("umbra/penumbra");
110   VERIFY( it5 == ms1.end() );
111
112   VERIFY( ms1.erase(*it6) == 1 );
113   VERIFY( ms1.size() == 4 );
114   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
115   it6 = ms1.find("one line behind");
116   VERIFY( it6 == ms1.end() );
117
118   iterator it7 = ms1.begin();
119   iterator it8 = it7;
120   ++it8;
121   iterator it9 = it8;
122   ++it9;
123
124   VERIFY( ms1.erase(*it8) == 1 );
125   VERIFY( ms1.size() == 3 );
126   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
127   VERIFY( ++it7 == it9 );
128
129   iterator it10 = it9;
130   ++it10;
131   iterator it11 = it10;
132
133   VERIFY( ms1.erase(*it9) == 1 );
134   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
135   VERIFY( ms1.size() == 2 );
136   VERIFY( ++it10 == ms1.end() );
137
138   VERIFY( ms1.erase(ms1.begin()) != ms1.end() );  
139   VERIFY( ms1.size() == 1 );
140   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
141   VERIFY( ms1.begin() == it11 );
142
143   VERIFY( ms1.erase(*ms1.begin()) == 1 );  
144   VERIFY( ms1.size() == 0 );
145   VERIFY( get_nb_bucket_elems(ms1) == ms1.size() );
146   VERIFY( ms1.begin() == ms1.end() );
147 }
148
149 int main()
150 {
151   test01();
152   return 0;
153 }