]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/tr1/6_containers/unordered_map/swap/2.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / tr1 / 6_containers / unordered_map / swap / 2.cc
1 // 2005-12-20 Paolo Carlini <pcarlini@suse.de>
2
3 // Copyright (C) 2005-2024 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 unordered_map::swap
21
22 #include <tr1/unordered_map>
23 #include <map>
24 #include <testsuite_hooks.h>
25 #include <testsuite_allocator.h>
26
27 // uneq_allocator, two different personalities.
28 void
29 test01()
30 {
31 using namespace std::tr1;
32 using std::pair;
33 using std::equal_to;
34 using std::map;
35
36 typedef pair<const char, int> my_pair;
37 typedef __gnu_test::uneq_allocator<my_pair> my_alloc;
38 typedef unordered_map<char, int, hash<char>, equal_to<char>, my_alloc>
39 my_umap;
40
41 const char title01[] = "Rivers of sand";
42 const char title02[] = "Concret PH";
43 const char title03[] = "Sonatas and Interludes for Prepared Piano";
44 const char title04[] = "never as tired as when i'm waking up";
45
46 const size_t N1 = sizeof(title01);
47 const size_t N2 = sizeof(title02);
48 const size_t N3 = sizeof(title03);
49 const size_t N4 = sizeof(title04);
50
51 typedef map<char, int> my_map;
52 my_map map01_ref;
53 for (size_t i = 0; i < N1; ++i)
54 map01_ref.insert(my_pair(title01[i], i));
55 my_map map02_ref;
56 for (size_t i = 0; i < N2; ++i)
57 map02_ref.insert(my_pair(title02[i], i));
58 my_map map03_ref;
59 for (size_t i = 0; i < N3; ++i)
60 map03_ref.insert(my_pair(title03[i], i));
61 my_map map04_ref;
62 for (size_t i = 0; i < N4; ++i)
63 map04_ref.insert(my_pair(title04[i], i));
64
65 my_umap::size_type size01, size02;
66
67 my_alloc alloc01(1), alloc02(2);
68 int personality01, personality02;
69
70 my_umap umap01(10, hash<char>(), equal_to<char>(), alloc01);
71 size01 = umap01.size();
72 personality01 = umap01.get_allocator().get_personality();
73 my_umap umap02(10, hash<char>(), equal_to<char>(), alloc02);
74 size02 = umap02.size();
75 personality02 = umap02.get_allocator().get_personality();
76
77 umap01.swap(umap02);
78 VERIFY( umap01.size() == size02 );
79 VERIFY( umap01.empty() );
80 VERIFY( umap02.size() == size01 );
81 VERIFY( umap02.empty() );
82 VERIFY( umap01.get_allocator().get_personality() == personality02 );
83 VERIFY( umap02.get_allocator().get_personality() == personality01 );
84
85 my_umap umap03(10, hash<char>(), equal_to<char>(), alloc02);
86 size01 = umap03.size();
87 personality01 = umap03.get_allocator().get_personality();
88 my_umap umap04(map02_ref.begin(), map02_ref.end(), 10, hash<char>(),
89 equal_to<char>(), alloc01);
90 size02 = umap04.size();
91 personality02 = umap04.get_allocator().get_personality();
92
93 umap03.swap(umap04);
94 VERIFY( umap03.size() == size02 );
95 VERIFY( my_map(umap03.begin(), umap03.end()) == map02_ref );
96 VERIFY( umap04.size() == size01 );
97 VERIFY( umap04.empty() );
98 VERIFY( umap03.get_allocator().get_personality() == personality02 );
99 VERIFY( umap04.get_allocator().get_personality() == personality01 );
100
101 my_umap umap05(map01_ref.begin(), map01_ref.end(), 10, hash<char>(),
102 equal_to<char>(), alloc01);
103 size01 = umap05.size();
104 personality01 = umap05.get_allocator().get_personality();
105 my_umap umap06(map02_ref.begin(), map02_ref.end(), 10, hash<char>(),
106 equal_to<char>(), alloc02);
107 size02 = umap06.size();
108 personality02 = umap06.get_allocator().get_personality();
109
110 umap05.swap(umap06);
111 VERIFY( umap05.size() == size02 );
112 VERIFY( my_map(umap05.begin(), umap05.end()) == map02_ref );
113 VERIFY( umap06.size() == size01 );
114 VERIFY( my_map(umap06.begin(), umap06.end()) == map01_ref );
115 VERIFY( umap05.get_allocator().get_personality() == personality02 );
116 VERIFY( umap06.get_allocator().get_personality() == personality01 );
117
118 my_umap umap07(map01_ref.begin(), map01_ref.end(), 10, hash<char>(),
119 equal_to<char>(), alloc02);
120 size01 = umap07.size();
121 personality01 = umap07.get_allocator().get_personality();
122 my_umap umap08(map03_ref.begin(), map03_ref.end(), 10, hash<char>(),
123 equal_to<char>(), alloc01);
124 size02 = umap08.size();
125 personality02 = umap08.get_allocator().get_personality();
126
127 umap07.swap(umap08);
128 VERIFY( umap07.size() == size02 );
129 VERIFY( my_map(umap07.begin(), umap07.end()) == map03_ref );
130 VERIFY( umap08.size() == size01 );
131 VERIFY( my_map(umap08.begin(), umap08.end()) == map01_ref );
132 VERIFY( umap07.get_allocator().get_personality() == personality02 );
133 VERIFY( umap08.get_allocator().get_personality() == personality01 );
134
135 my_umap umap09(map03_ref.begin(), map03_ref.end(), 10, hash<char>(),
136 equal_to<char>(), alloc01);
137 size01 = umap09.size();
138 personality01 = umap09.get_allocator().get_personality();
139 my_umap umap10(map04_ref.begin(), map04_ref.end(), 10, hash<char>(),
140 equal_to<char>(), alloc02);
141 size02 = umap10.size();
142 personality02 = umap10.get_allocator().get_personality();
143
144 umap09.swap(umap10);
145 VERIFY( umap09.size() == size02 );
146 VERIFY( my_map(umap09.begin(), umap09.end()) == map04_ref );
147 VERIFY( umap10.size() == size01 );
148 VERIFY( my_map(umap10.begin(), umap10.end()) == map03_ref );
149 VERIFY( umap09.get_allocator().get_personality() == personality02 );
150 VERIFY( umap10.get_allocator().get_personality() == personality01 );
151
152 my_umap umap11(map04_ref.begin(), map04_ref.end(), 10, hash<char>(),
153 equal_to<char>(), alloc02);
154 size01 = umap11.size();
155 personality01 = umap11.get_allocator().get_personality();
156 my_umap umap12(map01_ref.begin(), map01_ref.end(), 10, hash<char>(),
157 equal_to<char>(), alloc01);
158 size02 = umap12.size();
159 personality02 = umap12.get_allocator().get_personality();
160
161 umap11.swap(umap12);
162 VERIFY( umap11.size() == size02 );
163 VERIFY( my_map(umap11.begin(), umap11.end()) == map01_ref );
164 VERIFY( umap12.size() == size01 );
165 VERIFY( my_map(umap12.begin(), umap12.end()) == map04_ref );
166 VERIFY( umap11.get_allocator().get_personality() == personality02 );
167 VERIFY( umap12.get_allocator().get_personality() == personality01 );
168
169 my_umap umap13(map03_ref.begin(), map03_ref.end(), 10, hash<char>(),
170 equal_to<char>(), alloc01);
171 size01 = umap13.size();
172 personality01 = umap13.get_allocator().get_personality();
173 my_umap umap14(map03_ref.begin(), map03_ref.end(), 10, hash<char>(),
174 equal_to<char>(), alloc02);
175 size02 = umap14.size();
176 personality02 = umap14.get_allocator().get_personality();
177
178 umap13.swap(umap14);
179 VERIFY( umap13.size() == size02 );
180 VERIFY( my_map(umap13.begin(), umap13.end()) == map03_ref );
181 VERIFY( umap14.size() == size01 );
182 VERIFY( my_map(umap14.begin(), umap14.end()) == map03_ref );
183 VERIFY( umap13.get_allocator().get_personality() == personality02 );
184 VERIFY( umap14.get_allocator().get_personality() == personality01 );
185 }
186
187 int main()
188 {
189 test01();
190 return 0;
191 }