mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 09:46:52 -05:00
Add basic minisketch tests
This commit is contained in:
parent
0659f12b13
commit
ee9dc71c1b
2 changed files with 49 additions and 0 deletions
|
@ -102,6 +102,7 @@ BITCOIN_TESTS =\
|
|||
test/mempool_tests.cpp \
|
||||
test/merkle_tests.cpp \
|
||||
test/merkleblock_tests.cpp \
|
||||
test/minisketch_tests.cpp \
|
||||
test/miner_tests.cpp \
|
||||
test/multisig_tests.cpp \
|
||||
test/net_peer_eviction_tests.cpp \
|
||||
|
|
48
src/test/minisketch_tests.cpp
Normal file
48
src/test/minisketch_tests.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
// Copyright (c) 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 <minisketch.h>
|
||||
#include <random.h>
|
||||
#include <test/util/setup_common.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <utility>
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(minisketch_tests)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(minisketch_test)
|
||||
{
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
uint32_t errors = 0 + InsecureRandRange(11);
|
||||
uint32_t start_a = 1 + InsecureRandRange(1000000000);
|
||||
uint32_t a_not_b = InsecureRandRange(errors + 1);
|
||||
uint32_t b_not_a = errors - a_not_b;
|
||||
uint32_t both = InsecureRandRange(10000);
|
||||
uint32_t end_a = start_a + a_not_b + both;
|
||||
uint32_t start_b = start_a + a_not_b;
|
||||
uint32_t end_b = start_b + both + b_not_a;
|
||||
|
||||
Minisketch sketch_a(32, 0, 10);
|
||||
for (uint32_t a = start_a; a < end_a; ++a) sketch_a.Add(a);
|
||||
Minisketch sketch_b(32, 0, 10);
|
||||
for (uint32_t b = start_b; b < end_b; ++b) sketch_b.Add(b);
|
||||
|
||||
Minisketch sketch_ar(32, 0, 10);
|
||||
Minisketch sketch_br(32, 0, 10);
|
||||
sketch_ar.Deserialize(sketch_a.Serialize());
|
||||
sketch_br.Deserialize(sketch_b.Serialize());
|
||||
|
||||
Minisketch sketch_c = std::move(sketch_ar);
|
||||
sketch_c.Merge(sketch_br);
|
||||
auto dec = sketch_c.Decode(errors);
|
||||
BOOST_CHECK(dec.has_value());
|
||||
auto sols = std::move(*dec);
|
||||
std::sort(sols.begin(), sols.end());
|
||||
for (uint32_t i = 0; i < a_not_b; ++i) BOOST_CHECK_EQUAL(sols[i], start_a + i);
|
||||
for (uint32_t i = 0; i < b_not_a; ++i) BOOST_CHECK_EQUAL(sols[i + a_not_b], start_b + both + i);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
Loading…
Add table
Reference in a new issue