]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/util/testsuite_api.h
[multiple changes]
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / testsuite_api.h
1 // -*- C++ -*-
2 // Exception testing utils for the C++ library testsuite.
3 //
4 // Copyright (C) 2007, 2008, 2009, 2010 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 <cstddef>
23 #include <exception>
24 #include <testsuite_hooks.h>
25
26 #ifndef _TESTSUITE_API
27 #define _TESTSUITE_API 1
28
29 namespace __gnu_test
30 {
31 // Checks for virtual public derivation in exception classes.
32 // See:
33 // http://www.boost.org/more/error_handling.html
34 struct bad_non_virtual : virtual public std::exception { };
35
36 template<typename Exception, bool DefaultCons>
37 struct diamond_derivation_base;
38
39 template<typename Exception>
40 struct diamond_derivation_base<Exception, true>
41 {
42 struct diamond_derivation_error
43 : bad_non_virtual, Exception
44 {
45 diamond_derivation_error()
46 : bad_non_virtual(), Exception() { }
47 };
48 };
49
50 template<typename Exception>
51 struct diamond_derivation_base<Exception, false>
52 {
53 struct diamond_derivation_error
54 : bad_non_virtual, Exception
55 {
56 diamond_derivation_error()
57 : bad_non_virtual(), Exception("construct diamond") { }
58 };
59 };
60
61 template<typename Exception, bool DefaultCons>
62 struct diamond_derivation
63 : diamond_derivation_base<Exception, DefaultCons>
64 {
65 typedef diamond_derivation_base<Exception, DefaultCons> base_type;
66 typedef typename base_type::diamond_derivation_error error_type;
67
68 // NB: In the libstdc++-v3 testsuite, all the standard exception
69 // classes (+ a couple of extensions) are checked: since they
70 // all derive *non* virtually from std::exception, the expected
71 // behavior is ambiguity.
72 static void test()
73 {
74 bool test __attribute__((unused)) = true;
75 try
76 { throw error_type(); }
77 catch (std::exception const&)
78 { VERIFY( false ); }
79 catch (...)
80 { VERIFY( true ); }
81 }
82 };
83
84 // Testing type requirements for template arguments.
85 struct NonDefaultConstructible
86 {
87 NonDefaultConstructible(int) { }
88 NonDefaultConstructible(const NonDefaultConstructible&) { }
89
90 #ifdef __GXX_EXPERIMENTAL_CXX0X__
91 // For std::iota.
92 NonDefaultConstructible&
93 operator++()
94 { return *this; }
95 #endif
96 };
97
98 // See: 20.1.1 Template argument requirements.
99 inline bool
100 operator==(const NonDefaultConstructible&, const NonDefaultConstructible&)
101 { return false; }
102
103 inline bool
104 operator<(const NonDefaultConstructible&, const NonDefaultConstructible&)
105 { return false; }
106
107 // For 23 unordered_* requirements.
108 struct NonDefaultConstructible_hash
109 {
110 size_t
111 operator()(NonDefaultConstructible) const
112 { return 1; }
113 };
114
115 // For 26 numeric algorithms requirements, need addable,
116 // subtractable, multiplicable.
117 inline NonDefaultConstructible
118 operator+(const NonDefaultConstructible& lhs,
119 const NonDefaultConstructible& rhs)
120 { return NonDefaultConstructible(1); }
121
122 inline NonDefaultConstructible
123 operator-(const NonDefaultConstructible& lhs,
124 const NonDefaultConstructible& rhs)
125 { return NonDefaultConstructible(1); }
126
127 inline NonDefaultConstructible
128 operator*(const NonDefaultConstructible& lhs,
129 const NonDefaultConstructible& rhs)
130 { return NonDefaultConstructible(1); }
131
132 // Like unary_function, but takes no argument. (ie, void).
133 // Used for generator template parameter.
134 template<typename _Result>
135 struct void_function
136 {
137 typedef _Result result_type;
138
139 result_type
140 operator()() const
141 { return result_type(); }
142 };
143
144 template<>
145 struct void_function<NonDefaultConstructible>
146 {
147 typedef NonDefaultConstructible result_type;
148
149 result_type
150 operator()() const
151 { return result_type(2); }
152 };
153
154 // For std::addressof, etc.
155 struct OverloadedAddressAux { };
156
157 struct OverloadedAddress
158 {
159 OverloadedAddressAux
160 operator&() const { return OverloadedAddressAux(); }
161 };
162
163 inline bool
164 operator<(const OverloadedAddress&, const OverloadedAddress&)
165 { return false; }
166
167 inline bool
168 operator==(const OverloadedAddress&, const OverloadedAddress&)
169 { return false; }
170
171 struct OverloadedAddress_hash
172 {
173 size_t
174 operator()(const OverloadedAddress&) const
175 { return 1; }
176 };
177 }
178
179 #endif