added GT, GTE, LT, LTE (#54)

* added GT, GTE, LT, LTE

* GTE -> GreaterThanOrEqual
This commit is contained in:
Rigel 2017-05-18 00:01:17 +02:00 committed by Victor Quinn
parent d5fda14026
commit 16a9418214
2 changed files with 59 additions and 2 deletions

View file

@ -423,6 +423,28 @@ func (d Decimal) Equals(d2 Decimal) bool {
return d.Equal(d2) 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: // Sign returns:
// //
// -1 if d < 0 // -1 if d < 0

View file

@ -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) a := New(1234, 3)
b := New(1234, 3) b := New(1234, 3)
c := New(1234, 4)
if !a.Equal(b) { if !a.Equal(b) {
t.Errorf("%q should equal %q", a, 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) { if !a.Equals(b) {
t.Errorf("%q should equal %q", a, 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) { func TestDecimal_ScalesNotEqual(t *testing.T) {