github.com/annchain/OG@v0.0.9/common/math/bigint_test.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package math 15 16 import ( 17 "encoding/hex" 18 "fmt" 19 "github.com/stretchr/testify/assert" 20 "testing" 21 ) 22 23 func TestMoney(t *testing.T) { 24 s := "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" 25 a, success := NewBigIntFromString(s, 10) 26 if !success { 27 panic(s) 28 } 29 30 a.SetString(s, 10) 31 a2 := NewBigInt(2) 32 a3 := NewBigIntFromBigInt(a.Value.Add(a.Value, a2.Value)) 33 34 fmt.Println(a) 35 fmt.Println(a2) 36 fmt.Println(a3) 37 38 a3 = NewBigIntFromBigInt(a.Value.Add(a.Value, a2.Value)) 39 40 fmt.Println(a3) 41 } 42 43 func TestSerialization(t *testing.T) { 44 s := "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" 45 a, success := NewBigIntFromString(s, 10) 46 if !success { 47 panic(s) 48 } 49 bytes, err := a.MarshalMsg(nil) 50 assert.NoError(t, err) 51 hex.Dump(bytes) 52 b := &BigInt{} 53 bytes, err = b.UnmarshalMsg(bytes) 54 assert.NoError(t, err) 55 fmt.Println(b.String()) 56 assert.Equal(t, s, b.String()) 57 } 58 59 func TestBigInt_Add(t *testing.T) { 60 bi := NewBigInt(1000) 61 fmt.Println(bi) 62 bi = bi.Add(NewBigInt(99)) 63 fmt.Println(bi) 64 }