github.com/elastos/Elastos.ELA.SideChain.ETH@v0.2.2/common/math/big_test.go (about)

     1  // Copyright 2017 The Elastos.ELA.SideChain.ESC Authors
     2  // This file is part of the Elastos.ELA.SideChain.ESC library.
     3  //
     4  // The Elastos.ELA.SideChain.ESC 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 Elastos.ELA.SideChain.ESC 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 Elastos.ELA.SideChain.ESC 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  
    25  	"github.com/elastos/Elastos.ELA.SideChain.ESC/common"
    26  )
    27  
    28  func TestHexOrDecimal256(t *testing.T) {
    29  	tests := []struct {
    30  		input string
    31  		num   *big.Int
    32  		ok    bool
    33  	}{
    34  		{"", big.NewInt(0), true},
    35  		{"0", big.NewInt(0), true},
    36  		{"0x0", big.NewInt(0), true},
    37  		{"12345678", big.NewInt(12345678), true},
    38  		{"0x12345678", big.NewInt(0x12345678), true},
    39  		{"0X12345678", big.NewInt(0x12345678), true},
    40  		// Tests for leading zero behaviour:
    41  		{"0123456789", big.NewInt(123456789), true}, // note: not octal
    42  		{"00", big.NewInt(0), true},
    43  		{"0x00", big.NewInt(0), true},
    44  		{"0x012345678abc", big.NewInt(0x12345678abc), true},
    45  		// Invalid syntax:
    46  		{"abcdef", nil, false},
    47  		{"0xgg", nil, false},
    48  		// Larger than 256 bits:
    49  		{"115792089237316195423570985008687907853269984665640564039457584007913129639936", nil, false},
    50  	}
    51  	for _, test := range tests {
    52  		var num HexOrDecimal256
    53  		err := num.UnmarshalText([]byte(test.input))
    54  		if (err == nil) != test.ok {
    55  			t.Errorf("ParseBig(%q) -> (err == nil) == %t, want %t", test.input, err == nil, test.ok)
    56  			continue
    57  		}
    58  		if test.num != nil && (*big.Int)(&num).Cmp(test.num) != 0 {
    59  			t.Errorf("ParseBig(%q) -> %d, want %d", test.input, (*big.Int)(&num), test.num)
    60  		}
    61  	}
    62  }
    63  
    64  func TestMustParseBig256(t *testing.T) {
    65  	defer func() {
    66  		if recover() == nil {
    67  			t.Error("MustParseBig should've panicked")
    68  		}
    69  	}()
    70  	MustParseBig256("ggg")
    71  }
    72  
    73  func TestBigMax(t *testing.T) {
    74  	a := big.NewInt(10)
    75  	b := big.NewInt(5)
    76  
    77  	max1 := BigMax(a, b)
    78  	if max1 != a {
    79  		t.Errorf("Expected %d got %d", a, max1)
    80  	}
    81  
    82  	max2 := BigMax(b, a)
    83  	if max2 != a {
    84  		t.Errorf("Expected %d got %d", a, max2)
    85  	}
    86  }
    87  
    88  func TestBigMin(t *testing.T) {
    89  	a := big.NewInt(10)
    90  	b := big.NewInt(5)
    91  
    92  	min1 := BigMin(a, b)
    93  	if min1 != b {
    94  		t.Errorf("Expected %d got %d", b, min1)
    95  	}
    96  
    97  	min2 := BigMin(b, a)
    98  	if min2 != b {
    99  		t.Errorf("Expected %d got %d", b, min2)
   100  	}
   101  }
   102  
   103  func TestFirstBigSet(t *testing.T) {
   104  	tests := []struct {
   105  		num *big.Int
   106  		ix  int
   107  	}{
   108  		{big.NewInt(0), 0},
   109  		{big.NewInt(1), 0},
   110  		{big.NewInt(2), 1},
   111  		{big.NewInt(0x100), 8},
   112  	}
   113  	for _, test := range tests {
   114  		if ix := FirstBitSet(test.num); ix != test.ix {
   115  			t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix)
   116  		}
   117  	}
   118  }
   119  
   120  func TestPaddedBigBytes(t *testing.T) {
   121  	tests := []struct {
   122  		num    *big.Int
   123  		n      int
   124  		result []byte
   125  	}{
   126  		{num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}},
   127  		{num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}},
   128  		{num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}},
   129  		{num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}},
   130  	}
   131  	for _, test := range tests {
   132  		if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) {
   133  			t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result)
   134  		}
   135  	}
   136  }
   137  
   138  func BenchmarkPaddedBigBytesLargePadding(b *testing.B) {
   139  	bigint := MustParseBig256("123456789123456789123456789123456789")
   140  	for i := 0; i < b.N; i++ {
   141  		PaddedBigBytes(bigint, 200)
   142  	}
   143  }
   144  
   145  func BenchmarkPaddedBigBytesSmallPadding(b *testing.B) {
   146  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   147  	for i := 0; i < b.N; i++ {
   148  		PaddedBigBytes(bigint, 5)
   149  	}
   150  }
   151  
   152  func BenchmarkPaddedBigBytesSmallOnePadding(b *testing.B) {
   153  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   154  	for i := 0; i < b.N; i++ {
   155  		PaddedBigBytes(bigint, 32)
   156  	}
   157  }
   158  
   159  func BenchmarkByteAtBrandNew(b *testing.B) {
   160  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   161  	for i := 0; i < b.N; i++ {
   162  		bigEndianByteAt(bigint, 15)
   163  	}
   164  }
   165  
   166  func BenchmarkByteAt(b *testing.B) {
   167  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   168  	for i := 0; i < b.N; i++ {
   169  		bigEndianByteAt(bigint, 15)
   170  	}
   171  }
   172  
   173  func BenchmarkByteAtOld(b *testing.B) {
   174  
   175  	bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC")
   176  	for i := 0; i < b.N; i++ {
   177  		PaddedBigBytes(bigint, 32)
   178  	}
   179  }
   180  
   181  func TestReadBits(t *testing.T) {
   182  	check := func(input string) {
   183  		want, _ := hex.DecodeString(input)
   184  		int, _ := new(big.Int).SetString(input, 16)
   185  		buf := make([]byte, len(want))
   186  		ReadBits(int, buf)
   187  		if !bytes.Equal(buf, want) {
   188  			t.Errorf("have: %x\nwant: %x", buf, want)
   189  		}
   190  	}
   191  	check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0")
   192  	check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0")
   193  	check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0")
   194  }
   195  
   196  func TestU256(t *testing.T) {
   197  	tests := []struct{ x, y *big.Int }{
   198  		{x: big.NewInt(0), y: big.NewInt(0)},
   199  		{x: big.NewInt(1), y: big.NewInt(1)},
   200  		{x: BigPow(2, 255), y: BigPow(2, 255)},
   201  		{x: BigPow(2, 256), y: big.NewInt(0)},
   202  		{x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)},
   203  		// negative values
   204  		{x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))},
   205  		{x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))},
   206  		{x: BigPow(2, -255), y: big.NewInt(1)},
   207  	}
   208  	for _, test := range tests {
   209  		if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 {
   210  			t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y)
   211  		}
   212  	}
   213  }
   214  
   215  func TestBigEndianByteAt(t *testing.T) {
   216  	tests := []struct {
   217  		x   string
   218  		y   int
   219  		exp byte
   220  	}{
   221  		{"00", 0, 0x00},
   222  		{"01", 1, 0x00},
   223  		{"00", 1, 0x00},
   224  		{"01", 0, 0x01},
   225  		{"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x30},
   226  		{"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x20},
   227  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0xAB},
   228  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00},
   229  		{"ABCDEF0908070605040302010000000000000000000000000000000000000000", 500, 0x00},
   230  	}
   231  	for _, test := range tests {
   232  		v := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
   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  		v := new(big.Int).SetBytes(common.Hex2Bytes(test.x))
   266  		actual := Byte(v, 32, test.y)
   267  		if actual != test.exp {
   268  			t.Fatalf("Expected  [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual)
   269  		}
   270  
   271  	}
   272  }
   273  
   274  func TestS256(t *testing.T) {
   275  	tests := []struct{ x, y *big.Int }{
   276  		{x: big.NewInt(0), y: big.NewInt(0)},
   277  		{x: big.NewInt(1), y: big.NewInt(1)},
   278  		{x: big.NewInt(2), y: big.NewInt(2)},
   279  		{
   280  			x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
   281  			y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
   282  		},
   283  		{
   284  			x: BigPow(2, 255),
   285  			y: new(big.Int).Neg(BigPow(2, 255)),
   286  		},
   287  		{
   288  			x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)),
   289  			y: big.NewInt(-1),
   290  		},
   291  		{
   292  			x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)),
   293  			y: big.NewInt(-2),
   294  		},
   295  	}
   296  	for _, test := range tests {
   297  		if y := S256(test.x); y.Cmp(test.y) != 0 {
   298  			t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y)
   299  		}
   300  	}
   301  }
   302  
   303  func TestExp(t *testing.T) {
   304  	tests := []struct{ base, exponent, result *big.Int }{
   305  		{base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)},
   306  		{base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)},
   307  		{base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)},
   308  		{base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)},
   309  		{base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")},
   310  		{base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")},
   311  	}
   312  	for _, test := range tests {
   313  		if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 {
   314  			t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result)
   315  		}
   316  	}
   317  }