]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/allocator/char/copy_assign.cc
1 // Copyright (C) 2015-2023 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do run { target c++11 } }
19 // COW strings don't support C++11 allocator propagation:
20 // { dg-require-effective-target cxx11_abi }
23 #include <testsuite_hooks.h>
24 #include <testsuite_allocator.h>
25 #include <ext/throw_allocator.h>
29 using traits
= std::char_traits
<C
>;
31 using __gnu_test::propagating_allocator
;
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));
40 test_type
v2(alloc_type(2));
43 VERIFY(1 == v1
.get_allocator().get_personality());
44 VERIFY(2 == v2
.get_allocator().get_personality());
47 test_type
v3(alloc_type(3));
50 VERIFY(1 == v1
.get_allocator().get_personality());
51 VERIFY(3 == v3
.get_allocator().get_personality());
54 test_type
v4(alloc_type(4));
57 VERIFY(1 == v1
.get_allocator().get_personality());
58 VERIFY(4 == v4
.get_allocator().get_personality());
61 test_type
v5(alloc_type(5));
64 VERIFY(1 == v1
.get_allocator().get_personality());
65 VERIFY(5 == v5
.get_allocator().get_personality());
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));
75 test_type
v2(alloc_type(2));
78 VERIFY(1 == v1
.get_allocator().get_personality());
79 VERIFY(1 == v2
.get_allocator().get_personality());
82 test_type
v3(alloc_type(3));
85 VERIFY(1 == v1
.get_allocator().get_personality());
86 VERIFY(1 == v3
.get_allocator().get_personality());
89 test_type
v4(alloc_type(4));
92 VERIFY(1 == v1
.get_allocator().get_personality());
93 VERIFY(1 == v4
.get_allocator().get_personality());
96 test_type
v5(alloc_type(5));
99 VERIFY(1 == v1
.get_allocator().get_personality());
100 VERIFY(1 == v5
.get_allocator().get_personality());
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
= "a string that is longer than a small string";
112 const C
* s2
= "another string that is longer than a small string";
113 test_type
v1(s1
, a1
);
114 test_type
v2(s2
, a2
);
118 } catch (__gnu_cxx::forced_error
&) {
123 VERIFY( v1
.get_allocator() == a1
);
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() );
130 // No allocation when the contents fit in the small-string buffer:
133 VERIFY( v1
.get_allocator() == a2
);
139 typedef propagating_allocator
<C
, true> alloc_type
;
141 typedef std::basic_string
<C
, traits
, alloc_type
> test_type
;
143 test_type
v1("tralalala",alloc_type(1));
144 test_type
v2("content", alloc_type(2));
145 test_type
v3("content2", alloc_type(3));
149 VERIFY(2 == v1
.get_allocator().get_personality());
150 VERIFY(2 == v3
.get_allocator().get_personality());
157 typedef propagating_allocator
<C
, false> alloc_type
;
159 typedef std::basic_string
<C
, traits
, alloc_type
> test_type
;
161 test_type
v1("tralalala",alloc_type(1));
162 test_type
v2("content", alloc_type(2));
163 test_type
v3("content2", alloc_type(3));
167 VERIFY(1 == v1
.get_allocator().get_personality());
168 VERIFY(3 == v3
.get_allocator().get_personality());