1 /* 2 This file is part of solidity. 3 4 solidity is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 9 solidity is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with solidity. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 // SPDX-License-Identifier: GPL-3.0 18 /** 19 * Unit tests for LEB128. 20 */ 21 22 #include <libsolutil/LEB128.h> 23 24 #include <boost/test/unit_test.hpp> 25 26 using namespace std; 27 28 namespace solidity::util::test 29 { 30 31 BOOST_AUTO_TEST_SUITE(LEB128Test) 32 BOOST_AUTO_TEST_CASE(encode_unsigned)33BOOST_AUTO_TEST_CASE(encode_unsigned) 34 { 35 bytes zero = solidity::util::lebEncode(0); 36 BOOST_REQUIRE(zero.size() == 1); 37 BOOST_REQUIRE(zero[0] == 0x00); 38 39 bytes one = solidity::util::lebEncode(1); 40 BOOST_REQUIRE(one.size() == 1); 41 BOOST_REQUIRE(one[0] == 0x01); 42 43 bytes large = solidity::util::lebEncode(624485); 44 BOOST_REQUIRE(large.size() == 3); 45 BOOST_REQUIRE(large[0] == 0xE5); 46 BOOST_REQUIRE(large[1] == 0x8E); 47 BOOST_REQUIRE(large[2] == 0x26); 48 49 bytes larger = solidity::util::lebEncodeSigned(123456123456); 50 BOOST_REQUIRE(larger.size() == 6); 51 BOOST_REQUIRE(larger[0] == 0xC0); 52 BOOST_REQUIRE(larger[1] == 0xE4); 53 BOOST_REQUIRE(larger[2] == 0xBB); 54 BOOST_REQUIRE(larger[3] == 0xF4); 55 BOOST_REQUIRE(larger[4] == 0xCB); 56 BOOST_REQUIRE(larger[5] == 0x03); 57 } 58 BOOST_AUTO_TEST_CASE(encode_signed)59BOOST_AUTO_TEST_CASE(encode_signed) 60 { 61 bytes zero = solidity::util::lebEncodeSigned(0); 62 BOOST_REQUIRE(zero.size() == 1); 63 BOOST_REQUIRE(zero[0] == 0x00); 64 65 bytes one = solidity::util::lebEncodeSigned(1); 66 BOOST_REQUIRE(one.size() == 1); 67 BOOST_REQUIRE(one[0] == 0x01); 68 69 bytes negative_one = solidity::util::lebEncodeSigned(-1); 70 BOOST_REQUIRE(negative_one.size() == 1); 71 BOOST_REQUIRE(negative_one[0] == 0x7f); 72 73 bytes negative_two = solidity::util::lebEncodeSigned(-2); 74 BOOST_REQUIRE(negative_two.size() == 1); 75 BOOST_REQUIRE(negative_two[0] == 0x7e); 76 77 bytes large = solidity::util::lebEncodeSigned(624485); 78 BOOST_REQUIRE(large.size() == 3); 79 BOOST_REQUIRE(large[0] == 0xE5); 80 BOOST_REQUIRE(large[1] == 0x8E); 81 BOOST_REQUIRE(large[2] == 0x26); 82 83 bytes negative_large = solidity::util::lebEncodeSigned(-123456); 84 BOOST_REQUIRE(negative_large.size() == 3); 85 BOOST_REQUIRE(negative_large[0] == 0xC0); 86 BOOST_REQUIRE(negative_large[1] == 0xBB); 87 BOOST_REQUIRE(negative_large[2] == 0x78); 88 89 bytes negative_larger = solidity::util::lebEncodeSigned(-123456123456); 90 BOOST_REQUIRE(negative_larger.size() == 6); 91 BOOST_REQUIRE(negative_larger[0] == 0xC0); 92 BOOST_REQUIRE(negative_larger[1] == 0x9B); 93 BOOST_REQUIRE(negative_larger[2] == 0xC4); 94 BOOST_REQUIRE(negative_larger[3] == 0x8B); 95 BOOST_REQUIRE(negative_larger[4] == 0xB4); 96 BOOST_REQUIRE(negative_larger[5] == 0x7C); 97 } 98 99 BOOST_AUTO_TEST_SUITE_END() 100 101 } 102