kaspad/database/internal/treap/common_test.go
stasatdaglabs f46dec449d [NOD-510] Change all references to Bitcoin to Kaspa (#531)
* [NOD-510] Change coinbase flags to kaspad.

* [NOD-510] Removed superfluous spaces after periods in comments.

* [NOD-510] Rename btcd -> kaspad in the root folder.

* [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode.

* [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode.

* [NOD-510] Continue renaming btcd -> kaspad.

* [NOD-510] Rename btcjson -> kaspajson.

* [NOD-510] Rename file names inside kaspajson.

* [NOD-510] Rename kaspajson -> jsonrpc.

* [NOD-510] Finish renaming in addrmgr.

* [NOD-510] Rename package btcec to ecc.

* [NOD-510] Finish renaming stuff in blockdag.

* [NOD-510] Rename stuff in cmd.

* [NOD-510] Rename stuff in config.

* [NOD-510] Rename stuff in connmgr.

* [NOD-510] Rename stuff in dagconfig.

* [NOD-510] Rename stuff in database.

* [NOD-510] Rename stuff in docker.

* [NOD-510] Rename stuff in integration.

* [NOD-510] Rename jsonrpc to rpcmodel.

* [NOD-510] Rename stuff in limits.

* [NOD-510] Rename stuff in logger.

* [NOD-510] Rename stuff in mempool.

* [NOD-510] Rename stuff in mining.

* [NOD-510] Rename stuff in netsync.

* [NOD-510] Rename stuff in peer.

* [NOD-510] Rename stuff in release.

* [NOD-510] Rename stuff in rpcclient.

* [NOD-510] Rename stuff in server.

* [NOD-510] Rename stuff in signal.

* [NOD-510] Rename stuff in txscript.

* [NOD-510] Rename stuff in util.

* [NOD-510] Rename stuff in wire.

* [NOD-510] Fix failing tests.

* [NOD-510] Fix merge errors.

* [NOD-510] Fix go vet errors.

* [NOD-510] Remove merged file that's no longer relevant.

* [NOD-510] Add a comment above Op0.

* [NOD-510] Fix some comments referencing Bitcoin Core.

* [NOD-510] Fix some more comments referencing Bitcoin Core.

* [NOD-510] Fix bitcoin -> kaspa.

* [NOD-510] Fix more bitcoin -> kaspa.

* [NOD-510] Fix comments, remove DisconnectBlock in addrindex.

* [NOD-510] Rename KSPD to KASD.

* [NOD-510] Fix comments and user agent.
2019-12-12 15:21:41 +02:00

122 lines
3.0 KiB
Go

// Copyright (c) 2015-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package treap
import (
"encoding/binary"
"encoding/hex"
"math/rand"
"reflect"
"testing"
)
// fromHex converts the passed hex string into a byte slice and will panic if
// there is an error. This is only provided for the hard-coded constants so
// errors in the source code can be detected. It will only (and must only) be
// called for initialization purposes.
func fromHex(s string) []byte {
r, err := hex.DecodeString(s)
if err != nil {
panic("invalid hex in source file: " + s)
}
return r
}
// serializeUint32 returns the big-endian encoding of the passed uint32.
func serializeUint32(ui uint32) []byte {
var ret [4]byte
binary.BigEndian.PutUint32(ret[:], ui)
return ret[:]
}
// TestParentStack ensures the treapParentStack functionality works as intended.
func TestParentStack(t *testing.T) {
t.Parallel()
tests := []struct {
numNodes int
}{
{numNodes: 1},
{numNodes: staticDepth},
{numNodes: staticDepth + 1}, // Test dynamic code paths
}
testLoop:
for i, test := range tests {
nodes := make([]*treapNode, 0, test.numNodes)
for j := 0; j < test.numNodes; j++ {
var key [4]byte
binary.BigEndian.PutUint32(key[:], uint32(j))
node := newTreapNode(key[:], key[:], 0)
nodes = append(nodes, node)
}
// Push all of the nodes onto the parent stack while testing
// various stack properties.
stack := &parentStack{}
for j, node := range nodes {
stack.Push(node)
// Ensure the stack length is the expected value.
if stack.Len() != j+1 {
t.Errorf("Len #%d (%d): unexpected stack "+
"length - got %d, want %d", i, j,
stack.Len(), j+1)
continue testLoop
}
// Ensure the node at each index is the expected one.
for k := 0; k <= j; k++ {
atNode := stack.At(j - k)
if !reflect.DeepEqual(atNode, nodes[k]) {
t.Errorf("At #%d (%d): mismatched node "+
"- got %v, want %v", i, j-k,
atNode, nodes[k])
continue testLoop
}
}
}
// Ensure each popped node is the expected one.
for j := 0; j < len(nodes); j++ {
node := stack.Pop()
expected := nodes[len(nodes)-j-1]
if !reflect.DeepEqual(node, expected) {
t.Errorf("At #%d (%d): mismatched node - "+
"got %v, want %v", i, j, node, expected)
continue testLoop
}
}
// Ensure the stack is now empty.
if stack.Len() != 0 {
t.Errorf("Len #%d: stack is not empty - got %d", i,
stack.Len())
continue testLoop
}
// Ensure attempting to retrieve a node at an index beyond the
// stack's length returns nil.
if node := stack.At(2); node != nil {
t.Errorf("At #%d: did not give back nil - got %v", i,
node)
continue testLoop
}
// Ensure attempting to pop a node from an empty stack returns
// nil.
if node := stack.Pop(); node != nil {
t.Errorf("Pop #%d: did not give back nil - got %v", i,
node)
continue testLoop
}
}
}
func init() {
// Force the same pseudo random numbers for each test run.
rand.Seed(0)
}