github.com/trezor/blockbook@v0.4.1-0.20240328132726-e9a08582ee2c/bchain/baseparser_test.go (about) 1 //go:build unittest 2 3 package bchain 4 5 import ( 6 "math/big" 7 "testing" 8 9 "github.com/trezor/blockbook/common" 10 ) 11 12 func NewBaseParser(adp int) *BaseParser { 13 return &BaseParser{ 14 AmountDecimalPoint: adp, 15 } 16 } 17 18 var amounts = []struct { 19 a *big.Int 20 s string 21 adp int 22 alternative string 23 }{ 24 {big.NewInt(123456789), "1.23456789", 8, "!"}, 25 {big.NewInt(2), "0.00000002", 8, "!"}, 26 {big.NewInt(300000000), "3", 8, "!"}, 27 {big.NewInt(498700000), "4.987", 8, "!"}, 28 {big.NewInt(567890), "0.00000000000056789", 18, "!"}, 29 {big.NewInt(-100000000), "-1", 8, "!"}, 30 {big.NewInt(-8), "-0.00000008", 8, "!"}, 31 {big.NewInt(-89012345678), "-890.12345678", 8, "!"}, 32 {big.NewInt(-12345), "-0.00012345", 8, "!"}, 33 {big.NewInt(12345678), "0.123456789012", 8, "0.12345678"}, // test of truncation of too many decimal places 34 {big.NewInt(12345678), "0.0000000000000000000000000000000012345678", 1234, "!"}, // test of too big number decimal places 35 } 36 37 func TestBaseParser_AmountToDecimalString(t *testing.T) { 38 for _, tt := range amounts { 39 t.Run(tt.s, func(t *testing.T) { 40 if got := NewBaseParser(tt.adp).AmountToDecimalString(tt.a); got != tt.s && got != tt.alternative { 41 t.Errorf("BaseParser.AmountToDecimalString() = %v, want %v", got, tt.s) 42 } 43 }) 44 } 45 } 46 47 func TestBaseParser_AmountToBigInt(t *testing.T) { 48 for _, tt := range amounts { 49 t.Run(tt.s, func(t *testing.T) { 50 got, err := NewBaseParser(tt.adp).AmountToBigInt(common.JSONNumber(tt.s)) 51 if err != nil { 52 t.Errorf("BaseParser.AmountToBigInt() error = %v", err) 53 return 54 } 55 if got.Cmp(tt.a) != 0 { 56 t.Errorf("BaseParser.AmountToBigInt() = %v, want %v", got, tt.a) 57 } 58 }) 59 } 60 }