diff --git a/src/script/script.cpp b/src/script/script.cpp --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -274,23 +274,24 @@ } // Check for a negative bit - uint8_t msb = data[0] & 0x80; - bool start = false; + uint8_t neg = data[0] & 0x80; + bool pushedRes = false; for (size_t i = 0; i < data.size(); ++i) { uint8_t x = data[i]; if (i == 0 && (x & 0x7f) == 0) { continue; } - if (!start && x == 0) { + if (!pushedRes && x == 0) { continue; } - start = true; + pushedRes = true; res.push_back(x); } - if (res.size() != 0) { - res[0] |= msb; + if (res.size() == 0) { + res.push_back(0); } + res[0] |= neg; return res; } diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -415,19 +415,53 @@ } // namespace BOOST_AUTO_TEST_CASE(minimize_big_endian_test) { - std::vector<uint8_t> input( - {{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01}}); - std::vector<uint8_t> negInput( - {{0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01}}); + // Empty array case + BOOST_CHECK(MinimalizeBigEndianArray(std::vector<uint8_t>()) == std::vector<uint8_t>()); + + // Zero arrays of various lengths + std::vector<uint8_t> zeroArray; + std::vector<uint8_t> negZeroArray({{0x80}}); + for (int i = 0; i < 16; i++) { + zeroArray.push_back(0x00); + if (i > 0) { + negZeroArray.insert(negZeroArray.begin(), 0x00); + } + + BOOST_CHECK(MinimalizeBigEndianArray(zeroArray) == + std::vector<uint8_t>({{0x00}})); + BOOST_CHECK(MinimalizeBigEndianArray(negZeroArray) == + std::vector<uint8_t>({{0x80}})); + } + + // Shouldn't minimalize these arrays to negative numbers + std::vector<uint8_t> notNegArray({{0x05, 0x80, 0x00}}); + std::vector<uint8_t> notNegArrayRightPadded({{0x80, 0x00}}); + for (int i = 0; i < 16; i++) { + notNegArray.insert(notNegArray.begin() + 1, 0x00); + notNegArrayRightPadded.insert(notNegArrayRightPadded.end(), 0x00); + BOOST_CHECK(MinimalizeBigEndianArray(notNegArray) == notNegArray); + BOOST_CHECK(MinimalizeBigEndianArray(notNegArrayRightPadded) == notNegArrayRightPadded); + } + + // Shouldn't minimalize these arrays at all + std::vector<uint8_t> noMinArray; + for (int i = 0; i < 14; i++) { + noMinArray.push_back(i); + BOOST_CHECK(MinimalizeBigEndianArray(noMinArray) == noMinArray); + } + - std::vector<uint8_t> check({0x01, 0x00, 0x00, 0x01}); - std::vector<uint8_t> negCheck({0x81, 0x00, 0x00, 0x01}); - std::vector<uint8_t> out = MinimalizeBigEndianArray(input); - std::vector<uint8_t> negOut = MinimalizeBigEndianArray(negInput); + // TODO check these for partial minimalization + // Long arrays get minimalized + BOOST_CHECK(MinimalizeBigEndianArray( + std::vector<uint8_t>({0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01})) == + std::vector<uint8_t>({0x01, 0x00, 0x00, 0x01})); - BOOST_CHECK(out == check); - BOOST_CHECK(negOut == negCheck); + // Long negative arrays get minimalized + BOOST_CHECK(MinimalizeBigEndianArray( + std::vector<uint8_t>({0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01})) == + std::vector<uint8_t>({0x81, 0x00, 0x00, 0x01})); } BOOST_AUTO_TEST_CASE(script_build) {