merge bitcoin#23185: Add ParseMoney and ParseScript tests

This commit is contained in:
Kittywhiskers Van Gogh 2024-09-30 15:14:33 +00:00
parent 7c03133be3
commit b35dc7236d
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
3 changed files with 69 additions and 1 deletions

View File

@ -150,8 +150,9 @@ BITCOIN_TESTS =\
test/script_p2sh_tests.cpp \ test/script_p2sh_tests.cpp \
test/script_p2pk_tests.cpp \ test/script_p2pk_tests.cpp \
test/script_p2pkh_tests.cpp \ test/script_p2pkh_tests.cpp \
test/script_tests.cpp \ test/script_parse_tests.cpp \
test/script_standard_tests.cpp \ test/script_standard_tests.cpp \
test/script_tests.cpp \
test/scriptnum_tests.cpp \ test/scriptnum_tests.cpp \
test/serfloat_tests.cpp \ test/serfloat_tests.cpp \
test/serialize_tests.cpp \ test/serialize_tests.cpp \

View File

@ -0,0 +1,55 @@
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <core_io.h>
#include <script/script.h>
#include <util/strencodings.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(script_parse_tests)
BOOST_AUTO_TEST_CASE(parse_script)
{
const std::vector<std::pair<std::string,std::string>> IN_OUT{
// {IN: script string , OUT: hex string }
{"", ""},
{"0", "00"},
{"1", "51"},
{"2", "52"},
{"3", "53"},
{"4", "54"},
{"5", "55"},
{"6", "56"},
{"7", "57"},
{"8", "58"},
{"9", "59"},
{"10", "5a"},
{"11", "5b"},
{"12", "5c"},
{"13", "5d"},
{"14", "5e"},
{"15", "5f"},
{"16", "60"},
{"17", "0111"},
{"-9", "0189"},
{"0x17", "17"},
{"'17'", "023137"},
{"ELSE", "67"},
{"NOP10", "b9"},
{"11111111111111111111", "00"},
};
std::string all_in;
std::string all_out;
for (const auto& [in, out] : IN_OUT) {
BOOST_CHECK_EQUAL(HexStr(ParseScript(in)), out);
all_in += " " + in + " ";
all_out += out;
}
BOOST_CHECK_EQUAL(HexStr(ParseScript(all_in)), all_out);
BOOST_CHECK_EXCEPTION(ParseScript("11111111111"), std::runtime_error, HasReason("script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF"));
BOOST_CHECK_EXCEPTION(ParseScript("OP_CHECKSIGADD"), std::runtime_error, HasReason("script parse error: unknown opcode"));
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -1318,6 +1318,11 @@ BOOST_AUTO_TEST_CASE(util_FormatMoney)
BOOST_AUTO_TEST_CASE(util_ParseMoney) BOOST_AUTO_TEST_CASE(util_ParseMoney)
{ {
BOOST_CHECK_EQUAL(ParseMoney("0.0").value(), 0); BOOST_CHECK_EQUAL(ParseMoney("0.0").value(), 0);
BOOST_CHECK_EQUAL(ParseMoney(".").value(), 0);
BOOST_CHECK_EQUAL(ParseMoney("0.").value(), 0);
BOOST_CHECK_EQUAL(ParseMoney(".0").value(), 0);
BOOST_CHECK_EQUAL(ParseMoney(".6789").value(), 6789'0000);
BOOST_CHECK_EQUAL(ParseMoney("12345.").value(), COIN * 12345);
BOOST_CHECK_EQUAL(ParseMoney("12345.6789").value(), (COIN/10000)*123456789); BOOST_CHECK_EQUAL(ParseMoney("12345.6789").value(), (COIN/10000)*123456789);
@ -1355,11 +1360,18 @@ BOOST_AUTO_TEST_CASE(util_ParseMoney)
BOOST_CHECK(!ParseMoney(" ")); BOOST_CHECK(!ParseMoney(" "));
// Parsing two numbers should fail // Parsing two numbers should fail
BOOST_CHECK(!ParseMoney(".."));
BOOST_CHECK(!ParseMoney("0..0"));
BOOST_CHECK(!ParseMoney("1 2")); BOOST_CHECK(!ParseMoney("1 2"));
BOOST_CHECK(!ParseMoney(" 1 2 ")); BOOST_CHECK(!ParseMoney(" 1 2 "));
BOOST_CHECK(!ParseMoney(" 1.2 3 ")); BOOST_CHECK(!ParseMoney(" 1.2 3 "));
BOOST_CHECK(!ParseMoney(" 1 2.3 ")); BOOST_CHECK(!ParseMoney(" 1 2.3 "));
// Embedded whitespace should fail
BOOST_CHECK(!ParseMoney(" -1 .2 "));
BOOST_CHECK(!ParseMoney(" 1 .2 "));
BOOST_CHECK(!ParseMoney(" +1 .2 "));
// Attempted 63 bit overflow should fail // Attempted 63 bit overflow should fail
BOOST_CHECK(!ParseMoney("92233720368.54775808")); BOOST_CHECK(!ParseMoney("92233720368.54775808"));