github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/common/number/uint_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package number 13 14 import ( 15 "math/big" 16 "testing" 17 18 "github.com/Sberex/go-sberex/common" 19 ) 20 21 func TestSet(t *testing.T) { 22 a := Uint(0) 23 b := Uint(10) 24 a.Set(b) 25 if a.num.Cmp(b.num) != 0 { 26 t.Error("didn't compare", a, b) 27 } 28 29 c := Uint(0).SetBytes(common.Hex2Bytes("0a")) 30 if c.num.Cmp(big.NewInt(10)) != 0 { 31 t.Error("c set bytes failed.") 32 } 33 } 34 35 func TestInitialiser(t *testing.T) { 36 check := false 37 init := NewInitialiser(func(x *Number) *Number { 38 check = true 39 return x 40 }) 41 a := init(0).Add(init(1), init(2)) 42 if a.Cmp(init(3)) != 0 { 43 t.Error("expected 3. got", a) 44 } 45 if !check { 46 t.Error("expected limiter to be called") 47 } 48 } 49 50 func TestGet(t *testing.T) { 51 a := Uint(10) 52 if a.Uint64() != 10 { 53 t.Error("expected to get 10. got", a.Uint64()) 54 } 55 56 a = Uint(10) 57 if a.Int64() != 10 { 58 t.Error("expected to get 10. got", a.Int64()) 59 } 60 } 61 62 func TestCmp(t *testing.T) { 63 a := Uint(10) 64 b := Uint(10) 65 c := Uint(11) 66 67 if a.Cmp(b) != 0 { 68 t.Error("a b == 0 failed", a, b) 69 } 70 71 if a.Cmp(c) >= 0 { 72 t.Error("a c < 0 failed", a, c) 73 } 74 75 if c.Cmp(b) <= 0 { 76 t.Error("c b > 0 failed", c, b) 77 } 78 } 79 80 func TestMaxArith(t *testing.T) { 81 a := Uint(0).Add(MaxUint256, One) 82 if a.Cmp(Zero) != 0 { 83 t.Error("expected max256 + 1 = 0 got", a) 84 } 85 86 a = Uint(0).Sub(Uint(0), One) 87 if a.Cmp(MaxUint256) != 0 { 88 t.Error("expected 0 - 1 = max256 got", a) 89 } 90 91 a = Int(0).Sub(Int(0), One) 92 if a.Cmp(MinOne) != 0 { 93 t.Error("expected 0 - 1 = -1 got", a) 94 } 95 } 96 97 func TestConversion(t *testing.T) { 98 a := Int(-1) 99 b := a.Uint256() 100 if b.Cmp(MaxUint256) != 0 { 101 t.Error("expected -1 => unsigned to return max. got", b) 102 } 103 }