|
24 | 24 |
|
25 | 25 | #include <array>
|
26 | 26 | #include <optional>
|
| 27 | +#include <limits> |
| 28 | +#include <map> |
27 | 29 | #include <stdint.h>
|
28 | 30 | #include <string.h>
|
29 | 31 | #include <thread>
|
@@ -1621,6 +1623,11 @@ BOOST_AUTO_TEST_CASE(test_ToIntegral)
|
1621 | 1623 | BOOST_CHECK(!ToIntegral<uint8_t>("256"));
|
1622 | 1624 | }
|
1623 | 1625 |
|
| 1626 | +int64_t atoi64_legacy(const std::string& str) |
| 1627 | +{ |
| 1628 | + return strtoll(str.c_str(), nullptr, 10); |
| 1629 | +} |
| 1630 | + |
1624 | 1631 | BOOST_AUTO_TEST_CASE(test_LocaleIndependentAtoi)
|
1625 | 1632 | {
|
1626 | 1633 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1234"), 1'234);
|
@@ -1648,48 +1655,68 @@ BOOST_AUTO_TEST_CASE(test_LocaleIndependentAtoi)
|
1648 | 1655 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(""), 0);
|
1649 | 1656 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("aap"), 0);
|
1650 | 1657 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("0x1"), 0);
|
1651 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-32482348723847471234"), 0); |
1652 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("32482348723847471234"), 0); |
| 1658 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-32482348723847471234"), -2'147'483'647 - 1); |
| 1659 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("32482348723847471234"), 2'147'483'647); |
1653 | 1660 |
|
1654 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775809"), 0); |
| 1661 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775809"), -9'223'372'036'854'775'807LL - 1LL); |
1655 | 1662 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775808"), -9'223'372'036'854'775'807LL - 1LL);
|
1656 | 1663 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775807"), 9'223'372'036'854'775'807);
|
1657 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775808"), 0); |
| 1664 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775808"), 9'223'372'036'854'775'807); |
| 1665 | + |
| 1666 | + std::map<std::string, int64_t> atoi64_test_pairs = { |
| 1667 | + {"-9223372036854775809", std::numeric_limits<int64_t>::min()}, |
| 1668 | + {"-9223372036854775808", -9'223'372'036'854'775'807LL - 1LL}, |
| 1669 | + {"9223372036854775807", 9'223'372'036'854'775'807}, |
| 1670 | + {"9223372036854775808", std::numeric_limits<int64_t>::max()}, |
| 1671 | + {"+-", 0}, |
| 1672 | + {"0x1", 0}, |
| 1673 | + {"ox1", 0}, |
| 1674 | + {"", 0}, |
| 1675 | + }; |
| 1676 | + |
| 1677 | + for (const auto& pair : atoi64_test_pairs) { |
| 1678 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(pair.first), pair.second); |
| 1679 | + } |
| 1680 | + |
| 1681 | + // Ensure legacy compatibility with previous versions of Bitcoin Core's atoi64 |
| 1682 | + for (const auto& pair : atoi64_test_pairs) { |
| 1683 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(pair.first), atoi64_legacy(pair.first)); |
| 1684 | + } |
1658 | 1685 |
|
1659 | 1686 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("-1"), 0U);
|
1660 | 1687 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("0"), 0U);
|
1661 | 1688 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("18446744073709551615"), 18'446'744'073'709'551'615ULL);
|
1662 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("18446744073709551616"), 0U); |
| 1689 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("18446744073709551616"), 18'446'744'073'709'551'615ULL); |
1663 | 1690 |
|
1664 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483649"), 0); |
| 1691 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483649"), -2'147'483'648LL); |
1665 | 1692 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483648"), -2'147'483'648LL);
|
1666 | 1693 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483647"), 2'147'483'647);
|
1667 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483648"), 0); |
| 1694 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483648"), 2'147'483'647); |
1668 | 1695 |
|
1669 | 1696 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("-1"), 0U);
|
1670 | 1697 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("0"), 0U);
|
1671 | 1698 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967295"), 4'294'967'295U);
|
1672 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967296"), 0U); |
| 1699 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967296"), 4'294'967'295U); |
1673 | 1700 |
|
1674 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32769"), 0); |
| 1701 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32769"), -32'768); |
1675 | 1702 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32768"), -32'768);
|
1676 | 1703 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32767"), 32'767);
|
1677 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32768"), 0); |
| 1704 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32768"), 32'767); |
1678 | 1705 |
|
1679 | 1706 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("-1"), 0U);
|
1680 | 1707 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("0"), 0U);
|
1681 | 1708 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65535"), 65'535U);
|
1682 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65536"), 0U); |
| 1709 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65536"), 65'535U); |
1683 | 1710 |
|
1684 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("-129"), 0); |
| 1711 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("-129"), -128); |
1685 | 1712 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("-128"), -128);
|
1686 | 1713 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("127"), 127);
|
1687 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("128"), 0); |
| 1714 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("128"), 127); |
1688 | 1715 |
|
1689 | 1716 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("-1"), 0U);
|
1690 | 1717 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("0"), 0U);
|
1691 | 1718 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("255"), 255U);
|
1692 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("256"), 0U); |
| 1719 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("256"), 255U); |
1693 | 1720 | }
|
1694 | 1721 |
|
1695 | 1722 | BOOST_AUTO_TEST_CASE(test_ParseInt64)
|
|
0 commit comments