1 // Copyright (c) 2015-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
15 // fromHex converts the passed hex string into a byte slice and will panic if
16 // there is an error. This is only provided for the hard-coded constants so
17 // errors in the source code can be detected. It will only (and must only) be
18 // called for initialization purposes.
19 func fromHex(s string) []byte {
20 r, err := hex.DecodeString(s)
22 panic("invalid hex in source file: " + s)
27 // serializeUint32 returns the big-endian encoding of the passed uint32.
28 func serializeUint32(ui uint32) []byte {
30 binary.BigEndian.PutUint32(ret[:], ui)
34 // TestParentStack ensures the treapParentStack functionality works as intended.
35 func TestParentStack(t *testing.T) {
42 {numNodes: staticDepth},
43 {numNodes: staticDepth + 1}, // Test dynamic code paths
47 for i, test := range tests {
48 nodes := make([]*treapNode, 0, test.numNodes)
49 for j := 0; j < test.numNodes; j++ {
51 binary.BigEndian.PutUint32(key[:], uint32(j))
52 node := newTreapNode(key[:], key[:], 0)
53 nodes = append(nodes, node)
56 // Push all of the nodes onto the parent stack while testing
57 // various stack properties.
58 stack := &parentStack{}
59 for j, node := range nodes {
62 // Ensure the stack length is the expected value.
63 if stack.Len() != j+1 {
64 t.Errorf("Len #%d (%d): unexpected stack "+
65 "length - got %d, want %d", i, j,
70 // Ensure the node at each index is the expected one.
71 for k := 0; k <= j; k++ {
72 atNode := stack.At(j - k)
73 if !reflect.DeepEqual(atNode, nodes[k]) {
74 t.Errorf("At #%d (%d): mismatched node "+
75 "- got %v, want %v", i, j-k,
82 // Ensure each popped node is the expected one.
83 for j := 0; j < len(nodes); j++ {
85 expected := nodes[len(nodes)-j-1]
86 if !reflect.DeepEqual(node, expected) {
87 t.Errorf("At #%d (%d): mismatched node - "+
88 "got %v, want %v", i, j, node, expected)
93 // Ensure the stack is now empty.
95 t.Errorf("Len #%d: stack is not empty - got %d", i,
100 // Ensure attempting to retrieve a node at an index beyond the
101 // stack's length returns nil.
102 if node := stack.At(2); node != nil {
103 t.Errorf("At #%d: did not give back nil - got %v", i,
108 // Ensure attempting to pop a node from an empty stack returns
110 if node := stack.Pop(); node != nil {
111 t.Errorf("Pop #%d: did not give back nil - got %v", i,
119 // Force the same pseudo random numbers for each test run.