Commit 1f0440a4 authored by David Vorick's avatar David Vorick

clean up types/currency.go

switch from DEBUG wrapped panics to calls to build.Critical. Also add
some docstrings, and make the tests compatible with the calls to
build.Critical.
parent 9ead966c
......@@ -31,18 +31,23 @@ type (
)
var (
// ZeroCurrency defines a currency of value zero.
ZeroCurrency = NewCurrency64(0)
// ErrNegativeCurrency is the error that is returned if performing an
// operation results in a negative currency.
ErrNegativeCurrency = errors.New("negative currency not allowed")
// ErrUint64Overflow is the error that is returned if converting to a
// unit64 would cause an overflow.
ErrUint64Overflow = errors.New("cannot return the uint64 of this currency - result is an overflow")
)
// NewCurrency creates a Currency value from a big.Int. Undefined behavior
// occurs if a negative input is used.
func NewCurrency(b *big.Int) (c Currency) {
if b.Sign() < 0 {
if build.DEBUG {
panic(ErrNegativeCurrency)
}
build.Critical(ErrNegativeCurrency)
} else {
c.i = *b
}
......@@ -92,9 +97,7 @@ func (x Currency) Mul(y Currency) (c Currency) {
// Behavior is undefined when x is negative.
func (x Currency) MulFloat(y float64) (c Currency) {
if y < 0 {
if build.DEBUG {
panic(ErrNegativeCurrency)
}
build.Critical(ErrNegativeCurrency)
} else {
cRat := new(big.Rat).Mul(
new(big.Rat).SetInt(&x.i),
......@@ -108,9 +111,7 @@ func (x Currency) MulFloat(y float64) (c Currency) {
// MulRat returns a new Currency value c = x * y, where y is a big.Rat.
func (x Currency) MulRat(y *big.Rat) (c Currency) {
if y.Sign() < 0 {
if build.DEBUG {
panic(ErrNegativeCurrency)
}
build.Critical(ErrNegativeCurrency)
} else {
c.i.Mul(&x.i, y.Num())
c.i.Div(&c.i, y.Denom())
......@@ -151,9 +152,7 @@ func (x Currency) Sqrt() (c Currency) {
func (x Currency) Sub(y Currency) (c Currency) {
if x.Cmp(y) < 0 {
c = x
if build.DEBUG {
panic(ErrNegativeCurrency)
}
build.Critical(ErrNegativeCurrency)
} else {
c.i.Sub(&x.i, &y.i)
}
......
......@@ -225,7 +225,7 @@ func TestNegativeCurrencyMulRat(t *testing.T) {
// In debug mode, attempting to get a negative currency results in a panic.
defer func() {
r := recover()
if r != ErrNegativeCurrency {
if r == nil {
t.Error("no panic occured when trying to create a negative currency")
}
}()
......@@ -240,7 +240,7 @@ func TestNegativeCurrencySub(t *testing.T) {
// In debug mode, attempting to get a negative currency results in a panic.
defer func() {
r := recover()
if r != ErrNegativeCurrency {
if r == nil {
t.Error("no panic occured when trying to create a negative currency")
}
}()
......@@ -289,7 +289,7 @@ func TestNegativeNewCurrency(t *testing.T) {
// In debug mode, attempting to get a negative currency results in a panic.
defer func() {
r := recover()
if r != ErrNegativeCurrency {
if r == nil {
t.Error("no panic occured when trying to create a negative currency")
}
}()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment