]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/56166.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / 56166.cc
1 // Copyright (C) 2016-2023 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
20 // libstdc++/56166
21
22 #ifndef _GLIBCXX_USE_CXX11_ABI
23 # define _GLIBCXX_USE_CXX11_ABI 0
24 #endif
25 #include <string>
26 #include <new>
27
28 static int fail_after = -1;
29
30 template<typename T>
31 struct Allocator
32 {
33 using value_type = T;
34
35 // Need these typedefs because COW string doesn't use allocator_traits.
36 using pointer = T*;
37 using const_pointer = const T*;
38 using reference = T&;
39 using const_reference = const T&;
40 using difference_type = long;
41 using size_type = unsigned long;
42 template<typename U>
43 struct rebind {
44 using other = Allocator<U>;
45 };
46
47 Allocator() { }
48
49 template<typename U>
50 Allocator(const Allocator<U>&) { }
51
52 T* allocate(size_type n)
53 {
54 if (fail_after >= 0) {
55 if (fail_after-- == 0) {
56 throw std::bad_alloc();
57 }
58 }
59 return (T*)new char[n * sizeof(T)];
60 }
61
62 void deallocate(T* p, size_type)
63 {
64 delete[] (char*)p;
65 }
66 };
67
68 template<typename T, typename U>
69 bool operator==(const Allocator<T>&, const Allocator<U>&) { return true; }
70 template<typename T, typename U>
71 bool operator!=(const Allocator<T>&, const Allocator<U>&) { return false; }
72
73 using string = std::basic_string<char, std::char_traits<char>, Allocator<char>>;
74
75 string f()
76 {
77 string s1("xxxxxx");
78 string s2 = s1;
79 s1.clear();
80 return s2;
81 }
82
83 int main()
84 {
85 for (int i = 0; i < 10; i++) {
86 try {
87 fail_after = i;
88 f();
89 break;
90 } catch (const std::bad_alloc&) {
91 }
92 }
93 }