diff --git a/evmc b/evmc index 5915690317..649c66176e 160000 --- a/evmc +++ b/evmc @@ -1 +1 @@ -Subproject commit 5915690317989b02fd73d5aa338e4edfa120a452 +Subproject commit 649c66176ef5aa886441447c4bfee11600d8a512 diff --git a/test/unittests/utils_test.cpp b/test/unittests/utils_test.cpp index 4a478c9495..e137ee2b96 100644 --- a/test/unittests/utils_test.cpp +++ b/test/unittests/utils_test.cpp @@ -5,40 +5,6 @@ #include #include -TEST(utils, hex) -{ - auto data = bytes{0x0, 0x1, 0xa, 0xf, 0x1f, 0xa0, 0xff, 0xf0}; - EXPECT_EQ(hex(data), "00010a0f1fa0fff0"); -} - -TEST(utils, from_hex_empty) -{ - EXPECT_TRUE(from_hex({}).empty()); -} - -TEST(utils, from_hex_odd_input_length) -{ - EXPECT_THROW(from_hex("0"), std::length_error); -} - -TEST(utils, from_hex_capital_letters) -{ - EXPECT_EQ(from_hex("ABCDEF"), (bytes{0xab, 0xcd, 0xef})); -} - -TEST(utils, from_hex_invalid_encoding) -{ - EXPECT_THROW(from_hex({"\0\0", 2}), std::out_of_range); -} - -TEST(utils, hex_byte) -{ - auto b = uint8_t{}; - EXPECT_EQ(hex(b), "00"); - b = 0xaa; - EXPECT_EQ(hex(b), "aa"); -} - TEST(utils, from_hexx) { EXPECT_EQ(hex(from_hexx("")), ""); diff --git a/test/utils/CMakeLists.txt b/test/utils/CMakeLists.txt index 78016c22f4..303cedf499 100644 --- a/test/utils/CMakeLists.txt +++ b/test/utils/CMakeLists.txt @@ -10,7 +10,7 @@ add_library(testutils STATIC utils.hpp ) -target_link_libraries(testutils PRIVATE evmc::instructions) +target_link_libraries(testutils PRIVATE evmc::instructions evmc::hex) target_include_directories(testutils PUBLIC ${PROJECT_SOURCE_DIR}) add_library(testutils-dump STATIC dump.cpp dump.hpp) diff --git a/test/utils/utils.cpp b/test/utils/utils.cpp index a3a7ea603f..9584c65f52 100644 --- a/test/utils/utils.cpp +++ b/test/utils/utils.cpp @@ -2,47 +2,8 @@ // Copyright 2018-2019 The evmone Authors. // SPDX-License-Identifier: Apache-2.0 -#include +#include "utils.hpp" #include -#include - -bytes from_hex(std::string_view hex) -{ - if (hex.length() % 2 == 1) - throw std::length_error{"the length of the input is odd"}; - - bytes bs; - bs.reserve(hex.length() / 2); - int b = 0; - for (size_t i = 0; i < hex.size(); ++i) - { - const auto h = hex[i]; - int v; - if (h >= '0' && h <= '9') - v = h - '0'; - else if (h >= 'a' && h <= 'f') - v = h - 'a' + 10; - else if (h >= 'A' && h <= 'F') - v = h - 'A' + 10; - else - throw std::out_of_range{"not a hex digit"}; - - if (i % 2 == 0) - b = v << 4; - else - bs.push_back(static_cast(b | v)); - } - return bs; -} - -std::string hex(bytes_view bs) -{ - std::string str; - str.reserve(bs.size() * 2); - for (auto b : bs) - str += hex(b); - return str; -} bytes from_hexx(const std::string& hexx) { diff --git a/test/utils/utils.hpp b/test/utils/utils.hpp index 5f664b516a..8cfb3d65e4 100644 --- a/test/utils/utils.hpp +++ b/test/utils/utils.hpp @@ -3,28 +3,23 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once -#include -#include +#include -using bytes = std::basic_string; +using evmc::bytes; +using evmc::hex; using bytes_view = std::basic_string_view; -/// Encode a byte to a hex string. -inline std::string hex(uint8_t b) noexcept +/// Decodes hex encoded string to bytes. +inline bytes from_hex(std::string_view hex) { - static constexpr auto hex_chars = "0123456789abcdef"; - return {hex_chars[b >> 4], hex_chars[b & 0xf]}; + return evmc::from_hex(std::string{hex}); } -/// Decodes hex encoded string to bytes. -/// -/// Exceptions: -/// - std::length_error when the input has invalid length (must be even). -/// - std::out_of_range when invalid hex digit encountered. -bytes from_hex(std::string_view hex); - /// Encodes bytes as hex string. -std::string hex(bytes_view bs); +inline std::string hex(bytes_view bs) +{ + return evmc::hex(bs.data(), bs.size()); +} /// Decodes the hexx encoded string. ///