Skip to content

Commit ee9dc71

Browse files
sipafanquake
authored andcommitted
Add basic minisketch tests
1 parent 0659f12 commit ee9dc71

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ BITCOIN_TESTS =\
102102
test/mempool_tests.cpp \
103103
test/merkle_tests.cpp \
104104
test/merkleblock_tests.cpp \
105+
test/minisketch_tests.cpp \
105106
test/miner_tests.cpp \
106107
test/multisig_tests.cpp \
107108
test/net_peer_eviction_tests.cpp \

src/test/minisketch_tests.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <minisketch.h>
6+
#include <random.h>
7+
#include <test/util/setup_common.h>
8+
9+
#include <boost/test/unit_test.hpp>
10+
11+
#include <utility>
12+
13+
BOOST_AUTO_TEST_SUITE(minisketch_tests)
14+
15+
BOOST_AUTO_TEST_CASE(minisketch_test)
16+
{
17+
for (int i = 0; i < 100; ++i) {
18+
uint32_t errors = 0 + InsecureRandRange(11);
19+
uint32_t start_a = 1 + InsecureRandRange(1000000000);
20+
uint32_t a_not_b = InsecureRandRange(errors + 1);
21+
uint32_t b_not_a = errors - a_not_b;
22+
uint32_t both = InsecureRandRange(10000);
23+
uint32_t end_a = start_a + a_not_b + both;
24+
uint32_t start_b = start_a + a_not_b;
25+
uint32_t end_b = start_b + both + b_not_a;
26+
27+
Minisketch sketch_a(32, 0, 10);
28+
for (uint32_t a = start_a; a < end_a; ++a) sketch_a.Add(a);
29+
Minisketch sketch_b(32, 0, 10);
30+
for (uint32_t b = start_b; b < end_b; ++b) sketch_b.Add(b);
31+
32+
Minisketch sketch_ar(32, 0, 10);
33+
Minisketch sketch_br(32, 0, 10);
34+
sketch_ar.Deserialize(sketch_a.Serialize());
35+
sketch_br.Deserialize(sketch_b.Serialize());
36+
37+
Minisketch sketch_c = std::move(sketch_ar);
38+
sketch_c.Merge(sketch_br);
39+
auto dec = sketch_c.Decode(errors);
40+
BOOST_CHECK(dec.has_value());
41+
auto sols = std::move(*dec);
42+
std::sort(sols.begin(), sols.end());
43+
for (uint32_t i = 0; i < a_not_b; ++i) BOOST_CHECK_EQUAL(sols[i], start_a + i);
44+
for (uint32_t i = 0; i < b_not_a; ++i) BOOST_CHECK_EQUAL(sols[i + a_not_b], start_b + both + i);
45+
}
46+
}
47+
48+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)