test: Unroll && conditions in macros

Using `&&` in `BOOST_CHECK` is problematic as failures will not indicate
which condition failed. By unrolling these checks, the user knows
exactly which expression is the failing case.

As an example, here is a line that would be particularly hard to debug
if it failed:

```
src/test/net_tests.cpp

BOOST_CHECK((*ret)[1] && (*ret)[1]->m_type == "headers" && std::ranges::equal((*ret)[1]->m_recv, MakeByteSpan(msg_data_2)));
```

If any one of these conditions fail, the whole expression fails, with no
values printed or indication as to which condition failed.

This is also required when using test macros that support value
decomposition, which requires `&&` and `||` are `delete`. Examples
include `BOOST_TEST`, doctest, Catch2, etc.

ref: https://catch2-temp.readthedocs.io/en/latest/assertions.html#other-limitations
ref: https://fekir.info/post/decomposing-an-expression/
This commit is contained in:
rustaceanrob
2026-06-06 09:44:33 +01:00
parent 70d9ec7f3d
commit e8691056c0
19 changed files with 196 additions and 95 deletions

View File

@@ -21,7 +21,8 @@ std::list<CoinsCachePair> CreatePairs(CoinsCachePair& sentinel)
auto node{std::prev(nodes.end())};
CCoinsCacheEntry::SetDirty(*node, sentinel);
BOOST_CHECK(node->second.IsDirty() && !node->second.IsFresh());
BOOST_CHECK(node->second.IsDirty());
BOOST_CHECK(!node->second.IsFresh());
BOOST_CHECK_EQUAL(node->second.Next(), &sentinel);
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &(*node));
@@ -63,7 +64,8 @@ BOOST_AUTO_TEST_CASE(linked_list_iteration)
// Delete the nodes from the list to make sure there are no dangling pointers
for (auto it{nodes.begin()}; it != nodes.end(); it = nodes.erase(it)) {
BOOST_CHECK(!it->second.IsDirty() && !it->second.IsFresh());
BOOST_CHECK(!it->second.IsDirty());
BOOST_CHECK(!it->second.IsFresh());
}
}
@@ -105,9 +107,11 @@ BOOST_AUTO_TEST_CASE(linked_list_random_deletion)
nodes.erase(n2);
// Check that n1 now points to n3, and n3 still points to n4
// Also check that state was not altered
BOOST_CHECK(n1->second.IsDirty() && !n1->second.IsFresh());
BOOST_CHECK(n1->second.IsDirty());
BOOST_CHECK(!n1->second.IsFresh());
BOOST_CHECK_EQUAL(n1->second.Next(), &(*n3));
BOOST_CHECK(n3->second.IsDirty() && !n3->second.IsFresh());
BOOST_CHECK(n3->second.IsDirty());
BOOST_CHECK(!n3->second.IsFresh());
BOOST_CHECK_EQUAL(n3->second.Next(), &(*n4));
BOOST_CHECK_EQUAL(n3->second.Prev(), &(*n1));
@@ -116,7 +120,8 @@ BOOST_AUTO_TEST_CASE(linked_list_random_deletion)
nodes.erase(n1);
// Check that sentinel now points to n3, and n3 still points to n4
// Also check that state was not altered
BOOST_CHECK(n3->second.IsDirty() && !n3->second.IsFresh());
BOOST_CHECK(n3->second.IsDirty());
BOOST_CHECK(!n3->second.IsFresh());
BOOST_CHECK_EQUAL(sentinel.second.Next(), &(*n3));
BOOST_CHECK_EQUAL(n3->second.Next(), &(*n4));
BOOST_CHECK_EQUAL(n3->second.Prev(), &sentinel);
@@ -126,7 +131,8 @@ BOOST_AUTO_TEST_CASE(linked_list_random_deletion)
nodes.erase(n4);
// Check that sentinel still points to n3, and n3 points to sentinel
// Also check that state was not altered
BOOST_CHECK(n3->second.IsDirty() && !n3->second.IsFresh());
BOOST_CHECK(n3->second.IsDirty());
BOOST_CHECK(!n3->second.IsFresh());
BOOST_CHECK_EQUAL(sentinel.second.Next(), &(*n3));
BOOST_CHECK_EQUAL(n3->second.Next(), &sentinel);
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &(*n3));
@@ -148,7 +154,8 @@ BOOST_AUTO_TEST_CASE(linked_list_set_state)
// Check that setting DIRTY inserts it into linked list and sets state
CCoinsCacheEntry::SetDirty(n1, sentinel);
BOOST_CHECK(n1.second.IsDirty() && !n1.second.IsFresh());
BOOST_CHECK(n1.second.IsDirty());
BOOST_CHECK(!n1.second.IsFresh());
BOOST_CHECK_EQUAL(n1.second.Next(), &sentinel);
BOOST_CHECK_EQUAL(n1.second.Prev(), &sentinel);
BOOST_CHECK_EQUAL(sentinel.second.Next(), &n1);
@@ -156,7 +163,8 @@ BOOST_AUTO_TEST_CASE(linked_list_set_state)
// Check that setting FRESH on new node inserts it after n1
CCoinsCacheEntry::SetFresh(n2, sentinel);
BOOST_CHECK(n2.second.IsFresh() && !n2.second.IsDirty());
BOOST_CHECK(n2.second.IsFresh());
BOOST_CHECK(!n2.second.IsDirty());
BOOST_CHECK_EQUAL(n2.second.Next(), &sentinel);
BOOST_CHECK_EQUAL(n2.second.Prev(), &n1);
BOOST_CHECK_EQUAL(n1.second.Next(), &n2);
@@ -164,7 +172,8 @@ BOOST_AUTO_TEST_CASE(linked_list_set_state)
// Check that we can set extra state, but they don't change our position
CCoinsCacheEntry::SetFresh(n1, sentinel);
BOOST_CHECK(n1.second.IsDirty() && n1.second.IsFresh());
BOOST_CHECK(n1.second.IsDirty());
BOOST_CHECK(n1.second.IsFresh());
BOOST_CHECK_EQUAL(n1.second.Next(), &n2);
BOOST_CHECK_EQUAL(n1.second.Prev(), &sentinel);
BOOST_CHECK_EQUAL(sentinel.second.Next(), &n1);
@@ -172,7 +181,8 @@ BOOST_AUTO_TEST_CASE(linked_list_set_state)
// Check that we can clear state then re-set it
n1.second.SetClean();
BOOST_CHECK(!n1.second.IsDirty() && !n1.second.IsFresh());
BOOST_CHECK(!n1.second.IsDirty());
BOOST_CHECK(!n1.second.IsFresh());
BOOST_CHECK_EQUAL(sentinel.second.Next(), &n2);
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &n2);
BOOST_CHECK_EQUAL(n2.second.Next(), &sentinel);
@@ -180,7 +190,8 @@ BOOST_AUTO_TEST_CASE(linked_list_set_state)
// Calling `SetClean` a second time has no effect
n1.second.SetClean();
BOOST_CHECK(!n1.second.IsDirty() && !n1.second.IsFresh());
BOOST_CHECK(!n1.second.IsDirty());
BOOST_CHECK(!n1.second.IsFresh());
BOOST_CHECK_EQUAL(sentinel.second.Next(), &n2);
BOOST_CHECK_EQUAL(sentinel.second.Prev(), &n2);
BOOST_CHECK_EQUAL(n2.second.Next(), &sentinel);
@@ -188,7 +199,8 @@ BOOST_AUTO_TEST_CASE(linked_list_set_state)
// Adding DIRTY re-inserts it after n2
CCoinsCacheEntry::SetDirty(n1, sentinel);
BOOST_CHECK(n1.second.IsDirty() && !n1.second.IsFresh());
BOOST_CHECK(n1.second.IsDirty());
BOOST_CHECK(!n1.second.IsFresh());
BOOST_CHECK_EQUAL(n2.second.Next(), &n1);
BOOST_CHECK_EQUAL(n1.second.Prev(), &n2);
BOOST_CHECK_EQUAL(n1.second.Next(), &sentinel);