|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#include <cassert> |
| 5 | +#include <cstddef> |
| 6 | +#include <limits> |
| 7 | +#include <sstream> |
| 8 | +#include <type_traits> |
| 9 | + |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +template <class CharT, size_t N, enable_if_t<is_same_v<CharT, char>, int> = 0> |
| 13 | +constexpr const auto& choose_literal(const char (&s)[N], const wchar_t (&)[N]) noexcept { |
| 14 | + return s; |
| 15 | +} |
| 16 | +template <class CharT, size_t N, enable_if_t<is_same_v<CharT, wchar_t>, int> = 0> |
| 17 | +constexpr const auto& choose_literal(const char (&)[N], const wchar_t (&ws)[N]) noexcept { |
| 18 | + return ws; |
| 19 | +} |
| 20 | + |
| 21 | +#define STATICALLY_WIDEN(CharT, S) ::choose_literal<CharT>(S, L##S) |
| 22 | + |
| 23 | +template <class CharT, class F> |
| 24 | +void test() { |
| 25 | + // LWG-4084 "std::fixed ignores std::uppercase" |
| 26 | + { |
| 27 | + auto s = (basic_ostringstream<CharT>{} << fixed << uppercase << numeric_limits<F>::infinity()).str(); |
| 28 | + assert(s == STATICALLY_WIDEN(CharT, "INF")); |
| 29 | + } |
| 30 | + { |
| 31 | + auto s = (basic_ostringstream<CharT>{} << fixed << uppercase << numeric_limits<F>::quiet_NaN()).str(); |
| 32 | + assert(s == STATICALLY_WIDEN(CharT, "NAN")); |
| 33 | + } |
| 34 | + // also test other combinations |
| 35 | + { |
| 36 | + auto s = (basic_ostringstream<CharT>{} << fixed << numeric_limits<F>::infinity()).str(); |
| 37 | + assert(s == STATICALLY_WIDEN(CharT, "inf")); |
| 38 | + } |
| 39 | + { |
| 40 | + auto s = (basic_ostringstream<CharT>{} << fixed << numeric_limits<F>::quiet_NaN()).str(); |
| 41 | + assert(s == STATICALLY_WIDEN(CharT, "nan")); |
| 42 | + } |
| 43 | + |
| 44 | + { |
| 45 | + auto s = (basic_ostringstream<CharT>{} << uppercase << numeric_limits<F>::infinity()).str(); |
| 46 | + assert(s == STATICALLY_WIDEN(CharT, "INF")); |
| 47 | + } |
| 48 | + { |
| 49 | + auto s = (basic_ostringstream<CharT>{} << uppercase << numeric_limits<F>::quiet_NaN()).str(); |
| 50 | + assert(s == STATICALLY_WIDEN(CharT, "NAN")); |
| 51 | + } |
| 52 | + { |
| 53 | + auto s = (basic_ostringstream<CharT>{} << numeric_limits<F>::infinity()).str(); |
| 54 | + assert(s == STATICALLY_WIDEN(CharT, "inf")); |
| 55 | + } |
| 56 | + { |
| 57 | + auto s = (basic_ostringstream<CharT>{} << numeric_limits<F>::quiet_NaN()).str(); |
| 58 | + assert(s == STATICALLY_WIDEN(CharT, "nan")); |
| 59 | + } |
| 60 | + |
| 61 | + { |
| 62 | + auto s = (basic_ostringstream<CharT>{} << scientific << uppercase << numeric_limits<F>::infinity()).str(); |
| 63 | + assert(s == STATICALLY_WIDEN(CharT, "INF")); |
| 64 | + } |
| 65 | + { |
| 66 | + auto s = (basic_ostringstream<CharT>{} << scientific << uppercase << numeric_limits<F>::quiet_NaN()).str(); |
| 67 | + assert(s == STATICALLY_WIDEN(CharT, "NAN")); |
| 68 | + } |
| 69 | + { |
| 70 | + auto s = (basic_ostringstream<CharT>{} << scientific << numeric_limits<F>::infinity()).str(); |
| 71 | + assert(s == STATICALLY_WIDEN(CharT, "inf")); |
| 72 | + } |
| 73 | + { |
| 74 | + auto s = (basic_ostringstream<CharT>{} << scientific << numeric_limits<F>::quiet_NaN()).str(); |
| 75 | + assert(s == STATICALLY_WIDEN(CharT, "nan")); |
| 76 | + } |
| 77 | + |
| 78 | + { |
| 79 | + auto s = (basic_ostringstream<CharT>{} << hexfloat << uppercase << numeric_limits<F>::infinity()).str(); |
| 80 | + assert(s == STATICALLY_WIDEN(CharT, "INF")); |
| 81 | + } |
| 82 | + { |
| 83 | + auto s = (basic_ostringstream<CharT>{} << hexfloat << uppercase << numeric_limits<F>::quiet_NaN()).str(); |
| 84 | + assert(s == STATICALLY_WIDEN(CharT, "NAN")); |
| 85 | + } |
| 86 | + { |
| 87 | + auto s = (basic_ostringstream<CharT>{} << hexfloat << numeric_limits<F>::infinity()).str(); |
| 88 | + assert(s == STATICALLY_WIDEN(CharT, "inf")); |
| 89 | + } |
| 90 | + { |
| 91 | + auto s = (basic_ostringstream<CharT>{} << hexfloat << numeric_limits<F>::quiet_NaN()).str(); |
| 92 | + assert(s == STATICALLY_WIDEN(CharT, "nan")); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +int main() { |
| 97 | + test<char, float>(); |
| 98 | + test<char, double>(); |
| 99 | + test<char, long double>(); |
| 100 | + |
| 101 | + test<wchar_t, float>(); |
| 102 | + test<wchar_t, double>(); |
| 103 | + test<wchar_t, long double>(); |
| 104 | +} |
0 commit comments