1
0
mirror of https://github.com/golang/go synced 2024-11-26 02:07:57 -07:00

go/constant: make constant.Make produce "smallest" const representation

Fixes #42640.

Change-Id: I22b8142b0a47a0f957d1bda28cdfdbb8388cffc4
Reviewed-on: https://go-review.googlesource.com/c/go/+/273086
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2020-11-24 17:26:22 -08:00
parent e8de596f04
commit f6dcc975f7
2 changed files with 27 additions and 13 deletions

View File

@ -594,11 +594,11 @@ func Make(x interface{}) Value {
case int64:
return int64Val(x)
case *big.Int:
return intVal{x}
return makeInt(x)
case *big.Rat:
return ratVal{x}
return makeRat(x)
case *big.Float:
return floatVal{x}
return makeFloat(x)
default:
return unknownVal{}
}

View File

@ -620,18 +620,32 @@ func TestUnknown(t *testing.T) {
}
}
type makeTestCase struct {
kind Kind
arg, want interface{}
}
func dup(k Kind, x interface{}) makeTestCase { return makeTestCase{k, x, x} }
func TestMake(t *testing.T) {
for _, want := range []interface{}{
false,
"hello",
int64(1),
big.NewInt(10),
big.NewFloat(2.0),
big.NewRat(1, 3),
for _, test := range []makeTestCase{
{Bool, false, false},
{String, "hello", "hello"},
{Int, int64(1), int64(1)},
{Int, big.NewInt(10), int64(10)},
{Int, new(big.Int).Lsh(big.NewInt(1), 62), int64(1 << 62)},
dup(Int, new(big.Int).Lsh(big.NewInt(1), 63)),
{Float, big.NewFloat(0), floatVal0.val},
dup(Float, big.NewFloat(2.0)),
dup(Float, big.NewRat(1, 3)),
} {
got := Val(Make(want))
if got != want {
t.Errorf("got %v; want %v", got, want)
val := Make(test.arg)
got := Val(val)
if val.Kind() != test.kind || got != test.want {
t.Errorf("got %v (%T, kind = %d); want %v (%T, kind = %d)",
got, got, val.Kind(), test.want, test.want, test.kind)
}
}
}