| 1 |
// -*- C++ -*- |
| 2 |
// Exception testing utils for the C++ library testsuite. |
| 3 |
// |
| 4 |
// Copyright (C) 2007-2025 Free Software Foundation, Inc. |
| 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 |
| 9 |
// Free Software Foundation; either version 3, or (at your option) |
| 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 |
| 18 |
// with this library; see the file COPYING3. If not see |
| 19 |
// <http://www.gnu.org/licenses/>. |
| 20 |
// |
| 21 |
|
| 22 |
#include <exception> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
|
| 25 |
#ifndef _TESTSUITE_API |
| 26 |
#define _TESTSUITE_API 1 |
| 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 |
{ |
| 41 |
struct diamond_derivation_error |
| 42 |
: bad_non_virtual, Exception |
| 43 |
{ |
| 44 |
diamond_derivation_error() |
| 45 |
: bad_non_virtual(), Exception() { } |
| 46 |
}; |
| 47 |
}; |
| 48 |
|
| 49 |
template<typename Exception> |
| 50 |
struct diamond_derivation_base<Exception, false> |
| 51 |
{ |
| 52 |
struct diamond_derivation_error |
| 53 |
: bad_non_virtual, Exception |
| 54 |
{ |
| 55 |
diamond_derivation_error() |
| 56 |
: bad_non_virtual(), Exception("construct diamond") { } |
| 57 |
}; |
| 58 |
}; |
| 59 |
|
| 60 |
template<typename Exception, bool DefaultCons> |
| 61 |
struct diamond_derivation |
| 62 |
: diamond_derivation_base<Exception, DefaultCons> |
| 63 |
{ |
| 64 |
typedef diamond_derivation_base<Exception, DefaultCons> base_type; |
| 65 |
typedef typename base_type::diamond_derivation_error error_type; |
| 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. |
| 71 |
static void test() |
| 72 |
{ |
| 73 |
try |
| 74 |
{ throw error_type(); } |
| 75 |
catch (std::exception const&) |
| 76 |
{ VERIFY( false ); } |
| 77 |
catch (...) |
| 78 |
{ VERIFY( true ); } |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
// Testing type requirements for template arguments. |
| 83 |
struct NonDefaultConstructible |
| 84 |
{ |
| 85 |
NonDefaultConstructible(int) { } |
| 86 |
NonDefaultConstructible(const NonDefaultConstructible&) { } |
| 87 |
|
| 88 |
#if __cplusplus >= 201103L |
| 89 |
NonDefaultConstructible& |
| 90 |
operator=(const NonDefaultConstructible&) = default; |
| 91 |
|
| 92 |
// For std::iota. |
| 93 |
NonDefaultConstructible& |
| 94 |
operator++() |
| 95 |
{ return *this; } |
| 96 |
#endif |
| 97 |
}; |
| 98 |
|
| 99 |
// See: 20.1.1 Template argument requirements. |
| 100 |
inline bool |
| 101 |
operator==(const NonDefaultConstructible&, const NonDefaultConstructible&) |
| 102 |
{ return false; } |
| 103 |
|
| 104 |
inline bool |
| 105 |
operator<(const NonDefaultConstructible&, const NonDefaultConstructible&) |
| 106 |
{ return false; } |
| 107 |
|
| 108 |
// For 23 unordered_* requirements. |
| 109 |
struct NonDefaultConstructible_hash |
| 110 |
{ |
| 111 |
std::size_t |
| 112 |
operator()(NonDefaultConstructible) const |
| 113 |
{ return 1; } |
| 114 |
}; |
| 115 |
|
| 116 |
// For 26 numeric algorithms requirements, need addable, |
| 117 |
// subtractable, multiplicable. |
| 118 |
inline NonDefaultConstructible |
| 119 |
operator+(const NonDefaultConstructible&, const NonDefaultConstructible&) |
| 120 |
{ return NonDefaultConstructible(1); } |
| 121 |
|
| 122 |
inline NonDefaultConstructible |
| 123 |
operator-(const NonDefaultConstructible&, const NonDefaultConstructible&) |
| 124 |
{ return NonDefaultConstructible(1); } |
| 125 |
|
| 126 |
inline NonDefaultConstructible |
| 127 |
operator*(const NonDefaultConstructible&, const NonDefaultConstructible&) |
| 128 |
{ return NonDefaultConstructible(1); } |
| 129 |
|
| 130 |
// Like unary_function, but takes no argument. (ie, void). |
| 131 |
// Used for generator template parameter. |
| 132 |
template<typename _Result> |
| 133 |
struct void_function |
| 134 |
{ |
| 135 |
typedef _Result result_type; |
| 136 |
|
| 137 |
result_type |
| 138 |
operator()() const |
| 139 |
{ return result_type(); } |
| 140 |
}; |
| 141 |
|
| 142 |
template<> |
| 143 |
struct void_function<NonDefaultConstructible> |
| 144 |
{ |
| 145 |
typedef NonDefaultConstructible result_type; |
| 146 |
|
| 147 |
result_type |
| 148 |
operator()() const |
| 149 |
{ return result_type(2); } |
| 150 |
}; |
| 151 |
|
| 152 |
// For std::addressof, etc. |
| 153 |
struct OverloadedAddressAux { }; |
| 154 |
|
| 155 |
struct OverloadedAddress |
| 156 |
{ |
| 157 |
OverloadedAddressAux |
| 158 |
operator&() const { return OverloadedAddressAux(); } |
| 159 |
}; |
| 160 |
|
| 161 |
inline bool |
| 162 |
operator<(const OverloadedAddress&, const OverloadedAddress&) |
| 163 |
{ return false; } |
| 164 |
|
| 165 |
inline bool |
| 166 |
operator==(const OverloadedAddress&, const OverloadedAddress&) |
| 167 |
{ return false; } |
| 168 |
|
| 169 |
struct OverloadedAddress_hash |
| 170 |
{ |
| 171 |
std::size_t |
| 172 |
operator()(const OverloadedAddress&) const |
| 173 |
{ return 1; } |
| 174 |
}; |
| 175 |
|
| 176 |
#if __cplusplus >= 201103L |
| 177 |
struct NonCopyConstructible |
| 178 |
{ |
| 179 |
NonCopyConstructible() : num(-1) { } |
| 180 |
|
| 181 |
NonCopyConstructible(NonCopyConstructible&& other) |
| 182 |
: num(other.num) |
| 183 |
{ other.num = 0; } |
| 184 |
|
| 185 |
NonCopyConstructible(const NonCopyConstructible&) = delete; |
| 186 |
|
| 187 |
operator int() { return num; } |
| 188 |
|
| 189 |
private: |
| 190 |
int num; |
| 191 |
}; |
| 192 |
#endif |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
#endif |