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

Fix tests and minor refactoring #20861

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 2 additions & 39 deletions math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,8 @@ var dec128Context = apd.Context{
Traps: apd.DefaultTraps,
}

type SetupConstraint func(Dec) error

// AssertNotNegative greater or equal 0
func AssertNotNegative() SetupConstraint {
return func(d Dec) error {
if d.IsNegative() {
return ErrInvalidDec.Wrap("is negative")
}
return nil
}
}

// AssertGreaterThanZero greater than 0
func AssertGreaterThanZero() SetupConstraint {
return func(d Dec) error {
if !d.IsPositive() {
return ErrInvalidDec.Wrap("is negative")
}
return nil
}
}

// AssertMaxDecimals limit the decimal places
func AssertMaxDecimals(max uint32) SetupConstraint {
return func(d Dec) error {
if d.NumDecimalPlaces() > max {
return ErrInvalidDec.Wrapf("exceeds maximum decimal places: %d", max)
}
return nil
}
}

// NewDecFromString constructor
func NewDecFromString(s string, c ...SetupConstraint) (Dec, error) {
func NewDecFromString(s string) (Dec, error) {
d, _, err := apd.NewFromString(s)
if err != nil {
return Dec{}, ErrInvalidDec.Wrap(err.Error())
Expand All @@ -89,16 +57,11 @@ func NewDecFromString(s string, c ...SetupConstraint) (Dec, error) {
return Dec{}, ErrInvalidDec.Wrapf(s)
default:
result := Dec{*d}
for _, v := range c {
if err := v(result); err != nil {
return Dec{}, err
}
}
return result, nil
}
}

func NewDecFromInt64(x int64, c ...SetupConstraint) Dec {
func NewDecFromInt64(x int64) Dec {
var res Dec
res.dec.SetInt64(x)
return res
Expand Down
2 changes: 1 addition & 1 deletion math/dec_rapid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestDecWithRapid(t *testing.T) {
require.NoError(t, err)
require.True(t, res.Equal(two))

res, err = five.Rem(two)
res, err = five.Modulo(two)
require.NoError(t, err)
require.True(t, res.Equal(one))

Expand Down
12 changes: 6 additions & 6 deletions math/dec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func TestSub(t *testing.T) {
},
"1e100000 - 0 = 1e100000": {
x: NewDecWithPrec(1, 100_000),
y: NewDecFromInt64(0111),
y: NewDecFromInt64(0),
exp: must(NewDecFromString("1e100000")),
},
"1e100001 - 0 -> err": {
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestSub(t *testing.T) {
return
}
require.NoError(t, gotErr)
assert.True(t, spec.exp.Equal(got))
assert.True(t, spec.exp.Equal(got), got.String())
})
}
}
Expand Down Expand Up @@ -810,7 +810,7 @@ func TestQuoInteger(t *testing.T) {
"123 / 123 = 1": {
x: NewDecFromInt64(123),
y: NewDecFromInt64(123),
exp: NewDecFromInt64(5),
exp: NewDecFromInt64(1),
},
"-123 / 123 = -1": {
x: NewDecFromInt64(-123),
Expand Down Expand Up @@ -1198,15 +1198,15 @@ func TestMulExact(t *testing.T) {
y: NewDecWithPrec(1, 1),
expErr: ErrInvalidDec,
},
"1e0000 * -1 = 1e0000": {
"1e0000 * -1 = -1e0000": {
x: NewDecWithPrec(1, 100_000),
y: NewDecWithPrec(-1, 0),
exp: NewDecWithPrec(1, 100_000),
exp: NewDecWithPrec(-1, 100_000),
},
"1e100000 * -9 = 9e100000": {
x: NewDecWithPrec(1, 100_000),
y: NewDecFromInt64(-9),
exp: NewDecWithPrec(9, 100_000),
exp: NewDecWithPrec(-9, 100_000),
},
"1e100000 * -10 = err": {
x: NewDecWithPrec(1, 100_000),
Expand Down
Loading