]> git.ipfire.org Git - thirdparty/gcc.git/blob - 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
1 // Copyright (C) 2015-2024 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 allocator propagation:
20 // { dg-require-effective-target cxx11_abi }
21
22 #include <string>
23 #include <testsuite_hooks.h>
24 #include <testsuite_allocator.h>
25 #include <ext/throw_allocator.h>
26
27 using C = wchar_t;
28 const C c = L'a';
29 using traits = std::char_traits<C>;
30
31 using __gnu_test::propagating_allocator;
32
33 void test01()
34 {
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
68 void test02()
69 {
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
103 void 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 );
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 );
134 }
135
136 void 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
154 void 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 }
170 int main()
171 {
172 test01();
173 test02();
174 test03();
175 test04();
176 test05();
177 return 0;
178 }