mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-14 16:04:03 +02:00
refactor: use _MiB consistently for Mebibyte conversions
Replace hard-coded MiB byte conversions (e.g. `1024*1024`, `1<<20`, `1048576`) with the existing `_MiB` literal to improve readability and avoid repeating constants.
In the few spots where arithmetic involves signed values, the result is identical to the previous code assuming those quantities never turn negative.
Also switch to brace init on every declaration assigned from `_MiB`/`_GiB` literals so a future oversized value (e.g. `unsigned int x{4096_MiB}`) becomes a compile error through the C++11 narrowing check instead of silently truncating.
Extend unit tests to cover the 32-bit `size_t` overflow boundary and to assert equivalence for integer and floating-point conversions.
Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz>
Co-authored-by: w0xlt <94266259+w0xlt@users.noreply.github.com>
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <test/util/random.h>
|
||||
#include <test/util/setup_common.h>
|
||||
#include <uint256.h>
|
||||
#include <util/byte_units.h>
|
||||
#include <util/string.h>
|
||||
|
||||
#include <memory>
|
||||
@@ -74,7 +75,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
|
||||
// Perform tests both obfuscated and non-obfuscated.
|
||||
for (bool obfuscate : {false, true}) {
|
||||
fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_1_obfuscate_true" : "dbwrapper_1_obfuscate_false");
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = false, .wipe_data = true, .obfuscate = obfuscate});
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = false, .wipe_data = true, .obfuscate = obfuscate});
|
||||
|
||||
uint256 res;
|
||||
uint32_t res_uint_32;
|
||||
@@ -155,7 +156,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
|
||||
// Perform tests both obfuscated and non-obfuscated.
|
||||
for (const bool obfuscate : {false, true}) {
|
||||
fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_batch_obfuscate_true" : "dbwrapper_batch_obfuscate_false");
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
|
||||
|
||||
uint8_t key{'i'};
|
||||
uint256 in = m_rng.rand256();
|
||||
@@ -191,7 +192,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
|
||||
// Perform tests both obfuscated and non-obfuscated.
|
||||
for (const bool obfuscate : {false, true}) {
|
||||
fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_iterator_obfuscate_true" : "dbwrapper_iterator_obfuscate_false");
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
|
||||
|
||||
// The two keys are intentionally chosen for ordering
|
||||
uint8_t key{'j'};
|
||||
@@ -307,7 +308,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
|
||||
BOOST_AUTO_TEST_CASE(iterator_ordering)
|
||||
{
|
||||
fs::path ph = m_args.GetDataDirBase() / "iterator_ordering";
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = false});
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = false});
|
||||
for (int x=0x00; x<256; ++x) {
|
||||
uint8_t key = x;
|
||||
uint32_t value = x*x;
|
||||
@@ -375,7 +376,7 @@ struct StringContentsSerializer {
|
||||
BOOST_AUTO_TEST_CASE(iterator_string_ordering)
|
||||
{
|
||||
fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering";
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = false});
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = false});
|
||||
for (int x = 0; x < 10; ++x) {
|
||||
for (int y = 0; y < 10; ++y) {
|
||||
std::string key{ToString(x)};
|
||||
@@ -417,7 +418,7 @@ BOOST_AUTO_TEST_CASE(unicodepath)
|
||||
// the ANSI CreateDirectoryA call and the code page isn't UTF8.
|
||||
// It will succeed if created with CreateDirectoryW.
|
||||
fs::path ph = m_args.GetDataDirBase() / "test_runner_₿_🏃_20191128_104644";
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20});
|
||||
CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB});
|
||||
|
||||
fs::path lockPath = ph / "LOCK";
|
||||
BOOST_CHECK(fs::exists(lockPath));
|
||||
|
||||
Reference in New Issue
Block a user