github.com/avence12/go-ethereum@v1.5.10-0.20170320123548-1dfd65f6d047/common/math/big_test.go (about)

     1  // Copyright 2014 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  )
    25  
    26  func TestParseBig256(t *testing.T) {
    27  	tests := []struct {
    28  		input string
    29  		num   *big.Int
    30  		ok    bool
    31  	}{
    32  		{"", big.NewInt(0), true},
    33  		{"0", big.NewInt(0), true},
    34  		{"0x0", big.NewInt(0), true},
    35  		{"12345678", big.NewInt(12345678), true},
    36  		{"0x12345678", big.NewInt(0x12345678), true},
    37  		{"0X12345678", big.NewInt(0x12345678), true},
    38  		// Tests for leading zero behaviour:
    39  		{"0123456789", big.NewInt(123456789), true}, // note: not octal
    40  		{"00", big.NewInt(0), true},
    41  		{"0x00", big.NewInt(0), true},
    42  		{"0x012345678abc", big.NewInt(0x12345678abc), true},
    43  		// Invalid syntax:
    44  		{"abcdef", nil, false},
    45  		{"0xgg", nil, false},
    46  		// Larger than 256 bits:
    47  		{"115792089237316195423570985008687907853269984665640564039457584007913129639936", nil, false},
    48  	}
    49  	for _, test := range tests {
    50  		num, ok := ParseBig256(test.input)
    51  		if ok != test.ok {
    52  			t.Errorf("ParseBig(%q) -> ok = %t, want %t", test.input, ok, test.ok)
    53  			continue
    54  		}
    55  		if num != nil && test.num != nil && num.Cmp(test.num) != 0 {
    56  			t.Errorf("ParseBig(%q) -> %d, want %d", test.input, num, test.num)
    57  		}
    58  	}
    59  }
    60  
    61  func TestMustParseBig256(t *testing.T) {
    62  	defer func() {
    63  		if recover() == nil {
    64  			t.Error("MustParseBig should've panicked")
    65  		}
    66  	}()
    67  	MustParseBig256("ggg")
    68  }
    69  
    70  func TestBigMax(t *testing.T) {
    71  	a := big.NewInt(10)
    72  	b := big.NewInt(5)
    73  
    74  	max1 := BigMax(a, b)
    75  	if max1 != a {
    76  		t.Errorf("Expected %d got %d", a, max1)
    77  	}
    78  
    79  	max2 := BigMax(b, a)
    80  	if max2 != a {
    81  		t.Errorf("Expected %d got %d", a, max2)
    82  	}
    83  }
    84  
    85  func TestBigMin(t *testing.T) {
    86  	a := big.NewInt(10)
    87  	b := big.NewInt(5)
    88  
    89  	min1 := BigMin(a, b)
    90  	if min1 != b {
    91  		t.Errorf("Expected %d got %d", b, min1)
    92  	}
    93  
    94  	min2 := BigMin(b, a)
    95  	if min2 != b {
    96  		t.Errorf("Expected %d got %d", b, min2)
    97  	}
    98  }
    99  
   100  func TestFirstBigSet(t *testing.T) {
   101  	tests := []struct {
   102  		num *big.Int
   103  		ix  int
   104  	}{
   105  		{big.NewInt(0), 0},
   106  		{big.NewInt(1), 0},
   107  		{big.NewInt(2), 1},
   108  		{big.NewInt(0x100), 8},
   109  	}
   110  	for _, test := range tests {
   111  		if ix := FirstBitSet(test.num); ix != test.ix {
   112  			t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix)
   113  		}
   114  	}
   115  }
   116  
   117  func TestPaddedBigBytes(t *testing.T) {
   118  	tests := []struct {
   119  		num    *big.Int
   120  		n      int
   121  		result []byte
   122  	}{
   123  		{num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}},
   124  		{num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}},
   125  		{num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}},
   126  		{num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}},
   127  	}
   128  	for _, test := range tests {
   129  		if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) {
   130  			t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result)
   131  		}
   132  	}
   133  }
   134  
   135  func BenchmarkPaddedBigBytes(b *testing.B) {
   136  	bigint := MustParseBig256("123456789123456789123456789123456789")
   137  	for i := 0; i < b.N; i++ {
   138  		PaddedBigBytes(bigint, 32)
   139  	}
   140  }
   141  
   142  func TestReadBits(t *testing.T) {
   143  	check := func(input string) {
   144  		want, _ := hex.DecodeString(input)
   145  		int, _ := new(big.Int).SetString(input, 16)
   146  		buf := make([]byte, len(want))
   147  		ReadBits(int, buf)
   148  		if !bytes.Equal(buf, want) {
   149  			t.Errorf("have: %x\nwant: %x", buf, want)
   150  		}
   151  	}
   152  	check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0")
   153  	check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0")
   154  	check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0")
   155  }
   156  
   157  func TestU256(t *testing.T) {
   158  	tests := []struct{ x, y *big.Int }{
   159  		{x: big.NewInt(0), y: big.NewInt(0)},
   160  		{x: big.NewInt(1), y: big.NewInt(1)},
   161  		{x: BigPow(2, 255), y: BigPow(2, 255)},
   162  		{x: BigPow(2, 256), y: big.NewInt(0)},
   163  		{x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)},
   164  		// negative values
   165  		{x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))},
   166  		{x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))},
   167  		{x: BigPow(2, -255), y: big.NewInt(1)},
   168  	}
   169  	for _, test := range tests {
   170  		if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 {
   171  			t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y)
   172  		}
   173  	}
   174  }
   175  
   176  func TestS256(t *testing.T) {
   177  	tests := []struct{ x, y *big.Int }{
   178  		{x: big.NewInt(0), y: big.NewInt(0)},
   179  		{x: big.NewInt(1), y: big.NewInt(1)},
   180  		{x: big.NewInt(2), y: big.NewInt(2)},
   181  		{
   182  			x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
   183  			y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
   184  		},
   185  		{
   186  			x: BigPow(2, 255),
   187  			y: new(big.Int).Neg(BigPow(2, 255)),
   188  		},
   189  		{
   190  			x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)),
   191  			y: big.NewInt(-1),
   192  		},
   193  		{
   194  			x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)),
   195  			y: big.NewInt(-2),
   196  		},
   197  	}
   198  	for _, test := range tests {
   199  		if y := S256(test.x); y.Cmp(test.y) != 0 {
   200  			t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y)
   201  		}
   202  	}
   203  }
   204  
   205  func TestExp(t *testing.T) {
   206  	tests := []struct{ base, exponent, result *big.Int }{
   207  		{base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)},
   208  		{base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)},
   209  		{base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)},
   210  		{base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)},
   211  		{base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")},
   212  		{base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")},
   213  	}
   214  	for _, test := range tests {
   215  		if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 {
   216  			t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result)
   217  		}
   218  	}
   219  }