| 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14 |
| 10 | // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
| 11 | |
| 12 | // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=12712420 |
| 13 | // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-ops-limit): -fconstexpr-ops-limit=50000000 |
| 14 | |
| 15 | // <charconv> |
| 16 | |
| 17 | // constexpr to_chars_result to_chars(char* first, char* last, Integral value, |
| 18 | // int base = 10) |
| 19 | |
| 20 | #include <charconv> |
| 21 | #include <cstdint> |
| 22 | #include <system_error> |
| 23 | |
| 24 | #include "test_macros.h" |
| 25 | #include "charconv_test_helpers.h" |
| 26 | |
| 27 | #ifndef TEST_HAS_NO_INT128 |
| 28 | TEST_CONSTEXPR_CXX23 __uint128_t make_u128(__uint128_t a, std::uint64_t b) { |
| 29 | a *= 1000000000000000000UL; |
| 30 | a *= 10; |
| 31 | return a + b; |
| 32 | } |
| 33 | |
| 34 | TEST_CONSTEXPR_CXX23 __uint128_t make_u128(__uint128_t a, std::uint64_t b, std::uint64_t c) { |
| 35 | a *= 10000000000000ULL; |
| 36 | a += b; |
| 37 | a *= 10000000000000ULL; |
| 38 | return a + c; |
| 39 | } |
| 40 | |
| 41 | TEST_CONSTEXPR_CXX23 __int128_t make_i128(__int128_t a, std::int64_t b) { |
| 42 | if (a < 0) |
| 43 | return -make_u128(-a, b); |
| 44 | return make_u128(a, b); |
| 45 | } |
| 46 | |
| 47 | TEST_CONSTEXPR_CXX23 __int128_t make_i128(__int128_t a, __int128_t b, std::int64_t c) { |
| 48 | if (a < 0) |
| 49 | return -make_u128(-a, b, c); |
| 50 | return make_u128(a, b, c); |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | template <typename T> |
| 55 | struct test_basics : to_chars_test_base<T> |
| 56 | { |
| 57 | using to_chars_test_base<T>::test; |
| 58 | using to_chars_test_base<T>::test_value; |
| 59 | |
| 60 | TEST_CONSTEXPR_CXX23 void operator()() |
| 61 | { |
| 62 | test(0, "0" ); |
| 63 | test(42, "42" ); |
| 64 | test(32768, "32768" ); |
| 65 | test(0, "0" , 10); |
| 66 | test(42, "42" , 10); |
| 67 | test(32768, "32768" , 10); |
| 68 | test(0xf, "f" , 16); |
| 69 | test(0xdeadbeaf, "deadbeaf" , 16); |
| 70 | test(0755, "755" , 8); |
| 71 | |
| 72 | // Test each len till len of UINT64_MAX = 20 because to_chars algorithm |
| 73 | // makes branches based on decimal digits count in the value string |
| 74 | // representation. |
| 75 | // Test driver automatically skips values not fitting into source type. |
| 76 | test(1UL, "1" ); |
| 77 | test(12UL, "12" ); |
| 78 | test(123UL, "123" ); |
| 79 | test(1234UL, "1234" ); |
| 80 | test(12345UL, "12345" ); |
| 81 | test(123456UL, "123456" ); |
| 82 | test(1234567UL, "1234567" ); |
| 83 | test(12345678UL, "12345678" ); |
| 84 | test(123456789UL, "123456789" ); |
| 85 | test(1234567890UL, "1234567890" ); |
| 86 | test(12345678901UL, "12345678901" ); |
| 87 | test(123456789012UL, "123456789012" ); |
| 88 | test(1234567890123UL, "1234567890123" ); |
| 89 | test(12345678901234UL, "12345678901234" ); |
| 90 | test(123456789012345UL, "123456789012345" ); |
| 91 | test(1234567890123456UL, "1234567890123456" ); |
| 92 | test(12345678901234567UL, "12345678901234567" ); |
| 93 | test(123456789012345678UL, "123456789012345678" ); |
| 94 | test(1234567890123456789UL, "1234567890123456789" ); |
| 95 | test(12345678901234567890UL, "12345678901234567890" ); |
| 96 | #ifndef TEST_HAS_NO_INT128 |
| 97 | test(make_u128(12UL, 3456789012345678901UL), "123456789012345678901" ); |
| 98 | test(make_u128(123UL, 4567890123456789012UL), "1234567890123456789012" ); |
| 99 | test(make_u128(1234UL, 5678901234567890123UL), "12345678901234567890123" ); |
| 100 | test(make_u128(12345UL, 6789012345678901234UL), "123456789012345678901234" ); |
| 101 | test(make_u128(123456UL, 7890123456789012345UL), "1234567890123456789012345" ); |
| 102 | test(make_u128(1234567UL, 8901234567890123456UL), "12345678901234567890123456" ); |
| 103 | test(make_u128(12345678UL, 9012345678901234567UL), "123456789012345678901234567" ); |
| 104 | test(make_u128(123456789UL, 123456789012345678UL), "1234567890123456789012345678" ); |
| 105 | test(make_u128(123UL, 4567890123456UL, 7890123456789UL), "12345678901234567890123456789" ); |
| 106 | test(make_u128(1234UL, 5678901234567UL, 8901234567890UL), "123456789012345678901234567890" ); |
| 107 | test(make_u128(12345UL, 6789012345678UL, 9012345678901UL), "1234567890123456789012345678901" ); |
| 108 | test(make_u128(123456UL, 7890123456789UL, 123456789012UL), "12345678901234567890123456789012" ); |
| 109 | test(make_u128(1234567UL, 8901234567890UL, 1234567890123UL), "123456789012345678901234567890123" ); |
| 110 | test(make_u128(12345678UL, 9012345678901UL, 2345678901234UL), "1234567890123456789012345678901234" ); |
| 111 | test(make_u128(123456789UL, 123456789012UL, 3456789012345UL), "12345678901234567890123456789012345" ); |
| 112 | test(make_u128(1234567890UL, 1234567890123UL, 4567890123456UL), "123456789012345678901234567890123456" ); |
| 113 | test(make_u128(12345678901UL, 2345678901234UL, 5678901234567UL), "1234567890123456789012345678901234567" ); |
| 114 | test(make_u128(123456789012UL, 3456789012345UL, 6789012345678UL), "12345678901234567890123456789012345678" ); |
| 115 | test(make_u128(1234567890123UL, 4567890123456UL, 7890123456789UL), "123456789012345678901234567890123456789" ); |
| 116 | #endif |
| 117 | |
| 118 | // Test special cases with zeros inside a value string representation, |
| 119 | // to_chars algorithm processes them in a special way and should not |
| 120 | // skip trailing zeros |
| 121 | // Test driver automatically skips values not fitting into source type. |
| 122 | test(0UL, "0" ); |
| 123 | test(10UL, "10" ); |
| 124 | test(100UL, "100" ); |
| 125 | test(1000UL, "1000" ); |
| 126 | test(10000UL, "10000" ); |
| 127 | test(100000UL, "100000" ); |
| 128 | test(1000000UL, "1000000" ); |
| 129 | test(10000000UL, "10000000" ); |
| 130 | test(100000000UL, "100000000" ); |
| 131 | test(1000000000UL, "1000000000" ); |
| 132 | test(10000000000UL, "10000000000" ); |
| 133 | test(100000000000UL, "100000000000" ); |
| 134 | test(1000000000000UL, "1000000000000" ); |
| 135 | test(10000000000000UL, "10000000000000" ); |
| 136 | test(100000000000000UL, "100000000000000" ); |
| 137 | test(1000000000000000UL, "1000000000000000" ); |
| 138 | test(10000000000000000UL, "10000000000000000" ); |
| 139 | test(100000000000000000UL, "100000000000000000" ); |
| 140 | test(1000000000000000000UL, "1000000000000000000" ); |
| 141 | test(10000000000000000000UL, "10000000000000000000" ); |
| 142 | #ifndef TEST_HAS_NO_INT128 |
| 143 | test(make_u128(10UL, 0), "100000000000000000000" ); |
| 144 | test(make_u128(100UL, 0), "1000000000000000000000" ); |
| 145 | test(make_u128(1000UL, 0), "10000000000000000000000" ); |
| 146 | test(make_u128(10000UL, 0), "100000000000000000000000" ); |
| 147 | test(make_u128(100000UL, 0), "1000000000000000000000000" ); |
| 148 | test(make_u128(1000000UL, 0), "10000000000000000000000000" ); |
| 149 | test(make_u128(10000000UL, 0), "100000000000000000000000000" ); |
| 150 | test(make_u128(100000000UL, 0), "1000000000000000000000000000" ); |
| 151 | test(make_u128(100UL, 0, 0), "10000000000000000000000000000" ); |
| 152 | test(make_u128(1000UL, 0, 0), "100000000000000000000000000000" ); |
| 153 | test(make_u128(10000UL, 0, 0), "1000000000000000000000000000000" ); |
| 154 | test(make_u128(100000UL, 0, 0), "10000000000000000000000000000000" ); |
| 155 | test(make_u128(1000000UL, 0, 0), "100000000000000000000000000000000" ); |
| 156 | test(make_u128(10000000UL, 0, 0), "1000000000000000000000000000000000" ); |
| 157 | test(make_u128(100000000UL, 0, 0), "10000000000000000000000000000000000" ); |
| 158 | test(make_u128(1000000000UL, 0, 0), "100000000000000000000000000000000000" ); |
| 159 | test(make_u128(10000000000UL, 0, 0), "1000000000000000000000000000000000000" ); |
| 160 | test(make_u128(100000000000UL, 0, 0), "10000000000000000000000000000000000000" ); |
| 161 | test(make_u128(1000000000000UL, 0, 0), "100000000000000000000000000000000000000" ); |
| 162 | #endif |
| 163 | |
| 164 | for (int b = 2; b < 37; ++b) |
| 165 | { |
| 166 | using xl = std::numeric_limits<T>; |
| 167 | |
| 168 | test_value(1, b); |
| 169 | test_value(xl::lowest(), b); |
| 170 | test_value((xl::max)(), b); |
| 171 | test_value((xl::max)() / 2, b); |
| 172 | } |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | template <typename T> |
| 177 | struct test_signed : to_chars_test_base<T> |
| 178 | { |
| 179 | using to_chars_test_base<T>::test; |
| 180 | using to_chars_test_base<T>::test_value; |
| 181 | |
| 182 | TEST_CONSTEXPR_CXX23 void operator()() |
| 183 | { |
| 184 | test(-1, "-1" ); |
| 185 | test(-12, "-12" ); |
| 186 | test(-1, "-1" , 10); |
| 187 | test(-12, "-12" , 10); |
| 188 | test(-21734634, "-21734634" , 10); |
| 189 | test(-2647, "-101001010111" , 2); |
| 190 | test(-0xcc1, "-cc1" , 16); |
| 191 | |
| 192 | // Test each len till len of INT64_MAX = 19 because to_chars algorithm |
| 193 | // makes branches based on decimal digits count in the value string |
| 194 | // representation. |
| 195 | // Test driver automatically skips values not fitting into source type. |
| 196 | test(-1L, "-1" ); |
| 197 | test(-12L, "-12" ); |
| 198 | test(-123L, "-123" ); |
| 199 | test(-1234L, "-1234" ); |
| 200 | test(-12345L, "-12345" ); |
| 201 | test(-123456L, "-123456" ); |
| 202 | test(-1234567L, "-1234567" ); |
| 203 | test(-12345678L, "-12345678" ); |
| 204 | test(-123456789L, "-123456789" ); |
| 205 | test(-1234567890L, "-1234567890" ); |
| 206 | test(-12345678901L, "-12345678901" ); |
| 207 | test(-123456789012L, "-123456789012" ); |
| 208 | test(-1234567890123L, "-1234567890123" ); |
| 209 | test(-12345678901234L, "-12345678901234" ); |
| 210 | test(-123456789012345L, "-123456789012345" ); |
| 211 | test(-1234567890123456L, "-1234567890123456" ); |
| 212 | test(-12345678901234567L, "-12345678901234567" ); |
| 213 | test(-123456789012345678L, "-123456789012345678" ); |
| 214 | test(-1234567890123456789L, "-1234567890123456789" ); |
| 215 | #ifndef TEST_HAS_NO_INT128 |
| 216 | test(make_i128(-1L, 2345678901234567890L), "-12345678901234567890" ); |
| 217 | test(make_i128(-12L, 3456789012345678901L), "-123456789012345678901" ); |
| 218 | test(make_i128(-123L, 4567890123456789012L), "-1234567890123456789012" ); |
| 219 | test(make_i128(-1234L, 5678901234567890123L), "-12345678901234567890123" ); |
| 220 | test(make_i128(-12345L, 6789012345678901234L), "-123456789012345678901234" ); |
| 221 | test(make_i128(-123456L, 7890123456789012345L), "-1234567890123456789012345" ); |
| 222 | test(make_i128(-1234567L, 8901234567890123456L), "-12345678901234567890123456" ); |
| 223 | test(make_i128(-12345678L, 9012345678901234567L), "-123456789012345678901234567" ); |
| 224 | test(make_i128(-123456789L, 123456789012345678L), "-1234567890123456789012345678" ); |
| 225 | test(make_i128(-1234567890L, 1234567890123456789L), "-12345678901234567890123456789" ); |
| 226 | test(make_i128(-123L, 4567890123456L, 7890123456789L), "-12345678901234567890123456789" ); |
| 227 | test(make_i128(-1234L, 5678901234567L, 8901234567890L), "-123456789012345678901234567890" ); |
| 228 | test(make_i128(-12345L, 6789012345678L, 9012345678901L), "-1234567890123456789012345678901" ); |
| 229 | test(make_i128(-123456L, 7890123456789L, 123456789012L), "-12345678901234567890123456789012" ); |
| 230 | test(make_i128(-1234567L, 8901234567890L, 1234567890123L), "-123456789012345678901234567890123" ); |
| 231 | test(make_i128(-12345678L, 9012345678901L, 2345678901234L), "-1234567890123456789012345678901234" ); |
| 232 | test(make_i128(-123456789L, 123456789012L, 3456789012345L), "-12345678901234567890123456789012345" ); |
| 233 | test(make_i128(-1234567890L, 1234567890123L, 4567890123456L), "-123456789012345678901234567890123456" ); |
| 234 | test(make_i128(-12345678901L, 2345678901234L, 5678901234567L), "-1234567890123456789012345678901234567" ); |
| 235 | test(make_i128(-123456789012L, 3456789012345L, 6789012345678L), "-12345678901234567890123456789012345678" ); |
| 236 | test(make_i128(-1234567890123L, 4567890123456L, 7890123456789L), "-123456789012345678901234567890123456789" ); |
| 237 | #endif |
| 238 | |
| 239 | // Test special cases with zeros inside a value string representation, |
| 240 | // to_chars algorithm processes them in a special way and should not |
| 241 | // skip trailing zeros |
| 242 | // Test driver automatically skips values not fitting into source type. |
| 243 | test(-10L, "-10" ); |
| 244 | test(-100L, "-100" ); |
| 245 | test(-1000L, "-1000" ); |
| 246 | test(-10000L, "-10000" ); |
| 247 | test(-100000L, "-100000" ); |
| 248 | test(-1000000L, "-1000000" ); |
| 249 | test(-10000000L, "-10000000" ); |
| 250 | test(-100000000L, "-100000000" ); |
| 251 | test(-1000000000L, "-1000000000" ); |
| 252 | test(-10000000000L, "-10000000000" ); |
| 253 | test(-100000000000L, "-100000000000" ); |
| 254 | test(-1000000000000L, "-1000000000000" ); |
| 255 | test(-10000000000000L, "-10000000000000" ); |
| 256 | test(-100000000000000L, "-100000000000000" ); |
| 257 | test(-1000000000000000L, "-1000000000000000" ); |
| 258 | test(-10000000000000000L, "-10000000000000000" ); |
| 259 | test(-100000000000000000L, "-100000000000000000" ); |
| 260 | test(-1000000000000000000L, "-1000000000000000000" ); |
| 261 | #ifndef TEST_HAS_NO_INT128 |
| 262 | test(make_i128(-1L, 0L), "-10000000000000000000" ); |
| 263 | test(make_i128(-10L, 0L), "-100000000000000000000" ); |
| 264 | test(make_i128(-100L, 0L), "-1000000000000000000000" ); |
| 265 | test(make_i128(-1000L, 0L), "-10000000000000000000000" ); |
| 266 | test(make_i128(-10000L, 0L), "-100000000000000000000000" ); |
| 267 | test(make_i128(-100000L, 0L), "-1000000000000000000000000" ); |
| 268 | test(make_i128(-1000000L, 0L), "-10000000000000000000000000" ); |
| 269 | test(make_i128(-10000000L, 0L), "-100000000000000000000000000" ); |
| 270 | test(make_i128(-100000000L, 0L), "-1000000000000000000000000000" ); |
| 271 | test(make_i128(-1000000000L, 0L), "-10000000000000000000000000000" ); |
| 272 | test(make_i128(-100L, 0L, 0L), "-10000000000000000000000000000" ); |
| 273 | test(make_i128(-1000L, 0L, 0L), "-100000000000000000000000000000" ); |
| 274 | test(make_i128(-10000L, 0L, 0L), "-1000000000000000000000000000000" ); |
| 275 | test(make_i128(-100000L, 0L, 0L), "-10000000000000000000000000000000" ); |
| 276 | test(make_i128(-1000000L, 0L, 0L), "-100000000000000000000000000000000" ); |
| 277 | test(make_i128(-10000000L, 0L, 0L), "-1000000000000000000000000000000000" ); |
| 278 | test(make_i128(-100000000L, 0L, 0L), "-10000000000000000000000000000000000" ); |
| 279 | test(make_i128(-1000000000L, 0L, 0L), "-100000000000000000000000000000000000" ); |
| 280 | test(make_i128(-10000000000L, 0L, 0L), "-1000000000000000000000000000000000000" ); |
| 281 | test(make_i128(-100000000000L, 0L, 0L), "-10000000000000000000000000000000000000" ); |
| 282 | test(make_i128(-1000000000000L, 0L, 0L), "-100000000000000000000000000000000000000" ); |
| 283 | #endif |
| 284 | |
| 285 | for (int b = 2; b < 37; ++b) |
| 286 | { |
| 287 | using xl = std::numeric_limits<T>; |
| 288 | |
| 289 | test_value(0, b); |
| 290 | test_value(xl::lowest(), b); |
| 291 | test_value((xl::max)(), b); |
| 292 | } |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | TEST_CONSTEXPR_CXX23 bool test() |
| 297 | { |
| 298 | run<test_basics>(integrals); |
| 299 | run<test_signed>(all_signed); |
| 300 | |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | int main(int, char**) |
| 305 | { |
| 306 | test(); |
| 307 | #if TEST_STD_VER > 20 |
| 308 | static_assert(test()); |
| 309 | #endif |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |