Example #1
0
// Neg value make sure 'val' is not updated
func TestMoneyNegNotMutable(t *testing.T) {
	val := money.Money{M: 123456}
	neg := val.Neg()

	if val.Valuei() != int64(123456) {
		t.Error("val should be int64 '123456'", val.Valuei())
	}

	if val.Valuef() != float64(1234.56) {
		t.Error("val should be int64 '1234.56'", val.Valuef())
	}

	if neg.Valuei() != int64(-123456) {
		t.Error("neg.Valuei should be int64 '-123456'", neg.Valuei())
	}

	if neg.Valuef() != float64(-1234.56) {
		t.Error("neg.Valuef should be float64 '-1234.56'", neg.Valuef())
	}

	if val.StringD() != "1234.56" {
		t.Error("val.StringD() should be '1234.56'", val.StringD())
	}
	// TODO oddity for negative money values!!!!
	if neg.StringD() != "-1234.56" {
		t.Error("neg.StringD() should be '-1234.56'", neg.StringD())
	}
}
Example #2
0
// check multiplication is good but also that original values are not modified
func TestMoneyMul(t *testing.T) {
	m1 := money.Money{M: 67}   //67 cents!!
	m2 := money.Money{M: 6700} // 67 dollars
	res := m2.Mul(&m1)
	finResi := int64(4489)    // 44 dollars and 89 cents
	finResf := float64(44.89) // 44 dollars and 89 cents

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

	if res.Valuef() != finResf {
		t.Error("expected '44.89' 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() != "44.89" {
		t.Error("expected '44.89' got: ", res.StringD())
	}
}
Example #3
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())
	}
}
Example #4
0
// check subtraction is good but also that original values are not modified
func TestMoneySub(t *testing.T) {
	m1 := money.Money{M: 67}
	m2 := money.Money{M: 6700}
	res := m2.Sub(&m1)
	finResi := int64(6633)
	finResf := float64(66.33)

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

	if res.Valuef() != finResf {
		t.Error("expected '66.33' 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() != "66.33" {
		t.Error("expected '66.33' got: ", res.StringD())
	}
}
Example #5
0
func TestMoneyValueIntAndFloat(t *testing.T) {
	val := money.Money{M: 123456}

	if val.Valuei() != int64(123456) {
		t.Error("Valuei() should be int64 '123456'", val.Valuei())
	}

	if val.Valuef() != float64(1234.56) {
		t.Error("Valuef() should be float64 '123456'", val.Valuef())
	}

	if val.ValueiTrunc() != int64(1234) {
		t.Error("ValueiTrunc() should be int64 '1234'", val.ValueiTrunc())
	}

	if val.StringD() != "1234.56" {
		t.Error("money struct init StringD() should be value '1234.56'", val.StringD())
	}

	if val.StringC() != "1234,56" {
		t.Error("money struct init StringC() should be value '1234,56'", val.StringC())
	}
}
Example #6
0
// check addition is good but also that original values are not modified
func TestMoneyAdd(t *testing.T) {
	m1 := money.Money{M: int64(67)}
	m2 := money.Money{M: int64(6700)}
	res := m1.Add(&m2)
	finResi := int64(6767)
	finResf := float64(67.67)

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

	if res.Valuef() != finResf {
		t.Error("expected '6767' 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() != "67.67" {
		t.Error("expected '67.67' got: ", res.StringD())
	}

	if res.StringC() != "67,67" {
		t.Error("expected '67,67' got: ", res.StringC())
	}
}