-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathblockchain_test.go
59 lines (51 loc) · 1.08 KB
/
blockchain_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"testing"
"fmt"
)
var (
head = &Block {
Height: 0,
}
candidate = &Block {
PrevHash: head.GetCid(),
Height: 1,
}
)
func TestValidateBlock(t *testing.T){
var chain Blockchain
chain.Head = head
if !chain.ValidateBlock(candidate){
t.Fatal("Candidate block was not valid when expected to be", candidate.Serialize())
}
}
func TestAddBlock(t *testing.T){
var chain Blockchain
chain.Head = head
chain.AddBlock(candidate)
if chain.Head.GetHash() != candidate.GetHash() {
t.Fatal("Add block failed when it was expected to pass")
}
}
func TestPointers(t *testing.T) {
var blk *Block
takesBar(&blk)
fmt.Println(blk.Serialize())
}
func takesBar(b **Block) {
x := &Block{
Height: 97,
}
*b = x
}
func TestMemory(t *testing.T) {
buf := make([]byte, 128)
for i := 0; i < 10; i++ {
getMessageFromNetwork(&buf)
fmt.Println(string(buf))
}
}
func getMessageFromNetwork(buf *[]byte) {
n := copy(*buf, []byte("cats"))
*buf = (*buf)[:n]
}