1 // { dg-options "-mieee" { target alpha*-*-* } }
2
3 // 1999-08-23 bkoz
4
5 // Copyright (C) 1999, 2001, 2002 Free Software Foundation
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // USA.
22
23 // 18.2.1.1 template class numeric_limits
24
25 #include <limits>
26 #include <limits.h>
27 #include <float.h>
28 #include <cwchar>
29 #include <testsuite_hooks.h>
30
31 template<typename T>
32 struct extrema {
33 static T min;
34 static T max;
35 };
36
37
38 #define DEFINE_EXTREMA(T, m, M) \
39 template<> T extrema<T>::min = m; \
40 template<> T extrema<T>::max = M
41
42 DEFINE_EXTREMA(char, CHAR_MIN, CHAR_MAX);
43 DEFINE_EXTREMA(signed char, SCHAR_MIN, SCHAR_MAX);
44 DEFINE_EXTREMA(unsigned char, 0, UCHAR_MAX);
45 DEFINE_EXTREMA(short, SHRT_MIN, SHRT_MAX);
46 DEFINE_EXTREMA(unsigned short, 0, USHRT_MAX);
47 DEFINE_EXTREMA(int, INT_MIN, INT_MAX);
48 DEFINE_EXTREMA(unsigned, 0U, UINT_MAX);
49 DEFINE_EXTREMA(long, LONG_MIN, LONG_MAX);
50 DEFINE_EXTREMA(unsigned long, 0UL, ULONG_MAX);
51
52 #if defined(_GLIBCPP_USE_WCHAR_T)
53 DEFINE_EXTREMA(wchar_t, WCHAR_MIN, WCHAR_MAX);
54 #endif //_GLIBCPP_USE_WCHAR_T
55
56 DEFINE_EXTREMA(float, FLT_MIN, FLT_MAX);
57 DEFINE_EXTREMA(double, DBL_MIN, DBL_MAX);
58 DEFINE_EXTREMA(long double, LDBL_MIN, LDBL_MAX);
59
60 #undef DEFINE_EXTREMA
61
62 template<typename T>
test_extrema()63 void test_extrema()
64 {
65 bool test = true;
66 T limits_min = std::numeric_limits<T>::min();
67 T limits_max = std::numeric_limits<T>::max();
68 T extrema_min = extrema<T>::min;
69 T extrema_max = extrema<T>::max;
70 VERIFY( extrema_min == limits_min );
71 VERIFY( extrema_max == limits_max );
72 }
73
74 template<typename T>
test_epsilon()75 void test_epsilon()
76 {
77 bool test = true;
78 T epsilon = std::numeric_limits<T>::epsilon();
79 T one = 1;
80
81 VERIFY( one != (one + epsilon) );
82 }
83
84 #ifdef __CHAR_UNSIGNED__
85 #define char_is_signed false
86 #else
87 #define char_is_signed true
88 #endif
89
test_sign()90 void test_sign()
91 {
92 bool test = true;
93 VERIFY( std::numeric_limits<char>::is_signed == char_is_signed );
94 VERIFY( std::numeric_limits<signed char>::is_signed == true );
95 VERIFY( std::numeric_limits<unsigned char>::is_signed == false );
96 VERIFY( std::numeric_limits<short>::is_signed == true );
97 VERIFY( std::numeric_limits<unsigned short>::is_signed == false );
98 VERIFY( std::numeric_limits<int>::is_signed == true );
99 VERIFY( std::numeric_limits<unsigned>::is_signed == false );
100 VERIFY( std::numeric_limits<long>::is_signed == true );
101 VERIFY( std::numeric_limits<unsigned long>::is_signed == false );
102 VERIFY( std::numeric_limits<float>::is_signed == true );
103 VERIFY( std::numeric_limits<double>::is_signed == true );
104 VERIFY( std::numeric_limits<long double>::is_signed == true );
105 }
106
107
108 template<typename T>
109 void
test_infinity()110 test_infinity()
111 {
112 bool test;
113
114 if (std::numeric_limits<T>::has_infinity)
115 {
116 T inf = std::numeric_limits<T>::infinity();
117 test = (inf + inf == inf);
118 }
119 else
120 test = true;
121
122 VERIFY (test);
123 }
124
125 template<typename T>
126 void
test_denorm_min()127 test_denorm_min()
128 {
129 bool test;
130
131 if (std::numeric_limits<T>::has_denorm == std::denorm_present)
132 {
133 T denorm = std::numeric_limits<T>::denorm_min();
134 test = (denorm > 0);
135 }
136 else
137 test = true;
138
139 VERIFY (test);
140 }
141
142 template<typename T>
143 void
test_qnan()144 test_qnan()
145 {
146 bool test;
147
148 if (std::numeric_limits<T>::has_quiet_NaN)
149 {
150 T nan = std::numeric_limits<T>::quiet_NaN();
151 test = (nan != nan);
152 }
153 else
154 test = true;
155
156 VERIFY (test);
157 }
158
159
160 template<typename T>
161 void
test_is_iec559()162 test_is_iec559()
163 {
164 bool test;
165
166 if (std::numeric_limits<T>::is_iec559)
167 {
168 // IEC 559 requires all of the following.
169 test = (std::numeric_limits<T>::has_infinity
170 && std::numeric_limits<T>::has_quiet_NaN
171 && std::numeric_limits<T>::has_signaling_NaN);
172 }
173 else
174 {
175 // If we had all of the following, why didn't we set IEC 559?
176 test = (!std::numeric_limits<T>::has_infinity
177 || !std::numeric_limits<T>::has_quiet_NaN
178 || !std::numeric_limits<T>::has_signaling_NaN);
179 }
180
181 VERIFY (test);
182 }
183
184
185 template<typename T>
186 struct A
187 {
188 int key;
189 public:
AA190 A(int i = 0): key(i) { }
191 bool
operator ==A192 operator==(int i) { return i == key; }
193 };
194
195 struct B
196 {
BB197 B(int i = 0) { }
198 };
199
200
test01()201 bool test01()
202 {
203 bool test = true;
204 std::numeric_limits< A<B> > obj;
205
206 VERIFY( !obj.is_specialized );
207 VERIFY( obj.min() == 0 );
208 VERIFY( obj.max() == 0 );
209 VERIFY( obj.digits == 0 );
210 VERIFY( obj.digits10 == 0 );
211 VERIFY( !obj.is_signed );
212 VERIFY( !obj.is_integer );
213 VERIFY( !obj.is_exact );
214 VERIFY( obj.radix == 0 );
215 VERIFY( obj.epsilon() == 0 );
216 VERIFY( obj.round_error() == 0 );
217 VERIFY( obj.min_exponent == 0 );
218 VERIFY( obj.min_exponent10 == 0 );
219 VERIFY( obj.max_exponent == 0 );
220 VERIFY( obj.max_exponent10 == 0 );
221 VERIFY( !obj.has_infinity );
222 VERIFY( !obj.has_quiet_NaN );
223 VERIFY( !obj.has_signaling_NaN );
224 VERIFY( !obj.has_denorm );
225 VERIFY( !obj.has_denorm_loss );
226 VERIFY( obj.infinity() == 0 );
227 VERIFY( obj.quiet_NaN() == 0 );
228 VERIFY( obj.signaling_NaN() == 0 );
229 VERIFY( obj.denorm_min() == 0 );
230 VERIFY( !obj.is_iec559 );
231 VERIFY( !obj.is_bounded );
232 VERIFY( !obj.is_modulo );
233 VERIFY( !obj.traps );
234 VERIFY( !obj.tinyness_before );
235 VERIFY( obj.round_style == std::round_toward_zero );
236
237 #ifdef DEBUG_ASSERT
238 assert(test);
239 #endif
240
241 return test;
242 }
243
244 // test linkage of the generic bits
245 template struct std::numeric_limits<B>;
246
test02()247 void test02()
248 {
249 typedef std::numeric_limits<B> b_nl_type;
250
251 // Should probably do all of them...
252 const int* pi1 = &b_nl_type::digits;
253 const int* pi2 = &b_nl_type::digits10;
254 const int* pi3 = &b_nl_type::max_exponent10;
255 const bool* pb1 = &b_nl_type::traps;
256 }
257
258 // libstdc++/5045
test03()259 bool test03()
260 {
261 bool test = true;
262
263 VERIFY( std::numeric_limits<bool>::digits10 == 0 );
264 if (__CHAR_BIT__ == 8)
265 {
266 VERIFY( std::numeric_limits<signed char>::digits10 == 2 );
267 VERIFY( std::numeric_limits<unsigned char>::digits10 == 2 );
268 }
269 if (__CHAR_BIT__ * sizeof(short) == 16)
270 {
271 VERIFY( std::numeric_limits<signed short>::digits10 == 4 );
272 VERIFY( std::numeric_limits<unsigned short>::digits10 == 4 );
273 }
274 if (__CHAR_BIT__ * sizeof(int) == 32)
275 {
276 VERIFY( std::numeric_limits<signed int>::digits10 == 9 );
277 VERIFY( std::numeric_limits<unsigned int>::digits10 == 9 );
278 }
279 if (__CHAR_BIT__ * sizeof(long long) == 64)
280 {
281 VERIFY( std::numeric_limits<signed long long>::digits10 == 18 );
282 VERIFY( std::numeric_limits<unsigned long long>::digits10 == 19 );
283 }
284
285 #ifdef DEBUG_ASSERT
286 assert(test);
287 #endif
288
289 return test;
290 }
291
292 // libstdc++/8949
test04()293 bool test04()
294 {
295 bool test = true;
296
297 VERIFY( !std::numeric_limits<short>::is_iec559 );
298 VERIFY( !std::numeric_limits<unsigned short>::is_iec559 );
299 VERIFY( !std::numeric_limits<int>::is_iec559 );
300 VERIFY( !std::numeric_limits<unsigned int>::is_iec559 );
301 VERIFY( !std::numeric_limits<long>::is_iec559 );
302 VERIFY( !std::numeric_limits<unsigned long>::is_iec559 );
303 VERIFY( !std::numeric_limits<long long>::is_iec559 );
304 VERIFY( !std::numeric_limits<unsigned long long>::is_iec559 );
305
306 #ifdef DEBUG_ASSERT
307 assert(test);
308 #endif
309
310 return test;
311 }
312
main()313 int main()
314 {
315 test01();
316 test02();
317 test03();
318 test04();
319
320 test_extrema<char>();
321 test_extrema<signed char>();
322 test_extrema<unsigned char>();
323
324 test_extrema<short>();
325 test_extrema<unsigned short>();
326
327 test_extrema<int>();
328 test_extrema<unsigned>();
329
330 test_extrema<long>();
331 test_extrema<unsigned long>();
332
333 test_extrema<float>();
334 test_extrema<double>();
335 test_extrema<long double>();
336
337 test_epsilon<float>();
338 test_epsilon<double>();
339 test_epsilon<long double>();
340
341 test_sign();
342
343 test_infinity<float>();
344 test_infinity<double>();
345 test_infinity<long double>();
346
347 test_denorm_min<float>();
348 test_denorm_min<double>();
349 test_denorm_min<long double>();
350
351 test_qnan<float>();
352 test_qnan<double>();
353 test_qnan<long double>();
354
355 // ??? How to test SNaN? We'd perhaps have to be prepared
356 // to catch SIGFPE. Can't rely on a signal getting through
357 // since the exception can be disabled in the FPU.
358
359 test_is_iec559<float>();
360 test_is_iec559<double>();
361 test_is_iec559<long double>();
362
363 return 0;
364 }
365