Bitcoin Core  22.99.0
P2P Digital Currency
minisketch_tests.cpp
Go to the documentation of this file.
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>
7 #include <random.h>
9 
10 #include <boost/test/unit_test.hpp>
11 
12 #include <utility>
13 
14 BOOST_AUTO_TEST_SUITE(minisketch_tests)
15 
16 BOOST_AUTO_TEST_CASE(minisketch_test)
17 {
18  for (int i = 0; i < 100; ++i) {
19  uint32_t errors = 0 + InsecureRandRange(11);
20  uint32_t start_a = 1 + InsecureRandRange(1000000000);
21  uint32_t a_not_b = InsecureRandRange(errors + 1);
22  uint32_t b_not_a = errors - a_not_b;
23  uint32_t both = InsecureRandRange(10000);
24  uint32_t end_a = start_a + a_not_b + both;
25  uint32_t start_b = start_a + a_not_b;
26  uint32_t end_b = start_b + both + b_not_a;
27 
28  Minisketch sketch_a = MakeMinisketch32(10);
29  for (uint32_t a = start_a; a < end_a; ++a) sketch_a.Add(a);
30  Minisketch sketch_b = MakeMinisketch32(10);
31  for (uint32_t b = start_b; b < end_b; ++b) sketch_b.Add(b);
32 
33  Minisketch sketch_ar = MakeMinisketch32(10);
34  Minisketch sketch_br = MakeMinisketch32(10);
35  sketch_ar.Deserialize(sketch_a.Serialize());
36  sketch_br.Deserialize(sketch_b.Serialize());
37 
38  Minisketch sketch_c = std::move(sketch_ar);
39  sketch_c.Merge(sketch_br);
40  auto dec = sketch_c.Decode(errors);
41  BOOST_CHECK(dec.has_value());
42  auto sols = std::move(*dec);
43  std::sort(sols.begin(), sols.end());
44  for (uint32_t i = 0; i < a_not_b; ++i) BOOST_CHECK_EQUAL(sols[i], start_a + i);
45  for (uint32_t i = 0; i < b_not_a; ++i) BOOST_CHECK_EQUAL(sols[i + a_not_b], start_b + both + i);
46  }
47 }
48 
minisketchwrapper.h
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(minisketch_test)
Definition: minisketch_tests.cpp:16
setup_common.h
InsecureRandRange
static uint64_t InsecureRandRange(uint64_t range)
Definition: setup_common.h:68
minisketch.h
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
random.h
MakeMinisketch32
Minisketch MakeMinisketch32(size_t capacity)
Wrapper around Minisketch::Minisketch(32, implementation, capacity).
Definition: minisketchwrapper.cpp:69
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18