cosmos-sdk/math/dec_internal_test.go
Amaury eee23d9531
refactor: Move sdk.Dec to math package (#12634)
## Description





---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-07-20 15:13:45 +00:00

84 lines
2.4 KiB
Go

package math
import (
"encoding/json"
"math/big"
"testing"
"github.com/stretchr/testify/suite"
)
type decimalInternalTestSuite struct {
suite.Suite
}
func TestDecimalInternalTestSuite(t *testing.T) {
suite.Run(t, new(decimalInternalTestSuite))
}
func (s *decimalInternalTestSuite) TestPrecisionMultiplier() {
res := precisionMultiplier(5)
exp := big.NewInt(10000000000000)
s.Require().Equal(0, res.Cmp(exp), "equality was incorrect, res %v, exp %v", res, exp)
}
func (s *decimalInternalTestSuite) TestZeroDeserializationJSON() {
d := LegacyDec{new(big.Int)}
err := json.Unmarshal([]byte(`"0"`), &d)
s.Require().Nil(err)
err = json.Unmarshal([]byte(`"{}"`), &d)
s.Require().NotNil(err)
}
func (s *decimalInternalTestSuite) TestSerializationGocodecJSON() {
d := LegacyMustNewDecFromStr("0.333")
bz, err := json.Marshal(d)
s.Require().NoError(err)
d2 := LegacyDec{new(big.Int)}
err = json.Unmarshal(bz, &d2)
s.Require().NoError(err)
s.Require().True(d.Equal(d2), "original: %v, unmarshalled: %v", d, d2)
}
func (s *decimalInternalTestSuite) TestDecMarshalJSON() {
decimal := func(i int64) LegacyDec {
d := LegacyNewDec(0)
d.i = new(big.Int).SetInt64(i)
return d
}
tests := []struct {
name string
d LegacyDec
want string
wantErr bool // if wantErr = false, will also attempt unmarshaling
}{
{"zero", decimal(0), "\"0.000000000000000000\"", false},
{"one", decimal(1), "\"0.000000000000000001\"", false},
{"ten", decimal(10), "\"0.000000000000000010\"", false},
{"12340", decimal(12340), "\"0.000000000000012340\"", false},
{"zeroInt", LegacyNewDec(0), "\"0.000000000000000000\"", false},
{"oneInt", LegacyNewDec(1), "\"1.000000000000000000\"", false},
{"tenInt", LegacyNewDec(10), "\"10.000000000000000000\"", false},
{"12340Int", LegacyNewDec(12340), "\"12340.000000000000000000\"", false},
}
for _, tt := range tests {
tt := tt
s.T().Run(tt.name, func(t *testing.T) {
got, err := tt.d.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("Dec.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr {
s.Require().Equal(tt.want, string(got), "incorrect marshalled value")
unmarshalledDec := LegacyNewDec(0)
err := unmarshalledDec.UnmarshalJSON(got)
s.Require().NoError(err)
s.Require().Equal(tt.d, unmarshalledDec, "incorrect unmarshalled value")
}
})
}
}