Пример #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)
		}
	*/
}
Пример #2
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())
	}
}