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