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