github.com/lino-network/lino@v0.6.11/types/dec_test.go (about)

     1  package types
     2  
     3  import (
     4  	"testing"
     5  
     6  	sdk "github.com/cosmos/cosmos-sdk/types"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  // add test for NewDecFromRat
    11  func TestDecFromRat(t *testing.T) {
    12  	assert := assert.New(t)
    13  	rst := NewDecFromRat(1, 3)
    14  	assert.Equal(sdk.MustNewDecFromStr("0.333333333333333333"), rst)
    15  }
    16  
    17  // precision lost case.
    18  // NOTE(yumin): this test case does not guarantee anything, instead, it shows
    19  // a case where precision can be lost if you do:
    20  // rst = (a / b) * c
    21  // instead of,
    22  // rst = (a * c) / b
    23  // we should use the latter form throughout the project, if possible, namely @mul-first-form.
    24  func TestErrors(t *testing.T) {
    25  	assert := assert.New(t)
    26  	expected := NewDecFromRat(316250000, 63)
    27  	a := sdk.NewDec(316250000)
    28  	b := sdk.NewDec(63)
    29  	ratio := sdk.NewDec(1).Quo(b)
    30  	rst := a.Mul(ratio)
    31  	assert.NotEqual(expected, rst)
    32  }