Exemple #1
0
// check division is good but also that original values are not modified
func TestMoneyDiv(t *testing.T) {
	m1 := money.Money{M: 67}   //67 cents!!
	m2 := money.Money{M: 6700} // 67 dollars
	res := m2.Div(&m1)
	finResi := int64(10000)
	finResf := float64(100.00)

	if res.Valuei() != finResi {
		t.Error("expected '10000' got: ", res.Valuei())
	}

	if res.Valuef() != finResf {
		t.Error("expected '100.00' got: ", res.Valuef())
	}

	if m1.Valuei() != int64(67) {
		t.Error("expected '67' got: ", m1.Valuei())
	}

	if m2.Valuei() != int64(6700) {
		t.Error("expected '6700' got: ", m2.Valuei())
	}

	if m1.Valuef() != float64(0.67) {
		t.Error("expected '0.67' got: ", m1.Valuef())
	}

	if m2.Valuef() != float64(67.00) {
		t.Error("expected '67.00' got: ", m2.Valuef())
	}

	if res.StringD() != "100.00" {
		t.Error("expected '100.00' got: ", res.StringD())
	}
}
Exemple #2
0
// division with a large denominator
func TestMoneyDivLargeDenom(t *testing.T) {
	m1 := money.Money{M: 67}   //67 cents!!
	m2 := money.Money{M: 6700} // 67 dollars
	res := m1.Div(&m2)
	finResi := int64(1)
	finResf := float64(0.01)

	if res.Valuei() != finResi {
		t.Error("expected '1' got: ", res.Valuei())
	}

	if res.Valuef() != finResf {
		t.Error("expected '0.01' got: ", res.Valuef())
	}

	if res.StringD() != "0.01" {
		t.Error("expected '100' got: ", res.StringD())
	}
}
Exemple #3
0
func TestMoneyDivByZero(t *testing.T) {
	m1 := money.Money{}
	m2 := money.Money{M: int64(1)}
	m3 := money.Money{M: int64(72684839824903281)}
	m4 := money.Money{}
	m4.Updatef(float64(478326437489327489327838462381))

	res1 := m2.Div(&m1)
	res2 := m3.Div(&m1)
	res3 := m4.Div(&m1)

	if res1.Valuei() != int64(-9223372036854775807) {
		t.Error("expected '-9223372036854775807' got: ", res1.Valuei())
	}

	if res2.Valuei() != int64(-9223372036854775807) {
		t.Error("expected '-9223372036854775807' got: ", res2.Valuei())
	}

	if res3.Valuei() != int64(9223372036854775807) {
		t.Error("expected '9223372036854775807' got: ", res3.Valuei())
	}

	if res1.Valuef() != float64(-9.223372036854776e+16) {
		t.Error("expected '-9223372036854775807' got: ", res1.Valuef())
	}

	if res2.Valuef() != float64(-9.223372036854776e+16) {
		t.Error("expected '-9223372036854775807' got: ", res2.Valuef())
	}

	if res3.Valuef() != float64(9.223372036854776e+16) {
		t.Error("expected '9223372036854775807' got: ", res3.Valuef())
	}
}