| 1 | // { dg-do run { target c++20 } } |
| 2 | |
| 3 | #include <format> |
| 4 | #include <testsuite_hooks.h> |
| 5 | |
| 6 | template<typename T> |
| 7 | bool format_float() |
| 8 | { |
| 9 | auto s = std::format("{:#} != {:<+7.3f}", (T)-0.0, (T)0.5); |
| 10 | return s == "-0. != +0.500 "; |
| 11 | } |
| 12 | |
| 13 | #if __cplusplus > 202002L |
| 14 | template<typename T> |
| 15 | concept formattable = std::formattable<T, char>; |
| 16 | #else |
| 17 | template<typename T> |
| 18 | concept formattable = std::semiregular<std::formatter<T, char>>; |
| 19 | #endif |
| 20 | |
| 21 | void |
| 22 | test_float16() |
| 23 | { |
| 24 | #if __FLT16_DIG__ |
| 25 | if constexpr (formattable<_Float16>) |
| 26 | VERIFY( format_float<_Float16>() ); |
| 27 | else |
| 28 | std::puts("Cannot format _Float16 on this target"); |
| 29 | #endif |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | test_float32() |
| 34 | { |
| 35 | #if __FLT32_DIG__ |
| 36 | if constexpr (formattable<_Float32>) |
| 37 | VERIFY( format_float<_Float32>() ); |
| 38 | else |
| 39 | std::puts("Cannot format _Float32 on this target"); |
| 40 | #endif |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | test_float64() |
| 45 | { |
| 46 | #if __FLT64_DIG__ |
| 47 | if constexpr (formattable<_Float64>) |
| 48 | VERIFY( format_float<_Float64>() ); |
| 49 | else |
| 50 | std::puts("Cannot format _Float64 on this target"); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | test_float128() |
| 56 | { |
| 57 | #ifdef __SIZEOF_FLOAT128__ |
| 58 | if constexpr (formattable<__float128>) |
| 59 | VERIFY( format_float<__float128>() ); |
| 60 | else |
| 61 | std::puts("Cannot format __float128 on this target"); |
| 62 | #endif |
| 63 | #if __FLT128_DIG__ |
| 64 | if constexpr (formattable<_Float128>) |
| 65 | VERIFY( format_float<_Float128>() ); |
| 66 | else |
| 67 | std::puts("Cannot format _Float128 on this target"); |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | test_bfloat16() |
| 73 | { |
| 74 | #if __BFLT16_DIG__ |
| 75 | using bfloat16_t = decltype(0.0bf16); |
| 76 | |
| 77 | if constexpr (formattable<bfloat16_t>) |
| 78 | VERIFY( format_float<bfloat16_t>() ); |
| 79 | else |
| 80 | std::puts("Cannot format bfloat16_t on this target"); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | int main() |
| 85 | { |
| 86 | test_float16(); |
| 87 | test_float32(); |
| 88 | test_float64(); |
| 89 | test_float128(); |
| 90 | test_bfloat16(); |
| 91 | } |