github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/common/number/uint_test.go (about)

     1  // Copyright 2015 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 number
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/vntchain/go-vnt/common"
    24  )
    25  
    26  func TestSet(t *testing.T) {
    27  	a := Uint(0)
    28  	b := Uint(10)
    29  	a.Set(b)
    30  	if a.num.Cmp(b.num) != 0 {
    31  		t.Error("didn't compare", a, b)
    32  	}
    33  
    34  	c := Uint(0).SetBytes(common.Hex2Bytes("0a"))
    35  	if c.num.Cmp(big.NewInt(10)) != 0 {
    36  		t.Error("c set bytes failed.")
    37  	}
    38  }
    39  
    40  func TestInitialiser(t *testing.T) {
    41  	check := false
    42  	init := NewInitialiser(func(x *Number) *Number {
    43  		check = true
    44  		return x
    45  	})
    46  	a := init(0).Add(init(1), init(2))
    47  	if a.Cmp(init(3)) != 0 {
    48  		t.Error("expected 3. got", a)
    49  	}
    50  	if !check {
    51  		t.Error("expected limiter to be called")
    52  	}
    53  }
    54  
    55  func TestGet(t *testing.T) {
    56  	a := Uint(10)
    57  	if a.Uint64() != 10 {
    58  		t.Error("expected to get 10. got", a.Uint64())
    59  	}
    60  
    61  	a = Uint(10)
    62  	if a.Int64() != 10 {
    63  		t.Error("expected to get 10. got", a.Int64())
    64  	}
    65  }
    66  
    67  func TestCmp(t *testing.T) {
    68  	a := Uint(10)
    69  	b := Uint(10)
    70  	c := Uint(11)
    71  
    72  	if a.Cmp(b) != 0 {
    73  		t.Error("a b == 0 failed", a, b)
    74  	}
    75  
    76  	if a.Cmp(c) >= 0 {
    77  		t.Error("a c < 0 failed", a, c)
    78  	}
    79  
    80  	if c.Cmp(b) <= 0 {
    81  		t.Error("c b > 0 failed", c, b)
    82  	}
    83  }
    84  
    85  func TestMaxArith(t *testing.T) {
    86  	a := Uint(0).Add(MaxUint256, One)
    87  	if a.Cmp(Zero) != 0 {
    88  		t.Error("expected max256 + 1 = 0 got", a)
    89  	}
    90  
    91  	a = Uint(0).Sub(Uint(0), One)
    92  	if a.Cmp(MaxUint256) != 0 {
    93  		t.Error("expected 0 - 1 = max256 got", a)
    94  	}
    95  
    96  	a = Int(0).Sub(Int(0), One)
    97  	if a.Cmp(MinOne) != 0 {
    98  		t.Error("expected 0 - 1 = -1 got", a)
    99  	}
   100  }
   101  
   102  func TestConversion(t *testing.T) {
   103  	a := Int(-1)
   104  	b := a.Uint256()
   105  	if b.Cmp(MaxUint256) != 0 {
   106  		t.Error("expected -1 => unsigned to return max. got", b)
   107  	}
   108  }