OSDN Git Service

rename (#465)
[bytom/vapor.git] / protocol / bc / types / block_witness_test.go
1 package types
2
3 import (
4         "bytes"
5         "encoding/hex"
6         "testing"
7
8         "github.com/bytom/vapor/encoding/blockchain"
9         "github.com/bytom/vapor/testutil"
10 )
11
12 func TestReadWriteBlockWitness(t *testing.T) {
13         cases := []struct {
14                 bw        BlockWitness
15                 hexString string
16         }{
17                 {
18                         bw:        BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}}},
19                         hexString: "0102beef",
20                 },
21                 {
22                         bw:        BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, []byte{0xab, 0xcd}, []byte{0xcd, 0x68}}},
23                         hexString: "0302beef02abcd02cd68",
24                 },
25                 {
26                         bw:        BlockWitness{Witness: [][]byte{[]byte{0xbe, 0xef}, nil, []byte{0xcd, 0x68}}},
27                         hexString: "0302beef0002cd68",
28                 },
29                 {
30                         bw:        BlockWitness{Witness: [][]byte{[]byte{}}},
31                         hexString: "0100",
32                 },
33                 {
34                         bw:        BlockWitness{},
35                         hexString: "00",
36                 },
37         }
38
39         for _, c := range cases {
40                 buff := []byte{}
41                 buffer := bytes.NewBuffer(buff)
42                 if err := c.bw.writeTo(buffer); err != nil {
43                         t.Fatal(err)
44                 }
45
46                 hexString := hex.EncodeToString(buffer.Bytes())
47                 if hexString != c.hexString {
48                         t.Errorf("test write block commitment fail, got:%s, want:%s", hexString, c.hexString)
49                 }
50
51                 bc := &BlockWitness{}
52                 if err := bc.readFrom(blockchain.NewReader(buffer.Bytes())); err != nil {
53                         t.Fatal(err)
54                 }
55
56                 if !testutil.DeepEqual(*bc, c.bw) {
57                         t.Errorf("test read block commitment fail, got:%v, want:%v", *bc, c.bw)
58                 }
59         }
60 }
61
62 func TestBlockWitnessSet(t *testing.T) {
63         cases := []struct {
64                 bw    BlockWitness
65                 index uint64
66                 data  []byte
67                 want  BlockWitness
68         }{
69                 {
70                         bw:    BlockWitness{Witness: [][]byte{}},
71                         index: uint64(0),
72                         data:  []byte{0x01, 0x02, 0x03, 0x04},
73                         want:  BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}},
74                 },
75                 {
76                         bw:    BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}},
77                         index: uint64(1),
78                         data:  []byte{0x01, 0x01, 0x01, 0x01},
79                         want:  BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x01, 0x01, 0x01}}},
80                 },
81                 {
82                         bw:    BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}},
83                         index: uint64(4),
84                         data:  []byte{0x04, 0x04, 0x04, 0x04},
85                         want:  BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, nil, nil, []byte{0x04, 0x04, 0x04, 0x04}}},
86                 },
87         }
88
89         for i, c := range cases {
90                 newbw := c.bw
91                 newbw.Set(c.index, c.data)
92                 if !testutil.DeepEqual(c.want, newbw) {
93                         t.Errorf("update result mismatch: %v, got:%v, want:%v", i, newbw, c.want)
94                 }
95         }
96 }
97
98 func TestBlockWitnessDelete(t *testing.T) {
99         cases := []struct {
100                 bw    BlockWitness
101                 index uint64
102                 want  BlockWitness
103         }{
104                 {
105                         bw:    BlockWitness{Witness: [][]byte{}},
106                         index: uint64(0),
107                         want:  BlockWitness{Witness: [][]byte{}},
108                 },
109                 {
110                         bw:    BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}},
111                         index: uint64(0),
112                         want:  BlockWitness{Witness: [][]byte{[]byte{}}},
113                 },
114                 {
115                         bw:    BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}},
116                         index: uint64(1),
117                         want:  BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{}}},
118                 },
119                 {
120                         bw:    BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}},
121                         index: uint64(100),
122                         want:  BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}},
123                 },
124         }
125
126         for i, c := range cases {
127                 newbw := c.bw
128                 newbw.Delete(c.index)
129                 if !testutil.DeepEqual(c.want, newbw) {
130                         t.Errorf("update result mismatch: %v, got:%v, want:%v", i, newbw, c.want)
131                 }
132         }
133 }