Bitcoin ABC  0.24.10
P2P Digital Currency
proofcomparator_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 
7 #include <random.h>
8 
9 #include <avalanche/test/util.h>
10 #include <test/util/setup_common.h>
11 
12 #include <boost/test/unit_test.hpp>
13 
14 #include <cstdint>
15 #include <limits>
16 #include <memory>
17 
18 using namespace avalanche;
19 
20 BOOST_FIXTURE_TEST_SUITE(proofcomparator_tests, TestingSetup)
21 
22 BOOST_AUTO_TEST_CASE(proof_shared_pointer_comparator) {
23  uint32_t score = MIN_VALID_PROOF_SCORE;
24 
25  auto proofMinScore = buildRandomProof(MIN_VALID_PROOF_SCORE);
26  auto proofMaxScore = buildRandomProof(std::numeric_limits<uint32_t>::max());
27 
28  const ProofComparator comparator;
29 
30  auto prevProof = proofMinScore;
31  for (size_t i = 0; i < 100; i++) {
32  score += 1000 + GetRandInt(10000);
33  auto higherScoreProof = buildRandomProof(score);
34  BOOST_CHECK(comparator(higherScoreProof, proofMinScore));
35  BOOST_CHECK(comparator(higherScoreProof, prevProof));
36  BOOST_CHECK(!comparator(higherScoreProof, proofMaxScore));
37  prevProof = higherScoreProof;
38  }
39 
40  // Decrement slower than we incremented, so we don't have to check whether
41  // the score reached the minimal value.
42  for (size_t i = 0; i < 100; i++) {
43  score -= 1 + GetRandInt(100);
44  auto lowerScoreProof = buildRandomProof(score);
45  BOOST_CHECK(comparator(lowerScoreProof, proofMinScore));
46  BOOST_CHECK(!comparator(lowerScoreProof, prevProof));
47  BOOST_CHECK(!comparator(lowerScoreProof, proofMaxScore));
48  prevProof = lowerScoreProof;
49  }
50 
51  for (size_t i = 0; i < 100; i++) {
52  auto anotherProofMinScore = buildRandomProof(MIN_VALID_PROOF_SCORE);
53  BOOST_CHECK_EQUAL(comparator(anotherProofMinScore, proofMinScore),
54  anotherProofMinScore->getId() <
55  proofMinScore->getId());
56  }
57 }
58 
avalanche::MIN_VALID_PROOF_SCORE
constexpr uint32_t MIN_VALID_PROOF_SCORE
Definition: util.h:16
avalanche
Definition: avalanche.h:11
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
avalanche::buildRandomProof
ProofRef buildRandomProof(uint32_t score, const CKey &masterKey)
Definition: util.cpp:20
random.h
GetRandInt
int GetRandInt(int nMax) noexcept
Definition: random.cpp:654
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(proof_shared_pointer_comparator)
Definition: proofcomparator_tests.cpp:22
avalanche::ProofComparator
Compare proofs by score, then by id in case of equality.
Definition: proofcomparator.h:18
util.h
proofcomparator.h
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_AUTO_TEST_SUITE_END
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18