| 1 |
// { dg-do run { target c++11 } } |
| 2 |
// { dg-require-namedlocale "de_DE.ISO8859-15" } |
| 3 |
// { dg-add-options no_pch } |
| 4 |
|
| 5 |
// C++11 21.5 Numeric Conversions [string.conversions] |
| 6 |
|
| 7 |
#include <string> |
| 8 |
|
| 9 |
#if __cplusplus > 202302L |
| 10 |
|
| 11 |
#ifndef __cpp_lib_to_string |
| 12 |
# error "Feature-test macro for std::to_string missing in <string>" |
| 13 |
#elif __cpp_lib_to_string != 202306L |
| 14 |
# error "Feature-test macro for std::to_string has wrong value in <string>" |
| 15 |
#endif |
| 16 |
|
| 17 |
#else |
| 18 |
|
| 19 |
#ifdef __cpp_lib_to_string |
| 20 |
# error "__cpp_lib_to_string should not be defined for C++23" |
| 21 |
#endif |
| 22 |
|
| 23 |
#endif |
| 24 |
|
| 25 |
#include <format> |
| 26 |
#include <limits> |
| 27 |
#include <locale> |
| 28 |
#include <cstdio> |
| 29 |
#include <testsuite_hooks.h> |
| 30 |
|
| 31 |
namespace test |
| 32 |
{ |
| 33 |
// Canonical version of std::to_string(double) as specified in the standard. |
| 34 |
|
| 35 |
#if __cpp_lib_to_string |
| 36 |
static std::string to_string(float val) { return std::format("{}", val); } |
| 37 |
static std::string to_string(double val) { return std::format("{}", val); } |
| 38 |
static std::string to_string(long double val) { return std::format("{}", val); } |
| 39 |
#else |
| 40 |
static std::string to_string(double val) |
| 41 |
{ |
| 42 |
std::string str(100, '9'); |
| 43 |
retry: |
| 44 |
const int size = str.size(); |
| 45 |
const int len = std::snprintf(&str[0], size + 1, "%f", val); |
| 46 |
str.resize(len); |
| 47 |
if (len > size) |
| 48 |
goto retry; |
| 49 |
return str; |
| 50 |
} |
| 51 |
|
| 52 |
// snprintf promotes float to double |
| 53 |
static std::string to_string(float val) { return to_string((double)val); } |
| 54 |
|
| 55 |
static std::string to_string(long double val) |
| 56 |
{ |
| 57 |
std::string str(100, '9'); |
| 58 |
retry: |
| 59 |
const int size = str.size(); |
| 60 |
const int len = std::snprintf(&str[0], size + 1, "%Lf", val); |
| 61 |
str.resize(len); |
| 62 |
if (len > size) |
| 63 |
goto retry; |
| 64 |
return str; |
| 65 |
} |
| 66 |
#endif |
| 67 |
} // namespace test |
| 68 |
|
| 69 |
template<typename T> |
| 70 |
void check_value(T val) |
| 71 |
{ |
| 72 |
const std::string s = std::to_string(val); |
| 73 |
const std::string expected = test::to_string(val); |
| 74 |
VERIFY( s == expected ); |
| 75 |
VERIFY( s[s.size()] == '\0' ); // null-terminator not overwritten |
| 76 |
} |
| 77 |
|
| 78 |
template<typename T> |
| 79 |
void check_values() |
| 80 |
{ |
| 81 |
const T values[] = { |
| 82 |
0.0, 0.0625, 0.25, 0.5, 1.25, 1e2, 1e7, 1e8, 1e-2, 1e-7, 1e-8, |
| 83 |
2e38, 4.4e+19, 6.25e-12, 7.89e+23, |
| 84 |
12345.6789, (T) 1234567890123456.e100L, (T) 1213141516e-99L, |
| 85 |
std::numeric_limits<T>::min(), |
| 86 |
std::numeric_limits<T>::max(), |
| 87 |
std::numeric_limits<T>::epsilon(), |
| 88 |
std::numeric_limits<T>::infinity(), |
| 89 |
std::numeric_limits<T>::quiet_NaN(), |
| 90 |
}; |
| 91 |
|
| 92 |
std::locale::global(std::locale::classic()); |
| 93 |
|
| 94 |
for (auto v : values) |
| 95 |
{ |
| 96 |
check_value(v); |
| 97 |
check_value(-v); |
| 98 |
} |
| 99 |
|
| 100 |
std::locale::global(std::locale(ISO_8859(15,de_DE))); |
| 101 |
|
| 102 |
for (auto v : values) |
| 103 |
{ |
| 104 |
check_value(v); |
| 105 |
check_value(-v); |
| 106 |
} |
| 107 |
|
| 108 |
std::locale::global(std::locale::classic()); |
| 109 |
} |
| 110 |
|
| 111 |
void test01() |
| 112 |
{ |
| 113 |
// Examples from P2587R3 `to_string` or not `to_string` |
| 114 |
|
| 115 |
|
| 116 |
VERIFY( std::to_string(42) == "42" ); |
| 117 |
VERIFY( std::to_string(12345) == "12345" ); |
| 118 |
auto max = std::to_string(1.7976931348623157e+308); |
| 119 |
|
| 120 |
#if __cplusplus <= 202302L |
| 121 |
VERIFY( std::to_string(0.42) == "0.420000" ); |
| 122 |
VERIFY( std::to_string(1e-7) == "0.000000" ); |
| 123 |
VERIFY( std::to_string(-1e-7) == "-0.000000" ); |
| 124 |
VERIFY( max.substr(0, 17) == "17976931348623157" ); |
| 125 |
VERIFY( max.substr(max.size() - 7) == ".000000" ); |
| 126 |
#else |
| 127 |
VERIFY( std::to_string(0.42) == "0.42" ); |
| 128 |
VERIFY( std::to_string(1e-7) == "1e-07" ); |
| 129 |
VERIFY( std::to_string(-1e-7) == "-1e-07" ); |
| 130 |
VERIFY( max == "1.7976931348623157e+308" ); |
| 131 |
#endif |
| 132 |
|
| 133 |
std::locale::global(std::locale(ISO_8859(15,de_DE))); |
| 134 |
#if __cplusplus <= 202302L |
| 135 |
VERIFY( std::to_string(1234.5) == "1234,500000" ); |
| 136 |
#else |
| 137 |
VERIFY( std::to_string(1234.5) == "1234.5" ); |
| 138 |
#endif |
| 139 |
std::locale::global(std::locale::classic()); |
| 140 |
} |
| 141 |
|
| 142 |
void test02() |
| 143 |
{ |
| 144 |
check_values<float>(); |
| 145 |
check_values<double>(); |
| 146 |
check_values<long double>(); |
| 147 |
} |
| 148 |
|
| 149 |
int main() |
| 150 |
{ |
| 151 |
test01(); |
| 152 |
test02(); |
| 153 |
} |