From 16a941821474ee3986fdbeab535a68a8aa5a85d2 Mon Sep 17 00:00:00 2001 From: Rigel Date: Thu, 18 May 2017 00:01:17 +0200 Subject: [PATCH] added GT, GTE, LT, LTE (#54) * added GT, GTE, LT, LTE * GTE -> GreaterThanOrEqual --- decimal.go | 22 ++++++++++++++++++++++ decimal_test.go | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/decimal.go b/decimal.go index cf0f3ce..c4467a9 100644 --- a/decimal.go +++ b/decimal.go @@ -423,6 +423,28 @@ func (d Decimal) Equals(d2 Decimal) bool { return d.Equal(d2) } +// Greater Than (GT) returns true when d is greater than d2. +func (d Decimal) GreaterThan(d2 Decimal) bool { + return d.Cmp(d2) == 1 +} + +// Greater Than or Equal (GTE) returns true when d is greater than or equal to d2. +func (d Decimal) GreaterThanOrEqual(d2 Decimal) bool { + cmp := d.Cmp(d2) + return cmp == 1 || cmp == 0 +} + +// Less Than (LT) returns true when d is less than d2. +func (d Decimal) LessThan(d2 Decimal) bool { + return d.Cmp(d2) == -1 +} + +// Less Than or Equal (LTE) returns true when d is less than or equal to d2. +func (d Decimal) LessThanOrEqual(d2 Decimal) bool { + cmp := d.Cmp(d2) + return cmp == -1 || cmp == 0 +} + // Sign returns: // // -1 if d < 0 diff --git a/decimal_test.go b/decimal_test.go index 96f7340..358609d 100644 --- a/decimal_test.go +++ b/decimal_test.go @@ -1322,18 +1322,53 @@ func TestDecimal_Abs2(t *testing.T) { } } -func TestDecimal_Equal(t *testing.T) { +func TestDecimal_Equalities(t *testing.T) { a := New(1234, 3) b := New(1234, 3) + c := New(1234, 4) if !a.Equal(b) { t.Errorf("%q should equal %q", a, b) } + if a.Equal(c) { + t.Errorf("%q should not equal %q", a, c) + } - // note, this should be deprecated, here for backwards compatibility + // note, this block should be deprecated, here for backwards compatibility if !a.Equals(b) { t.Errorf("%q should equal %q", a, b) } + + if !c.GreaterThan(b) { + t.Errorf("%q should be greater than %q", c, b) + } + if b.GreaterThan(c) { + t.Errorf("%q should not be greater than %q", b, c) + } + if !a.GreaterThanOrEqual(b) { + t.Errorf("%q should be greater or equal %q", a, b) + } + if !c.GreaterThanOrEqual(b) { + t.Errorf("%q should be greater or equal %q", c, b) + } + if b.GreaterThanOrEqual(c) { + t.Errorf("%q should not be greater or equal %q", b, c) + } + if !b.LessThan(c) { + t.Errorf("%q should be less than %q", a, b) + } + if c.LessThan(b) { + t.Errorf("%q should not be less than %q", a, b) + } + if !a.LessThanOrEqual(b) { + t.Errorf("%q should be less than or equal %q", a, b) + } + if !b.LessThanOrEqual(c) { + t.Errorf("%q should be less than or equal %q", a, b) + } + if c.LessThanOrEqual(b) { + t.Errorf("%q should not be less than or equal %q", a, b) + } } func TestDecimal_ScalesNotEqual(t *testing.T) {