add unit test for recursivePruneName and merklehash computation test in claimtriecache_tests.cpp

This commit is contained in:
Kay Kurokawa 2018-03-12 21:41:36 -04:00 committed by Umpei Kay Kurokawa
parent 0a2c49911d
commit 62f4bedb17
3 changed files with 94 additions and 1 deletions

View file

@ -61,6 +61,7 @@ BITCOIN_TESTS =\
test/miner_tests.cpp \
test/multisig_tests.cpp \
test/claimtrie_tests.cpp \
test/claimtriecache_tests.cpp \
test/claimtriebranching_tests.cpp \
test/netbase_tests.cpp \
test/pmt_tests.cpp \

View file

@ -493,7 +493,7 @@ public:
CClaimTrieProof getProofForName(const std::string& name) const;
bool finalizeDecrement() const;
private:
protected:
CClaimTrie* base;
bool fRequireTakeoverHeights;

View file

@ -0,0 +1,92 @@
#include "claimtrie.h"
#include "main.h"
#include "uint256.h"
#include "test/test_bitcoin.h"
#include <boost/test/unit_test.hpp>
using namespace std;
class CClaimTrieCacheTest : public CClaimTrieCache {
public:
CClaimTrieCacheTest(CClaimTrie* base):
CClaimTrieCache(base, false){}
bool recursiveComputeMerkleHash(CClaimTrieNode* tnCurrent,
std::string sPos) const
{
return CClaimTrieCache::recursiveComputeMerkleHash(tnCurrent, sPos);
}
bool recursivePruneName(CClaimTrieNode* tnCurrent, unsigned int nPos, std::string sName, bool* pfNullified) const
{
return CClaimTrieCache::recursivePruneName(tnCurrent,nPos,sName, pfNullified);
}
int cacheSize()
{
return cache.size();
}
nodeCacheType::iterator getCache(std::string key)
{
return cache.find(key);
}
};
BOOST_FIXTURE_TEST_SUITE(claimtriecache_tests, RegTestingSetup)
BOOST_AUTO_TEST_CASE(merklehash_test)
{
// check empty trie
uint256 one(uint256S("0000000000000000000000000000000000000000000000000000000000000001"));
CClaimTrieCacheTest cc(pclaimTrie);
BOOST_CHECK(one == cc.getMerkleHash());
// check trie with only root node
CClaimTrieNode base_node;
cc.recursiveComputeMerkleHash(&base_node, "");
BOOST_CHECK(one == cc.getMerkleHash());
}
BOOST_AUTO_TEST_CASE(recursiveprune_test)
{
CClaimTrieCacheTest cc(pclaimTrie);
BOOST_CHECK_EQUAL(0, cc.cacheSize());
COutPoint outpoint;
uint160 claimId;
CAmount amount(20);
int height = 0;
int validAtHeight = 0;
CClaimValue test_claim(outpoint, claimId, amount, height, validAtHeight);
CClaimTrieNode base_node;
// base node has a claim, so it should not be pruned
base_node.insertClaim(test_claim);
// node 1 has a claim so it should not be pruned
CClaimTrieNode node_1;
const char c = 't';
base_node.children[c] = &node_1;
node_1.insertClaim(test_claim);
// set this just to make sure we get the right CClaimTrieNode back
node_1.nHeightOfLastTakeover = 10;
//node 2 does not have a claim so it should be pruned
// thus we should find pruned node 1 in cache
CClaimTrieNode node_2;
const char c_2 = 'e';
node_1.children[c_2] = &node_2;
cc.recursivePruneName(&base_node, 0, std::string("te"), NULL);
BOOST_CHECK_EQUAL(1, cc.cacheSize());
nodeCacheType::iterator it = cc.getCache(std::string("t"));
BOOST_CHECK_EQUAL(10, it->second->nHeightOfLastTakeover);
BOOST_CHECK_EQUAL(1, it->second->claims.size());
BOOST_CHECK_EQUAL(0, it->second->children.size());
}
BOOST_AUTO_TEST_SUITE_END()