Skip to content

Commit

Permalink
Fix tests and minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jul 3, 2024
1 parent 51596e9 commit 95af2f7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 46 deletions.
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

0 comments on commit 95af2f7

Please sign in to comment.