Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add DecApproxEq to decimal #10592

Merged
merged 11 commits into from
Nov 30, 2021
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
## [Unreleased]

### Features

* [\#10592](https://github.com/cosmos/cosmos-sdk/pull/10592) Add `DecApproxEq` method to `decimal_test.go` to simplify decimal testing.
pwang00 marked this conversation as resolved.
Show resolved Hide resolved
* [\#10393](https://github.com/cosmos/cosmos-sdk/pull/10393) Add `HasSupply` method to bank keeper to ensure that input denom actually exists on chain.
* [\#9933](https://github.com/cosmos/cosmos-sdk/pull/9933) Introduces the notion of a Cosmos "Scalar" type, which would just be simple aliases that give human-understandable meaning to the underlying type, both in Go code and in Proto definitions.
* [\#9884](https://github.com/cosmos/cosmos-sdk/pull/9884) Provide a new gRPC query handler, `/cosmos/params/v1beta1/subspaces`, that allows the ability to query for all registered subspaces and their respective keys.
Expand Down
5 changes: 5 additions & 0 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,8 @@ func MaxDec(d1, d2 Dec) Dec {
func DecEq(t *testing.T, exp, got Dec) (*testing.T, bool, string, string, string) {
return t, exp.Equal(got), "expected:\t%v\ngot:\t\t%v", exp.String(), got.String()
}

func DecApproxEq(t *testing.T, d1 Dec, d2 Dec, tol Dec) (*testing.T, bool, string, string, string) {
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
diff := d1.Sub(d2).Abs()
return t, diff.LTE(tol), "expected |d1 - d2| <:\t%v\ngot |d1 - d2| = \t\t%v", tol.String(), diff.String()
}
26 changes: 26 additions & 0 deletions types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"sigs.k8s.io/yaml"

Expand All @@ -21,6 +22,31 @@ func TestDecimalTestSuite(t *testing.T) {
suite.Run(t, new(decimalTestSuite))
}

func TestDecApproxEq(t *testing.T) {

// d1 = 5.5, d2 = 6, tol = 0.1
pwang00 marked this conversation as resolved.
Show resolved Hide resolved
d1 := sdk.NewDecWithPrec(55, 2)
d2 := sdk.NewDecWithPrec(6, 1)
tol := sdk.NewDecWithPrec(1, 1)

assert.True(sdk.DecApproxEq(t, d1, d2, tol))

// d1 = 5.5, d2 = 6, tol = 1E-5
d1 = sdk.NewDecWithPrec(55, 2)
d2 = sdk.NewDecWithPrec(6, 1)
tol = sdk.NewDecWithPrec(1, 5)

assert.False(sdk.DecApproxEq(t, d1, d2, tol))

// d1 = 6, d2 = 6.1, tol = 0.1
d1 = sdk.NewDecWithPrec(6, 1)
d2 = sdk.NewDecWithPrec(61, 2)
pwang00 marked this conversation as resolved.
Show resolved Hide resolved
tol = sdk.NewDecWithPrec(1, 1)

assert.True(sdk.DecApproxEq(t, d1, d2, tol))

}

// create a decimal from a decimal string (ex. "1234.5678")
func (s *decimalTestSuite) mustNewDecFromStr(str string) (d sdk.Dec) {
d, err := sdk.NewDecFromStr(str)
Expand Down