]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/21_strings/basic_string/allocator/wchar_t/copy_assign.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / allocator / wchar_t / copy_assign.cc
CommitLineData
a945c346 1// Copyright (C) 2015-2024 Free Software Foundation, Inc.
5caff414
JW
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.
5e961dba 8
5caff414
JW
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.
5e961dba 13
5caff414
JW
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
52066eae 18// { dg-do run { target c++11 } }
5e961dba 19// COW strings don't support C++11 allocator propagation:
a04b73e1 20// { dg-require-effective-target cxx11_abi }
5caff414
JW
21
22#include <string>
23#include <testsuite_hooks.h>
24#include <testsuite_allocator.h>
a7d47f35 25#include <ext/throw_allocator.h>
5e961dba 26
589a30d1
JW
27using C = wchar_t;
28const C c = L'a';
5caff414
JW
29using traits = std::char_traits<C>;
30
31using __gnu_test::propagating_allocator;
32
33void test01()
34{
5caff414
JW
35 typedef propagating_allocator<C, false> alloc_type;
36 typedef std::basic_string<C, traits, alloc_type> test_type;
37 test_type v1(alloc_type(1));
38
39 v1.assign(1, c);
40 test_type v2(alloc_type(2));
41 v2.assign(1, c);
42 v2 = v1;
43 VERIFY(1 == v1.get_allocator().get_personality());
44 VERIFY(2 == v2.get_allocator().get_personality());
45
46 v1.assign(1, c);
47 test_type v3(alloc_type(3));
48 v3.assign(100, c);
49 v3 = v1;
50 VERIFY(1 == v1.get_allocator().get_personality());
51 VERIFY(3 == v3.get_allocator().get_personality());
52
53 v1.assign(100, c);
54 test_type v4(alloc_type(4));
55 v4.assign(1, c);
56 v4 = v1;
57 VERIFY(1 == v1.get_allocator().get_personality());
58 VERIFY(4 == v4.get_allocator().get_personality());
59
60 v1.assign(100, c);
61 test_type v5(alloc_type(5));
62 v5.assign(100, c);
63 v5 = v1;
64 VERIFY(1 == v1.get_allocator().get_personality());
65 VERIFY(5 == v5.get_allocator().get_personality());
66}
67
68void test02()
69{
5caff414
JW
70 typedef propagating_allocator<C, true> alloc_type;
71 typedef std::basic_string<C, traits, alloc_type> test_type;
72 test_type v1(alloc_type(1));
73
74 v1.assign(1, c);
75 test_type v2(alloc_type(2));
76 v2.assign(1, c);
77 v2 = v1;
78 VERIFY(1 == v1.get_allocator().get_personality());
79 VERIFY(1 == v2.get_allocator().get_personality());
80
81 v1.assign(1, c);
82 test_type v3(alloc_type(3));
83 v3.assign(100, c);
84 v3 = v1;
85 VERIFY(1 == v1.get_allocator().get_personality());
86 VERIFY(1 == v3.get_allocator().get_personality());
87
88 v1.assign(100, c);
89 test_type v4(alloc_type(4));
90 v4.assign(1, c);
91 v4 = v1;
92 VERIFY(1 == v1.get_allocator().get_personality());
93 VERIFY(1 == v4.get_allocator().get_personality());
94
95 v1.assign(100, c);
96 test_type v5(alloc_type(5));
97 v5.assign(100, c);
98 v5 = v1;
99 VERIFY(1 == v1.get_allocator().get_personality());
100 VERIFY(1 == v5.get_allocator().get_personality());
101}
102
a7d47f35
JW
103void test03()
104{
105 // PR libstdc++/79254
106 using throw_alloc = __gnu_cxx::throw_allocator_limit<C>;
107 typedef propagating_allocator<C, true, throw_alloc> alloc_type;
108 typedef std::basic_string<C, traits, alloc_type> test_type;
109 alloc_type a1(1), a2(2);
110 throw_alloc::set_limit(2); // Throw on third allocation (during assignment).
111 const C* s1 = L"a string that is longer than a small string";
112 const C* s2 = L"another string that is longer than a small string";
113 test_type v1(s1, a1);
114 test_type v2(s2, a2);
115 bool caught = false;
116 try {
117 v1 = v2;
118 } catch (__gnu_cxx::forced_error&) {
119 caught = true;
120 }
121 VERIFY( caught );
122 VERIFY( v1 == s1 );
123 VERIFY( v1.get_allocator() == a1 );
11d10beb
JW
124
125 throw_alloc::set_limit(1); // Allow one more allocation (and no more).
126 test_type v3(s1, a1);
127 // No allocation when allocators are equal and capacity is sufficient:
128 VERIFY( v1.capacity() >= v3.size() );
129 v1 = v3;
130 // No allocation when the contents fit in the small-string buffer:
131 v2 = L"sso";
132 v1 = v2;
133 VERIFY( v1.get_allocator() == a2 );
a7d47f35
JW
134}
135
db33efde
NDR
136void test04()
137{
138 // LWG2579
139 typedef propagating_allocator<C, true> alloc_type;
140
141 typedef std::basic_string<C, traits, alloc_type> test_type;
142
143 test_type v1(L"tralalala",alloc_type(1));
144 test_type v2(L"content", alloc_type(2));
145 test_type v3(L"content2", alloc_type(3));
146
147 v1.assign(v2);
148 v3 = v2;
149 VERIFY(2 == v1.get_allocator().get_personality());
150 VERIFY(2 == v3.get_allocator().get_personality());
151
152}
153
154void test05()
155{
156 // LWG2579
157 typedef propagating_allocator<C, false> alloc_type;
158
159 typedef std::basic_string<C, traits, alloc_type> test_type;
160
161 test_type v1(L"tralalala",alloc_type(1));
162 test_type v2(L"content", alloc_type(2));
163 test_type v3(L"content2", alloc_type(3));
164
165 v1.assign(v2);
166 v3 = v2;
167 VERIFY(1 == v1.get_allocator().get_personality());
168 VERIFY(3 == v3.get_allocator().get_personality());
169}
5caff414
JW
170int main()
171{
172 test01();
173 test02();
a7d47f35 174 test03();
db33efde
NDR
175 test04();
176 test05();
5caff414
JW
177 return 0;
178}