Branch data Line data Source code
1 : : // Copyright (c) 2021-2022 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 <node/minisketchwrapper.h> 7 : : #include <random.h> 8 : : #include <test/util/random.h> 9 : : #include <test/util/setup_common.h> 10 : : 11 : : #include <boost/test/unit_test.hpp> 12 : : 13 : : #include <utility> 14 : : 15 : : using node::MakeMinisketch32; 16 : : 17 : 0 : BOOST_AUTO_TEST_SUITE(minisketch_tests) 18 : : 19 : 0 : BOOST_AUTO_TEST_CASE(minisketch_test) 20 : : { 21 : 0 : for (int i = 0; i < 100; ++i) { 22 : 0 : uint32_t errors = 0 + InsecureRandRange(11); 23 : 0 : uint32_t start_a = 1 + InsecureRandRange(1000000000); 24 : 0 : uint32_t a_not_b = InsecureRandRange(errors + 1); 25 : 0 : uint32_t b_not_a = errors - a_not_b; 26 : 0 : uint32_t both = InsecureRandRange(10000); 27 : 0 : uint32_t end_a = start_a + a_not_b + both; 28 : 0 : uint32_t start_b = start_a + a_not_b; 29 : 0 : uint32_t end_b = start_b + both + b_not_a; 30 : : 31 : 0 : Minisketch sketch_a = MakeMinisketch32(10); 32 : 0 : for (uint32_t a = start_a; a < end_a; ++a) sketch_a.Add(a); 33 : 0 : Minisketch sketch_b = MakeMinisketch32(10); 34 : 0 : for (uint32_t b = start_b; b < end_b; ++b) sketch_b.Add(b); 35 : : 36 : 0 : Minisketch sketch_ar = MakeMinisketch32(10); 37 : 0 : Minisketch sketch_br = MakeMinisketch32(10); 38 : 0 : sketch_ar.Deserialize(sketch_a.Serialize()); 39 : 0 : sketch_br.Deserialize(sketch_b.Serialize()); 40 : : 41 : 0 : Minisketch sketch_c = std::move(sketch_ar); 42 : 0 : sketch_c.Merge(sketch_br); 43 : 0 : auto dec = sketch_c.Decode(errors); 44 : 0 : BOOST_REQUIRE(dec.has_value()); 45 : 0 : auto sols = std::move(*dec); 46 : 0 : std::sort(sols.begin(), sols.end()); 47 : 0 : for (uint32_t i = 0; i < a_not_b; ++i) BOOST_CHECK_EQUAL(sols[i], start_a + i); 48 : 0 : for (uint32_t i = 0; i < b_not_a; ++i) BOOST_CHECK_EQUAL(sols[i + a_not_b], start_b + both + i); 49 : 0 : } 50 : 0 : } 51 : : 52 : 0 : BOOST_AUTO_TEST_SUITE_END()