1 // Copyright (C) 2015-2018 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3.  If not see
16 // <http://www.gnu.org/licenses/>.
17 
18 // { dg-do run { target c++11 } }
19 // COW strings don't support C++11 allocators:
20 // { dg-require-effective-target cxx11-abi }
21 
22 #include <string>
23 #include <testsuite_hooks.h>
24 #include <testsuite_allocator.h>
25 
26 using C = wchar_t;
27 const C c = L'a';
28 using traits = std::char_traits<C>;
29 
30 using __gnu_test::propagating_allocator;
31 
test01()32 void test01()
33 {
34   typedef propagating_allocator<C, false> alloc_type;
35   typedef std::basic_string<C, traits, alloc_type> test_type;
36 
37   test_type v1(alloc_type(1));
38   v1.assign(1, c);
39   test_type v2(alloc_type(2));
40   v2.assign(1, c);
41   v2 = std::move(v1);
42   VERIFY(1 == v1.get_allocator().get_personality());
43   VERIFY(2 == v2.get_allocator().get_personality());
44 
45   test_type v3(alloc_type(3));
46   v3.assign(1, c);
47   test_type v4(alloc_type(4));
48   v4.assign(100, c);
49   v4 = std::move(v3);
50   VERIFY(3 == v3.get_allocator().get_personality());
51   VERIFY(4 == v4.get_allocator().get_personality());
52 
53   test_type v5(alloc_type(5));
54   v5.assign(100, c);
55   test_type v6(alloc_type(6));
56   v6.assign(1, c);
57   v6 = std::move(v5);
58   VERIFY(5 == v5.get_allocator().get_personality());
59   VERIFY(6 == v6.get_allocator().get_personality());
60 
61   test_type v7(alloc_type(7));
62   v7.assign(100, c);
63   test_type v8(alloc_type(8));
64   v8.assign(100, c);
65   v8 = std::move(v7);
66   VERIFY(7 == v7.get_allocator().get_personality());
67   VERIFY(8 == v8.get_allocator().get_personality());
68 }
69 
test02()70 void test02()
71 {
72   typedef propagating_allocator<C, true> alloc_type;
73   typedef std::basic_string<C, traits, alloc_type> test_type;
74 
75   test_type v1(alloc_type(1));
76   v1.assign(1, c);
77   test_type v2(alloc_type(2));
78   v2.assign(1, c);
79   v2 = std::move(v1);
80   VERIFY(0 == v1.get_allocator().get_personality());
81   VERIFY(1 == v2.get_allocator().get_personality());
82 
83   test_type v3(alloc_type(3));
84   v3.assign(1, c);
85   test_type v4(alloc_type(4));
86   v4.assign(100, c);
87   v4 = std::move(v3);
88   VERIFY(0 == v3.get_allocator().get_personality());
89   VERIFY(3 == v4.get_allocator().get_personality());
90 
91   test_type v5(alloc_type(5));
92   v5.assign(100, c);
93   test_type v6(alloc_type(6));
94   v6.assign(1, c);
95   v6 = std::move(v5);
96   VERIFY(0 == v5.get_allocator().get_personality());
97   VERIFY(5 == v6.get_allocator().get_personality());
98 
99   test_type v7(alloc_type(7));
100   v7.assign(100, c);
101   test_type v8(alloc_type(8));
102   v8.assign(100, c);
103   v8 = std::move(v7);
104   VERIFY(0 == v7.get_allocator().get_personality());
105   VERIFY(7 == v8.get_allocator().get_personality());
106 }
107 
test03()108 void test03()
109 {
110   typedef propagating_allocator<C, false> alloc_type;
111   typedef std::basic_string<C, traits, alloc_type> test_type;
112 
113   test_type v1(alloc_type(1));
114   v1.assign(1, c);
115   test_type v2(alloc_type(1));
116   v2.assign(1, c);
117   v2 = std::move(v1);
118   VERIFY(1 == v1.get_allocator().get_personality());
119   VERIFY(1 == v2.get_allocator().get_personality());
120 
121   test_type v3(alloc_type(3));
122   v3.assign(1, c);
123   test_type v4(alloc_type(3));
124   v4.assign(100, c);
125   v4 = std::move(v3);
126   VERIFY(3 == v3.get_allocator().get_personality());
127   VERIFY(3 == v4.get_allocator().get_personality());
128 
129   test_type v5(alloc_type(5));
130   v5.assign(100, c);
131   test_type v6(alloc_type(5));
132   v6.assign(1, c);
133   v6 = std::move(v5);
134   VERIFY(5 == v5.get_allocator().get_personality());
135   VERIFY(5 == v6.get_allocator().get_personality());
136 
137   test_type v7(alloc_type(7));
138   v7.assign(100, c);
139   test_type v8(alloc_type(7));
140   v8.assign(100, c);
141   v8 = std::move(v7);
142   VERIFY(7 == v7.get_allocator().get_personality());
143   VERIFY(7 == v8.get_allocator().get_personality());
144 }
145 
main()146 int main()
147 {
148   test01();
149   test02();
150   test03();
151   return 0;
152 }
153