X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=protocol%2Fbc%2Ftypes%2Fblock_witness_test.go;h=9dc53b94c4a50ab1808d89327b403d1434d85ae5;hp=048a629c65191b53811c25514fde5b6aaece224b;hb=f47413c46121216dae3b1a905fbe53ae559adee2;hpb=4cd4945c706817ae5b3dcd76d874a5072cfcc4c2 diff --git a/protocol/bc/types/block_witness_test.go b/protocol/bc/types/block_witness_test.go index 048a629c..9dc53b94 100644 --- a/protocol/bc/types/block_witness_test.go +++ b/protocol/bc/types/block_witness_test.go @@ -58,3 +58,76 @@ func TestReadWriteBlockWitness(t *testing.T) { } } } + +func TestBlockWitnessSet(t *testing.T) { + cases := []struct { + bw BlockWitness + index uint64 + data []byte + want BlockWitness + }{ + { + bw: BlockWitness{Witness: [][]byte{}}, + index: uint64(0), + data: []byte{0x01, 0x02, 0x03, 0x04}, + want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}}, + }, + { + bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}}, + index: uint64(1), + data: []byte{0x01, 0x01, 0x01, 0x01}, + want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x01, 0x01, 0x01}}}, + }, + { + bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}}, + index: uint64(4), + data: []byte{0x04, 0x04, 0x04, 0x04}, + want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, []byte{}, []byte{}, []byte{0x04, 0x04, 0x04, 0x04}}}, + }, + } + + for i, c := range cases { + newbw := c.bw + newbw.Set(c.index, c.data) + if !testutil.DeepEqual(c.want, newbw) { + t.Errorf("update result mismatch: %v, got:%v, want:%v", i, newbw, c.want) + } + } +} + +func TestBlockWitnessDelete(t *testing.T) { + cases := []struct { + bw BlockWitness + index uint64 + want BlockWitness + }{ + { + bw: BlockWitness{Witness: [][]byte{}}, + index: uint64(0), + want: BlockWitness{Witness: [][]byte{}}, + }, + { + bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}}}, + index: uint64(0), + want: BlockWitness{Witness: [][]byte{[]byte{}}}, + }, + { + bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}}, + index: uint64(1), + want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{}}}, + }, + { + bw: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}}, + index: uint64(100), + want: BlockWitness{Witness: [][]byte{[]byte{0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}}}, + }, + } + + for i, c := range cases { + newbw := c.bw + newbw.Delete(c.index) + if !testutil.DeepEqual(c.want, newbw) { + t.Errorf("update result mismatch: %v, got:%v, want:%v", i, newbw, c.want) + } + } +}