kaspad/blockdag/blockheap_test.go
Ori Newman 904f2cf2e3 [DEV-72] Write Blues()
* [DEV-62] add phantom constructs to blocknode

* [DEV-62] add phantom constructs to blocknode

* [DEV-72] write blues()

* [DEV-72] write blues()

* [DEV-72] write blues()

* [DEV-62] add comments to new phantom constructs in blocknode

* Fixed dbIndexConnectBlock. (#33)

* Fixed dbIndexConnectBlock.

* Removed redundant check in storeFilter.

* Created a new method to BlockHeader: IsGenesis.

* [DEV-71] Implement BlockHeap (#35)

* [DEV-71] Implemented BlockHeap.

* [DEV-71] Removed irrelevant comment.

* [DEV-71] Renamed variables in Pop() and split Less() to multiple lines.

* [DEV-72] write blues()

* [DEV-72] write blues()

* [DEV-72] write blues()

* [DEV-72] write blues tests

* [DEV-72] write blues tests

* [DEV-72] remove relevant past

* [DEV-72] write blues tests

* [DEV-72] write blues tests

* [DEV-72] write blues tests

* [DEV-72] write functions to order blockSet by hash and write blue tests

* [DEV-72] add secret mining and censorship attack tests

* [DEV-72] remove prints

* [DEV-72] remove K from dagconfig.Params

* [DEV-72] remove K from dagconfig.Params

* [DEV-72] change blueScore to uint64

* [DEV-72] block V was missing, so renamed w -> v, x -> w etc

* [DEV-72] use node.String instead of %v

* [DEV-72] block V was missing, so renamed w -> v, x -> w etc

* [DEV-72] add K to dagconfig.Params, and add expected reds to all phantom tests

* [DEV-72] set K=10 and add comments to phantom and phantom tests

* [DEV-72] fix formatting and add comments to TestPhantom

* [DEV-72] fix grammar
2018-08-02 16:34:40 +03:00

91 lines
2.6 KiB
Go

package blockdag
import (
"testing"
"github.com/daglabs/btcd/dagconfig"
"github.com/daglabs/btcd/dagconfig/daghash"
)
// TestBlockHeap tests pushing, popping, and determining the length of the heap.
func TestBlockHeap(t *testing.T) {
block0Header := dagconfig.MainNetParams.GenesisBlock.Header
block0 := newBlockNode(&block0Header, newSet(), dagconfig.MainNetParams.K)
block100000Header := Block100000.Header
block100000 := newBlockNode(&block100000Header, setFromSlice(block0), dagconfig.MainNetParams.K)
block0smallHash := newBlockNode(&block0Header, newSet(), dagconfig.MainNetParams.K)
block0smallHash.hash = daghash.Hash{}
tests := []struct {
name string
toPush []*blockNode
expectedLength int
expectedPop *blockNode
}{
{
name: "empty heap must have length 0",
toPush: []*blockNode{},
expectedLength: 0,
expectedPop: nil,
},
{
name: "heap with one push must have length 1",
toPush: []*blockNode{block0},
expectedLength: 1,
expectedPop: nil,
},
{
name: "heap with one push and one pop",
toPush: []*blockNode{block0},
expectedLength: 0,
expectedPop: block0,
},
{
name: "push two blocks with different heights, heap shouldn't have to rebalance",
toPush: []*blockNode{block100000, block0},
expectedLength: 1,
expectedPop: block100000,
},
{
name: "push two blocks with different heights, heap must rebalance",
toPush: []*blockNode{block0, block100000},
expectedLength: 1,
expectedPop: block100000,
},
{
name: "push two blocks with equal heights but different hashes, heap shouldn't have to rebalance",
toPush: []*blockNode{block0, block0smallHash},
expectedLength: 1,
expectedPop: block0,
},
{
name: "push two blocks with equal heights but different hashes, heap must rebalance",
toPush: []*blockNode{block0smallHash, block0},
expectedLength: 1,
expectedPop: block0,
},
}
for _, test := range tests {
heap := NewHeap()
for _, block := range test.toPush {
heap.Push(block)
}
var poppedBlock *blockNode
if test.expectedPop != nil {
poppedBlock = heap.Pop()
}
if heap.Len() != test.expectedLength {
t.Errorf("unexpected heap length in test \"%s\". "+
"Expected: %v, got: %v", test.name, test.expectedLength, heap.Len())
}
if poppedBlock != test.expectedPop {
t.Errorf("unexpected popped block in test \"%s\". "+
"Expected: %v, got: %v", test.name, test.expectedPop, poppedBlock)
}
}
}