]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/tr1/6_containers/unordered_multimap/swap/2.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / tr1 / 6_containers / unordered_multimap / swap / 2.cc
1 // 2005-12-20 Paolo Carlini <pcarlini@suse.de>
2
3 // Copyright (C) 2005, 2009 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.6 unordered_multimap::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 bool test __attribute__((unused)) = true;
32 using namespace std::tr1;
33 using std::pair;
34 using std::equal_to;
35 using std::map;
36 using std::multimap;
37
38 typedef pair<const char, int> my_pair;
39 typedef __gnu_test::uneq_allocator<my_pair> my_alloc;
40 typedef unordered_multimap<char, int, hash<char>, equal_to<char>, my_alloc>
41 my_ummap;
42
43 const char title01[] = "Rivers of sand";
44 const char title02[] = "Concret PH";
45 const char title03[] = "Sonatas and Interludes for Prepared Piano";
46 const char title04[] = "never as tired as when i'm waking up";
47
48 const size_t N1 = sizeof(title01);
49 const size_t N2 = sizeof(title02);
50 const size_t N3 = sizeof(title03);
51 const size_t N4 = sizeof(title04);
52
53 typedef multimap<char, int> my_mmap;
54 my_mmap mmap01_ref;
55 for (size_t i = 0; i < N1; ++i)
56 mmap01_ref.insert(my_pair(title01[i], i));
57 my_mmap mmap02_ref;
58 for (size_t i = 0; i < N2; ++i)
59 mmap02_ref.insert(my_pair(title02[i], i));
60 my_mmap mmap03_ref;
61 for (size_t i = 0; i < N3; ++i)
62 mmap03_ref.insert(my_pair(title03[i], i));
63 my_mmap mmap04_ref;
64 for (size_t i = 0; i < N4; ++i)
65 mmap04_ref.insert(my_pair(title04[i], i));
66
67 typedef map<char, int> my_map;
68
69 my_ummap::size_type size01, size02;
70
71 my_alloc alloc01(1), alloc02(2);
72 int personality01, personality02;
73
74 my_ummap ummap01(10, hash<char>(), equal_to<char>(), alloc01);
75 size01 = ummap01.size();
76 personality01 = ummap01.get_allocator().get_personality();
77 my_ummap ummap02(10, hash<char>(), equal_to<char>(), alloc02);
78 size02 = ummap02.size();
79 personality02 = ummap02.get_allocator().get_personality();
80
81 ummap01.swap(ummap02);
82 VERIFY( ummap01.size() == size02 );
83 VERIFY( ummap01.empty() );
84 VERIFY( ummap02.size() == size01 );
85 VERIFY( ummap02.empty() );
86 VERIFY( ummap01.get_allocator().get_personality() == personality02 );
87 VERIFY( ummap02.get_allocator().get_personality() == personality01 );
88
89 my_ummap ummap03(10, hash<char>(), equal_to<char>(), alloc02);
90 size01 = ummap03.size();
91 personality01 = ummap03.get_allocator().get_personality();
92 my_ummap ummap04(mmap02_ref.begin(), mmap02_ref.end(), 10, hash<char>(),
93 equal_to<char>(), alloc01);
94 size02 = ummap04.size();
95 personality02 = ummap04.get_allocator().get_personality();
96
97 ummap03.swap(ummap04);
98 VERIFY( ummap03.size() == size02 );
99 VERIFY( my_map(ummap03.begin(), ummap03.end())
100 == my_map(mmap02_ref.begin(), mmap02_ref.end()) );
101 VERIFY( ummap04.size() == size01 );
102 VERIFY( ummap04.empty() );
103 VERIFY( ummap03.get_allocator().get_personality() == personality02 );
104 VERIFY( ummap04.get_allocator().get_personality() == personality01 );
105
106 my_ummap ummap05(mmap01_ref.begin(), mmap01_ref.end(), 10, hash<char>(),
107 equal_to<char>(), alloc01);
108 size01 = ummap05.size();
109 personality01 = ummap05.get_allocator().get_personality();
110 my_ummap ummap06(mmap02_ref.begin(), mmap02_ref.end(), 10, hash<char>(),
111 equal_to<char>(), alloc02);
112 size02 = ummap06.size();
113 personality02 = ummap06.get_allocator().get_personality();
114
115 ummap05.swap(ummap06);
116 VERIFY( ummap05.size() == size02 );
117 VERIFY( my_map(ummap05.begin(), ummap05.end())
118 == my_map(mmap02_ref.begin(), mmap02_ref.end()) );
119 VERIFY( ummap06.size() == size01 );
120 VERIFY( my_map(ummap06.begin(), ummap06.end())
121 == my_map(mmap01_ref.begin(), mmap01_ref.end()) );
122 VERIFY( ummap05.get_allocator().get_personality() == personality02 );
123 VERIFY( ummap06.get_allocator().get_personality() == personality01 );
124
125 my_ummap ummap07(mmap01_ref.begin(), mmap01_ref.end(), 10, hash<char>(),
126 equal_to<char>(), alloc02);
127 size01 = ummap07.size();
128 personality01 = ummap07.get_allocator().get_personality();
129 my_ummap ummap08(mmap03_ref.begin(), mmap03_ref.end(), 10, hash<char>(),
130 equal_to<char>(), alloc01);
131 size02 = ummap08.size();
132 personality02 = ummap08.get_allocator().get_personality();
133
134 ummap07.swap(ummap08);
135 VERIFY( ummap07.size() == size02 );
136 VERIFY( my_map(ummap07.begin(), ummap07.end())
137 == my_map(mmap03_ref.begin(), mmap03_ref.end()) );
138 VERIFY( ummap08.size() == size01 );
139 VERIFY( my_map(ummap08.begin(), ummap08.end())
140 == my_map(mmap01_ref.begin(), mmap01_ref.end()) );
141 VERIFY( ummap07.get_allocator().get_personality() == personality02 );
142 VERIFY( ummap08.get_allocator().get_personality() == personality01 );
143
144 my_ummap ummap09(mmap03_ref.begin(), mmap03_ref.end(), 10, hash<char>(),
145 equal_to<char>(), alloc01);
146 size01 = ummap09.size();
147 personality01 = ummap09.get_allocator().get_personality();
148 my_ummap ummap10(mmap04_ref.begin(), mmap04_ref.end(), 10, hash<char>(),
149 equal_to<char>(), alloc02);
150 size02 = ummap10.size();
151 personality02 = ummap10.get_allocator().get_personality();
152
153 ummap09.swap(ummap10);
154 VERIFY( ummap09.size() == size02 );
155 VERIFY( my_map(ummap09.begin(), ummap09.end())
156 == my_map(mmap04_ref.begin(), mmap04_ref.end()) );
157 VERIFY( ummap10.size() == size01 );
158 VERIFY( my_map(ummap10.begin(), ummap10.end())
159 == my_map(mmap03_ref.begin(), mmap03_ref.end()) );
160 VERIFY( ummap09.get_allocator().get_personality() == personality02 );
161 VERIFY( ummap10.get_allocator().get_personality() == personality01 );
162
163 my_ummap ummap11(mmap04_ref.begin(), mmap04_ref.end(), 10, hash<char>(),
164 equal_to<char>(), alloc02);
165 size01 = ummap11.size();
166 personality01 = ummap11.get_allocator().get_personality();
167 my_ummap ummap12(mmap01_ref.begin(), mmap01_ref.end(), 10, hash<char>(),
168 equal_to<char>(), alloc01);
169 size02 = ummap12.size();
170 personality02 = ummap12.get_allocator().get_personality();
171
172 ummap11.swap(ummap12);
173 VERIFY( ummap11.size() == size02 );
174 VERIFY( my_map(ummap11.begin(), ummap11.end())
175 == my_map(mmap01_ref.begin(), mmap01_ref.end()) );
176 VERIFY( ummap12.size() == size01 );
177 VERIFY( my_map(ummap12.begin(), ummap12.end())
178 == my_map(mmap04_ref.begin(), mmap04_ref.end()) );
179 VERIFY( ummap11.get_allocator().get_personality() == personality02 );
180 VERIFY( ummap12.get_allocator().get_personality() == personality01 );
181
182 my_ummap ummap13(mmap03_ref.begin(), mmap03_ref.end(), 10, hash<char>(),
183 equal_to<char>(), alloc01);
184 size01 = ummap13.size();
185 personality01 = ummap13.get_allocator().get_personality();
186 my_ummap ummap14(mmap03_ref.begin(), mmap03_ref.end(), 10, hash<char>(),
187 equal_to<char>(), alloc02);
188 size02 = ummap14.size();
189 personality02 = ummap14.get_allocator().get_personality();
190
191 ummap13.swap(ummap14);
192 VERIFY( ummap13.size() == size02 );
193 VERIFY( my_map(ummap13.begin(), ummap13.end())
194 == my_map(mmap03_ref.begin(), mmap03_ref.end()) );
195 VERIFY( ummap14.size() == size01 );
196 VERIFY( my_map(ummap14.begin(), ummap14.end())
197 == my_map(mmap03_ref.begin(), mmap03_ref.end()) );
198 VERIFY( ummap13.get_allocator().get_personality() == personality02 );
199 VERIFY( ummap14.get_allocator().get_personality() == personality01 );
200 }
201
202 int main()
203 {
204 test01();
205 return 0;
206 }