github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/common/number/uint_test.go (about)

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