github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/common/math/big_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package math
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"math/big"
    23  	"testing"
    24  	// "github.com/wanchain/go-wanchain/common"
    25  )
    26  
    27  func TestHexOrDecimal256(t *testing.T) {
    28  	tests := []struct {
    29  		input string
    30  		num   *big.Int
    31  		ok    bool
    32  	}{
    33  		{"", big.NewInt(0), true},
    34  		{"0", big.NewInt(0), true},
    35  		{"0x0", big.NewInt(0), true},
    36  		{"12345678", big.NewInt(12345678), true},
    37  		{"0x12345678", big.NewInt(0x12345678), true},
    38  		{"0X12345678", big.NewInt(0x12345678), true},
    39  		// Tests for leading zero behaviour:
    40  		{"0123456789", big.NewInt(123456789), true}, // note: not octal
    41  		{"00", big.NewInt(0), true},
    42  		{"0x00", big.NewInt(0), true},
    43  		{"0x012345678abc", big.NewInt(0x12345678abc), true},
    44  		// Invalid syntax:
    45  		{"abcdef", nil, false},
    46  		{"0xgg", nil, false},
    47  		// Larger than 256 bits:
    48  		{"115792089237316195423570985008687907853269984665640564039457584007913129639936", nil, false},
    49  	}
    50  	for _, test := range tests {
    51  		var num HexOrDecimal256
    52  		err := num.UnmarshalText([]byte(test.input))
    53  		if (err == nil) != test.ok {
    54  			t.Errorf("ParseBig(%q) -> (err == nil) == %t, want %t", test.input, err == nil, test.ok)
    55  			continue
    56  		}
    57  		if test.num != nil && (*big.Int)(&num).Cmp(test.num) != 0 {
    58  			t.Errorf("ParseBig(%q) -> %d, want %d", test.input, (*big.Int)(&num), test.num)
    59  		}
    60  	}
    61  }
    62  
    63  func TestMustParseBig256(t *testing.T) {
    64  	defer func() {
    65  		if recover() == nil {
    66  			t.Error("MustParseBig should've panicked")
    67  		}
    68  	}()
    69  	MustParseBig256("ggg")
    70  }
    71  
    72  func TestBigMax(t *testing.T) {
    73  	a := big.NewInt(10)
    74  	b := big.NewInt(5)
    75  
    76  	max1 := BigMax(a, b)
    77  	if max1 != a {
    78  		t.Errorf("Expected %d got %d", a, max1)
    79  	}
    80  
    81  	max2 := BigMax(b, a)
    82  	if max2 != a {
    83  		t.Errorf("Expected %d got %d", a, max2)
    84  	}
    85  }
    86  
    87  func TestBigMin(t *testing.T) {
    88  	a := big.NewInt(10)
    89  	b := big.NewInt(5)
    90  
    91  	min1 := BigMin(a, b)
    92  	if min1 != b {
    93  		t.Errorf("Expected %d got %d", b, min1)
    94  	}
    95  
    96  	min2 := BigMin(b, a)
    97  	if min2 != b {
    98  		t.Errorf("Expected %d got %d", b, min2)
    99  	}
   100  }
   101  
   102  func TestFirstBigSet(t *testing.T) {
   103  	tests := []struct {
   104  		num *big.Int
   105  		ix  int
   106  	}{
   107  		{big.NewInt(0), 0},
   108  		{big.NewInt(1), 0},
   109  		{big.NewInt(2), 1},
   110  		{big.NewInt(0x100), 8},
   111  	}
   112  	for _, test := range tests {
   113  		if ix := FirstBitSet(test.num); ix != test.ix {
   114  			t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix)
   115  		}
   116  	}
   117  }
   118  
   119  func TestPaddedBigBytes(t *testing.T) {
   120  	tests := []struct {
   121  		num    *big.Int
   122  		n      int
   123  		result []byte
   124  	}{
   125  		{num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}},
   126  		{num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}},
   127  		{num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}},
   128  		{num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}},
   129  	}
   130  	for _, test := range tests {
   131  		if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) {
   132  			t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result)
   133  		}
   134  	}
   135  }
   136  
   137  func BenchmarkPaddedBigBytesLargePadding(b *testing.B) {
   138  	bigint := MustParseBig256("123456789123456789123456789123456789")
   139  	for i := 0; i < b.N; i++ {
   140  		PaddedBigBytes(bigint, 200)
   141  	}
   142  }
   143  
   144  func BenchmarkPaddedBigBytesSmallPadding(b *testing.B) {
   145  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   146  	for i := 0; i < b.N; i++ {
   147  		PaddedBigBytes(bigint, 5)
   148  	}
   149  }
   150  
   151  func BenchmarkPaddedBigBytesSmallOnePadding(b *testing.B) {
   152  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   153  	for i := 0; i < b.N; i++ {
   154  		PaddedBigBytes(bigint, 32)
   155  	}
   156  }
   157  
   158  func BenchmarkByteAtBrandNew(b *testing.B) {
   159  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   160  	for i := 0; i < b.N; i++ {
   161  		bigEndianByteAt(bigint, 15)
   162  	}
   163  }
   164  
   165  func BenchmarkByteAt(b *testing.B) {
   166  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   167  	for i := 0; i < b.N; i++ {
   168  		bigEndianByteAt(bigint, 15)
   169  	}
   170  }
   171  
   172  func BenchmarkByteAtOld(b *testing.B) {
   173  
   174  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   175  	for i := 0; i < b.N; i++ {
   176  		PaddedBigBytes(bigint, 32)
   177  	}
   178  }
   179  
   180  func TestReadBits(t *testing.T) {
   181  	check := func(input string) {
   182  		want, _ := hex.DecodeString(input)
   183  		int, _ := new(big.Int).SetString(input, 16)
   184  		buf := make([]byte, len(want))
   185  		ReadBits(int, buf)
   186  		if !bytes.Equal(buf, want) {
   187  			t.Errorf("have: %x\nwant: %x", buf, want)
   188  		}
   189  	}
   190  	check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0")
   191  	check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0")
   192  	check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0")
   193  }
   194  
   195  func TestU256(t *testing.T) {
   196  	tests := []struct{ x, y *big.Int }{
   197  		{x: big.NewInt(0), y: big.NewInt(0)},
   198  		{x: big.NewInt(1), y: big.NewInt(1)},
   199  		{x: BigPow(2, 255), y: BigPow(2, 255)},
   200  		{x: BigPow(2, 256), y: big.NewInt(0)},
   201  		{x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)},
   202  		// negative values
   203  		{x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))},
   204  		{x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))},
   205  		{x: BigPow(2, -255), y: big.NewInt(1)},
   206  	}
   207  	for _, test := range tests {
   208  		if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 {
   209  			t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y)
   210  		}
   211  	}
   212  }
   213  
   214  func TestBigEndianByteAt(t *testing.T) {
   215  	tests := []struct {
   216  		x   string
   217  		y   int
   218  		exp byte
   219  	}{
   220  		{"00", 0, 0x00},
   221  		{"01", 1, 0x00},
   222  		{"00", 1, 0x00},
   223  		{"01", 0, 0x01},
   224  		{"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x30},
   225  		{"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x20},
   226  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0xAB},
   227  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
   228  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 500, 0x00},
   229  	}
   230  	for _, test := range tests {
   231  		raw, _ := hex.DecodeString(test.x)
   232  		v := new(big.Int).SetBytes(raw)
   233  		actual := bigEndianByteAt(v, test.y)
   234  		if actual != test.exp {
   235  			t.Fatalf("Expected  [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual)
   236  		}
   237  
   238  	}
   239  }
   240  func TestLittleEndianByteAt(t *testing.T) {
   241  	tests := []struct {
   242  		x   string
   243  		y   int
   244  		exp byte
   245  	}{
   246  		{"00", 0, 0x00},
   247  		{"01", 1, 0x00},
   248  		{"00", 1, 0x00},
   249  		{"01", 0, 0x00},
   250  		{"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x00},
   251  		{"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x00},
   252  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0x00},
   253  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
   254  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, 0xAB},
   255  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, 0xCD},
   256  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, 0x00},
   257  		{"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, 0xCD},
   258  		{"0000000000000000000000000000000000000000000000000000000000102030", 31, 0x30},
   259  		{"0000000000000000000000000000000000000000000000000000000000102030", 30, 0x20},
   260  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, 0x0},
   261  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 0xFF},
   262  		{"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFF, 0x0},
   263  	}
   264  	for _, test := range tests {
   265  		raw, _ := hex.DecodeString(test.x)
   266  		v := new(big.Int).SetBytes(raw)
   267  		actual := Byte(v, 32, test.y)
   268  		if actual != test.exp {
   269  			t.Fatalf("Expected  [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual)
   270  		}
   271  
   272  	}
   273  }
   274  
   275  func TestS256(t *testing.T) {
   276  	tests := []struct{ x, y *big.Int }{
   277  		{x: big.NewInt(0), y: big.NewInt(0)},
   278  		{x: big.NewInt(1), y: big.NewInt(1)},
   279  		{x: big.NewInt(2), y: big.NewInt(2)},
   280  		{
   281  			x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
   282  			y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
   283  		},
   284  		{
   285  			x: BigPow(2, 255),
   286  			y: new(big.Int).Neg(BigPow(2, 255)),
   287  		},
   288  		{
   289  			x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)),
   290  			y: big.NewInt(-1),
   291  		},
   292  		{
   293  			x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)),
   294  			y: big.NewInt(-2),
   295  		},
   296  	}
   297  	for _, test := range tests {
   298  		if y := S256(test.x); y.Cmp(test.y) != 0 {
   299  			t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y)
   300  		}
   301  	}
   302  }
   303  
   304  func TestExp(t *testing.T) {
   305  	tests := []struct{ base, exponent, result *big.Int }{
   306  		{base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)},
   307  		{base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)},
   308  		{base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)},
   309  		{base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)},
   310  		{base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")},
   311  		{base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")},
   312  	}
   313  	for _, test := range tests {
   314  		if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 {
   315  			t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result)
   316  		}
   317  	}
   318  }