github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/common/math/big_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2017 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package math
    26  
    27  import (
    28  	"bytes"
    29  	"encoding/hex"
    30  	"math/big"
    31  	"testing"
    32  
    33  	"github.com/ethereum/go-ethereum/common"
    34  )
    35  
    36  func TestHexOrDecimal256(t *testing.T) {
    37  	tests := []struct {
    38  		input string
    39  		num   *big.Int
    40  		ok    bool
    41  	}{
    42  		{"", big.NewInt(0), true},
    43  		{"0", big.NewInt(0), true},
    44  		{"0x0", big.NewInt(0), true},
    45  		{"12345678", big.NewInt(12345678), true},
    46  		{"0x12345678", big.NewInt(0x12345678), true},
    47  		{"0X12345678", big.NewInt(0x12345678), true},
    48  //超前零行为测试:
    49  {"0123456789", big.NewInt(123456789), true}, //注:不是八进制
    50  		{"00", big.NewInt(0), true},
    51  		{"0x00", big.NewInt(0), true},
    52  		{"0x012345678abc", big.NewInt(0x12345678abc), true},
    53  //无效语法:
    54  		{"abcdef", nil, false},
    55  		{"0xgg", nil, false},
    56  //大于256位:
    57  		{"115792089237316195423570985008687907853269984665640564039457584007913129639936", nil, false},
    58  	}
    59  	for _, test := range tests {
    60  		var num HexOrDecimal256
    61  		err := num.UnmarshalText([]byte(test.input))
    62  		if (err == nil) != test.ok {
    63  			t.Errorf("ParseBig(%q) -> (err == nil) == %t, want %t", test.input, err == nil, test.ok)
    64  			continue
    65  		}
    66  		if test.num != nil && (*big.Int)(&num).Cmp(test.num) != 0 {
    67  			t.Errorf("ParseBig(%q) -> %d, want %d", test.input, (*big.Int)(&num), test.num)
    68  		}
    69  	}
    70  }
    71  
    72  func TestMustParseBig256(t *testing.T) {
    73  	defer func() {
    74  		if recover() == nil {
    75  			t.Error("MustParseBig should've panicked")
    76  		}
    77  	}()
    78  	MustParseBig256("ggg")
    79  }
    80  
    81  func TestBigMax(t *testing.T) {
    82  	a := big.NewInt(10)
    83  	b := big.NewInt(5)
    84  
    85  	max1 := BigMax(a, b)
    86  	if max1 != a {
    87  		t.Errorf("Expected %d got %d", a, max1)
    88  	}
    89  
    90  	max2 := BigMax(b, a)
    91  	if max2 != a {
    92  		t.Errorf("Expected %d got %d", a, max2)
    93  	}
    94  }
    95  
    96  func TestBigMin(t *testing.T) {
    97  	a := big.NewInt(10)
    98  	b := big.NewInt(5)
    99  
   100  	min1 := BigMin(a, b)
   101  	if min1 != b {
   102  		t.Errorf("Expected %d got %d", b, min1)
   103  	}
   104  
   105  	min2 := BigMin(b, a)
   106  	if min2 != b {
   107  		t.Errorf("Expected %d got %d", b, min2)
   108  	}
   109  }
   110  
   111  func TestFirstBigSet(t *testing.T) {
   112  	tests := []struct {
   113  		num *big.Int
   114  		ix  int
   115  	}{
   116  		{big.NewInt(0), 0},
   117  		{big.NewInt(1), 0},
   118  		{big.NewInt(2), 1},
   119  		{big.NewInt(0x100), 8},
   120  	}
   121  	for _, test := range tests {
   122  		if ix := FirstBitSet(test.num); ix != test.ix {
   123  			t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix)
   124  		}
   125  	}
   126  }
   127  
   128  func TestPaddedBigBytes(t *testing.T) {
   129  	tests := []struct {
   130  		num    *big.Int
   131  		n      int
   132  		result []byte
   133  	}{
   134  		{num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}},
   135  		{num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}},
   136  		{num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}},
   137  		{num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}},
   138  	}
   139  	for _, test := range tests {
   140  		if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) {
   141  			t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result)
   142  		}
   143  	}
   144  }
   145  
   146  func BenchmarkPaddedBigBytesLargePadding(b *testing.B) {
   147  	bigint := MustParseBig256("123456789123456789123456789123456789")
   148  	for i := 0; i < b.N; i++ {
   149  		PaddedBigBytes(bigint, 200)
   150  	}
   151  }
   152  
   153  func BenchmarkPaddedBigBytesSmallPadding(b *testing.B) {
   154  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   155  	for i := 0; i < b.N; i++ {
   156  		PaddedBigBytes(bigint, 5)
   157  	}
   158  }
   159  
   160  func BenchmarkPaddedBigBytesSmallOnePadding(b *testing.B) {
   161  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   162  	for i := 0; i < b.N; i++ {
   163  		PaddedBigBytes(bigint, 32)
   164  	}
   165  }
   166  
   167  func BenchmarkByteAtBrandNew(b *testing.B) {
   168  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   169  	for i := 0; i < b.N; i++ {
   170  		bigEndianByteAt(bigint, 15)
   171  	}
   172  }
   173  
   174  func BenchmarkByteAt(b *testing.B) {
   175  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   176  	for i := 0; i < b.N; i++ {
   177  		bigEndianByteAt(bigint, 15)
   178  	}
   179  }
   180  
   181  func BenchmarkByteAtOld(b *testing.B) {
   182  
   183  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   184  	for i := 0; i < b.N; i++ {
   185  		PaddedBigBytes(bigint, 32)
   186  	}
   187  }
   188  
   189  func TestReadBits(t *testing.T) {
   190  	check := func(input string) {
   191  		want, _ := hex.DecodeString(input)
   192  		int, _ := new(big.Int).SetString(input, 16)
   193  		buf := make([]byte, len(want))
   194  		ReadBits(int, buf)
   195  		if !bytes.Equal(buf, want) {
   196  			t.Errorf("have: %x\nwant: %x", buf, want)
   197  		}
   198  	}
   199  	check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0")
   200  	check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0")
   201  	check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0")
   202  }
   203  
   204  func TestU256(t *testing.T) {
   205  	tests := []struct{ x, y *big.Int }{
   206  		{x: big.NewInt(0), y: big.NewInt(0)},
   207  		{x: big.NewInt(1), y: big.NewInt(1)},
   208  		{x: BigPow(2, 255), y: BigPow(2, 255)},
   209  		{x: BigPow(2, 256), y: big.NewInt(0)},
   210  		{x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)},
   211  //负值
   212  		{x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))},
   213  		{x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))},
   214  		{x: BigPow(2, -255), y: big.NewInt(1)},
   215  	}
   216  	for _, test := range tests {
   217  		if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 {
   218  			t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y)
   219  		}
   220  	}
   221  }
   222  
   223  func TestBigEndianByteAt(t *testing.T) {
   224  	tests := []struct {
   225  		x   string
   226  		y   int
   227  		exp byte
   228  	}{
   229  		{"00", 0, 0x00},
   230  		{"01", 1, 0x00},
   231  		{"00", 1, 0x00},
   232  		{"01", 0, 0x01},
   233  		{"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x30},
   234  		{"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x20},
   235  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0xAB},
   236  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
   237  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 500, 0x00},
   238  	}
   239  	for _, test := range tests {
   240  		v := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
   241  		actual := bigEndianByteAt(v, test.y)
   242  		if actual != test.exp {
   243  			t.Fatalf("Expected  [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual)
   244  		}
   245  
   246  	}
   247  }
   248  func TestLittleEndianByteAt(t *testing.T) {
   249  	tests := []struct {
   250  		x   string
   251  		y   int
   252  		exp byte
   253  	}{
   254  		{"00", 0, 0x00},
   255  		{"01", 1, 0x00},
   256  		{"00", 1, 0x00},
   257  		{"01", 0, 0x00},
   258  		{"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x00},
   259  		{"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x00},
   260  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0x00},
   261  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
   262  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, 0xAB},
   263  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, 0xCD},
   264  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, 0x00},
   265  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, 0xCD},
   266  		{"0000000000000000000000000000000000000000000000000000000000102030", 31, 0x30},
   267  		{"0000000000000000000000000000000000000000000000000000000000102030", 30, 0x20},
   268  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, 0x0},
   269  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 0xFF},
   270  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFF, 0x0},
   271  	}
   272  	for _, test := range tests {
   273  		v := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
   274  		actual := Byte(v, 32, test.y)
   275  		if actual != test.exp {
   276  			t.Fatalf("Expected  [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual)
   277  		}
   278  
   279  	}
   280  }
   281  
   282  func TestS256(t *testing.T) {
   283  	tests := []struct{ x, y *big.Int }{
   284  		{x: big.NewInt(0), y: big.NewInt(0)},
   285  		{x: big.NewInt(1), y: big.NewInt(1)},
   286  		{x: big.NewInt(2), y: big.NewInt(2)},
   287  		{
   288  			x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
   289  			y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
   290  		},
   291  		{
   292  			x: BigPow(2, 255),
   293  			y: new(big.Int).Neg(BigPow(2, 255)),
   294  		},
   295  		{
   296  			x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)),
   297  			y: big.NewInt(-1),
   298  		},
   299  		{
   300  			x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)),
   301  			y: big.NewInt(-2),
   302  		},
   303  	}
   304  	for _, test := range tests {
   305  		if y := S256(test.x); y.Cmp(test.y) != 0 {
   306  			t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y)
   307  		}
   308  	}
   309  }
   310  
   311  func TestExp(t *testing.T) {
   312  	tests := []struct{ base, exponent, result *big.Int }{
   313  		{base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)},
   314  		{base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)},
   315  		{base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)},
   316  		{base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)},
   317  		{base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")},
   318  		{base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")},
   319  	}
   320  	for _, test := range tests {
   321  		if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 {
   322  			t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result)
   323  		}
   324  	}
   325  }