Esempio n. 1
0
func TestMoneyOverflow(t *testing.T) {
	flag.Parse()
	if ovf == 0 {
		t.Skip("Skip overflow no 'overflow' cli flag set: 'ovf=1 will run it'")
	}

	r := random(1, 25)
	m1 := money.Money{M: money.MaxInt}
	m2 := money.Money{M: money.MinInt}
	if (r % 2) == 1 {
		_ = m1.Add(&m1)
	} else {
		_ = m2.Sub(&m1)
	}

	/*
		** this was testing using fields on the struct to check for overlfow errors
		// overflow error should return value of struct attempting op
		if res.Valuei() != int64(0) {
			t.Error("expected overflow value to be zero", res.Valuei())
		}

		if res.OvfErr != true {
			t.Error("expected ok to be false", res.OvfErr)
		}

		if len(res.Ovf) < 1 {
			t.Error("need overflow message", res.Ovf)
		}
	*/
}
Esempio n. 2
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())
	}
}