add tests for Cmp64, Equals, Equals64

parent fd5d2f18
......@@ -41,6 +41,53 @@ func TestCurrencyToBig(t *testing.T) {
}
}
// TestCurrencyCmp tests the Cmp method for the currency type
func TestCurrencyCmp(t *testing.T) {
tests := []struct {
x, y Currency
exp int
}{
{NewCurrency64(0), NewCurrency64(0), 0},
{NewCurrency64(0), NewCurrency64(1), -1},
{NewCurrency64(1), NewCurrency64(0), 1},
{NewCurrency64(100), NewCurrency64(7), 1},
{NewCurrency64(777), NewCurrency(big.NewInt(777)), 0},
{NewCurrency(big.NewInt(7)), NewCurrency(big.NewInt(8)), -1},
}
for _, test := range tests {
if c := test.x.Cmp(test.y); c != test.exp {
t.Errorf("expected %v.Cmp(%v) == %v, got %v", test.x, test.y, test.exp, c)
} else if bc := test.x.Big().Cmp(test.y.Big()); c != bc {
t.Errorf("Currency.Cmp (%v) does not match big.Int.Cmp (%v) for %v.Cmp(%v)", c, bc, test.x, test.y)
}
}
}
// TestCurrencyCmp64 tests the Cmp64 method for the currency type
func TestCurrencyCmp64(t *testing.T) {
tests := []struct {
x Currency
y uint64
exp int
}{
{NewCurrency64(0), 0, 0},
{NewCurrency64(0), 1, -1},
{NewCurrency64(1), 0, 1},
{NewCurrency64(100), 7, 1},
{NewCurrency64(777), 777, 0},
{NewCurrency(big.NewInt(7)), 8, -1},
}
for _, test := range tests {
if c := test.x.Cmp64(test.y); c != test.exp {
t.Errorf("expected %v.Cmp64(%v) == %v, got %v", test.x, test.y, test.exp, c)
} else if bc := test.x.Big().Cmp(big.NewInt(int64(test.y))); c != bc {
t.Errorf("Currency.Cmp64 (%v) does not match big.Int.Cmp (%v) for %v.Cmp64(%v)", c, bc, test.x, test.y)
}
}
}
// TestCurrencyDiv checks that the div function has been correctly implemented.
func TestCurrencyDiv(t *testing.T) {
c9 := NewCurrency64(9)
......@@ -75,6 +122,53 @@ func TestCurrencyDiv64(t *testing.T) {
}
}
// TestCurrencyEquals tests the Equals method for the currency type
func TestCurrencyEquals(t *testing.T) {
tests := []struct {
x, y Currency
exp bool
}{
{NewCurrency64(0), NewCurrency64(0), true},
{NewCurrency64(0), NewCurrency64(1), false},
{NewCurrency64(1), NewCurrency64(0), false},
{NewCurrency64(100), NewCurrency64(7), false},
{NewCurrency64(777), NewCurrency(big.NewInt(777)), true},
{NewCurrency(big.NewInt(7)), NewCurrency(big.NewInt(8)), false},
}
for _, test := range tests {
if eq := test.x.Equals(test.y); eq != test.exp {
t.Errorf("expected %v.Equals(%v) == %v, got %v", test.x, test.y, test.exp, eq)
} else if bc := test.x.Big().Cmp(test.y.Big()); (bc == 0) != eq {
t.Errorf("Currency.Equals (%v) does not match big.Int.Cmp (%v) for %v.Equals(%v)", eq, bc, test.x, test.y)
}
}
}
// TestCurrencyEquals64 tests the Equals64 method for the currency type
func TestCurrencyEquals64(t *testing.T) {
tests := []struct {
x Currency
y uint64
exp bool
}{
{NewCurrency64(0), 0, true},
{NewCurrency64(0), 1, false},
{NewCurrency64(1), 0, false},
{NewCurrency64(100), 7, false},
{NewCurrency64(777), 777, true},
{NewCurrency(big.NewInt(7)), 8, false},
}
for _, test := range tests {
if eq := test.x.Equals64(test.y); eq != test.exp {
t.Errorf("expected %v.Equals64(%v) == %v, got %v", test.x, test.y, test.exp, eq)
} else if bc := test.x.Big().Cmp(big.NewInt(int64(test.y))); (bc == 0) != eq {
t.Errorf("Currency.Equals64 (%v) does not match big.Int.Cmp (%v) for %v.Equals64(%v)", eq, bc, test.x, test.y)
}
}
}
// TestCurrencyMul probes the Mul function of the currency type.
func TestCurrencyMul(t *testing.T) {
c5 := NewCurrency64(5)
......
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