Skip to content

Commit 9275869

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23185: test: Add ParseMoney and ParseScript tests
fa1477e test: Add ParseMoney and ParseScript tests (MarcoFalke) Pull request description: Add missing tests ACKs for top commit: practicalswift: cr ACK fa1477e shaavan: tACK fa1477e Tree-SHA512: e57b4e8da4abe075b4ad7e7abd68c4d0eecf0c805acd2c72076aac4993d3ec5748fd02b721c4c110494db56fdbc199301e5cfd1dc0212f2002f355b47f70e539
2 parents 97e2435 + fa1477e commit 9275869

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/Makefile.test.include

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ BITCOIN_TESTS =\
119119
test/sanity_tests.cpp \
120120
test/scheduler_tests.cpp \
121121
test/script_p2sh_tests.cpp \
122-
test/script_tests.cpp \
122+
test/script_parse_tests.cpp \
123123
test/script_standard_tests.cpp \
124+
test/script_tests.cpp \
124125
test/scriptnum_tests.cpp \
125126
test/serfloat_tests.cpp \
126127
test/serialize_tests.cpp \

src/test/script_parse_tests.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <core_io.h>
6+
#include <script/script.h>
7+
#include <util/strencodings.h>
8+
#include <test/util/setup_common.h>
9+
10+
#include <boost/test/unit_test.hpp>
11+
12+
BOOST_AUTO_TEST_SUITE(script_parse_tests)
13+
BOOST_AUTO_TEST_CASE(parse_script)
14+
{
15+
const std::vector<std::pair<std::string,std::string>> IN_OUT{
16+
// {IN: script string , OUT: hex string }
17+
{"", ""},
18+
{"0", "00"},
19+
{"1", "51"},
20+
{"2", "52"},
21+
{"3", "53"},
22+
{"4", "54"},
23+
{"5", "55"},
24+
{"6", "56"},
25+
{"7", "57"},
26+
{"8", "58"},
27+
{"9", "59"},
28+
{"10", "5a"},
29+
{"11", "5b"},
30+
{"12", "5c"},
31+
{"13", "5d"},
32+
{"14", "5e"},
33+
{"15", "5f"},
34+
{"16", "60"},
35+
{"17", "0111"},
36+
{"-9", "0189"},
37+
{"0x17", "17"},
38+
{"'17'", "023137"},
39+
{"ELSE", "67"},
40+
{"NOP10", "b9"},
41+
{"11111111111111111111", "00"},
42+
};
43+
std::string all_in;
44+
std::string all_out;
45+
for (const auto& [in, out] : IN_OUT) {
46+
BOOST_CHECK_EQUAL(HexStr(ParseScript(in)), out);
47+
all_in += " " + in + " ";
48+
all_out += out;
49+
}
50+
BOOST_CHECK_EQUAL(HexStr(ParseScript(all_in)), all_out);
51+
52+
BOOST_CHECK_EXCEPTION(ParseScript("11111111111"), std::runtime_error, HasReason("script parse error: decimal numeric value only allowed in the range -0xFFFFFFFF...0xFFFFFFFF"));
53+
BOOST_CHECK_EXCEPTION(ParseScript("OP_CHECKSIGADD"), std::runtime_error, HasReason("script parse error: unknown opcode"));
54+
}
55+
BOOST_AUTO_TEST_SUITE_END()

src/test/util_tests.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,11 @@ BOOST_AUTO_TEST_CASE(util_FormatMoney)
12391239
BOOST_AUTO_TEST_CASE(util_ParseMoney)
12401240
{
12411241
BOOST_CHECK_EQUAL(ParseMoney("0.0").value(), 0);
1242+
BOOST_CHECK_EQUAL(ParseMoney(".").value(), 0);
1243+
BOOST_CHECK_EQUAL(ParseMoney("0.").value(), 0);
1244+
BOOST_CHECK_EQUAL(ParseMoney(".0").value(), 0);
1245+
BOOST_CHECK_EQUAL(ParseMoney(".6789").value(), 6789'0000);
1246+
BOOST_CHECK_EQUAL(ParseMoney("12345.").value(), COIN * 12345);
12421247

12431248
BOOST_CHECK_EQUAL(ParseMoney("12345.6789").value(), (COIN/10000)*123456789);
12441249

@@ -1276,11 +1281,18 @@ BOOST_AUTO_TEST_CASE(util_ParseMoney)
12761281
BOOST_CHECK(!ParseMoney(" "));
12771282

12781283
// Parsing two numbers should fail
1284+
BOOST_CHECK(!ParseMoney(".."));
1285+
BOOST_CHECK(!ParseMoney("0..0"));
12791286
BOOST_CHECK(!ParseMoney("1 2"));
12801287
BOOST_CHECK(!ParseMoney(" 1 2 "));
12811288
BOOST_CHECK(!ParseMoney(" 1.2 3 "));
12821289
BOOST_CHECK(!ParseMoney(" 1 2.3 "));
12831290

1291+
// Embedded whitespace should fail
1292+
BOOST_CHECK(!ParseMoney(" -1 .2 "));
1293+
BOOST_CHECK(!ParseMoney(" 1 .2 "));
1294+
BOOST_CHECK(!ParseMoney(" +1 .2 "));
1295+
12841296
// Attempted 63 bit overflow should fail
12851297
BOOST_CHECK(!ParseMoney("92233720368.54775808"));
12861298

0 commit comments

Comments
 (0)