| 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 = wchar_t; |
| 27 |
const C c = L'a'; |
| 28 |
using traits = std::char_traits<C>; |
| 29 |
|
| 30 |
using __gnu_test::propagating_allocator; |
| 31 |
|
| 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 |
static_assert(std::is_move_assignable<test_type>::value, ""); |
| 38 |
static_assert(!std::is_nothrow_move_assignable<test_type>::value, ""); |
| 39 |
|
| 40 |
test_type v1(alloc_type(1)); |
| 41 |
v1.assign(1, c); |
| 42 |
test_type v2(alloc_type(2)); |
| 43 |
v2.assign(1, c); |
| 44 |
v2 = std::move(v1); |
| 45 |
VERIFY(1 == v1.get_allocator().get_personality()); |
| 46 |
VERIFY(2 == v2.get_allocator().get_personality()); |
| 47 |
|
| 48 |
test_type v3(alloc_type(3)); |
| 49 |
v3.assign(1, c); |
| 50 |
test_type v4(alloc_type(4)); |
| 51 |
v4.assign(100, c); |
| 52 |
v4 = std::move(v3); |
| 53 |
VERIFY(3 == v3.get_allocator().get_personality()); |
| 54 |
VERIFY(4 == v4.get_allocator().get_personality()); |
| 55 |
|
| 56 |
test_type v5(alloc_type(5)); |
| 57 |
v5.assign(100, c); |
| 58 |
test_type v6(alloc_type(6)); |
| 59 |
v6.assign(1, c); |
| 60 |
v6 = std::move(v5); |
| 61 |
VERIFY(5 == v5.get_allocator().get_personality()); |
| 62 |
VERIFY(6 == v6.get_allocator().get_personality()); |
| 63 |
|
| 64 |
test_type v7(alloc_type(7)); |
| 65 |
v7.assign(100, c); |
| 66 |
test_type v8(alloc_type(8)); |
| 67 |
v8.assign(100, c); |
| 68 |
v8 = std::move(v7); |
| 69 |
VERIFY(7 == v7.get_allocator().get_personality()); |
| 70 |
VERIFY(8 == v8.get_allocator().get_personality()); |
| 71 |
} |
| 72 |
|
| 73 |
void test02() |
| 74 |
{ |
| 75 |
typedef propagating_allocator<C, true> alloc_type; |
| 76 |
typedef std::basic_string<C, traits, alloc_type> test_type; |
| 77 |
|
| 78 |
test_type v1(alloc_type(1)); |
| 79 |
v1.assign(1, c); |
| 80 |
test_type v2(alloc_type(2)); |
| 81 |
v2.assign(1, c); |
| 82 |
v2 = std::move(v1); |
| 83 |
VERIFY(1 == v1.get_allocator().get_personality()); |
| 84 |
VERIFY(1 == v2.get_allocator().get_personality()); |
| 85 |
|
| 86 |
test_type v3(alloc_type(3)); |
| 87 |
v3.assign(1, c); |
| 88 |
test_type v4(alloc_type(4)); |
| 89 |
v4.assign(100, c); |
| 90 |
v4 = std::move(v3); |
| 91 |
VERIFY(3 == v3.get_allocator().get_personality()); |
| 92 |
VERIFY(3 == v4.get_allocator().get_personality()); |
| 93 |
|
| 94 |
test_type v5(alloc_type(5)); |
| 95 |
v5.assign(100, c); |
| 96 |
test_type v6(alloc_type(6)); |
| 97 |
v6.assign(1, c); |
| 98 |
v6 = std::move(v5); |
| 99 |
VERIFY(5 == v5.get_allocator().get_personality()); |
| 100 |
VERIFY(5 == v6.get_allocator().get_personality()); |
| 101 |
|
| 102 |
test_type v7(alloc_type(7)); |
| 103 |
v7.assign(100, c); |
| 104 |
test_type v8(alloc_type(8)); |
| 105 |
v8.assign(100, c); |
| 106 |
v8 = std::move(v7); |
| 107 |
VERIFY(7 == v7.get_allocator().get_personality()); |
| 108 |
VERIFY(7 == v8.get_allocator().get_personality()); |
| 109 |
} |
| 110 |
|
| 111 |
void test03() |
| 112 |
{ |
| 113 |
typedef propagating_allocator<C, false> alloc_type; |
| 114 |
typedef std::basic_string<C, traits, alloc_type> test_type; |
| 115 |
|
| 116 |
test_type v1(alloc_type(1)); |
| 117 |
v1.assign(1, c); |
| 118 |
test_type v2(alloc_type(1)); |
| 119 |
v2.assign(1, c); |
| 120 |
v2 = std::move(v1); |
| 121 |
VERIFY(1 == v1.get_allocator().get_personality()); |
| 122 |
VERIFY(1 == v2.get_allocator().get_personality()); |
| 123 |
|
| 124 |
test_type v3(alloc_type(3)); |
| 125 |
v3.assign(1, c); |
| 126 |
test_type v4(alloc_type(3)); |
| 127 |
v4.assign(100, c); |
| 128 |
v4 = std::move(v3); |
| 129 |
VERIFY(3 == v3.get_allocator().get_personality()); |
| 130 |
VERIFY(3 == v4.get_allocator().get_personality()); |
| 131 |
|
| 132 |
test_type v5(alloc_type(5)); |
| 133 |
v5.assign(100, c); |
| 134 |
test_type v6(alloc_type(5)); |
| 135 |
v6.assign(1, c); |
| 136 |
v6 = std::move(v5); |
| 137 |
VERIFY(5 == v5.get_allocator().get_personality()); |
| 138 |
VERIFY(5 == v6.get_allocator().get_personality()); |
| 139 |
|
| 140 |
test_type v7(alloc_type(7)); |
| 141 |
v7.assign(100, c); |
| 142 |
test_type v8(alloc_type(7)); |
| 143 |
v8.assign(100, c); |
| 144 |
v8 = std::move(v7); |
| 145 |
VERIFY(7 == v7.get_allocator().get_personality()); |
| 146 |
VERIFY(7 == v8.get_allocator().get_personality()); |
| 147 |
} |
| 148 |
|
| 149 |
int main() |
| 150 |
{ |
| 151 |
test01(); |
| 152 |
test02(); |
| 153 |
test03(); |
| 154 |
return 0; |
| 155 |
} |