github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/database/internal/treap/common_test.go (about)

     1  // Copyright (c) 2015-2016 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package treap
     7  
     8  import (
     9  	"encoding/binary"
    10  	"encoding/hex"
    11  	"math/rand"
    12  	"reflect"
    13  	"testing"
    14  )
    15  
    16  // fromHex converts the passed hex string into a byte slice and will panic if
    17  // there is an error.  This is only provided for the hard-coded constants so
    18  // errors in the source code can be detected. It will only (and must only) be
    19  // called for initialization purposes.
    20  func fromHex(s string) []byte {
    21  	r, err := hex.DecodeString(s)
    22  	if err != nil {
    23  		panic("invalid hex in source file: " + s)
    24  	}
    25  	return r
    26  }
    27  
    28  // serializeUint32 returns the big-endian encoding of the passed uint32.
    29  func serializeUint32(ui uint32) []byte {
    30  	var ret [4]byte
    31  	binary.BigEndian.PutUint32(ret[:], ui)
    32  	return ret[:]
    33  }
    34  
    35  // TestParentStack ensures the treapParentStack functionality works as intended.
    36  func TestParentStack(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	tests := []struct {
    40  		numNodes int
    41  	}{
    42  		{numNodes: 1},
    43  		{numNodes: staticDepth},
    44  		{numNodes: staticDepth + 1}, // Test dynamic code paths
    45  	}
    46  
    47  testLoop:
    48  	for i, test := range tests {
    49  		nodes := make([]*treapNode, 0, test.numNodes)
    50  		for j := 0; j < test.numNodes; j++ {
    51  			var key [4]byte
    52  			binary.BigEndian.PutUint32(key[:], uint32(j))
    53  			node := newTreapNode(key[:], key[:], 0)
    54  			nodes = append(nodes, node)
    55  		}
    56  
    57  		// Push all of the nodes onto the parent stack while testing
    58  		// various stack properties.
    59  		stack := &parentStack{}
    60  		for j, node := range nodes {
    61  			stack.Push(node)
    62  
    63  			// Ensure the stack length is the expected value.
    64  			if stack.Len() != j+1 {
    65  				t.Errorf("Len #%d (%d): unexpected stack "+
    66  					"length - got %d, want %d", i, j,
    67  					stack.Len(), j+1)
    68  				continue testLoop
    69  			}
    70  
    71  			// Ensure the node at each index is the expected one.
    72  			for k := 0; k <= j; k++ {
    73  				atNode := stack.At(j - k)
    74  				if !reflect.DeepEqual(atNode, nodes[k]) {
    75  					t.Errorf("At #%d (%d): mismatched node "+
    76  						"- got %v, want %v", i, j-k,
    77  						atNode, nodes[k])
    78  					continue testLoop
    79  				}
    80  			}
    81  		}
    82  
    83  		// Ensure each popped node is the expected one.
    84  		for j := 0; j < len(nodes); j++ {
    85  			node := stack.Pop()
    86  			expected := nodes[len(nodes)-j-1]
    87  			if !reflect.DeepEqual(node, expected) {
    88  				t.Errorf("At #%d (%d): mismatched node - "+
    89  					"got %v, want %v", i, j, node, expected)
    90  				continue testLoop
    91  			}
    92  		}
    93  
    94  		// Ensure the stack is now empty.
    95  		if stack.Len() != 0 {
    96  			t.Errorf("Len #%d: stack is not empty - got %d", i,
    97  				stack.Len())
    98  			continue testLoop
    99  		}
   100  
   101  		// Ensure attempting to retrieve a node at an index beyond the
   102  		// stack's length returns nil.
   103  		if node := stack.At(2); node != nil {
   104  			t.Errorf("At #%d: did not give back nil - got %v", i,
   105  				node)
   106  			continue testLoop
   107  		}
   108  
   109  		// Ensure attempting to pop a node from an empty stack returns
   110  		// nil.
   111  		if node := stack.Pop(); node != nil {
   112  			t.Errorf("Pop #%d: did not give back nil - got %v", i,
   113  				node)
   114  			continue testLoop
   115  		}
   116  	}
   117  }
   118  
   119  func init() {
   120  	// Force the same pseudo random numbers for each test run.
   121  	rand.Seed(0)
   122  }