]> Git Repo - VerusCoin.git/blob - src/test/skiplist_tests.cpp
Merge pull request #5576
[VerusCoin.git] / src / test / skiplist_tests.cpp
1 // Copyright (c) 2014 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 "main.h"
6 #include "random.h"
7 #include "util.h"
8
9 #include <vector>
10
11 #include <boost/test/unit_test.hpp>
12
13 #define SKIPLIST_LENGTH 300000
14
15 BOOST_AUTO_TEST_SUITE(skiplist_tests)
16
17 BOOST_AUTO_TEST_CASE(skiplist_test)
18 {
19     std::vector<CBlockIndex> vIndex(SKIPLIST_LENGTH);
20
21     for (int i=0; i<SKIPLIST_LENGTH; i++) {
22         vIndex[i].nHeight = i;
23         vIndex[i].pprev = (i == 0) ? NULL : &vIndex[i - 1];
24         vIndex[i].BuildSkip();
25     }
26
27     for (int i=0; i<SKIPLIST_LENGTH; i++) {
28         if (i > 0) {
29             BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
30             BOOST_CHECK(vIndex[i].pskip->nHeight < i);
31         } else {
32             BOOST_CHECK(vIndex[i].pskip == NULL);
33         }
34     }
35
36     for (int i=0; i < 1000; i++) {
37         int from = insecure_rand() % (SKIPLIST_LENGTH - 1);
38         int to = insecure_rand() % (from + 1);
39
40         BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]);
41         BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
42         BOOST_CHECK(vIndex[from].GetAncestor(0) == &vIndex[0]);
43     }
44 }
45
46 BOOST_AUTO_TEST_CASE(getlocator_test)
47 {
48     // Build a main chain 100000 blocks long.
49     std::vector<uint256> vHashMain(100000);
50     std::vector<CBlockIndex> vBlocksMain(100000);
51     for (unsigned int i=0; i<vBlocksMain.size(); i++) {
52         vHashMain[i] = i; // Set the hash equal to the height, so we can quickly check the distances.
53         vBlocksMain[i].nHeight = i;
54         vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : NULL;
55         vBlocksMain[i].phashBlock = &vHashMain[i];
56         vBlocksMain[i].BuildSkip();
57         BOOST_CHECK_EQUAL((int)vBlocksMain[i].GetBlockHash().GetLow64(), vBlocksMain[i].nHeight);
58         BOOST_CHECK(vBlocksMain[i].pprev == NULL || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1);
59     }
60
61     // Build a branch that splits off at block 49999, 50000 blocks long.
62     std::vector<uint256> vHashSide(50000);
63     std::vector<CBlockIndex> vBlocksSide(50000);
64     for (unsigned int i=0; i<vBlocksSide.size(); i++) {
65         vHashSide[i] = i + 50000 + (uint256(1) << 128); // Add 1<<128 to the hashes, so GetLow64() still returns the height.
66         vBlocksSide[i].nHeight = i + 50000;
67         vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : &vBlocksMain[49999];
68         vBlocksSide[i].phashBlock = &vHashSide[i];
69         vBlocksSide[i].BuildSkip();
70         BOOST_CHECK_EQUAL((int)vBlocksSide[i].GetBlockHash().GetLow64(), vBlocksSide[i].nHeight);
71         BOOST_CHECK(vBlocksSide[i].pprev == NULL || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 1);
72     }
73
74     // Build a CChain for the main branch.
75     CChain chain;
76     chain.SetTip(&vBlocksMain.back());
77
78     // Test 100 random starting points for locators.
79     for (int n=0; n<100; n++) {
80         int r = insecure_rand() % 150000;
81         CBlockIndex* tip = (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000];
82         CBlockLocator locator = chain.GetLocator(tip);
83
84         // The first result must be the block itself, the last one must be genesis.
85         BOOST_CHECK(locator.vHave.front() == tip->GetBlockHash());
86         BOOST_CHECK(locator.vHave.back() == vBlocksMain[0].GetBlockHash());
87
88         // Entries 1 through 11 (inclusive) go back one step each.
89         for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) {
90             BOOST_CHECK_EQUAL(locator.vHave[i].GetLow64(), tip->nHeight - i);
91         }
92
93         // The further ones (excluding the last one) go back with exponential steps.
94         unsigned int dist = 2;
95         for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) {
96             BOOST_CHECK_EQUAL(locator.vHave[i - 1].GetLow64() - locator.vHave[i].GetLow64(), dist);
97             dist *= 2;
98         }
99     }
100 }
101
102 BOOST_AUTO_TEST_SUITE_END()
This page took 0.046789 seconds and 4 git commands to generate.