diff options
author | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2024-09-03 15:33:08 +0200 |
---|---|---|
committer | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2024-09-05 10:57:19 +0200 |
commit | faecca9a85c1c1917d024f55cca34d19cc94f3b9 (patch) | |
tree | 2c40587ad90ba98ce784e33a8873ef3278e7373e /src | |
parent | fac973647d69dd14089cee9972e8dfa0074c8a97 (diff) |
test: Use span for raw data
This change allows to drop brittle sizeof calls in favor of the
std::span::size method.
Other improvements include:
* Use of a namespace to mark test and bench data
* Use of the modern std::byte
* Drop of a no longer used std::vector copy and the bench/data module
Diffstat (limited to 'src')
-rw-r--r-- | src/bench/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/bench/checkblock.cpp | 2 | ||||
-rw-r--r-- | src/bench/data.cpp | 16 | ||||
-rw-r--r-- | src/bench/data.h | 19 | ||||
-rw-r--r-- | src/bench/load_external.cpp | 2 | ||||
-rw-r--r-- | src/bench/readblock.cpp | 2 | ||||
-rw-r--r-- | src/bench/rpc_blockchain.cpp | 2 | ||||
-rw-r--r-- | src/bench/strencodings.cpp | 2 | ||||
-rw-r--r-- | src/test/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/test/addrman_tests.cpp | 11 |
10 files changed, 13 insertions, 48 deletions
diff --git a/src/bench/CMakeLists.txt b/src/bench/CMakeLists.txt index 61a1126904..8a52980e07 100644 --- a/src/bench/CMakeLists.txt +++ b/src/bench/CMakeLists.txt @@ -3,12 +3,11 @@ # file COPYING or https://opensource.org/license/mit/. include(GenerateHeaders) -generate_header_from_raw(data/block413567.raw) +generate_header_from_raw(data/block413567.raw benchmark::data) add_executable(bench_bitcoin bench_bitcoin.cpp bench.cpp - data.cpp nanobench.cpp ${CMAKE_CURRENT_BINARY_DIR}/data/block413567.raw.h # Benchmarks: diff --git a/src/bench/checkblock.cpp b/src/bench/checkblock.cpp index 580265fc52..9558d64f19 100644 --- a/src/bench/checkblock.cpp +++ b/src/bench/checkblock.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> -#include <bench/data.h> +#include <bench/data/block413567.raw.h> #include <chainparams.h> #include <common/args.h> #include <consensus/validation.h> diff --git a/src/bench/data.cpp b/src/bench/data.cpp deleted file mode 100644 index 8c5bb13f75..0000000000 --- a/src/bench/data.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) 2019-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 <bench/data.h> - -#include <iterator> - -namespace benchmark { -namespace data { - -#include <bench/data/block413567.raw.h> -const std::vector<uint8_t> block413567{std::begin(block413567_raw), std::end(block413567_raw)}; - -} // namespace data -} // namespace benchmark diff --git a/src/bench/data.h b/src/bench/data.h deleted file mode 100644 index 5f13d766ea..0000000000 --- a/src/bench/data.h +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) 2019 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_BENCH_DATA_H -#define BITCOIN_BENCH_DATA_H - -#include <cstdint> -#include <vector> - -namespace benchmark { -namespace data { - -extern const std::vector<uint8_t> block413567; - -} // namespace data -} // namespace benchmark - -#endif // BITCOIN_BENCH_DATA_H diff --git a/src/bench/load_external.cpp b/src/bench/load_external.cpp index 2ed5a3979d..8f9399c60d 100644 --- a/src/bench/load_external.cpp +++ b/src/bench/load_external.cpp @@ -3,7 +3,7 @@ // file COPYING or https://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> -#include <bench/data.h> +#include <bench/data/block413567.raw.h> #include <chainparams.h> #include <flatfile.h> #include <node/blockstorage.h> diff --git a/src/bench/readblock.cpp b/src/bench/readblock.cpp index 0b88663db6..058d953b4e 100644 --- a/src/bench/readblock.cpp +++ b/src/bench/readblock.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> -#include <bench/data.h> +#include <bench/data/block413567.raw.h> #include <flatfile.h> #include <node/blockstorage.h> #include <primitives/block.h> diff --git a/src/bench/rpc_blockchain.cpp b/src/bench/rpc_blockchain.cpp index 54356598e7..7e3e2d8e48 100644 --- a/src/bench/rpc_blockchain.cpp +++ b/src/bench/rpc_blockchain.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> -#include <bench/data.h> +#include <bench/data/block413567.raw.h> #include <chain.h> #include <core_io.h> #include <primitives/block.h> diff --git a/src/bench/strencodings.cpp b/src/bench/strencodings.cpp index 72eb6b442b..dd5829caf2 100644 --- a/src/bench/strencodings.cpp +++ b/src/bench/strencodings.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <bench/bench.h> -#include <bench/data.h> +#include <bench/data/block413567.raw.h> #include <span.h> #include <util/strencodings.h> diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index a666a76f8f..4e24b87e9b 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -12,7 +12,7 @@ generate_header_from_json(data/script_tests.json) generate_header_from_json(data/sighash.json) generate_header_from_json(data/tx_invalid.json) generate_header_from_json(data/tx_valid.json) -generate_header_from_raw(data/asmap.raw) +generate_header_from_raw(data/asmap.raw test::data) # Do not use generator expressions in test sources because the # SOURCES property is processed to gather test suite macros. diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index e5d25637bd..da6ff77924 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -47,11 +47,12 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0) } -static std::vector<bool> FromBytes(const unsigned char* source, int vector_size) +static std::vector<bool> FromBytes(std::span<const std::byte> source) { + int vector_size(source.size() * 8); std::vector<bool> result(vector_size); for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) { - unsigned char cur_byte = source[byte_i]; + uint8_t cur_byte{std::to_integer<uint8_t>(source[byte_i])}; for (int bit_i = 0; bit_i < 8; ++bit_i) { result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1; } @@ -576,7 +577,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy) // 101.8.0.0/16 AS8 BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) { - std::vector<bool> asmap = FromBytes(asmap_raw, sizeof(asmap_raw) * 8); + std::vector<bool> asmap = FromBytes(test::data::asmap); NetGroupManager ngm_asmap{asmap}; CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE); @@ -630,7 +631,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket) BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) { - std::vector<bool> asmap = FromBytes(asmap_raw, sizeof(asmap_raw) * 8); + std::vector<bool> asmap = FromBytes(test::data::asmap); NetGroupManager ngm_asmap{asmap}; CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE); @@ -708,7 +709,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket) BOOST_AUTO_TEST_CASE(addrman_serialization) { - std::vector<bool> asmap1 = FromBytes(asmap_raw, sizeof(asmap_raw) * 8); + std::vector<bool> asmap1 = FromBytes(test::data::asmap); NetGroupManager netgroupman{asmap1}; const auto ratio = GetCheckRatio(m_node); |