From fb6fd1b3c25fd9c2bc9b8abb97fd150bc4ce219c Mon Sep 17 00:00:00 2001 From: Kevin Kelley Date: Thu, 22 Nov 2018 08:02:25 +0800 Subject: add a 'readable' format for large hex values --- test/libdevcore/CommonData.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 test/libdevcore/CommonData.cpp (limited to 'test/libdevcore/CommonData.cpp') diff --git a/test/libdevcore/CommonData.cpp b/test/libdevcore/CommonData.cpp new file mode 100644 index 00000000..2020ddb0 --- /dev/null +++ b/test/libdevcore/CommonData.cpp @@ -0,0 +1,87 @@ +/* + This file is part of solidity. + + solidity is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + solidity is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with solidity. If not, see . +*/ +/** + * Unit tests for the StringUtils routines. + */ + + +#include +#include +#include // for IntegerType + +#include + +using namespace std; +using namespace dev::solidity; + +namespace dev +{ +namespace test +{ + +BOOST_AUTO_TEST_SUITE(CommonData) + +BOOST_AUTO_TEST_CASE(test_to_hex) +{ + BOOST_CHECK_EQUAL(toHex(fromHex("FF"), 2, HexPrefix::DontAdd, HexCase::Lower), "ff"); +} + +BOOST_AUTO_TEST_CASE(test_format_number) +{ + BOOST_CHECK_EQUAL(formatNumber(u256(0x8000000)), "0x08000000"); + BOOST_CHECK_EQUAL(formatNumber(u256(0x80000000)), "0x80000000"); + BOOST_CHECK_EQUAL(formatNumber(u256(0x800000000)), "0x0800000000"); + BOOST_CHECK_EQUAL(formatNumber(u256(0x8000000000)), "0x8000000000"); + BOOST_CHECK_EQUAL(formatNumber(u256(0x80000000000)), "0x080000000000"); + + BOOST_CHECK_EQUAL(formatNumber(u256(0x7ffffff)), "0x07ffffff"); + BOOST_CHECK_EQUAL(formatNumber(u256(0x7fffffff)), "0x7fffffff"); + BOOST_CHECK_EQUAL(formatNumber(u256(0x7ffffffff)), "0x07ffffffff"); + BOOST_CHECK_EQUAL(formatNumber(u256(0x7fffffffff)), "0x7fffffffff"); + BOOST_CHECK_EQUAL(formatNumber(u256(0x7ffffffffff)), "0x07ffffffffff"); + + BOOST_CHECK_EQUAL(formatNumber(u256(0x88000000)), "0x88000000"); + BOOST_CHECK_EQUAL(formatNumber(u256(0x8888888888000000)), "0x8888888888000000"); + + u256 b = 0; + for (int i = 0; i < 32; i++) + { + b <<= 8; + b |= 0x55; + } + u256 c = u256(FixedHash<32>( + fromHex("0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789") + )); + u256 d = u256(0xAAAAaaaaAAAAaaaa) << 192 | + u256(0xFFFFffffFFFFffff) << 128 | + u256(0xFFFFffffFFFFffff) << 64 | + u256(0xFFFFffffFFFFffff); + BOOST_CHECK_EQUAL(formatNumber(b), "0x5555555555555555555555555555555555555555555555555555555555555555"); + BOOST_CHECK_EQUAL(formatNumber(c), "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"); + BOOST_CHECK_EQUAL(formatNumber(d), "0xaaaaaaaaaaaaaaaaffffffffffffffffffffffffffffffffffffffffffffffff"); + + BOOST_CHECK_EQUAL(formatNumber(IntegerType(256).minValue()), "0"); + BOOST_CHECK_EQUAL( + formatNumber(IntegerType(256).maxValue()), + "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + ); +} + +BOOST_AUTO_TEST_SUITE_END() + +} +} -- cgit v1.2.3 From bc6ddbdd09860542061baed8df993cb3dafaa406 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 5 Dec 2018 22:11:31 +0100 Subject: Remove `w` parameter for toHex. --- test/libdevcore/CommonData.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/libdevcore/CommonData.cpp') diff --git a/test/libdevcore/CommonData.cpp b/test/libdevcore/CommonData.cpp index 2020ddb0..8da937de 100644 --- a/test/libdevcore/CommonData.cpp +++ b/test/libdevcore/CommonData.cpp @@ -37,7 +37,7 @@ BOOST_AUTO_TEST_SUITE(CommonData) BOOST_AUTO_TEST_CASE(test_to_hex) { - BOOST_CHECK_EQUAL(toHex(fromHex("FF"), 2, HexPrefix::DontAdd, HexCase::Lower), "ff"); + BOOST_CHECK_EQUAL(toHex(fromHex("FF"), HexPrefix::DontAdd, HexCase::Lower), "ff"); } BOOST_AUTO_TEST_CASE(test_format_number) -- cgit v1.2.3