diff --git a/math/dec.go b/math/dec.go index 354d5b870f81..d355c6b1c4fa 100644 --- a/math/dec.go +++ b/math/dec.go @@ -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()) @@ -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 diff --git a/math/dec_rapid_test.go b/math/dec_rapid_test.go index 27d6ba441f86..c4896159bfeb 100644 --- a/math/dec_rapid_test.go +++ b/math/dec_rapid_test.go @@ -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)) diff --git a/math/dec_test.go b/math/dec_test.go index bc659383de15..d05afcf3e196 100644 --- a/math/dec_test.go +++ b/math/dec_test.go @@ -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": { @@ -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()) }) } } @@ -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), @@ -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),