xref: /llvm-project/libcxx/test/std/numerics/c.math/isnormal.pass.cpp (revision b9a2658a3e8bd13b0f9e7a8a440832a95b377216)
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 // XFAIL: FROZEN-CXX03-HEADERS-FIXME
15 
16 #include <cassert>
17 #include <cmath>
18 #include <limits>
19 
20 #include "test_macros.h"
21 #include "type_algorithms.h"
22 
23 struct TestFloat {
24   template <class T>
25   static TEST_CONSTEXPR_CXX23 bool test() {
26     assert(std::isnormal(std::numeric_limits<T>::max()));
27     assert(!std::isnormal(std::numeric_limits<T>::infinity()));
28     assert(std::isnormal(std::numeric_limits<T>::min()));
29     assert(!std::isnormal(std::numeric_limits<T>::denorm_min()));
30     assert(std::isnormal(std::numeric_limits<T>::lowest()));
31     assert(!std::isnormal(-std::numeric_limits<T>::infinity()));
32     assert(!std::isnormal(T(0)));
33     assert(!std::isnormal(std::numeric_limits<T>::quiet_NaN()));
34     assert(!std::isnormal(std::numeric_limits<T>::signaling_NaN()));
35 
36     return true;
37   }
38 
39   template <class T>
40   TEST_CONSTEXPR_CXX23 void operator()() {
41     test<T>();
42 #if TEST_STD_VER >= 23
43     static_assert(test<T>());
44 #endif
45   }
46 };
47 
48 struct TestInt {
49   template <class T>
50   static TEST_CONSTEXPR_CXX23 bool test() {
51     assert(std::isnormal(std::numeric_limits<T>::max()));
52     assert(std::isnormal(std::numeric_limits<T>::lowest()) == std::is_signed<T>::value);
53     assert(!std::isnormal(T(0)));
54 
55     return true;
56   }
57 
58   template <class T>
59   TEST_CONSTEXPR_CXX23 void operator()() {
60     test<T>();
61 #if TEST_STD_VER >= 23
62     static_assert(test<T>());
63 #endif
64   }
65 };
66 
67 template <typename T>
68 struct ConvertibleTo {
69   operator T() const { return T(1); }
70 };
71 
72 int main(int, char**) {
73   types::for_each(types::floating_point_types(), TestFloat());
74   types::for_each(types::integral_types(), TestInt());
75 
76   // Make sure we can call `std::isnormal` with convertible types. This checks
77   // whether overloads for all cv-unqualified floating-point types are working
78   // as expected.
79   {
80     assert(std::isnormal(ConvertibleTo<float>()));
81     assert(std::isnormal(ConvertibleTo<double>()));
82     assert(std::isnormal(ConvertibleTo<long double>()));
83   }
84 
85   return 0;
86 }
87