|
29 | 29 | #include <fstream>
|
30 | 30 | #include <deque>
|
31 | 31 | #include <optional>
|
| 32 | +#include <limits> |
| 33 | +#include <map> |
32 | 34 | #include <random>
|
33 | 35 | #include <stdint.h>
|
34 | 36 | #include <string.h>
|
@@ -1737,6 +1739,11 @@ BOOST_AUTO_TEST_CASE(test_ToIntegral)
|
1737 | 1739 | BOOST_CHECK(!ToIntegral<uint8_t>("256"));
|
1738 | 1740 | }
|
1739 | 1741 |
|
| 1742 | +int64_t atoi64_legacy(const std::string& str) |
| 1743 | +{ |
| 1744 | + return strtoll(str.c_str(), nullptr, 10); |
| 1745 | +} |
| 1746 | + |
1740 | 1747 | BOOST_AUTO_TEST_CASE(test_LocaleIndependentAtoi)
|
1741 | 1748 | {
|
1742 | 1749 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("1234"), 1'234);
|
@@ -1764,48 +1771,68 @@ BOOST_AUTO_TEST_CASE(test_LocaleIndependentAtoi)
|
1764 | 1771 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>(""), 0);
|
1765 | 1772 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("aap"), 0);
|
1766 | 1773 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("0x1"), 0);
|
1767 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-32482348723847471234"), 0); |
1768 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("32482348723847471234"), 0); |
| 1774 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-32482348723847471234"), -2'147'483'647 - 1); |
| 1775 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("32482348723847471234"), 2'147'483'647); |
1769 | 1776 |
|
1770 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775809"), 0); |
| 1777 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775809"), -9'223'372'036'854'775'807LL - 1LL); |
1771 | 1778 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("-9223372036854775808"), -9'223'372'036'854'775'807LL - 1LL);
|
1772 | 1779 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775807"), 9'223'372'036'854'775'807);
|
1773 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775808"), 0); |
| 1780 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>("9223372036854775808"), 9'223'372'036'854'775'807); |
| 1781 | + |
| 1782 | + std::map<std::string, int64_t> atoi64_test_pairs = { |
| 1783 | + {"-9223372036854775809", std::numeric_limits<int64_t>::min()}, |
| 1784 | + {"-9223372036854775808", -9'223'372'036'854'775'807LL - 1LL}, |
| 1785 | + {"9223372036854775807", 9'223'372'036'854'775'807}, |
| 1786 | + {"9223372036854775808", std::numeric_limits<int64_t>::max()}, |
| 1787 | + {"+-", 0}, |
| 1788 | + {"0x1", 0}, |
| 1789 | + {"ox1", 0}, |
| 1790 | + {"", 0}, |
| 1791 | + }; |
| 1792 | + |
| 1793 | + for (const auto& pair : atoi64_test_pairs) { |
| 1794 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(pair.first), pair.second); |
| 1795 | + } |
| 1796 | + |
| 1797 | + // Ensure legacy compatibility with previous versions of Bitcoin Core's atoi64 |
| 1798 | + for (const auto& pair : atoi64_test_pairs) { |
| 1799 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int64_t>(pair.first), atoi64_legacy(pair.first)); |
| 1800 | + } |
1774 | 1801 |
|
1775 | 1802 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("-1"), 0U);
|
1776 | 1803 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("0"), 0U);
|
1777 | 1804 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("18446744073709551615"), 18'446'744'073'709'551'615ULL);
|
1778 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("18446744073709551616"), 0U); |
| 1805 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint64_t>("18446744073709551616"), 18'446'744'073'709'551'615ULL); |
1779 | 1806 |
|
1780 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483649"), 0); |
| 1807 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483649"), -2'147'483'648LL); |
1781 | 1808 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("-2147483648"), -2'147'483'648LL);
|
1782 | 1809 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483647"), 2'147'483'647);
|
1783 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483648"), 0); |
| 1810 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int32_t>("2147483648"), 2'147'483'647); |
1784 | 1811 |
|
1785 | 1812 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("-1"), 0U);
|
1786 | 1813 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("0"), 0U);
|
1787 | 1814 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967295"), 4'294'967'295U);
|
1788 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967296"), 0U); |
| 1815 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint32_t>("4294967296"), 4'294'967'295U); |
1789 | 1816 |
|
1790 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32769"), 0); |
| 1817 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32769"), -32'768); |
1791 | 1818 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("-32768"), -32'768);
|
1792 | 1819 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32767"), 32'767);
|
1793 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32768"), 0); |
| 1820 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int16_t>("32768"), 32'767); |
1794 | 1821 |
|
1795 | 1822 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("-1"), 0U);
|
1796 | 1823 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("0"), 0U);
|
1797 | 1824 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65535"), 65'535U);
|
1798 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65536"), 0U); |
| 1825 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint16_t>("65536"), 65'535U); |
1799 | 1826 |
|
1800 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("-129"), 0); |
| 1827 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("-129"), -128); |
1801 | 1828 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("-128"), -128);
|
1802 | 1829 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("127"), 127);
|
1803 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("128"), 0); |
| 1830 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<int8_t>("128"), 127); |
1804 | 1831 |
|
1805 | 1832 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("-1"), 0U);
|
1806 | 1833 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("0"), 0U);
|
1807 | 1834 | BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("255"), 255U);
|
1808 |
| - BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("256"), 0U); |
| 1835 | + BOOST_CHECK_EQUAL(LocaleIndependentAtoi<uint8_t>("256"), 255U); |
1809 | 1836 | }
|
1810 | 1837 |
|
1811 | 1838 | BOOST_AUTO_TEST_CASE(test_ParseInt64)
|
|
0 commit comments