Add testcase of PR c++/92542, already fixed.
[official-gcc.git] / libstdc++-v3 / testsuite / 19_diagnostics / stdexcept.cc
bloba512c66e65ec86769da086e8ee7560d63b6c4728
1 // 2011-03-16 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2011-2020 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 #include <string>
21 #include <stdexcept>
22 #include <testsuite_hooks.h>
24 void test01()
26 using namespace std;
27 std::string s("error");
29 try
31 logic_error e1(s);
32 throw e1;
34 catch(const exception& e)
36 s = e.what();
39 try
41 domain_error e2(s);
42 throw e2;
44 catch(const exception& e)
46 s = e.what();
49 try
51 invalid_argument e3(s);
52 throw e3;
54 catch(const exception& e)
56 s = e.what();
59 try
61 length_error e4(s);
62 throw e4;
64 catch(const exception& e)
66 s = e.what();
69 try
71 out_of_range e5(s);
72 throw e5;
74 catch(const exception& e)
76 s = e.what();
79 try
81 runtime_error e6(s);
82 throw e6;
84 catch(const exception& e)
86 s = e.what();
89 try
91 range_error e7(s);
92 throw e7;
94 catch(const exception& e)
96 s = e.what();
99 try
101 overflow_error e8(s);
102 throw e8;
104 catch(const exception& e)
106 s = e.what();
111 underflow_error e9(s);
112 throw e9;
114 catch(const exception& e)
116 s = e.what();
120 template<typename _Tp>
121 struct extra_error : public _Tp
123 extra_error(const std::string& s) : _Tp(s) { }
126 void test02()
128 using namespace std;
129 std::string s("error");
133 extra_error<logic_error> e1(s);
134 throw e1;
136 catch(const exception& e)
138 s = e.what();
143 extra_error<domain_error> e2(s);
144 throw e2;
146 catch(const exception& e)
148 s = e.what();
153 extra_error<invalid_argument> e3(s);
154 throw e3;
156 catch(const exception& e)
158 s = e.what();
163 extra_error<length_error> e4(s);
164 throw e4;
166 catch(const exception& e)
168 s = e.what();
173 extra_error<out_of_range> e5(s);
174 throw e5;
176 catch(const exception& e)
178 s = e.what();
183 extra_error<runtime_error> e6(s);
184 throw e6;
186 catch(const exception& e)
188 s = e.what();
193 extra_error<range_error> e7(s);
194 throw e7;
196 catch(const exception& e)
198 s = e.what();
203 extra_error<overflow_error> e8(s);
204 throw e8;
206 catch(const exception& e)
208 s = e.what();
213 extra_error<underflow_error> e9(s);
214 throw e9;
216 catch(const exception& e)
218 s = e.what();
222 void test03()
224 std::logic_error le1("");
225 // Copy constructor:
226 std::logic_error le2(le1);
227 // Copy assignment operator:
228 le1 = le2;
229 #if __cplusplus >= 201103L
230 // Move constructor:
231 std::logic_error le3 = std::move(le1);
232 // Move assignment operator:
233 le1 = std::move(le3);
234 #endif
236 std::runtime_error re1("");
237 // Copy constructor:
238 std::runtime_error re2(re1);
239 // Copy assignment operator:
240 re1 = re2;
241 #if __cplusplus >= 201103L
242 // Move constructor:
243 std::runtime_error re3 = std::move(re1);
244 // Move assignment operator:
245 re1 = std::move(re3);
246 #endif
249 int main(void)
251 test01();
252 test02();
253 test03();
254 return 0;