github.com/turingchain2020/turingchain@v1.1.21/common/difficulty/difficulty_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 difficulty 6 7 import ( 8 "math/big" 9 "testing" 10 11 "bytes" 12 13 "github.com/turingchain2020/turingchain/common" 14 ) 15 16 type ByteSlice []byte 17 18 func (p ByteSlice) Len() int { return len(p) } 19 func (p ByteSlice) Less(i, j int) bool { return p[i] < p[j] } 20 func (p ByteSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 21 func TestHashToBig(t *testing.T) { 22 hashstr := "0xb80b27b4a795b26b3af11340683891c6714b671054bd091d0674e07daec33772" 23 hash, _ := common.FromHex(hashstr) 24 bigint := HashToBig(hash) 25 if len(bigint.Bytes()) != 32 { 26 t.Error("Wrong hash len after HashToBig") 27 } 28 if !bytes.Equal(hash, bigint.Bytes()) { 29 t.Error("Failed to HashToBig") 30 } 31 } 32 33 func TestCompactToBigSmallPositive(t *testing.T) { 34 var compact uint32 = 0x030000ff 35 bigint := CompactToBig(compact) 36 if 1 != bigint.Sign() { 37 t.Error("Bad sign for compact", compact) 38 } 39 40 value := bigint.Bytes() 41 if 0xff != value[0] { 42 t.Errorf("Bad value for compact %x", compact) 43 } 44 45 if 1 != len(value) { 46 t.Error("Bad value len for compact ", len(value)) 47 } 48 } 49 50 func TestCompactToBigBigPositive(t *testing.T) { 51 var compact uint32 = 0x050000ff 52 bigint := CompactToBig(compact) 53 if 1 != bigint.Sign() { 54 t.Error("Bad sign for compact", compact) 55 } 56 57 expectRes := []byte{0xff, 0, 0} 58 59 value := bigint.Bytes() 60 if !bytes.Equal(expectRes, value) { 61 t.Errorf("Bad value for compact %x", value) 62 } 63 64 if 3 != len(value) { 65 t.Error("Bad value len for compact ", len(value)) 66 } 67 } 68 69 func TestCompactToBigSmallNegative(t *testing.T) { 70 var compact uint32 = 0x038000ff 71 bigint := CompactToBig(compact) 72 if -1 != bigint.Sign() { 73 t.Error("Bad sign for compact", compact) 74 } 75 76 value := bigint.Bytes() 77 if 0xff != value[0] { 78 t.Errorf("Bad value for compact %x", compact) 79 } 80 81 if 1 != len(value) { 82 t.Error("Bad value len for compact ", len(value)) 83 } 84 } 85 86 func TestCompactToBigBigNegative(t *testing.T) { 87 var compact uint32 = 0x058000ff 88 bigint := CompactToBig(compact) 89 if -1 != bigint.Sign() { 90 t.Error("Bad sign for compact", compact) 91 } 92 93 expectRes := []byte{0xff, 0, 0} 94 value := bigint.Bytes() 95 if !bytes.Equal(expectRes, value) { 96 t.Errorf("Bad value for compact %x", value) 97 } 98 99 if 3 != len(value) { 100 t.Error("Bad value len for compact ", len(value)) 101 } 102 103 another := big.NewInt(int64(0xff0000)) 104 another.Neg(another) 105 if 0 != another.Cmp(bigint) { 106 t.Error("Different big int ") 107 } 108 } 109 110 ///////////////////////////////////////////////////////////////// 111 func TestBigToCompactBigNegative(t *testing.T) { 112 bn := big.NewInt(int64(0xff0000)) 113 bn.Neg(bn) 114 compact := BigToCompact(bn) 115 if compact != 0x0480ff00 { //其实和0x058000ff表示的含义一致,只是移位方式不一样而已 116 t.Errorf("Failed to do BigToCompact for big int:0x%x", bn.Bytes()) 117 } 118 } 119 120 func TestBigToCompactBigPositive(t *testing.T) { 121 bn := big.NewInt(int64(0xff0000)) 122 compact := BigToCompact(bn) 123 if compact != 0x0400ff00 { 124 t.Errorf("Failed to do BigToCompact for big int:0x%x", bn.Bytes()) 125 } 126 } 127 128 func TestBigToCompactSmallBigNegative(t *testing.T) { 129 bn := big.NewInt(int64(0xff)) 130 bn.Neg(bn) 131 compact := BigToCompact(bn) 132 if compact != 0x280ff00 { 133 t.Errorf("Failed to do BigToCompact for big int:0x%x,compact:%x", bn.Bytes(), compact) 134 } 135 } 136 137 func TestBigToCompactSmallBigPositive(t *testing.T) { 138 bn := big.NewInt(int64(0xff)) 139 compact := BigToCompact(bn) 140 if compact != 0x200ff00 { 141 t.Errorf("Failed to do BigToCompact for big int:0x%x", bn.Bytes()) 142 } 143 } 144 145 func TestCalcWorkNegative(t *testing.T) { 146 bigint := CalcWork(uint32(0x280ff00)) 147 zero := big.NewInt(0) 148 149 if zero.Cmp(bigint) != 0 { 150 t.Errorf("Failed to do CalcWork for uint32:0x%x", 0x280ff00) 151 } 152 } 153 154 func TestCalcWork(t *testing.T) { 155 bigint := CalcWork(uint32(0x0400ff00)) 156 byteInfo := bigint.Bytes() 157 len := len(byteInfo) 158 expectres := []byte{0x01, 0x01, 0x00, 0xff, 0xfe, 0xfd, 0xfd, 0xff, 0x01, 0x03, 159 0x04, 0x02, 0xff, 0xfb, 0xf8, 0xf8, 0xfd, 0x04, 0x0b, 0x0e, 0x09, 0xfe, 0xf0, 0xe6, 0xe7, 0xf7, 0x10, 0x28, 0x31, 0x20} 160 161 if len != 30 || !bytes.Equal(expectres, byteInfo) { 162 t.Errorf("Failed to do CalcWork for uint32:0x%x", 0x0400ff00) 163 } 164 }