Exemplo n.º 1
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())
	}
}
Exemplo n.º 2
0
// adding large numbers floats via an `Updatef`
func TestMoneyAddLarge(t *testing.T) {
	var val1 float64 = 12390678659.32 //12390678659.32
	var val2 float64 = 8937670084.36  // 8937670084.36
	m1 := money.Money{}
	m2 := money.Money{}
	m1Set := m1.Updatef(val1)
	m2Set := m2.Updatef(val2)
	res := m1Set.Add(m2Set)

	//12390678659.32 + 8937670084.36 = 21,328,348,743.68
	if res.Valuef() != float64(21328348743.68) {
		t.Error("expected negaitve '21328348743.68' got: ", res.Valuef())
	}

	if res.Valuei() != int64(2132834874368) {
		t.Error("expected negaitve '2132834874368' got: ", res.Valuei())
	}

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

	//12390678659.32 + 8937670084.36 = 21,328,348,743.68
	if res.StringC() != "21328348743,68" {
		t.Error("expected '21328348743,68' got: ", res.StringC())
	}
}
Exemplo n.º 3
0
func TestMoneyAbs(t *testing.T) {
	v1 := int64(7839)
	v2 := float64(38261748.09)
	m1 := money.Money{M: -v1}
	m2 := money.Money{}
	m2.Updatef(-v2)

	m1abs := m1.Abs()
	m2abs := m2.Abs()

	m1pos := money.Money{M: v1}
	m2pos := money.Money{}
	m2pos.Updatef(v2)

	if m1abs.Valuei() != m1pos.Valuei() {
		t.Error("expected '7839' got: ", m1abs.Valuei())
	}

	if m2abs.Valuei() != m2pos.Valuei() {
		t.Error("expected '3826174809' got: ", m2abs.Valuei())
	}

	if m1.Abs().Valuei() != m1pos.Valuei() {
		t.Error("expected '7839' got: ", m1.Abs().Valuei())
	}

	if m2.Abs().Valuei() != m2pos.Valuei() {
		t.Error("expected '3826174809' got:", m2.Abs().Valuei())
	}

	//verify original values are still negative
	if m1.Valuei() != -m1pos.Valuei() {
		t.Error("expected '-7839' got: ", m1.Valuei())
	}

	if m2.Valuei() != -m2pos.Valuei() {
		t.Error("expected '-3826174809' got: ", m2.Valuei())
	}
}