1 // 2005-12-20  Paolo Carlini  <pcarlini@suse.de>
2 
3 // Copyright (C) 2005-2021 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.2.4.3 vector::swap
21 
22 #include <vector>
23 #include <testsuite_hooks.h>
24 #include <testsuite_allocator.h>
25 
26 // uneq_allocator, two different personalities.
27 void
test01()28 test01()
29 {
30   using namespace std;
31 
32   typedef __gnu_test::uneq_allocator<char> my_alloc;
33   typedef vector<char, my_alloc> my_vector;
34 
35   const char title01[] = "Rivers of sand";
36   const char title02[] = "Concret PH";
37   const char title03[] = "Sonatas and Interludes for Prepared Piano";
38   const char title04[] = "never as tired as when i'm waking up";
39 
40   const size_t N1 = sizeof(title01);
41   const size_t N2 = sizeof(title02);
42   const size_t N3 = sizeof(title03);
43   const size_t N4 = sizeof(title04);
44 
45   my_vector::size_type size01, size02;
46 
47   my_alloc alloc01(1), alloc02(2);
48   int personality01, personality02;
49 
50   my_vector vec01(alloc01);
51   size01 = vec01.size();
52   personality01 = vec01.get_allocator().get_personality();
53   my_vector vec02(alloc02);
54   size02 = vec02.size();
55   personality02 = vec02.get_allocator().get_personality();
56 
57   vec01.swap(vec02);
58   VERIFY( vec01.size() == size02 );
59   VERIFY( vec01.empty() );
60   VERIFY( vec02.size() == size01 );
61   VERIFY( vec02.empty() );
62   VERIFY( vec01.get_allocator().get_personality() == personality02 );
63   VERIFY( vec02.get_allocator().get_personality() == personality01 );
64 
65   my_vector vec03(alloc02);
66   size01 = vec03.size();
67   personality01 = vec03.get_allocator().get_personality();
68   my_vector vec04(title02, title02 + N2, alloc01);
69   size02 = vec04.size();
70   personality02 = vec04.get_allocator().get_personality();
71 
72   vec03.swap(vec04);
73   VERIFY( vec03.size() == size02 );
74   VERIFY( equal(vec03.begin(), vec03.end(), title02) );
75   VERIFY( vec04.size() == size01 );
76   VERIFY( vec04.empty() );
77   VERIFY( vec03.get_allocator().get_personality() == personality02 );
78   VERIFY( vec04.get_allocator().get_personality() == personality01 );
79 
80   my_vector vec05(title01, title01 + N1, alloc01);
81   size01 = vec05.size();
82   personality01 = vec05.get_allocator().get_personality();
83   my_vector vec06(title02, title02 + N2, alloc02);
84   size02 = vec06.size();
85   personality02 = vec06.get_allocator().get_personality();
86 
87   vec05.swap(vec06);
88   VERIFY( vec05.size() == size02 );
89   VERIFY( equal(vec05.begin(), vec05.end(), title02) );
90   VERIFY( vec06.size() == size01 );
91   VERIFY( equal(vec06.begin(), vec06.end(), title01) );
92   VERIFY( vec05.get_allocator().get_personality() == personality02 );
93   VERIFY( vec06.get_allocator().get_personality() == personality01 );
94 
95   my_vector vec07(title01, title01 + N1, alloc02);
96   size01 = vec07.size();
97   personality01 = vec07.get_allocator().get_personality();
98   my_vector vec08(title03, title03 + N3, alloc01);
99   size02 = vec08.size();
100   personality02 = vec08.get_allocator().get_personality();
101 
102   vec07.swap(vec08);
103   VERIFY( vec07.size() == size02 );
104   VERIFY( equal(vec07.begin(), vec07.end(), title03) );
105   VERIFY( vec08.size() == size01 );
106   VERIFY( equal(vec08.begin(), vec08.end(), title01) );
107   VERIFY( vec07.get_allocator().get_personality() == personality02 );
108   VERIFY( vec08.get_allocator().get_personality() == personality01 );
109 
110   my_vector vec09(title03, title03 + N3, alloc01);
111   size01 = vec09.size();
112   personality01 = vec09.get_allocator().get_personality();
113   my_vector vec10(title04, title04 + N4, alloc02);
114   size02 = vec10.size();
115   personality02 = vec10.get_allocator().get_personality();
116 
117   vec09.swap(vec10);
118   VERIFY( vec09.size() == size02 );
119   VERIFY( equal(vec09.begin(), vec09.end(), title04) );
120   VERIFY( vec10.size() == size01 );
121   VERIFY( equal(vec10.begin(), vec10.end(), title03) );
122   VERIFY( vec09.get_allocator().get_personality() == personality02 );
123   VERIFY( vec10.get_allocator().get_personality() == personality01 );
124 
125   my_vector vec11(title04, title04 + N4, alloc02);
126   size01 = vec11.size();
127   personality01 = vec11.get_allocator().get_personality();
128   my_vector vec12(title01, title01 + N1, alloc01);
129   size02 = vec12.size();
130   personality02 = vec12.get_allocator().get_personality();
131 
132   vec11.swap(vec12);
133   VERIFY( vec11.size() == size02 );
134   VERIFY( equal(vec11.begin(), vec11.end(), title01) );
135   VERIFY( vec12.size() == size01 );
136   VERIFY( equal(vec12.begin(), vec12.end(), title04) );
137   VERIFY( vec11.get_allocator().get_personality() == personality02 );
138   VERIFY( vec12.get_allocator().get_personality() == personality01 );
139 
140   my_vector vec13(title03, title03 + N3, alloc01);
141   size01 = vec13.size();
142   personality01 = vec13.get_allocator().get_personality();
143   my_vector vec14(title03, title03 + N3, alloc02);
144   size02 = vec14.size();
145   personality02 = vec14.get_allocator().get_personality();
146 
147   vec13.swap(vec14);
148   VERIFY( vec13.size() == size02 );
149   VERIFY( equal(vec13.begin(), vec13.end(), title03) );
150   VERIFY( vec14.size() == size01 );
151   VERIFY( equal(vec14.begin(), vec14.end(), title03) );
152   VERIFY( vec13.get_allocator().get_personality() == personality02 );
153   VERIFY( vec14.get_allocator().get_personality() == personality01 );
154 }
155 
main()156 int main()
157 {
158   test01();
159   return 0;
160 }
161