From 4343f114cc661cf031ec915538c11b9b030e2e15 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Thu, 30 Sep 2021 14:18:50 +0000 Subject: =?UTF-8?q?Replace=20use=20of=20locale=20dependent=20atoi(?= =?UTF-8?q?=E2=80=A6)=20with=20locale-independent=20std::from=5Fchars(?= =?UTF-8?q?=E2=80=A6)=20(C++17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test: Add test cases for LocaleIndependentAtoi fuzz: Assert legacy atoi(s) == LocaleIndependentAtoi(s) fuzz: Assert legacy atoi64(s) == LocaleIndependentAtoi(s) --- src/test/util_tests.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'src/test/util_tests.cpp') diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index a13700d733..5d1ad2ebf6 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1549,6 +1549,77 @@ BOOST_AUTO_TEST_CASE(test_ToIntegral) BOOST_CHECK(!ToIntegral("256")); } +BOOST_AUTO_TEST_CASE(test_LocaleIndependentAtoi) +{ + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("1234"), 1'234); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("0"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("01234"), 1'234); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-1234"), -1'234); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi(" 1"), 1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("1 "), 1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("1a"), 1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("1.1"), 1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("1.9"), 1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("+01.9"), 1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-1"), -1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi(" -1"), -1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-1 "), -1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi(" -1 "), -1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("+1"), 1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi(" +1"), 1); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi(" +1 "), 1); + + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("+-1"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-+1"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("++1"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("--1"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi(""), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("aap"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("0x1"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-32482348723847471234"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("32482348723847471234"), 0); + + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-9223372036854775809"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-9223372036854775808"), -9'223'372'036'854'775'807LL - 1LL); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("9223372036854775807"), 9'223'372'036'854'775'807); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("9223372036854775808"), 0); + + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-1"), 0U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("0"), 0U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("18446744073709551615"), 18'446'744'073'709'551'615ULL); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("18446744073709551616"), 0U); + + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-2147483649"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-2147483648"), -2'147'483'648LL); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("2147483647"), 2'147'483'647); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("2147483648"), 0); + + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-1"), 0U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("0"), 0U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("4294967295"), 4'294'967'295U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("4294967296"), 0U); + + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-32769"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-32768"), -32'768); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("32767"), 32'767); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("32768"), 0); + + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-1"), 0U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("0"), 0U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("65535"), 65'535U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("65536"), 0U); + + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-129"), 0); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-128"), -128); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("127"), 127); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("128"), 0); + + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("-1"), 0U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("0"), 0U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("255"), 255U); + BOOST_CHECK_EQUAL(LocaleIndependentAtoi("256"), 0U); +} + BOOST_AUTO_TEST_CASE(test_ParseInt64) { int64_t n; -- cgit v1.2.3