github.com/Finschia/finschia-sdk@v0.48.1/types/decimal_internal_test.go (about)

     1  package types
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/suite"
     8  
     9  	"github.com/Finschia/finschia-sdk/codec"
    10  )
    11  
    12  type decimalInternalTestSuite struct {
    13  	suite.Suite
    14  }
    15  
    16  func TestDecimalInternalTestSuite(t *testing.T) {
    17  	suite.Run(t, new(decimalInternalTestSuite))
    18  }
    19  
    20  func (s *decimalInternalTestSuite) TestPrecisionMultiplier() {
    21  	res := precisionMultiplier(5)
    22  	exp := big.NewInt(10000000000000)
    23  	s.Require().Equal(0, res.Cmp(exp), "equality was incorrect, res %v, exp %v", res, exp)
    24  }
    25  
    26  func (s *decimalInternalTestSuite) TestZeroDeserializationJSON() {
    27  	var cdc = codec.NewLegacyAmino()
    28  	d := Dec{new(big.Int)}
    29  	err := cdc.UnmarshalJSON([]byte(`"0"`), &d)
    30  	s.Require().Nil(err)
    31  	err = cdc.UnmarshalJSON([]byte(`"{}"`), &d)
    32  	s.Require().NotNil(err)
    33  }
    34  
    35  func (s *decimalInternalTestSuite) TestSerializationGocodecJSON() {
    36  	var cdc = codec.NewLegacyAmino()
    37  	d := MustNewDecFromStr("0.333")
    38  
    39  	bz, err := cdc.MarshalJSON(d)
    40  	s.Require().NoError(err)
    41  
    42  	d2 := Dec{new(big.Int)}
    43  	err = cdc.UnmarshalJSON(bz, &d2)
    44  	s.Require().NoError(err)
    45  	s.Require().True(d.Equal(d2), "original: %v, unmarshalled: %v", d, d2)
    46  }
    47  
    48  func (s *decimalInternalTestSuite) TestDecMarshalJSON() {
    49  	decimal := func(i int64) Dec {
    50  		d := NewDec(0)
    51  		d.i = new(big.Int).SetInt64(i)
    52  		return d
    53  	}
    54  	tests := []struct {
    55  		name    string
    56  		d       Dec
    57  		want    string
    58  		wantErr bool // if wantErr = false, will also attempt unmarshaling
    59  	}{
    60  		{"zero", decimal(0), "\"0.000000000000000000\"", false},
    61  		{"one", decimal(1), "\"0.000000000000000001\"", false},
    62  		{"ten", decimal(10), "\"0.000000000000000010\"", false},
    63  		{"12340", decimal(12340), "\"0.000000000000012340\"", false},
    64  		{"zeroInt", NewDec(0), "\"0.000000000000000000\"", false},
    65  		{"oneInt", NewDec(1), "\"1.000000000000000000\"", false},
    66  		{"tenInt", NewDec(10), "\"10.000000000000000000\"", false},
    67  		{"12340Int", NewDec(12340), "\"12340.000000000000000000\"", false},
    68  	}
    69  	for _, tt := range tests {
    70  		tt := tt
    71  		s.T().Run(tt.name, func(t *testing.T) {
    72  			got, err := tt.d.MarshalJSON()
    73  			if (err != nil) != tt.wantErr {
    74  				t.Errorf("Dec.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
    75  				return
    76  			}
    77  			if !tt.wantErr {
    78  				s.Require().Equal(tt.want, string(got), "incorrect marshalled value")
    79  				unmarshalledDec := NewDec(0)
    80  				err := unmarshalledDec.UnmarshalJSON(got)
    81  				s.Require().NoError(err)
    82  				s.Require().Equal(tt.d, unmarshalledDec, "incorrect unmarshalled value")
    83  			}
    84  		})
    85  	}
    86  }