| 1 | // 2019-04-30 Nina Dinka Ranns <[email protected]> |
| 2 | // Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 3 | // |
| 4 | // This file is part of the GNU ISO C++ Library. This library is free |
| 5 | // software; you can redistribute it and/or modify it under the |
| 6 | // terms of the GNU General Public License as published by the |
| 7 | // Free Software Foundation; either version 3, or (at your option) |
| 8 | // any later version. |
| 9 | |
| 10 | // This library is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | |
| 15 | // You should have received a copy of the GNU General Public License along |
| 16 | // with this library; see the file COPYING3. If not see |
| 17 | // <http://www.gnu.org/licenses/>. |
| 18 | |
| 19 | // { dg-do run { target c++11 } } |
| 20 | // COW strings don't support C++11 allocator propagation: |
| 21 | // { dg-require-effective-target cxx11_abi } |
| 22 | |
| 23 | #include <string> |
| 24 | #include <testsuite_hooks.h> |
| 25 | #include <testsuite_allocator.h> |
| 26 | #include <ext/throw_allocator.h> |
| 27 | |
| 28 | using C = char; |
| 29 | using traits = std::char_traits<C>; |
| 30 | |
| 31 | using __gnu_test::propagating_allocator; |
| 32 | |
| 33 | void test01() |
| 34 | { |
| 35 | typedef propagating_allocator<C, true> alloc_type; |
| 36 | typedef std::basic_string<C, traits, alloc_type> test_type; |
| 37 | |
| 38 | test_type v1("something",alloc_type(1)); |
| 39 | test_type v2("something",alloc_type(2)); |
| 40 | auto r1 = v1 + v2; |
| 41 | VERIFY(r1.get_allocator().get_personality() == 1); |
| 42 | |
| 43 | auto r2 = v1 + std::move(v2); |
| 44 | VERIFY(r2.get_allocator().get_personality() == 2); |
| 45 | |
| 46 | test_type v3("something", alloc_type(3)); |
| 47 | test_type v4("something", alloc_type(4)); |
| 48 | auto r3 = std::move(v3) + v4; |
| 49 | VERIFY(r3.get_allocator().get_personality() == 3); |
| 50 | |
| 51 | auto r4 = std::move(v1) +std::move(v4); |
| 52 | VERIFY(r4.get_allocator().get_personality() == 1); |
| 53 | |
| 54 | test_type v5("something", alloc_type(5)); |
| 55 | auto r5 = v5 + "str"; |
| 56 | VERIFY(r5.get_allocator().get_personality() == 5); |
| 57 | |
| 58 | auto r6 = v5 + 'c'; |
| 59 | VERIFY(r6.get_allocator().get_personality() == 5); |
| 60 | |
| 61 | auto r7 = std::move(v5) + "str"; |
| 62 | VERIFY(r7.get_allocator().get_personality() == 5); |
| 63 | |
| 64 | test_type v6("something", alloc_type(6)); |
| 65 | auto r8 = std::move(v6) + 'c'; |
| 66 | VERIFY(r8.get_allocator().get_personality() == 6); |
| 67 | |
| 68 | test_type v7("something", alloc_type(7)); |
| 69 | auto r9 = "str" + v7; |
| 70 | VERIFY(r9.get_allocator().get_personality() == 7); |
| 71 | |
| 72 | auto r10 = 'c' + v7; |
| 73 | VERIFY(r10.get_allocator().get_personality() == 7); |
| 74 | |
| 75 | auto r11 = "str" + std::move(v7); |
| 76 | VERIFY(r11.get_allocator().get_personality() == 7); |
| 77 | |
| 78 | test_type v8("something", alloc_type(8)); |
| 79 | auto r12 = 'c' + std::move(v8); |
| 80 | VERIFY(r12.get_allocator().get_personality() == 8); |
| 81 | } |
| 82 | void test02() |
| 83 | { |
| 84 | typedef propagating_allocator<C, false> alloc_type; |
| 85 | typedef std::basic_string<C, traits, alloc_type> test_type; |
| 86 | |
| 87 | test_type v1("something",alloc_type(1)); |
| 88 | test_type v2("something",alloc_type(2)); |
| 89 | auto r1 = v1 + v2; |
| 90 | VERIFY(r1.get_allocator().get_personality() != 1); |
| 91 | |
| 92 | auto r2 = v1 + std::move(v2); |
| 93 | VERIFY(r2.get_allocator().get_personality() == 2); |
| 94 | |
| 95 | test_type v3("something", alloc_type(3)); |
| 96 | test_type v4("something", alloc_type(4)); |
| 97 | auto r3 = std::move(v3) + v4; |
| 98 | VERIFY(r3.get_allocator().get_personality() == 3); |
| 99 | |
| 100 | auto r4 = std::move(v1) +std::move(v4); |
| 101 | VERIFY(r4.get_allocator().get_personality() == 1); |
| 102 | |
| 103 | test_type v5("something", alloc_type(5)); |
| 104 | auto r5 = v5 + "str"; |
| 105 | VERIFY(r5.get_allocator().get_personality() != 5); |
| 106 | |
| 107 | auto r6 = v5 + 'c'; |
| 108 | VERIFY(r6.get_allocator().get_personality() != 5); |
| 109 | |
| 110 | auto r7 = std::move(v5) + "str"; |
| 111 | VERIFY(r7.get_allocator().get_personality() == 5); |
| 112 | |
| 113 | test_type v6("something", alloc_type(6)); |
| 114 | auto r8 = std::move(v6) + 'c'; |
| 115 | VERIFY(r8.get_allocator().get_personality() == 6); |
| 116 | |
| 117 | test_type v7("something", alloc_type(7)); |
| 118 | auto r9 = "str" + v7; |
| 119 | VERIFY(r9.get_allocator().get_personality() != 7); |
| 120 | |
| 121 | auto r10 = 'c' + v7; |
| 122 | VERIFY(r10.get_allocator().get_personality() != 7); |
| 123 | |
| 124 | auto r11 = "str" + std::move(v7); |
| 125 | VERIFY(r11.get_allocator().get_personality() == 7); |
| 126 | |
| 127 | test_type v8("something", alloc_type(8)); |
| 128 | auto r12 = 'c' + std::move(v8); |
| 129 | VERIFY(r12.get_allocator().get_personality() == 8); |
| 130 | } |
| 131 | void test03() |
| 132 | { |
| 133 | typedef propagating_allocator<C, false> alloc_type; |
| 134 | typedef std::basic_string<C, traits, alloc_type> test_type; |
| 135 | |
| 136 | test_type v1("s",alloc_type(1)); |
| 137 | v1.resize(10); |
| 138 | v1.shrink_to_fit(); |
| 139 | test_type v2(10000,'x',alloc_type(2)); |
| 140 | v2.reserve(10010); |
| 141 | |
| 142 | auto r=std::move(v1)+std::move(v2); |
| 143 | VERIFY(r.get_allocator().get_personality() == 1); |
| 144 | } |
| 145 | int main() |
| 146 | { |
| 147 | test01(); |
| 148 | test02(); |
| 149 | test03(); |
| 150 | return 0; |
| 151 | } |