83 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 18#include "test_macros.h"19#include "type_algorithms.h"20 21struct 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 >= 2341 static_assert(test<T>());42#endif43 }44};45 46struct 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 >= 2360 static_assert(test<T>());61#endif62 }63};64 65template <typename T>66struct ConvertibleTo {67 operator T() const { return T(); }68};69 70int 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 types75 {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}83