]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_set/erase/1.cc
Update copyright years in libstdc++-v3/
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_set / 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-2014 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_set<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_set<std::string> Set;
43 typedef Set::iterator iterator;
44 typedef Set::const_iterator const_iterator;
45
46 Set s1;
47
48 s1.insert("because to why");
49 s1.insert("the stockholm syndrome");
50 s1.insert("a cereous night");
51 s1.insert("eeilo");
52 s1.insert("protean");
53 s1.insert("the way you are when");
54 s1.insert("tillsammans");
55 s1.insert("umbra/penumbra");
56 s1.insert("belonging (no longer mix)");
57 s1.insert("one line behind");
58 VERIFY( s1.size() == 10 );
59 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
60 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
61
62 VERIFY( s1.erase("eeilo") == 1 );
63 VERIFY( s1.size() == 9 );
64 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
65 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
66 iterator it1 = s1.find("eeilo");
67 VERIFY( it1 == s1.end() );
68
69 VERIFY( s1.erase("tillsammans") == 1 );
70 VERIFY( s1.size() == 8 );
71 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
72 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
73 iterator it2 = s1.find("tillsammans");
74 VERIFY( it2 == s1.end() );
75
76 // Must work (see DR 526)
77 iterator it3 = s1.find("belonging (no longer mix)");
78 VERIFY( it3 != s1.end() );
79 VERIFY( s1.erase(*it3) == 1 );
80 VERIFY( s1.size() == 7 );
81 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
82 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
83 it3 = s1.find("belonging (no longer mix)");
84 VERIFY( it3 == s1.end() );
85
86 VERIFY( !s1.erase("abra") );
87 VERIFY( s1.size() == 7 );
88 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
89 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
90
91 VERIFY( !s1.erase("eeilo") );
92 VERIFY( s1.size() == 7 );
93
94 VERIFY( s1.erase("because to why") == 1 );
95 VERIFY( s1.size() == 6 );
96 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
97 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
98 iterator it4 = s1.find("because to why");
99 VERIFY( it4 == s1.end() );
100
101 iterator it5 = s1.find("umbra/penumbra");
102 iterator it6 = s1.find("one line behind");
103 VERIFY( it5 != s1.end() );
104 VERIFY( it6 != s1.end() );
105
106 VERIFY( s1.find("the stockholm syndrome") != s1.end() );
107 VERIFY( s1.find("a cereous night") != s1.end() );
108 VERIFY( s1.find("the way you are when") != s1.end() );
109 VERIFY( s1.find("a cereous night") != s1.end() );
110
111 VERIFY( s1.erase(*it5) == 1 );
112 VERIFY( s1.size() == 5 );
113 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
114 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
115 it5 = s1.find("umbra/penumbra");
116 VERIFY( it5 == s1.end() );
117
118 VERIFY( s1.erase(*it6) == 1 );
119 VERIFY( s1.size() == 4 );
120 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
121 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
122 it6 = s1.find("one line behind");
123 VERIFY( it6 == s1.end() );
124
125 iterator it7 = s1.begin();
126 iterator it8 = it7;
127 ++it8;
128 iterator it9 = it8;
129 ++it9;
130
131 VERIFY( s1.erase(*it8) == 1 );
132 VERIFY( s1.size() == 3 );
133 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
134 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
135 VERIFY( ++it7 == it9 );
136
137 iterator it10 = it9;
138 ++it10;
139 iterator it11 = it10;
140
141 VERIFY( s1.erase(*it9) == 1 );
142 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
143 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
144 VERIFY( s1.size() == 2 );
145 VERIFY( ++it10 == s1.end() );
146
147 VERIFY( s1.erase(s1.begin()) != s1.end() );
148 VERIFY( s1.size() == 1 );
149 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
150 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
151 VERIFY( s1.begin() == it11 );
152
153 VERIFY( s1.erase(*s1.begin()) == 1 );
154 VERIFY( s1.size() == 0 );
155 VERIFY( get_nb_bucket_elems(s1) == s1.size() );
156 VERIFY( distance(s1.begin(), s1.end()) - s1.size() == 0 );
157 VERIFY( s1.begin() == s1.end() );
158 }
159
160 int main()
161 {
162 test01();
163 return 0;
164 }