]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/allocator/wchar_t/operator_plus.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / allocator / wchar_t / operator_plus.cc
1 // 2019-04-30 Nina Dinka Ranns <dinka.ranns@gmail.com>
2 // Copyright (C) 2019-2024 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 = wchar_t;
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(L"something",alloc_type(1));
39 test_type v2(L"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(L"something", alloc_type(3));
47 test_type v4(L"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(L"something", alloc_type(5));
55 auto r5 = v5 + L"str";
56 VERIFY(r5.get_allocator().get_personality() == 5);
57
58 auto r6 = v5 + L'c';
59 VERIFY(r6.get_allocator().get_personality() == 5);
60
61 auto r7 = std::move(v5) + L"str";
62 VERIFY(r7.get_allocator().get_personality() == 5);
63
64 test_type v6(L"something", alloc_type(6));
65 auto r8 = std::move(v6) + L'c';
66 VERIFY(r8.get_allocator().get_personality() == 6);
67
68 test_type v7(L"something", alloc_type(7));
69 auto r9 = L"str" + v7;
70 VERIFY(r9.get_allocator().get_personality() == 7);
71
72 auto r10 = L'c' + v7;
73 VERIFY(r10.get_allocator().get_personality() == 7);
74
75 auto r11 = L"str" + std::move(v7);
76 VERIFY(r11.get_allocator().get_personality() == 7);
77
78 test_type v8(L"something", alloc_type(8));
79 auto r12 = L'c' + std::move(v8);
80 VERIFY(r12.get_allocator().get_personality() == 8);
81 }
82
83 void test02()
84 {
85 typedef propagating_allocator<C, false> alloc_type;
86 typedef std::basic_string<C, traits, alloc_type> test_type;
87
88 test_type v1(L"something",alloc_type(1));
89 test_type v2(L"something",alloc_type(2));
90 auto r1 = v1 + v2;
91 VERIFY(r1.get_allocator().get_personality() != 1);
92
93 auto r2 = v1 + std::move(v2);
94 VERIFY(r2.get_allocator().get_personality() == 2);
95
96 test_type v3(L"something", alloc_type(3));
97 test_type v4(L"something", alloc_type(4));
98 auto r3 = std::move(v3) + v4;
99 VERIFY(r3.get_allocator().get_personality() == 3);
100
101 auto r4 = std::move(v1) +std::move(v4);
102 VERIFY(r4.get_allocator().get_personality() == 1);
103
104 test_type v5(L"something", alloc_type(5));
105 auto r5 = v5 + L"str";
106 VERIFY(r5.get_allocator().get_personality() != 5);
107
108 auto r6 = v5 + L'c';
109 VERIFY(r6.get_allocator().get_personality() != 5);
110
111 auto r7 = std::move(v5) + L"str";
112 VERIFY(r7.get_allocator().get_personality() == 5);
113
114 test_type v6(L"something", alloc_type(6));
115 auto r8 = std::move(v6) + L'c';
116 VERIFY(r8.get_allocator().get_personality() == 6);
117
118 test_type v7(L"something", alloc_type(7));
119 auto r9 = L"str" + v7;
120 VERIFY(r9.get_allocator().get_personality() != 7);
121
122 auto r10 = L'c' + v7;
123 VERIFY(r10.get_allocator().get_personality() != 7);
124
125 auto r11 = L"str" + std::move(v7);
126 VERIFY(r11.get_allocator().get_personality() == 7);
127
128 test_type v8(L"something", alloc_type(8));
129 auto r12 = L'c' + std::move(v8);
130 VERIFY(r12.get_allocator().get_personality() == 8);
131 }
132 void test03()
133 {
134 typedef propagating_allocator<C, false> alloc_type;
135 typedef std::basic_string<C, traits, alloc_type> test_type;
136
137 test_type v1(L"s",alloc_type(1));
138 v1.resize(10);
139 v1.shrink_to_fit();
140 test_type v2(10000,L'x',alloc_type(2));
141 v2.reserve(10010);
142
143 auto r=std::move(v1)+std::move(v2);
144 VERIFY(r.get_allocator().get_personality() == 1);
145 }
146 int main()
147 {
148 test01();
149 test02();
150 test03();
151 return 0;
152 }