| 1 |
// Copyright (C) 2015-2025 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 |
|
| 26 |
using C = char; |
| 27 |
const C c = 'a'; |
| 28 |
using traits = std::char_traits<C>; |
| 29 |
|
| 30 |
using __gnu_test::propagating_allocator; |
| 31 |
using __gnu_test::tracker_allocator_counter; |
| 32 |
using __gnu_test::tracker_allocator; |
| 33 |
|
| 34 |
void test01() |
| 35 |
{ |
| 36 |
typedef propagating_allocator<C, false> alloc_type; |
| 37 |
typedef std::basic_string<C, traits, alloc_type> test_type; |
| 38 |
|
| 39 |
static_assert(std::is_move_assignable<test_type>::value, ""); |
| 40 |
static_assert(!std::is_nothrow_move_assignable<test_type>::value, ""); |
| 41 |
|
| 42 |
test_type v1(alloc_type(1)); |
| 43 |
v1.assign(1, c); |
| 44 |
test_type v2(alloc_type(2)); |
| 45 |
v2.assign(1, c); |
| 46 |
v2 = std::move(v1); |
| 47 |
VERIFY(1 == v1.get_allocator().get_personality()); |
| 48 |
VERIFY(2 == v2.get_allocator().get_personality()); |
| 49 |
|
| 50 |
test_type v3(alloc_type(3)); |
| 51 |
v3.assign(1, c); |
| 52 |
test_type v4(alloc_type(4)); |
| 53 |
v4.assign(100, c); |
| 54 |
v4 = std::move(v3); |
| 55 |
VERIFY(3 == v3.get_allocator().get_personality()); |
| 56 |
VERIFY(4 == v4.get_allocator().get_personality()); |
| 57 |
|
| 58 |
test_type v5(alloc_type(5)); |
| 59 |
v5.assign(100, c); |
| 60 |
test_type v6(alloc_type(6)); |
| 61 |
v6.assign(1, c); |
| 62 |
v6 = std::move(v5); |
| 63 |
VERIFY(5 == v5.get_allocator().get_personality()); |
| 64 |
VERIFY(6 == v6.get_allocator().get_personality()); |
| 65 |
|
| 66 |
test_type v7(alloc_type(7)); |
| 67 |
v7.assign(100, c); |
| 68 |
test_type v8(alloc_type(8)); |
| 69 |
v8.assign(100, c); |
| 70 |
v8 = std::move(v7); |
| 71 |
VERIFY(7 == v7.get_allocator().get_personality()); |
| 72 |
VERIFY(8 == v8.get_allocator().get_personality()); |
| 73 |
} |
| 74 |
|
| 75 |
void test02() |
| 76 |
{ |
| 77 |
typedef propagating_allocator<C, true> alloc_type; |
| 78 |
typedef std::basic_string<C, traits, alloc_type> test_type; |
| 79 |
|
| 80 |
test_type v1(alloc_type(1)); |
| 81 |
v1.assign(1, c); |
| 82 |
test_type v2(alloc_type(2)); |
| 83 |
v2.assign(1, c); |
| 84 |
v2 = std::move(v1); |
| 85 |
VERIFY(1 == v1.get_allocator().get_personality()); |
| 86 |
VERIFY(1 == v2.get_allocator().get_personality()); |
| 87 |
|
| 88 |
test_type v3(alloc_type(3)); |
| 89 |
v3.assign(1, c); |
| 90 |
test_type v4(alloc_type(4)); |
| 91 |
v4.assign(100, c); |
| 92 |
v4 = std::move(v3); |
| 93 |
VERIFY(3 == v3.get_allocator().get_personality()); |
| 94 |
VERIFY(3 == v4.get_allocator().get_personality()); |
| 95 |
|
| 96 |
test_type v5(alloc_type(5)); |
| 97 |
v5.assign(100, c); |
| 98 |
test_type v6(alloc_type(6)); |
| 99 |
v6.assign(1, c); |
| 100 |
v6 = std::move(v5); |
| 101 |
VERIFY(5 == v5.get_allocator().get_personality()); |
| 102 |
VERIFY(5 == v6.get_allocator().get_personality()); |
| 103 |
|
| 104 |
test_type v7(alloc_type(7)); |
| 105 |
v7.assign(100, c); |
| 106 |
test_type v8(alloc_type(8)); |
| 107 |
v8.assign(100, c); |
| 108 |
v8 = std::move(v7); |
| 109 |
VERIFY(7 == v7.get_allocator().get_personality()); |
| 110 |
VERIFY(7 == v8.get_allocator().get_personality()); |
| 111 |
} |
| 112 |
|
| 113 |
void test03() |
| 114 |
{ |
| 115 |
typedef propagating_allocator<C, false> alloc_type; |
| 116 |
typedef std::basic_string<C, traits, alloc_type> test_type; |
| 117 |
|
| 118 |
test_type v1(alloc_type(1)); |
| 119 |
v1.assign(1, c); |
| 120 |
test_type v2(alloc_type(1)); |
| 121 |
v2.assign(1, c); |
| 122 |
v2 = std::move(v1); |
| 123 |
VERIFY(1 == v1.get_allocator().get_personality()); |
| 124 |
VERIFY(1 == v2.get_allocator().get_personality()); |
| 125 |
|
| 126 |
test_type v3(alloc_type(3)); |
| 127 |
v3.assign(1, c); |
| 128 |
test_type v4(alloc_type(3)); |
| 129 |
v4.assign(100, c); |
| 130 |
v4 = std::move(v3); |
| 131 |
VERIFY(3 == v3.get_allocator().get_personality()); |
| 132 |
VERIFY(3 == v4.get_allocator().get_personality()); |
| 133 |
|
| 134 |
test_type v5(alloc_type(5)); |
| 135 |
v5.assign(100, c); |
| 136 |
test_type v6(alloc_type(5)); |
| 137 |
v6.assign(1, c); |
| 138 |
v6 = std::move(v5); |
| 139 |
VERIFY(5 == v5.get_allocator().get_personality()); |
| 140 |
VERIFY(5 == v6.get_allocator().get_personality()); |
| 141 |
|
| 142 |
test_type v7(alloc_type(7)); |
| 143 |
v7.assign(100, c); |
| 144 |
test_type v8(alloc_type(7)); |
| 145 |
v8.assign(100, c); |
| 146 |
v8 = std::move(v7); |
| 147 |
VERIFY(7 == v7.get_allocator().get_personality()); |
| 148 |
VERIFY(7 == v8.get_allocator().get_personality()); |
| 149 |
} |
| 150 |
|
| 151 |
void test04() |
| 152 |
{ |
| 153 |
typedef propagating_allocator<C, true, tracker_allocator<C>> alloc_type; |
| 154 |
typedef std::basic_string<C, traits, alloc_type> test_type; |
| 155 |
|
| 156 |
{ |
| 157 |
tracker_allocator_counter::reset(); |
| 158 |
test_type v1(alloc_type(1)); |
| 159 |
v1 = "abcdefghijklmnopqr10"; |
| 160 |
auto ref_alloc_count = tracker_allocator_counter::get_allocation_count(); |
| 161 |
|
| 162 |
test_type v2(alloc_type(2)); |
| 163 |
v2 = "abcdefghijklmnopqr20"; |
| 164 |
v2 = std::move(v1); |
| 165 |
VERIFY(1 == v1.get_allocator().get_personality()); |
| 166 |
VERIFY(1 == v2.get_allocator().get_personality()); |
| 167 |
|
| 168 |
VERIFY( tracker_allocator_counter::get_allocation_count() == 2 * ref_alloc_count ); |
| 169 |
VERIFY( tracker_allocator_counter::get_deallocation_count() == ref_alloc_count ); |
| 170 |
|
| 171 |
v1 = "abcdefghijklmnopqr11"; |
| 172 |
|
| 173 |
VERIFY( tracker_allocator_counter::get_allocation_count() == 3 * ref_alloc_count ); |
| 174 |
} |
| 175 |
|
| 176 |
{ |
| 177 |
tracker_allocator_counter::reset(); |
| 178 |
test_type v1(alloc_type(1)); |
| 179 |
v1 = "abcdefghijklmnopqr10"; |
| 180 |
auto ref_alloc_count = tracker_allocator_counter::get_allocation_count(); |
| 181 |
|
| 182 |
test_type v2(alloc_type(1)); |
| 183 |
v2 = "abcdefghijklmnopqr20"; |
| 184 |
v2 = std::move(v1); |
| 185 |
VERIFY(1 == v1.get_allocator().get_personality()); |
| 186 |
VERIFY(1 == v2.get_allocator().get_personality()); |
| 187 |
|
| 188 |
VERIFY( tracker_allocator_counter::get_allocation_count() == 2 * ref_alloc_count ); |
| 189 |
VERIFY( tracker_allocator_counter::get_deallocation_count() == 0 ); |
| 190 |
|
| 191 |
v1 = "abcdefghijklmnopqr11"; |
| 192 |
|
| 193 |
VERIFY( tracker_allocator_counter::get_allocation_count() == 2 * ref_alloc_count ); |
| 194 |
} |
| 195 |
|
| 196 |
VERIFY( tracker_allocator_counter::get_allocation_count() == |
| 197 |
tracker_allocator_counter::get_deallocation_count() ); |
| 198 |
} |
| 199 |
|
| 200 |
int main() |
| 201 |
{ |
| 202 |
test01(); |
| 203 |
test02(); |
| 204 |
test03(); |
| 205 |
test04(); |
| 206 |
return 0; |
| 207 |
} |