86 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// bool isfinite(floating-point-type x); // constexpr since C++2310 11// We don't control the implementation on windows12// UNSUPPORTED: windows13 14#include <cassert>15#include <cmath>16#include <limits>17#include <type_traits>18 19#include "test_macros.h"20#include "type_algorithms.h"21 22struct TestFloat {23 template <class T>24 static TEST_CONSTEXPR_CXX23 bool test() {25 assert(std::isnormal(std::numeric_limits<T>::max()));26 assert(!std::isnormal(std::numeric_limits<T>::infinity()));27 assert(std::isnormal(std::numeric_limits<T>::min()));28 assert(!std::isnormal(std::numeric_limits<T>::denorm_min()));29 assert(std::isnormal(std::numeric_limits<T>::lowest()));30 assert(!std::isnormal(-std::numeric_limits<T>::infinity()));31 assert(!std::isnormal(T(0)));32 assert(!std::isnormal(std::numeric_limits<T>::quiet_NaN()));33 assert(!std::isnormal(std::numeric_limits<T>::signaling_NaN()));34 35 return true;36 }37 38 template <class T>39 TEST_CONSTEXPR_CXX23 void operator()() {40 test<T>();41#if TEST_STD_VER >= 2342 static_assert(test<T>());43#endif44 }45};46 47struct TestInt {48 template <class T>49 static TEST_CONSTEXPR_CXX23 bool test() {50 assert(std::isnormal(std::numeric_limits<T>::max()));51 assert(std::isnormal(std::numeric_limits<T>::lowest()) == std::is_signed<T>::value);52 assert(!std::isnormal(T(0)));53 54 return true;55 }56 57 template <class T>58 TEST_CONSTEXPR_CXX23 void operator()() {59 test<T>();60#if TEST_STD_VER >= 2361 static_assert(test<T>());62#endif63 }64};65 66template <typename T>67struct ConvertibleTo {68 operator T() const { return T(1); }69};70 71int main(int, char**) {72 types::for_each(types::floating_point_types(), TestFloat());73 types::for_each(types::integral_types(), TestInt());74 75 // Make sure we can call `std::isnormal` with convertible types. This checks76 // whether overloads for all cv-unqualified floating-point types are working77 // as expected.78 {79 assert(std::isnormal(ConvertibleTo<float>()));80 assert(std::isnormal(ConvertibleTo<double>()));81 assert(std::isnormal(ConvertibleTo<long double>()));82 }83 84 return 0;85}86