]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/18_support/uncaught_exceptions/uncaught_exceptions.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 18_support / uncaught_exceptions / uncaught_exceptions.cc
1 // Copyright (C) 2015-2021 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-options "-std=gnu++17" }
19 // { dg-do run { target c++17 } }
20
21 #include <cassert>
22 #include <exception>
23 #include <testsuite_hooks.h>
24
25 #ifndef __cpp_lib_uncaught_exceptions
26 # error "Feature-test macro for uncaught_exceptions missing"
27 #elif __cpp_lib_uncaught_exceptions != 201411
28 # error "Feature-test macro for uncaught_exceptions has wrong value"
29 #endif
30
31 struct UncaughtVerifier
32 {
33 int expected_count_ = 0;
34 UncaughtVerifier(int expected_count) : expected_count_(expected_count) {}
35 ~UncaughtVerifier()
36 {
37 VERIFY(std::uncaught_exceptions() == expected_count_);
38 }
39 };
40
41 struct Transaction
42 {
43 int initial_count_;
44 bool& result_;
45 Transaction(bool& result)
46 : initial_count_(std::uncaught_exceptions()),
47 result_(result) {}
48 ~Transaction()
49 {
50 if (std::uncaught_exceptions() != initial_count_) {
51 result_ = false;
52 }
53 }
54 };
55
56 void test01()
57 {
58 try {
59 UncaughtVerifier uv{0};
60 } catch (...) {
61 UncaughtVerifier uv{0};
62 }
63 }
64
65 void test02()
66 {
67 try {
68 UncaughtVerifier uv{1};
69 throw 0;
70 } catch (...) {
71 UncaughtVerifier uv{0};
72 }
73 }
74
75 void test03()
76 {
77 try {
78 struct Wrap
79 {
80 UncaughtVerifier uv_{1};
81 ~Wrap() {try {UncaughtVerifier uv2{2}; throw 0;} catch(...) {}}
82 };
83 Wrap w;
84 throw 0;
85 } catch (...) {
86 UncaughtVerifier uv{0};
87 }
88 }
89
90 void test04()
91 {
92 bool result = true;
93 try {
94 Transaction t{result};
95 } catch(...) {
96 }
97 VERIFY(result);
98 }
99
100 void test05()
101 {
102 bool result = true;
103 try {
104 Transaction t{result};
105 throw 0;
106 } catch(...) {
107 }
108 VERIFY(!result);
109 }
110
111 void test06()
112 {
113 bool result = true;
114 bool result2 = true;
115 try {
116 struct Wrap
117 {
118 bool& result_;
119 Wrap(bool& result) : result_(result) {}
120 ~Wrap()
121 {
122 Transaction t{result_};
123 }
124 };
125 Transaction t{result};
126 Wrap w{result2};
127 throw 0;
128 } catch(...) {
129 }
130 VERIFY(!result);
131 VERIFY(result2);
132 }
133
134 void test07()
135 {
136 bool result = true;
137 bool result2 = true;
138 try {
139 struct Wrap
140 {
141 bool& result_;
142 Wrap(bool& result) : result_(result) {}
143 ~Wrap()
144 {
145 try {
146 Transaction t{result_};
147 throw 0;
148 } catch(...) {}
149 }
150 };
151 Transaction t{result};
152 Wrap w{result2};
153 throw 0;
154 } catch(...) {
155 }
156 VERIFY(!result);
157 VERIFY(!result2);
158 }
159
160 int main()
161 {
162 test01();
163 test02();
164 test03();
165 test04();
166 test05();
167 test06();
168 test07();
169 }