Repository: bitcoin/bitcoin
File: src/test/addrman_tests.cpp (C++)

Code before:
static NetGroupManager EMPTY_NETGROUPMAN{std::vector<bool>()};
static const bool DETERMINISTIC{true};

static int32_t GetCheckRatio(const NodeContext& node_ctx)
{
    return std::clamp<int32_t>(node_ctx.args->GetIntArg("-checkaddrman", 100), 0, 1000000);
}

static CNetAddr ResolveIP(const std::string& ip)
{
    const std::optional<CNetAddr> addr{LookupHost(ip, false)};
    BOOST_CHECK_MESSAGE(addr.has_value(), strprintf("failed to resolve: %s", ip));
    return addr.value_or(CNetAddr{});
}

static CService ResolveService(const std::string& ip, uint16_t port = 0)
{
    const std::optional<CService> serv{Lookup(ip, port, false)};
    BOOST_CHECK_MESSAGE(serv.has_value(), strprintf("failed to resolve: %s:%i", ip, port));
    return serv.value_or(CService{});
}


static std::vector<bool> FromBytes(const unsigned char* source, int vector_size)
{
    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];
        for (int bit_i = 0; bit_i < 8; ++bit_i) {
            result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
        }
    }
    return result;
}

BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(addrman_simple)
{
    auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));

    CNetAddr source = ResolveIP("252.2.2.2");

    // Test: Does Addrman respond correctly when empty.
    BOOST_CHECK_EQUAL(addrman->Size(), 0U);
    auto addr_null = addrman->Select().first;
    BOOST_CHECK_EQUAL(addr_null.ToStringAddrPort(), "[::]:0");

    // Test: Does Addrman::Add work as expected.
    CService addr1 = ResolveService("250.1.1.1", 8333);
    BOOST_CHECK(addrman->Add({CAddress(addr1, NODE_NONE)}, source));

Review comment (refactor):
nit: I think the code is more clear without this helper variable, avoids the first multiplying and then dividing by 8 again:

<details>
<summary>git diff on faecca9a85</summary>

```diff
diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp
index da6ff77924..fd7a1f2ba2 100644
--- a/src/test/addrman_tests.cpp
+++ b/src/test/addrman_tests.cpp
@@ -49,11 +49,10 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0)
 
 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) {
+    std::vector<bool> result(source.size() * 8);
+    for (size_t byte_i = 0; byte_i < source.size(); ++byte_i) {
         uint8_t cur_byte{std::to_integer<uint8_t>(source[byte_i])};
-        for (int bit_i = 0; bit_i < 8; ++bit_i) {
+        for (size_t bit_i = 0; bit_i < 8; ++bit_i) {
             result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
         }
     }

```
</details>


Code after:
static NetGroupManager EMPTY_NETGROUPMAN{std::vector<bool>()};
static const bool DETERMINISTIC{true};

static int32_t GetCheckRatio(const NodeContext& node_ctx)
{
    return std::clamp<int32_t>(node_ctx.args->GetIntArg("-checkaddrman", 100), 0, 1000000);
}

static CNetAddr ResolveIP(const std::string& ip)
{
    const std::optional<CNetAddr> addr{LookupHost(ip, false)};
    BOOST_CHECK_MESSAGE(addr.has_value(), strprintf("failed to resolve: %s", ip));
    return addr.value_or(CNetAddr{});
}

static CService ResolveService(const std::string& ip, uint16_t port = 0)
{
    const std::optional<CService> serv{Lookup(ip, port, false)};
    BOOST_CHECK_MESSAGE(serv.has_value(), strprintf("failed to resolve: %s:%i", ip, port));
    return serv.value_or(CService{});
}


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) {
        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;
        }
    }
    return result;
}

BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(addrman_simple)
{
    auto addrman = std::make_unique<AddrMan>(EMPTY_NETGROUPMAN, DETERMINISTIC, GetCheckRatio(m_node));

    CNetAddr source = ResolveIP("252.2.2.2");

    // Test: Does Addrman respond correctly when empty.
    BOOST_CHECK_EQUAL(addrman->Size(), 0U);
    auto addr_null = addrman->Select().first;
    BOOST_CHECK_EQUAL(addr_null.ToStringAddrPort(), "[::]:0");

    // Test: Does Addrman::Add work as expected.
    CService addr1 = ResolveService("250.1.1.1", 8333);