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