github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/types/balance_test.go (about) 1 package types_test 2 3 import ( 4 "math/big" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/onflow/cadence" 10 11 "github.com/onflow/flow-go/fvm/evm/types" 12 ) 13 14 func TestBalance(t *testing.T) { 15 // test attoflow to flow 16 bal := types.OneFlowBalance 17 require.Equal(t, bal, types.NewBalanceFromUFix64(types.OneFlowInUFix64)) 18 19 // 100.0002 Flow 20 u, err := cadence.NewUFix64("100.0002") 21 require.NoError(t, err) 22 require.Equal(t, "100.00020000", u.String()) 23 24 bb := types.NewBalanceFromUFix64(u) 25 require.Equal(t, "100000200000000000000", types.BalanceToBigInt(bb).String()) 26 require.False(t, types.BalanceConvertionToUFix64ProneToRoundingError(bb)) 27 bret, roundedOff, err := types.ConvertBalanceToUFix64(bb) 28 require.NoError(t, err) 29 require.Equal(t, u, bret) 30 require.False(t, roundedOff) 31 32 // rounded off flag 33 bal = types.NewBalance(big.NewInt(1)) 34 require.NoError(t, err) 35 require.True(t, types.BalanceConvertionToUFix64ProneToRoundingError(bal)) 36 bret, roundedOff, err = types.ConvertBalanceToUFix64(bal) 37 require.NoError(t, err) 38 require.Equal(t, cadence.UFix64(0), bret) 39 require.True(t, roundedOff) 40 }