diff options
author | merge-script <fanquake@gmail.com> | 2024-08-28 10:34:47 +0100 |
---|---|---|
committer | merge-script <fanquake@gmail.com> | 2024-08-28 10:34:47 +0100 |
commit | 80f00cafdeef0600fa1a5e906686720786c2336c (patch) | |
tree | 49235a0adf446c488732ca433382c7b48600d2dd /src/test | |
parent | 128ade02e44547de22942a5fcc4f2d23b9ce777f (diff) | |
parent | fad0cf6f2619df8df435a2da6da49eeb5510a10f (diff) |
Merge bitcoin/bitcoin#29071: refactor: Remove Span operator==, Use std::ranges::equal
fad0cf6f2619df8df435a2da6da49eeb5510a10f refactor: Use std::ranges::equal in GetNetworkForMagic (MarcoFalke)
fadf0a7e15d66ba3230153e789b785e6cf8ab84c refactor: Remove Span operator==, Use std::ranges::equal (MarcoFalke)
Pull request description:
`std::span` removed the comparison operators, so it makes sense to remove them for the `Span` "backport" as well. Using `std::ranges::equal` also has the benefit that some `Span` temporary constructions can now be dropped.
This is required to move from `Span` toward `std::span`.
ACKs for top commit:
hodlinator:
Untested Code Review re-ACK fad0cf6
stickies-v:
ACK fad0cf6f2619df8df435a2da6da49eeb5510a10f
TheCharlatan:
ACK fad0cf6f2619df8df435a2da6da49eeb5510a10f
Tree-SHA512: 5b9d1826ceac2aabae2295bc89893dd23ac3a1cc0d41988331cdbdc21be531aa91795d5273819f349f79648c6c4f30ed31af6e7a3816153e92080061b92ffe00
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/base32_tests.cpp | 4 | ||||
-rw-r--r-- | src/test/base64_tests.cpp | 4 | ||||
-rw-r--r-- | src/test/bip324_tests.cpp | 15 | ||||
-rw-r--r-- | src/test/crypto_tests.cpp | 7 | ||||
-rw-r--r-- | src/test/fuzz/bip324.cpp | 7 | ||||
-rw-r--r-- | src/test/fuzz/hex.cpp | 3 | ||||
-rw-r--r-- | src/test/fuzz/miniscript.cpp | 4 | ||||
-rw-r--r-- | src/test/fuzz/p2p_transport_serialization.cpp | 7 | ||||
-rw-r--r-- | src/test/fuzz/poolresource.cpp | 2 | ||||
-rw-r--r-- | src/test/fuzz/span.cpp | 6 | ||||
-rw-r--r-- | src/test/key_io_tests.cpp | 5 | ||||
-rw-r--r-- | src/test/miniscript_tests.cpp | 11 | ||||
-rw-r--r-- | src/test/net_tests.cpp | 20 |
13 files changed, 51 insertions, 44 deletions
diff --git a/src/test/base32_tests.cpp b/src/test/base32_tests.cpp index 4617beecd9..be3b0c2d1f 100644 --- a/src/test/base32_tests.cpp +++ b/src/test/base32_tests.cpp @@ -5,6 +5,8 @@ #include <util/strencodings.h> #include <boost/test/unit_test.hpp> + +#include <algorithm> #include <string> using namespace std::literals; @@ -24,7 +26,7 @@ BOOST_AUTO_TEST_CASE(base32_testvectors) BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]); auto dec = DecodeBase32(vstrOut[i]); BOOST_REQUIRE(dec); - BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]); + BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]); } // Decoding strings with embedded NUL characters should fail diff --git a/src/test/base64_tests.cpp b/src/test/base64_tests.cpp index 6462aa82fb..b9d0d2b241 100644 --- a/src/test/base64_tests.cpp +++ b/src/test/base64_tests.cpp @@ -5,6 +5,8 @@ #include <util/strencodings.h> #include <boost/test/unit_test.hpp> + +#include <algorithm> #include <string> using namespace std::literals; @@ -21,7 +23,7 @@ BOOST_AUTO_TEST_CASE(base64_testvectors) BOOST_CHECK_EQUAL(strEnc, vstrOut[i]); auto dec = DecodeBase64(strEnc); BOOST_REQUIRE(dec); - BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]); + BOOST_CHECK_MESSAGE(std::ranges::equal(*dec, vstrIn[i]), vstrOut[i]); } { diff --git a/src/test/bip324_tests.cpp b/src/test/bip324_tests.cpp index 1ed7e23bcf..1caea4000c 100644 --- a/src/test/bip324_tests.cpp +++ b/src/test/bip324_tests.cpp @@ -11,6 +11,7 @@ #include <test/util/setup_common.h> #include <util/strencodings.h> +#include <algorithm> #include <array> #include <cstddef> #include <cstdint> @@ -62,9 +63,9 @@ void TestBIP324PacketVector( BOOST_CHECK(cipher); // Compare session variables. - BOOST_CHECK(Span{out_session_id} == cipher.GetSessionID()); - BOOST_CHECK(Span{mid_send_garbage} == cipher.GetSendGarbageTerminator()); - BOOST_CHECK(Span{mid_recv_garbage} == cipher.GetReceiveGarbageTerminator()); + BOOST_CHECK(std::ranges::equal(out_session_id, cipher.GetSessionID())); + BOOST_CHECK(std::ranges::equal(mid_send_garbage, cipher.GetSendGarbageTerminator())); + BOOST_CHECK(std::ranges::equal(mid_recv_garbage, cipher.GetReceiveGarbageTerminator())); // Vector of encrypted empty messages, encrypted in order to seek to the right position. std::vector<std::vector<std::byte>> dummies(in_idx); @@ -89,7 +90,7 @@ void TestBIP324PacketVector( BOOST_CHECK(out_ciphertext == ciphertext); } else { BOOST_CHECK(ciphertext.size() >= out_ciphertext_endswith.size()); - BOOST_CHECK(Span{out_ciphertext_endswith} == Span{ciphertext}.last(out_ciphertext_endswith.size())); + BOOST_CHECK(std::ranges::equal(out_ciphertext_endswith, Span{ciphertext}.last(out_ciphertext_endswith.size()))); } for (unsigned error = 0; error <= 12; ++error) { @@ -109,9 +110,9 @@ void TestBIP324PacketVector( BOOST_CHECK(dec_cipher); // Compare session variables. - BOOST_CHECK((Span{out_session_id} == dec_cipher.GetSessionID()) == (error != 1)); - BOOST_CHECK((Span{mid_send_garbage} == dec_cipher.GetSendGarbageTerminator()) == (error != 1)); - BOOST_CHECK((Span{mid_recv_garbage} == dec_cipher.GetReceiveGarbageTerminator()) == (error != 1)); + BOOST_CHECK(std::ranges::equal(out_session_id, dec_cipher.GetSessionID()) == (error != 1)); + BOOST_CHECK(std::ranges::equal(mid_send_garbage, dec_cipher.GetSendGarbageTerminator()) == (error != 1)); + BOOST_CHECK(std::ranges::equal(mid_recv_garbage, dec_cipher.GetReceiveGarbageTerminator()) == (error != 1)); // Seek to the numbered packet. if (in_idx == 0 && error == 12) continue; diff --git a/src/test/crypto_tests.cpp b/src/test/crypto_tests.cpp index f84e04e819..9b485a24e2 100644 --- a/src/test/crypto_tests.cpp +++ b/src/test/crypto_tests.cpp @@ -21,6 +21,7 @@ #include <test/util/setup_common.h> #include <util/strencodings.h> +#include <algorithm> #include <vector> #include <boost/test/unit_test.hpp> @@ -833,9 +834,9 @@ BOOST_AUTO_TEST_CASE(chacha20_midblock) c20.Keystream(b2); c20.Keystream(b3); - BOOST_CHECK(Span{block}.first(5) == Span{b1}); - BOOST_CHECK(Span{block}.subspan(5, 7) == Span{b2}); - BOOST_CHECK(Span{block}.last(52) == Span{b3}); + BOOST_CHECK(std::ranges::equal(Span{block}.first(5), b1)); + BOOST_CHECK(std::ranges::equal(Span{block}.subspan(5, 7), b2)); + BOOST_CHECK(std::ranges::equal(Span{block}.last(52), b3)); } BOOST_AUTO_TEST_CASE(poly1305_testvector) diff --git a/src/test/fuzz/bip324.cpp b/src/test/fuzz/bip324.cpp index 9892e7a81c..f1fa15d8a3 100644 --- a/src/test/fuzz/bip324.cpp +++ b/src/test/fuzz/bip324.cpp @@ -10,6 +10,7 @@ #include <test/fuzz/fuzz.h> #include <test/fuzz/util.h> +#include <algorithm> #include <cstdint> #include <vector> @@ -59,9 +60,9 @@ FUZZ_TARGET(bip324_cipher_roundtrip, .init=Initialize) InsecureRandomContext rng(provider.ConsumeIntegral<uint64_t>()); // Compare session IDs and garbage terminators. - assert(initiator.GetSessionID() == responder.GetSessionID()); - assert(initiator.GetSendGarbageTerminator() == responder.GetReceiveGarbageTerminator()); - assert(initiator.GetReceiveGarbageTerminator() == responder.GetSendGarbageTerminator()); + assert(std::ranges::equal(initiator.GetSessionID(), responder.GetSessionID())); + assert(std::ranges::equal(initiator.GetSendGarbageTerminator(), responder.GetReceiveGarbageTerminator())); + assert(std::ranges::equal(initiator.GetReceiveGarbageTerminator(), responder.GetSendGarbageTerminator())); LIMITED_WHILE(provider.remaining_bytes(), 1000) { // Mode: diff --git a/src/test/fuzz/hex.cpp b/src/test/fuzz/hex.cpp index 0140c5947e..964e30cc7e 100644 --- a/src/test/fuzz/hex.cpp +++ b/src/test/fuzz/hex.cpp @@ -12,6 +12,7 @@ #include <util/strencodings.h> #include <util/transaction_identifier.h> +#include <algorithm> #include <cassert> #include <cstdint> #include <string> @@ -22,7 +23,7 @@ FUZZ_TARGET(hex) const std::string random_hex_string(buffer.begin(), buffer.end()); const std::vector<unsigned char> data = ParseHex(random_hex_string); const std::vector<std::byte> bytes{ParseHex<std::byte>(random_hex_string)}; - assert(AsBytes(Span{data}) == Span{bytes}); + assert(std::ranges::equal(AsBytes(Span{data}), bytes)); const std::string hex_data = HexStr(data); if (IsHex(random_hex_string)) { assert(ToLower(random_hex_string) == hex_data); diff --git a/src/test/fuzz/miniscript.cpp b/src/test/fuzz/miniscript.cpp index 1f9ed9a064..5b9e168856 100644 --- a/src/test/fuzz/miniscript.cpp +++ b/src/test/fuzz/miniscript.cpp @@ -13,6 +13,8 @@ #include <test/fuzz/util.h> #include <util/strencodings.h> +#include <algorithm> + namespace { using Fragment = miniscript::Fragment; @@ -293,7 +295,7 @@ const struct CheckerContext: BaseSignatureChecker { XOnlyPubKey pk{pubkey}; auto it = TEST_DATA.schnorr_sigs.find(pk); if (it == TEST_DATA.schnorr_sigs.end()) return false; - return it->second.first == sig; + return std::ranges::equal(it->second.first, sig); } bool CheckLockTime(const CScriptNum& nLockTime) const override { return nLockTime.GetInt64() & 1; } bool CheckSequence(const CScriptNum& nSequence) const override { return nSequence.GetInt64() & 1; } diff --git a/src/test/fuzz/p2p_transport_serialization.cpp b/src/test/fuzz/p2p_transport_serialization.cpp index 93f77b6e5b..cf3ef45c0a 100644 --- a/src/test/fuzz/p2p_transport_serialization.cpp +++ b/src/test/fuzz/p2p_transport_serialization.cpp @@ -12,6 +12,7 @@ #include <test/fuzz/util.h> #include <util/chaintype.h> +#include <algorithm> #include <cassert> #include <cstdint> #include <limits> @@ -185,12 +186,12 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa // Compare with expected more. if (expect_more[side].has_value()) assert(!bytes.empty() == *expect_more[side]); // Verify consistency between the two results. - assert(bytes == bytes_next); + assert(std::ranges::equal(bytes, bytes_next)); assert(msg_type == msg_type_next); if (more_nonext) assert(more_next); // Compare with previously reported output. assert(to_send[side].size() <= bytes.size()); - assert(to_send[side] == Span{bytes}.first(to_send[side].size())); + assert(std::ranges::equal(to_send[side], Span{bytes}.first(to_send[side].size()))); to_send[side].resize(bytes.size()); std::copy(bytes.begin(), bytes.end(), to_send[side].begin()); // Remember 'more' results. @@ -278,7 +279,7 @@ void SimulationTest(Transport& initiator, Transport& responder, R& rng, FuzzedDa // The m_type must match what is expected. assert(received.m_type == expected[side].front().m_type); // The data must match what is expected. - assert(MakeByteSpan(received.m_recv) == MakeByteSpan(expected[side].front().data)); + assert(std::ranges::equal(received.m_recv, MakeByteSpan(expected[side].front().data))); expected[side].pop_front(); progress = true; } diff --git a/src/test/fuzz/poolresource.cpp b/src/test/fuzz/poolresource.cpp index 28bf7175c0..dd8d5b07e5 100644 --- a/src/test/fuzz/poolresource.cpp +++ b/src/test/fuzz/poolresource.cpp @@ -78,7 +78,7 @@ public: { std::vector<std::byte> expect(entry.span.size()); InsecureRandomContext(entry.seed).fillrand(expect); - assert(entry.span == expect); + assert(std::ranges::equal(entry.span, expect)); } void Deallocate(const Entry& entry) diff --git a/src/test/fuzz/span.cpp b/src/test/fuzz/span.cpp index 8f753948df..cd436d582f 100644 --- a/src/test/fuzz/span.cpp +++ b/src/test/fuzz/span.cpp @@ -30,10 +30,4 @@ FUZZ_TARGET(span) (void)span.subspan(idx, span.size() - idx); (void)span[idx]; } - - std::string another_str = fuzzed_data_provider.ConsumeBytesAsString(32); - const Span<const char> another_span{another_str}; - assert((span <= another_span) != (span > another_span)); - assert((span == another_span) != (span != another_span)); - assert((span >= another_span) != (span < another_span)); } diff --git a/src/test/key_io_tests.cpp b/src/test/key_io_tests.cpp index 66b4e09ebf..4dd77edc16 100644 --- a/src/test/key_io_tests.cpp +++ b/src/test/key_io_tests.cpp @@ -10,12 +10,13 @@ #include <script/script.h> #include <test/util/json.h> #include <test/util/setup_common.h> +#include <univalue.h> #include <util/chaintype.h> #include <util/strencodings.h> #include <boost/test/unit_test.hpp> -#include <univalue.h> +#include <algorithm> BOOST_FIXTURE_TEST_SUITE(key_io_tests, BasicTestingSetup) @@ -46,7 +47,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse) privkey = DecodeSecret(exp_base58string); BOOST_CHECK_MESSAGE(privkey.IsValid(), "!IsValid:" + strTest); BOOST_CHECK_MESSAGE(privkey.IsCompressed() == isCompressed, "compressed mismatch:" + strTest); - BOOST_CHECK_MESSAGE(Span{privkey} == Span{exp_payload}, "key mismatch:" + strTest); + BOOST_CHECK_MESSAGE(std::ranges::equal(privkey, exp_payload), "key mismatch:" + strTest); // Private key must be invalid public key destination = DecodeDestination(exp_base58string); diff --git a/src/test/miniscript_tests.cpp b/src/test/miniscript_tests.cpp index 815c278b8c..55aa90e29e 100644 --- a/src/test/miniscript_tests.cpp +++ b/src/test/miniscript_tests.cpp @@ -2,10 +2,6 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include <stdint.h> -#include <string> -#include <vector> - #include <test/util/random.h> #include <test/util/setup_common.h> #include <boost/test/unit_test.hpp> @@ -22,6 +18,11 @@ #include <script/script_error.h> #include <script/signingprovider.h> +#include <algorithm> +#include <cstdint> +#include <string> +#include <vector> + namespace { /** TestData groups various kinds of precomputed data necessary in this test. */ @@ -274,7 +275,7 @@ public: XOnlyPubKey pk{pubkey}; auto it = g_testdata->schnorr_signatures.find(pk); if (it == g_testdata->schnorr_signatures.end()) return false; - return sig == it->second; + return std::ranges::equal(sig, it->second); } bool CheckLockTime(const CScriptNum& locktime) const override { diff --git a/src/test/net_tests.cpp b/src/test/net_tests.cpp index 46a6a33b34..26c52013fa 100644 --- a/src/test/net_tests.cpp +++ b/src/test/net_tests.cpp @@ -1255,7 +1255,7 @@ public: for (garblen = 0; garblen <= V2Transport::MAX_GARBAGE_LEN; ++garblen) { BOOST_REQUIRE(m_received.size() >= garblen + BIP324Cipher::GARBAGE_TERMINATOR_LEN); auto term_span = MakeByteSpan(Span{m_received}.subspan(garblen, BIP324Cipher::GARBAGE_TERMINATOR_LEN)); - if (term_span == m_cipher.GetReceiveGarbageTerminator()) break; + if (std::ranges::equal(term_span, m_cipher.GetReceiveGarbageTerminator())) break; } // Copy the garbage to a buffer. m_recv_garbage.assign(m_received.begin(), m_received.begin() + garblen); @@ -1280,7 +1280,7 @@ public: auto ret = ReceivePacket(); BOOST_CHECK(ret.size() == payload.size() + 1); BOOST_CHECK(ret[0] == short_id); - BOOST_CHECK(Span{ret}.subspan(1) == payload); + BOOST_CHECK(std::ranges::equal(Span{ret}.subspan(1), payload)); } /** Expect application packet to have been received, with specified 12-char message type and @@ -1297,7 +1297,7 @@ public: BOOST_CHECK(ret[1 + i] == 0); } } - BOOST_CHECK(Span{ret}.subspan(1 + CMessageHeader::COMMAND_SIZE) == payload); + BOOST_CHECK(std::ranges::equal(Span{ret}.subspan(1 + CMessageHeader::COMMAND_SIZE), payload)); } /** Schedule an encrypted packet with specified message type and payload to be sent to @@ -1365,9 +1365,9 @@ BOOST_AUTO_TEST_CASE(v2transport_test) tester.SendMessage("tx", msg_data_2); // 12-character encoded message type ret = tester.Interact(); BOOST_REQUIRE(ret && ret->size() == 3); - BOOST_CHECK((*ret)[0] && (*ret)[0]->m_type == "cmpctblock" && Span{(*ret)[0]->m_recv} == MakeByteSpan(msg_data_1)); + BOOST_CHECK((*ret)[0] && (*ret)[0]->m_type == "cmpctblock" && std::ranges::equal((*ret)[0]->m_recv, MakeByteSpan(msg_data_1))); BOOST_CHECK(!(*ret)[1]); - BOOST_CHECK((*ret)[2] && (*ret)[2]->m_type == "tx" && Span{(*ret)[2]->m_recv} == MakeByteSpan(msg_data_2)); + BOOST_CHECK((*ret)[2] && (*ret)[2]->m_type == "tx" && std::ranges::equal((*ret)[2]->m_recv, MakeByteSpan(msg_data_2))); // Then send a message with a bit error, expecting failure. It's possible this failure does // not occur immediately (when the length descriptor was modified), but it should come @@ -1405,8 +1405,8 @@ BOOST_AUTO_TEST_CASE(v2transport_test) tester.SendMessage(uint8_t(19), msg_data_2); // pong short id ret = tester.Interact(); BOOST_REQUIRE(ret && ret->size() == 2); - BOOST_CHECK((*ret)[0] && (*ret)[0]->m_type == "inv" && Span{(*ret)[0]->m_recv} == MakeByteSpan(msg_data_1)); - BOOST_CHECK((*ret)[1] && (*ret)[1]->m_type == "pong" && Span{(*ret)[1]->m_recv} == MakeByteSpan(msg_data_2)); + BOOST_CHECK((*ret)[0] && (*ret)[0]->m_type == "inv" && std::ranges::equal((*ret)[0]->m_recv, MakeByteSpan(msg_data_1))); + BOOST_CHECK((*ret)[1] && (*ret)[1]->m_type == "pong" && std::ranges::equal((*ret)[1]->m_recv, MakeByteSpan(msg_data_2))); // Then send a too-large message. auto msg_data_3 = g_insecure_rand_ctx.randbytes<uint8_t>(4005000); @@ -1471,8 +1471,8 @@ BOOST_AUTO_TEST_CASE(v2transport_test) tester.AddMessage("barfoo", {}); // test sending unknown message type ret = tester.Interact(); BOOST_REQUIRE(ret && ret->size() == 4); - BOOST_CHECK((*ret)[0] && (*ret)[0]->m_type == "addrv2" && Span{(*ret)[0]->m_recv} == MakeByteSpan(msg_data_1)); - BOOST_CHECK((*ret)[1] && (*ret)[1]->m_type == "headers" && Span{(*ret)[1]->m_recv} == MakeByteSpan(msg_data_2)); + BOOST_CHECK((*ret)[0] && (*ret)[0]->m_type == "addrv2" && std::ranges::equal((*ret)[0]->m_recv, MakeByteSpan(msg_data_1))); + BOOST_CHECK((*ret)[1] && (*ret)[1]->m_type == "headers" && std::ranges::equal((*ret)[1]->m_recv, MakeByteSpan(msg_data_2))); BOOST_CHECK(!(*ret)[2]); BOOST_CHECK((*ret)[3] && (*ret)[3]->m_type == "foobar" && (*ret)[3]->m_recv.empty()); tester.ReceiveMessage("barfoo", {}); @@ -1539,7 +1539,7 @@ BOOST_AUTO_TEST_CASE(v2transport_test) ret = tester.Interact(); BOOST_REQUIRE(ret && ret->size() == 2); BOOST_CHECK(!(*ret)[0]); - BOOST_CHECK((*ret)[1] && (*ret)[1]->m_type == "block" && Span{(*ret)[1]->m_recv} == MakeByteSpan(msg_data_1)); + BOOST_CHECK((*ret)[1] && (*ret)[1]->m_type == "block" && std::ranges::equal((*ret)[1]->m_recv, MakeByteSpan(msg_data_1))); tester.ReceiveMessage(uint8_t(3), msg_data_2); // "blocktxn" short id } |