From a5baa859aed222fc8f1ba9aaf5460e8f34eb08fa Mon Sep 17 00:00:00 2001 From: Richard Musiol Date: Tue, 12 Nov 2013 10:06:24 -0800 Subject: [PATCH] go.tools/go/exact: Fix for exact.BinaryOp. It was modifying its argument. R=golang-dev, gri, mail CC=golang-dev https://golang.org/cl/24570043 --- go/exact/exact.go | 4 ++-- go/exact/exact_test.go | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/go/exact/exact.go b/go/exact/exact.go index aa691bc2fc3..b67c35860e5 100644 --- a/go/exact/exact.go +++ b/go/exact/exact.go @@ -610,12 +610,12 @@ func BinaryOp(x Value, op token.Token, y Value) Value { im.Add(&bc, &ad) case token.QUO: // (ac+bd)/s + i(bc-ad)/s, with s = cc + dd - var ac, bd, bc, ad, s big.Rat + var ac, bd, bc, ad, s, cc, dd big.Rat ac.Mul(a, c) bd.Mul(b, d) bc.Mul(b, c) ad.Mul(a, d) - s.Add(c.Mul(c, c), d.Mul(d, d)) + s.Add(cc.Mul(c, c), dd.Mul(d, d)) re.Add(&ac, &bd) re.Quo(&re, &s) im.Sub(&bc, &ad) diff --git a/go/exact/exact_test.go b/go/exact/exact_test.go index 061eec96b04..afe47060e5d 100644 --- a/go/exact/exact_test.go +++ b/go/exact/exact_test.go @@ -46,6 +46,7 @@ var tests = []string{ `0 / 0 = "division_by_zero"`, `10 / 2 = 5`, `5 / 3 = 5/3`, + `5i / 3i = 5/3`, `0 % 0 = "runtime_error:_integer_divide_by_zero"`, // TODO(gri) should be the same as for / `10 % 3 = 1`, @@ -85,11 +86,23 @@ func TestOps(t *testing.T) { switch a := strings.Split(test, " "); len(a) { case 4: - got = doOp(nil, op[a[0]], val(a[1])) + x := val(a[1]) + got = doOp(nil, op[a[0]], x) want = val(a[3]) + if !Compare(x, token.EQL, val(a[1])) { + t.Errorf("%s failed: x changed to %s", test, x) + } case 5: - got = doOp(val(a[0]), op[a[1]], val(a[2])) + x := val(a[0]) + y := val(a[2]) + got = doOp(x, op[a[1]], y) want = val(a[4]) + if !Compare(x, token.EQL, val(a[0])) { + t.Errorf("%s failed: x changed to %s", test, x) + } + if !Compare(y, token.EQL, val(a[2])) { + t.Errorf("%s failed: y changed to %s", test, y) + } default: t.Errorf("invalid test case: %s", test) continue