mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-03-17 19:10:40 +01:00
Replace use of locale dependent atoi(…) with locale-independent std::from_chars(…) (C++17)
test: Add test cases for LocaleIndependentAtoi fuzz: Assert legacy atoi(s) == LocaleIndependentAtoi<int>(s) fuzz: Assert legacy atoi64(s) == LocaleIndependentAtoi<int64_t>(s)
This commit is contained in:
@@ -1549,6 +1549,77 @@ BOOST_AUTO_TEST_CASE(test_ToIntegral)
|
||||
BOOST_CHECK(!ToIntegral<uint8_t>("256"));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_LocaleIndependentAtoi)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1234"), 1'234);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("0"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("01234"), 1'234);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-1234"), -1'234);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" 1"), 1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1 "), 1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1a"), 1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1.1"), 1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1.9"), 1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("+01.9"), 1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-1"), -1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" -1"), -1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-1 "), -1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" -1 "), -1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("+1"), 1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" +1"), 1);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(" +1 "), 1);
|
||||
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("+-1"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-+1"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("++1"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("--1"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(""), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("aap"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("0x1"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-32482348723847471234"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("32482348723847471234"), 0);
|
||||
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775809"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775808"), -9'223'372'036'854'775'807LL - 1LL);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775807"), 9'223'372'036'854'775'807);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775808"), 0);
|
||||
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("-1"), 0U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("0"), 0U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("18446744073709551615"), 18'446'744'073'709'551'615ULL);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("18446744073709551616"), 0U);
|
||||
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483649"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483648"), -2'147'483'648LL);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483647"), 2'147'483'647);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483648"), 0);
|
||||
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("-1"), 0U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("0"), 0U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967295"), 4'294'967'295U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967296"), 0U);
|
||||
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32769"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32768"), -32'768);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32767"), 32'767);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32768"), 0);
|
||||
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("-1"), 0U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("0"), 0U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65535"), 65'535U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65536"), 0U);
|
||||
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("-129"), 0);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("-128"), -128);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("127"), 127);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("128"), 0);
|
||||
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("-1"), 0U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("0"), 0U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("255"), 255U);
|
||||
BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("256"), 0U);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_ParseInt64)
|
||||
{
|
||||
int64_t n;
|
||||
|
||||
Reference in New Issue
Block a user