github.com/turingchain2020/turingchain@v1.1.21/util/calcbitmap_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package util 6 7 import ( 8 "testing" 9 10 "github.com/turingchain2020/turingchain/common" 11 "github.com/turingchain2020/turingchain/types" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestCalcByteBitMap(t *testing.T) { 16 ori := [][]byte{} //{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17} 17 for i := 0; i < 18; i++ { 18 ori = append(ori, common.Sha256([]byte(string(i)))) 19 } 20 cur := [][]byte{} 21 arry := []byte{3, 7, 8, 11, 15, 17} 22 for _, v := range arry { 23 cur = append(cur, common.Sha256([]byte(string(v)))) 24 } 25 26 d0 := &types.ReceiptData{Ty: types.ExecOk} 27 d1 := &types.ReceiptData{Ty: types.ExecPack} 28 d2 := &types.ReceiptData{Ty: types.ExecOk} 29 d3 := &types.ReceiptData{Ty: types.ExecOk} 30 d4 := &types.ReceiptData{Ty: types.ExecOk} 31 d5 := &types.ReceiptData{Ty: types.ExecOk} 32 data := []*types.ReceiptData{d0, d1, d2, d3, d4, d5} 33 34 // {17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0} 35 //rst: 1, 0, 1, 0, 0, 0, 1, 0, 0,1,0,0,0,0,1,0,0,0 36 // 2 0x89 8 37 rst := CalcBitMap(ori, cur, data) 38 //t.Log(rst) 39 check := []byte{0x2, 0x89, 0x8} 40 assert.Equal(t, check, rst) 41 } 42 43 func TestCalcSubBitMap(t *testing.T) { 44 ori := [][]byte{} //{0,1,2,3,4,5,6,7,8,9} 45 for i := 0; i < 10; i++ { 46 ori = append(ori, common.Sha256([]byte(string(i)))) 47 } 48 sub := [][]byte{} 49 arry := []byte{0, 2, 4, 6, 7, 9} 50 for _, v := range arry { 51 sub = append(sub, common.Sha256([]byte(string(v)))) 52 } 53 54 d0 := &types.ReceiptData{Ty: types.ExecOk} 55 d1 := &types.ReceiptData{Ty: types.ExecPack} 56 d2 := &types.ReceiptData{Ty: types.ExecOk} 57 d3 := &types.ReceiptData{Ty: types.ExecPack} 58 d4 := &types.ReceiptData{Ty: types.ExecOk} 59 d5 := &types.ReceiptData{Ty: types.ExecOk} 60 d6 := &types.ReceiptData{Ty: types.ExecPack} 61 d7 := &types.ReceiptData{Ty: types.ExecOk} 62 d8 := &types.ReceiptData{Ty: types.ExecPack} 63 d9 := &types.ReceiptData{Ty: types.ExecPack} 64 data := []*types.ReceiptData{d0, d1, d2, d3, d4, d5, d6, d7, d8, d9} 65 66 rst := CalcBitMap(sub, ori, data) 67 //t.Log(rst) 68 check := []byte{0x17} 69 assert.Equal(t, check, rst) 70 } 71 72 func TestDecodeByteBitMap(t *testing.T) { 73 var i uint32 74 rst := []byte{0x2, 0x89, 0x8} 75 i = 2 76 ret := BitMapBit(rst, i) 77 assert.False(t, ret) 78 79 i = 3 80 ret = BitMapBit(rst, i) 81 assert.True(t, ret) 82 83 i = 8 84 ret = BitMapBit(rst, i) 85 assert.True(t, ret) 86 87 //test for beyond array 88 i = 100 89 ret = BitMapBit(rst, i) 90 assert.False(t, ret) 91 } 92 93 func TestCalcSingleBitMap(t *testing.T) { 94 ori := [][]byte{} //{0,1,2,3,4,5,6,7,8,9} 95 for i := 0; i < 10; i++ { 96 ori = append(ori, common.Sha256([]byte(string(i)))) 97 } 98 99 d0 := &types.ReceiptData{Ty: types.ExecOk} 100 d1 := &types.ReceiptData{Ty: types.ExecPack} 101 d2 := &types.ReceiptData{Ty: types.ExecOk} 102 d3 := &types.ReceiptData{Ty: types.ExecPack} 103 d4 := &types.ReceiptData{Ty: types.ExecOk} 104 d5 := &types.ReceiptData{Ty: types.ExecOk} 105 d6 := &types.ReceiptData{Ty: types.ExecPack} 106 d7 := &types.ReceiptData{Ty: types.ExecOk} 107 d8 := &types.ReceiptData{Ty: types.ExecPack} 108 d9 := &types.ReceiptData{Ty: types.ExecPack} 109 data := []*types.ReceiptData{d0, d1, d2, d3, d4, d5, d6, d7, d8, d9} 110 111 rst := CalcSingleBitMap(ori, data) 112 //t.Log(rst) 113 check := []byte{0xb5} 114 assert.Equal(t, check, rst) 115 116 } 117 118 func TestCalcBitMapByBitMap(t *testing.T) { 119 ori := [][]byte{} //{0,1,2,3,4,5,6,7,8,9} 120 for i := 0; i < 10; i++ { 121 ori = append(ori, common.Sha256([]byte(string(i)))) 122 } 123 sub := [][]byte{} 124 arry := []byte{0, 2, 4, 6, 7, 9} 125 for _, v := range arry { 126 sub = append(sub, common.Sha256([]byte(string(v)))) 127 } 128 129 bitmap := []byte{0x97} 130 rst := CalcBitMapByBitMap(sub, ori, bitmap) 131 //t.Log(rst) 132 check := []byte{0x17} 133 assert.Equal(t, check, rst) 134 } 135 136 func TestSetAddrsBitMap(t *testing.T) { 137 group := []string{"aa", "bb", "cc", "dd", "ee", "ff", "gg", "h", "i", "j"} 138 addrs := []string{"aa", "ee"} 139 140 rst, mis := SetAddrsBitMap(group, addrs) 141 assert.Equal(t, []byte{0x11}, rst) 142 assert.Equal(t, 0, len(mis)) 143 144 addrs = []string{"f", "ee"} 145 rst, mis = SetAddrsBitMap(group, addrs) 146 assert.Equal(t, []byte{0x10}, rst) 147 assert.Equal(t, 1, len(mis)) 148 149 addrs = []string{"i", "j", "ee"} 150 rst, mis = SetAddrsBitMap(group, addrs) 151 assert.Equal(t, []byte{0x3, 0x10}, rst) 152 assert.Equal(t, 0, len(mis)) 153 } 154 155 func TestGetAddrsByBitMap(t *testing.T) { 156 group := []string{"aa", "bb", "cc", "dd", "ee"} 157 bitmap := []byte{0x10} 158 159 addrs := GetAddrsByBitMap(group, bitmap) 160 expect := []string{"ee"} 161 assert.Equal(t, expect, addrs) 162 163 bitmap = []byte{0x11} 164 addrs = GetAddrsByBitMap(group, bitmap) 165 expect = []string{"aa", "ee"} 166 assert.Equal(t, expect, addrs) 167 168 bitmap = []byte{0x1f} 169 addrs = GetAddrsByBitMap(group, bitmap) 170 assert.Equal(t, group, addrs) 171 }