Debian Codegraph @

libcxx/test/std/numerics/c.math/isfinite.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // bool isfinite(floating-point-type x); // constexpr since C++23
10
11 // We don't control the implementation on windows
12 // UNSUPPORTED: windows
13
14 #include <cassert>
15 #include <cmath>
16 #include <limits>
17
18 #include "test_macros.h"
19 #include "type_algorithms.h"
20
21 struct TestFloat {
22 template <class T>
23 static TEST_CONSTEXPR_CXX23 bool test() {
24 assert(std::isfinite(std::numeric_limits<T>::max()));
25 assert(!std::isfinite(std::numeric_limits<T>::infinity()));
26 assert(std::isfinite(std::numeric_limits<T>::min()));
27 assert(std::isfinite(std::numeric_limits<T>::denorm_min()));
28 assert(std::isfinite(std::numeric_limits<T>::lowest()));
29 assert(!std::isfinite(-std::numeric_limits<T>::infinity()));
30 assert(std::isfinite(T(0)));
31 assert(!std::isfinite(std::numeric_limits<T>::quiet_NaN()));
32 assert(!std::isfinite(std::numeric_limits<T>::signaling_NaN()));
33
34 return true;
35 }
36
37 template <class T>
38 TEST_CONSTEXPR_CXX23 void operator()() {
39 test<T>();
40 #if TEST_STD_VER >= 23
41 static_assert(test<T>());
42 #endif
43 }
44 };
45
46 struct TestInt {
47 template <class T>
48 static TEST_CONSTEXPR_CXX23 bool test() {
49 assert(std::isfinite(std::numeric_limits<T>::max()));
50 assert(std::isfinite(std::numeric_limits<T>::lowest()));
51 assert(std::isfinite(T(0)));
52
53 return true;
54 }
55
56 template <class T>
57 TEST_CONSTEXPR_CXX23 void operator()() {
58 test<T>();
59 #if TEST_STD_VER >= 23
60 static_assert(test<T>());
61 #endif
62 }
63 };
64
65 template <typename T>
66 struct ConvertibleTo {
67 operator T() const { return T(); }
68 };
69
70 int main(int, char**) {
71 types::for_each(types::floating_point_types(), TestFloat());
72 types::for_each(types::integral_types(), TestInt());
73
74 // Make sure we can call `std::isfinite` with convertible types
75 {
76 assert(std::isfinite(ConvertibleTo<float>()));
77 assert(std::isfinite(ConvertibleTo<double>()));
78 assert(std::isfinite(ConvertibleTo<long double>()));
79 }
80
81 return 0;
82 }