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// 23.3.2 multimap::swap
21
22#include <map>
23#include <testsuite_hooks.h>
24#include <testsuite_allocator.h>
25
26// uneq_allocator, two different personalities.
27void
28test01()
29{
30  bool test __attribute__((unused)) = true;
31  using namespace std;
32
33  typedef pair<const char, int> my_pair;
34  typedef __gnu_test::uneq_allocator<my_pair> my_alloc;
35  typedef multimap<char, int, less<char>, my_alloc> my_mmap;
36
37  const char title01[] = "Rivers of sand";
38  const char title02[] = "Concret PH";
39  const char title03[] = "Sonatas and Interludes for Prepared Piano";
40  const char title04[] = "never as tired as when i'm waking up";
41
42  const size_t N1 = sizeof(title01);
43  const size_t N2 = sizeof(title02);
44  const size_t N3 = sizeof(title03);
45  const size_t N4 = sizeof(title04);
46
47  multimap<char, int> mmap01_ref;
48  for (size_t i = 0; i < N1; ++i)
49    mmap01_ref.insert(my_pair(title01[i], i));
50  multimap<char, int> mmap02_ref;
51  for (size_t i = 0; i < N2; ++i)
52    mmap02_ref.insert(my_pair(title02[i], i));
53  multimap<char, int> mmap03_ref;
54  for (size_t i = 0; i < N3; ++i)
55    mmap03_ref.insert(my_pair(title03[i], i));
56  multimap<char, int> mmap04_ref;
57  for (size_t i = 0; i < N4; ++i)
58    mmap04_ref.insert(my_pair(title04[i], i));
59
60  my_mmap::size_type size01, size02;
61
62  my_alloc alloc01(1), alloc02(2);
63  int personality01, personality02;
64
65  my_mmap mmap01(less<char>(), alloc01);
66  size01 = mmap01.size();
67  personality01 = mmap01.get_allocator().get_personality();
68  my_mmap mmap02(less<char>(), alloc02);
69  size02 = mmap02.size();
70  personality02 = mmap02.get_allocator().get_personality();
71
72  mmap01.swap(mmap02);
73  VERIFY( mmap01.size() == size02 );
74  VERIFY( mmap01.empty() );
75  VERIFY( mmap02.size() == size01 );
76  VERIFY( mmap02.empty() );
77  VERIFY( mmap01.get_allocator().get_personality() == personality02 );
78  VERIFY( mmap02.get_allocator().get_personality() == personality01 );
79
80  my_mmap mmap03(less<char>(), alloc02);
81  size01 = mmap03.size();
82  personality01 = mmap03.get_allocator().get_personality();
83  my_mmap mmap04(mmap02_ref.begin(), mmap02_ref.end(), less<char>(), alloc01);
84  size02 = mmap04.size();
85  personality02 = mmap04.get_allocator().get_personality();
86
87  mmap03.swap(mmap04);
88  VERIFY( mmap03.size() == size02 );
89  VERIFY( equal(mmap03.begin(), mmap03.end(), mmap02_ref.begin()) );
90  VERIFY( mmap04.size() == size01 );
91  VERIFY( mmap04.empty() );
92  VERIFY( mmap03.get_allocator().get_personality() == personality02 );
93  VERIFY( mmap04.get_allocator().get_personality() == personality01 );
94
95  my_mmap mmap05(mmap01_ref.begin(), mmap01_ref.end(), less<char>(), alloc01);
96  size01 = mmap05.size();
97  personality01 = mmap05.get_allocator().get_personality();
98  my_mmap mmap06(mmap02_ref.begin(), mmap02_ref.end(), less<char>(), alloc02);
99  size02 = mmap06.size();
100  personality02 = mmap06.get_allocator().get_personality();
101
102  mmap05.swap(mmap06);
103  VERIFY( mmap05.size() == size02 );
104  VERIFY( equal(mmap05.begin(), mmap05.end(), mmap02_ref.begin()) );
105  VERIFY( mmap06.size() == size01 );
106  VERIFY( equal(mmap06.begin(), mmap06.end(), mmap01_ref.begin()) );
107  VERIFY( mmap05.get_allocator().get_personality() == personality02 );
108  VERIFY( mmap06.get_allocator().get_personality() == personality01 );
109
110  my_mmap mmap07(mmap01_ref.begin(), mmap01_ref.end(), less<char>(), alloc02);
111  size01 = mmap07.size();
112  personality01 = mmap07.get_allocator().get_personality();
113  my_mmap mmap08(mmap03_ref.begin(), mmap03_ref.end(), less<char>(), alloc01);
114  size02 = mmap08.size();
115  personality02 = mmap08.get_allocator().get_personality();
116
117  mmap07.swap(mmap08);
118  VERIFY( mmap07.size() == size02 );
119  VERIFY( equal(mmap07.begin(), mmap07.end(), mmap03_ref.begin()) );
120  VERIFY( mmap08.size() == size01 );
121  VERIFY( equal(mmap08.begin(), mmap08.end(), mmap01_ref.begin()) );
122  VERIFY( mmap07.get_allocator().get_personality() == personality02 );
123  VERIFY( mmap08.get_allocator().get_personality() == personality01 );
124
125  my_mmap mmap09(mmap03_ref.begin(), mmap03_ref.end(), less<char>(), alloc01);
126  size01 = mmap09.size();
127  personality01 = mmap09.get_allocator().get_personality();
128  my_mmap mmap10(mmap04_ref.begin(), mmap04_ref.end(), less<char>(), alloc02);
129  size02 = mmap10.size();
130  personality02 = mmap10.get_allocator().get_personality();
131
132  mmap09.swap(mmap10);
133  VERIFY( mmap09.size() == size02 );
134  VERIFY( equal(mmap09.begin(), mmap09.end(), mmap04_ref.begin()) );
135  VERIFY( mmap10.size() == size01 );
136  VERIFY( equal(mmap10.begin(), mmap10.end(), mmap03_ref.begin()) );
137  VERIFY( mmap09.get_allocator().get_personality() == personality02 );
138  VERIFY( mmap10.get_allocator().get_personality() == personality01 );
139
140  my_mmap mmap11(mmap04_ref.begin(), mmap04_ref.end(), less<char>(), alloc02);
141  size01 = mmap11.size();
142  personality01 = mmap11.get_allocator().get_personality();
143  my_mmap mmap12(mmap01_ref.begin(), mmap01_ref.end(), less<char>(), alloc01);
144  size02 = mmap12.size();
145  personality02 = mmap12.get_allocator().get_personality();
146
147  mmap11.swap(mmap12);
148  VERIFY( mmap11.size() == size02 );
149  VERIFY( equal(mmap11.begin(), mmap11.end(), mmap01_ref.begin()) );
150  VERIFY( mmap12.size() == size01 );
151  VERIFY( equal(mmap12.begin(), mmap12.end(), mmap04_ref.begin()) );
152  VERIFY( mmap11.get_allocator().get_personality() == personality02 );
153  VERIFY( mmap12.get_allocator().get_personality() == personality01 );
154
155  my_mmap mmap13(mmap03_ref.begin(), mmap03_ref.end(), less<char>(), alloc01);
156  size01 = mmap13.size();
157  personality01 = mmap13.get_allocator().get_personality();
158  my_mmap mmap14(mmap03_ref.begin(), mmap03_ref.end(), less<char>(), alloc02);
159  size02 = mmap14.size();
160  personality02 = mmap14.get_allocator().get_personality();
161
162  mmap13.swap(mmap14);
163  VERIFY( mmap13.size() == size02 );
164  VERIFY( equal(mmap13.begin(), mmap13.end(), mmap03_ref.begin()) );
165  VERIFY( mmap14.size() == size01 );
166  VERIFY( equal(mmap14.begin(), mmap14.end(), mmap03_ref.begin()) );
167  VERIFY( mmap13.get_allocator().get_personality() == personality02 );
168  VERIFY( mmap14.get_allocator().get_personality() == personality01 );
169}
170
171int main()
172{
173  test01();
174  return 0;
175}
176