Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/voterecord_tests.cpp
// Copyright (c) 2018-2022 The Bitcoin developers | // Copyright (c) 2018-2022 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <avalanche/voterecord.h> | #include <avalanche/voterecord.h> | ||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
using namespace avalanche; | using namespace avalanche; | ||||
NodeId nextNodeId(NodeId &nodeid) { | |||||
nodeid++; | |||||
if (nodeid >= 8) { | |||||
nodeid = 0; | |||||
} | |||||
return nodeid; | |||||
} | |||||
BOOST_FIXTURE_TEST_SUITE(voterecord_tests, TestingSetup) | BOOST_FIXTURE_TEST_SUITE(voterecord_tests, TestingSetup) | ||||
#define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | #define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | ||||
vr.registerVote(NO_NODE, vote); \ | vr.registerVote(nextNodeId(nodeid), vote); \ | ||||
BOOST_CHECK_EQUAL(vr.isAccepted(), state); \ | BOOST_CHECK_EQUAL(vr.isAccepted(), state); \ | ||||
BOOST_CHECK_EQUAL(vr.hasFinalized(), finalized); \ | BOOST_CHECK_EQUAL(vr.hasFinalized(), finalized); \ | ||||
BOOST_CHECK_EQUAL(vr.getConfidence(), confidence); | BOOST_CHECK_EQUAL(vr.getConfidence(), confidence); | ||||
BOOST_AUTO_TEST_CASE(vote_record) { | BOOST_AUTO_TEST_CASE(vote_record) { | ||||
NodeId nodeid = -1; | |||||
VoteRecord vraccepted(true); | VoteRecord vraccepted(true); | ||||
// Check initial state. | // Check initial state. | ||||
BOOST_CHECK_EQUAL(vraccepted.isAccepted(), true); | BOOST_CHECK_EQUAL(vraccepted.isAccepted(), true); | ||||
BOOST_CHECK_EQUAL(vraccepted.hasFinalized(), false); | BOOST_CHECK_EQUAL(vraccepted.hasFinalized(), false); | ||||
BOOST_CHECK_EQUAL(vraccepted.getConfidence(), 0); | BOOST_CHECK_EQUAL(vraccepted.getConfidence(), 0); | ||||
VoteRecord vr(false); | VoteRecord vr(false); | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | for (int i = 1; i < AVALANCHE_MAX_INFLIGHT_POLL; i++) { | ||||
BOOST_CHECK(vrinflight.shouldPoll()); | BOOST_CHECK(vrinflight.shouldPoll()); | ||||
} | } | ||||
BOOST_CHECK(vrinflight.registerPoll()); | BOOST_CHECK(vrinflight.registerPoll()); | ||||
BOOST_CHECK(!vrinflight.shouldPoll()); | BOOST_CHECK(!vrinflight.shouldPoll()); | ||||
} | } | ||||
} | } | ||||
namespace { | |||||
NodeId nextNodeId(NodeId &nodeid) { | |||||
nodeid++; | |||||
if (nodeid >= 8) { | |||||
nodeid = 0; | |||||
} | |||||
return nodeid; | |||||
} | |||||
} // namespace | |||||
BOOST_AUTO_TEST_CASE(duplicate_votes) { | BOOST_AUTO_TEST_CASE(duplicate_votes) { | ||||
VoteRecord vr(true); | VoteRecord vr(true); | ||||
NodeId nodeid = -1; | NodeId nodeid = -1; | ||||
// Register some votes, expecting confidence to increase | // Register some votes, expecting confidence to increase | ||||
for (auto i = 0; i < 7; i++) { | for (auto i = 0; i < 7; i++) { | ||||
BOOST_CHECK_EQUAL(vr.getConfidence(), 0); | BOOST_CHECK_EQUAL(vr.getConfidence(), 0); | ||||
BOOST_CHECK(!vr.registerVote(nextNodeId(nodeid), 0)); | BOOST_CHECK(!vr.registerVote(nextNodeId(nodeid), 0)); | ||||
Show All 33 Lines |