mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 22:18:54 +01:00
util: Add ParseInt64 and ParseDouble functions
Strict parsing functions for other numeric types. - ParseInt64 analogous to ParseInt32, but for 64-bit values. - ParseDouble for doubles. - Make all three Parse* functions more strict (e.g. reject whitespace on the inside) Also add tests.
This commit is contained in:
committed by
Jonas Schnelli
parent
043df2b568
commit
7e98a3c642
@@ -322,9 +322,16 @@ BOOST_AUTO_TEST_CASE(test_ParseInt32)
|
||||
BOOST_CHECK(ParseInt32("-2147483648", &n) && n == -2147483648);
|
||||
BOOST_CHECK(ParseInt32("-1234", &n) && n == -1234);
|
||||
// Invalid values
|
||||
BOOST_CHECK(!ParseInt32("", &n));
|
||||
BOOST_CHECK(!ParseInt32(" 1", &n)); // no padding inside
|
||||
BOOST_CHECK(!ParseInt32("1 ", &n));
|
||||
BOOST_CHECK(!ParseInt32("1a", &n));
|
||||
BOOST_CHECK(!ParseInt32("aap", &n));
|
||||
BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
|
||||
BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
|
||||
const char test_bytes[] = {'1', 0, '1'};
|
||||
std::string teststr(test_bytes, sizeof(test_bytes));
|
||||
BOOST_CHECK(!ParseInt32(teststr, &n)); // no embedded NULs
|
||||
// Overflow and underflow
|
||||
BOOST_CHECK(!ParseInt32("-2147483649", NULL));
|
||||
BOOST_CHECK(!ParseInt32("2147483648", NULL));
|
||||
@@ -332,6 +339,64 @@ BOOST_AUTO_TEST_CASE(test_ParseInt32)
|
||||
BOOST_CHECK(!ParseInt32("32482348723847471234", NULL));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_ParseInt64)
|
||||
{
|
||||
int64_t n;
|
||||
// Valid values
|
||||
BOOST_CHECK(ParseInt64("1234", NULL));
|
||||
BOOST_CHECK(ParseInt64("0", &n) && n == 0LL);
|
||||
BOOST_CHECK(ParseInt64("1234", &n) && n == 1234LL);
|
||||
BOOST_CHECK(ParseInt64("01234", &n) && n == 1234LL); // no octal
|
||||
BOOST_CHECK(ParseInt64("2147483647", &n) && n == 2147483647LL);
|
||||
BOOST_CHECK(ParseInt64("-2147483648", &n) && n == -2147483648LL);
|
||||
BOOST_CHECK(ParseInt64("9223372036854775807", &n) && n == 9223372036854775807LL);
|
||||
BOOST_CHECK(ParseInt64("-9223372036854775808", &n) && n == 9223372036854775808LL);
|
||||
BOOST_CHECK(ParseInt64("-1234", &n) && n == -1234LL);
|
||||
// Invalid values
|
||||
BOOST_CHECK(!ParseInt64("", &n));
|
||||
BOOST_CHECK(!ParseInt64(" 1", &n)); // no padding inside
|
||||
BOOST_CHECK(!ParseInt64("1 ", &n));
|
||||
BOOST_CHECK(!ParseInt64("1a", &n));
|
||||
BOOST_CHECK(!ParseInt64("aap", &n));
|
||||
BOOST_CHECK(!ParseInt64("0x1", &n)); // no hex
|
||||
const char test_bytes[] = {'1', 0, '1'};
|
||||
std::string teststr(test_bytes, sizeof(test_bytes));
|
||||
BOOST_CHECK(!ParseInt64(teststr, &n)); // no embedded NULs
|
||||
// Overflow and underflow
|
||||
BOOST_CHECK(!ParseInt64("-9223372036854775809", NULL));
|
||||
BOOST_CHECK(!ParseInt64("9223372036854775808", NULL));
|
||||
BOOST_CHECK(!ParseInt64("-32482348723847471234", NULL));
|
||||
BOOST_CHECK(!ParseInt64("32482348723847471234", NULL));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_ParseDouble)
|
||||
{
|
||||
double n;
|
||||
// Valid values
|
||||
BOOST_CHECK(ParseDouble("1234", NULL));
|
||||
BOOST_CHECK(ParseDouble("0", &n) && n == 0.0);
|
||||
BOOST_CHECK(ParseDouble("1234", &n) && n == 1234.0);
|
||||
BOOST_CHECK(ParseDouble("01234", &n) && n == 1234.0); // no octal
|
||||
BOOST_CHECK(ParseDouble("2147483647", &n) && n == 2147483647.0);
|
||||
BOOST_CHECK(ParseDouble("-2147483648", &n) && n == -2147483648.0);
|
||||
BOOST_CHECK(ParseDouble("-1234", &n) && n == -1234.0);
|
||||
BOOST_CHECK(ParseDouble("1e6", &n) && n == 1e6);
|
||||
BOOST_CHECK(ParseDouble("-1e6", &n) && n == -1e6);
|
||||
// Invalid values
|
||||
BOOST_CHECK(!ParseDouble("", &n));
|
||||
BOOST_CHECK(!ParseDouble(" 1", &n)); // no padding inside
|
||||
BOOST_CHECK(!ParseDouble("1 ", &n));
|
||||
BOOST_CHECK(!ParseDouble("1a", &n));
|
||||
BOOST_CHECK(!ParseDouble("aap", &n));
|
||||
BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex
|
||||
const char test_bytes[] = {'1', 0, '1'};
|
||||
std::string teststr(test_bytes, sizeof(test_bytes));
|
||||
BOOST_CHECK(!ParseDouble(teststr, &n)); // no embedded NULs
|
||||
// Overflow and underflow
|
||||
BOOST_CHECK(!ParseDouble("-1e10000", NULL));
|
||||
BOOST_CHECK(!ParseDouble("1e10000", NULL));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_FormatParagraph)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");
|
||||
|
||||
Reference in New Issue
Block a user