]>
Commit | Line | Data |
---|---|---|
db016a4e | 1 | // -*- C++ -*- |
2 | // Exception testing utils for the C++ library testsuite. | |
3 | // | |
f1717362 | 4 | // Copyright (C) 2007-2016 Free Software Foundation, Inc. |
db016a4e | 5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
6bc9506f | 9 | // Free Software Foundation; either version 3, or (at your option) |
db016a4e | 10 | // any later version. |
11 | // | |
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | // | |
17 | // You should have received a copy of the GNU General Public License along | |
6bc9506f | 18 | // with this library; see the file COPYING3. If not see |
19 | // <http://www.gnu.org/licenses/>. | |
db016a4e | 20 | // |
db016a4e | 21 | |
32ce5c15 | 22 | #include <exception> |
db016a4e | 23 | #include <testsuite_hooks.h> |
24 | ||
9414f705 | 25 | #ifndef _TESTSUITE_API |
26 | #define _TESTSUITE_API 1 | |
db016a4e | 27 | |
28 | namespace __gnu_test | |
29 | { | |
30 | // Checks for virtual public derivation in exception classes. | |
31 | // See: | |
32 | // http://www.boost.org/more/error_handling.html | |
33 | struct bad_non_virtual : virtual public std::exception { }; | |
34 | ||
35 | template<typename Exception, bool DefaultCons> | |
36 | struct diamond_derivation_base; | |
37 | ||
38 | template<typename Exception> | |
39 | struct diamond_derivation_base<Exception, true> | |
40 | { | |
0b782a43 | 41 | struct diamond_derivation_error |
42 | : bad_non_virtual, Exception | |
db016a4e | 43 | { |
0b782a43 | 44 | diamond_derivation_error() |
45 | : bad_non_virtual(), Exception() { } | |
db016a4e | 46 | }; |
47 | }; | |
48 | ||
49 | template<typename Exception> | |
50 | struct diamond_derivation_base<Exception, false> | |
51 | { | |
0b782a43 | 52 | struct diamond_derivation_error |
53 | : bad_non_virtual, Exception | |
db016a4e | 54 | { |
55 | diamond_derivation_error() | |
56 | : bad_non_virtual(), Exception("construct diamond") { } | |
57 | }; | |
58 | }; | |
59 | ||
60 | template<typename Exception, bool DefaultCons> | |
0b782a43 | 61 | struct diamond_derivation |
62 | : diamond_derivation_base<Exception, DefaultCons> | |
db016a4e | 63 | { |
64 | typedef diamond_derivation_base<Exception, DefaultCons> base_type; | |
65 | typedef typename base_type::diamond_derivation_error error_type; | |
0b782a43 | 66 | |
67 | // NB: In the libstdc++-v3 testsuite, all the standard exception | |
68 | // classes (+ a couple of extensions) are checked: since they | |
69 | // all derive *non* virtually from std::exception, the expected | |
70 | // behavior is ambiguity. | |
db016a4e | 71 | static void test() |
72 | { | |
73 | bool test __attribute__((unused)) = true; | |
0b782a43 | 74 | try |
75 | { throw error_type(); } | |
76 | catch (std::exception const&) | |
db016a4e | 77 | { VERIFY( false ); } |
0b782a43 | 78 | catch (...) |
79 | { VERIFY( true ); } | |
db016a4e | 80 | } |
81 | }; | |
9414f705 | 82 | |
83 | // Testing type requirements for template arguments. | |
84 | struct NonDefaultConstructible | |
85 | { | |
86 | NonDefaultConstructible(int) { } | |
87 | NonDefaultConstructible(const NonDefaultConstructible&) { } | |
6e48a8d0 | 88 | |
0c8766b1 | 89 | #if __cplusplus >= 201103L |
6e48a8d0 | 90 | // For std::iota. |
91 | NonDefaultConstructible& | |
92 | operator++() | |
93 | { return *this; } | |
94 | #endif | |
9414f705 | 95 | }; |
96 | ||
97 | // See: 20.1.1 Template argument requirements. | |
98 | inline bool | |
99 | operator==(const NonDefaultConstructible&, const NonDefaultConstructible&) | |
100 | { return false; } | |
101 | ||
102 | inline bool | |
103 | operator<(const NonDefaultConstructible&, const NonDefaultConstructible&) | |
104 | { return false; } | |
105 | ||
95757e52 | 106 | // For 23 unordered_* requirements. |
107 | struct NonDefaultConstructible_hash | |
108 | { | |
6b5322d5 | 109 | std::size_t |
95757e52 | 110 | operator()(NonDefaultConstructible) const |
111 | { return 1; } | |
112 | }; | |
113 | ||
9414f705 | 114 | // For 26 numeric algorithms requirements, need addable, |
115 | // subtractable, multiplicable. | |
116 | inline NonDefaultConstructible | |
117 | operator+(const NonDefaultConstructible& lhs, | |
118 | const NonDefaultConstructible& rhs) | |
119 | { return NonDefaultConstructible(1); } | |
120 | ||
121 | inline NonDefaultConstructible | |
122 | operator-(const NonDefaultConstructible& lhs, | |
123 | const NonDefaultConstructible& rhs) | |
124 | { return NonDefaultConstructible(1); } | |
125 | ||
126 | inline NonDefaultConstructible | |
127 | operator*(const NonDefaultConstructible& lhs, | |
128 | const NonDefaultConstructible& rhs) | |
129 | { return NonDefaultConstructible(1); } | |
130 | ||
131 | // Like unary_function, but takes no argument. (ie, void). | |
132 | // Used for generator template parameter. | |
133 | template<typename _Result> | |
134 | struct void_function | |
135 | { | |
136 | typedef _Result result_type; | |
651e93b9 | 137 | |
9414f705 | 138 | result_type |
139 | operator()() const | |
651e93b9 | 140 | { return result_type(); } |
9414f705 | 141 | }; |
142 | ||
143 | template<> | |
144 | struct void_function<NonDefaultConstructible> | |
145 | { | |
146 | typedef NonDefaultConstructible result_type; | |
651e93b9 | 147 | |
9414f705 | 148 | result_type |
149 | operator()() const | |
150 | { return result_type(2); } | |
151 | }; | |
53af708b | 152 | |
153 | // For std::addressof, etc. | |
154 | struct OverloadedAddressAux { }; | |
155 | ||
156 | struct OverloadedAddress | |
157 | { | |
158 | OverloadedAddressAux | |
159 | operator&() const { return OverloadedAddressAux(); } | |
160 | }; | |
161 | ||
162 | inline bool | |
163 | operator<(const OverloadedAddress&, const OverloadedAddress&) | |
164 | { return false; } | |
165 | ||
166 | inline bool | |
167 | operator==(const OverloadedAddress&, const OverloadedAddress&) | |
168 | { return false; } | |
169 | ||
170 | struct OverloadedAddress_hash | |
171 | { | |
6b5322d5 | 172 | std::size_t |
53af708b | 173 | operator()(const OverloadedAddress&) const |
174 | { return 1; } | |
175 | }; | |
6b5322d5 | 176 | |
0c8766b1 | 177 | #if __cplusplus >= 201103L |
6b5322d5 | 178 | struct NonCopyConstructible |
179 | { | |
180 | NonCopyConstructible() : num(-1) { } | |
181 | ||
182 | NonCopyConstructible(NonCopyConstructible&& other) | |
183 | : num(other.num) | |
184 | { other.num = 0; } | |
185 | ||
186 | NonCopyConstructible(const NonCopyConstructible&) = delete; | |
187 | ||
188 | operator int() { return num; } | |
189 | ||
190 | private: | |
191 | int num; | |
192 | }; | |
193 | #endif | |
194 | ||
db016a4e | 195 | } |
95757e52 | 196 | |
db016a4e | 197 | #endif |