diff --git a/src/math/big/int.go b/src/math/big/int.go index 8e52f0ab27b..23221c083d0 100644 --- a/src/math/big/int.go +++ b/src/math/big/int.go @@ -323,6 +323,8 @@ func (x *Int) Cmp(y *Int) (r int) { // (-x) cmp y == y // (-x) cmp (-y) == -(x cmp y) switch { + case x == y: + // nothing to do case x.neg == y.neg: r = x.abs.cmp(y.abs) if x.neg { diff --git a/src/math/big/int_test.go b/src/math/big/int_test.go index ade973b2074..da12a4b0016 100644 --- a/src/math/big/int_test.go +++ b/src/math/big/int_test.go @@ -1071,6 +1071,20 @@ func TestCmpAbs(t *testing.T) { } } +func TestIntCmpSelf(t *testing.T) { + for _, s := range cmpAbsTests { + x, ok := new(Int).SetString(s, 0) + if !ok { + t.Fatalf("SetString(%s, 0) failed", s) + } + got := x.Cmp(x) + want := 0 + if got != want { + t.Errorf("x = %s: x.Cmp(x): got %d; want %d", x, got, want) + } + } +} + var int64Tests = []string{ // int64 "0",