mirror of
https://github.com/golang/go
synced 2024-11-26 07:27:59 -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:
parent
e8de596f04
commit
f6dcc975f7
@ -594,11 +594,11 @@ func Make(x interface{}) Value {
|
|||||||
case int64:
|
case int64:
|
||||||
return int64Val(x)
|
return int64Val(x)
|
||||||
case *big.Int:
|
case *big.Int:
|
||||||
return intVal{x}
|
return makeInt(x)
|
||||||
case *big.Rat:
|
case *big.Rat:
|
||||||
return ratVal{x}
|
return makeRat(x)
|
||||||
case *big.Float:
|
case *big.Float:
|
||||||
return floatVal{x}
|
return makeFloat(x)
|
||||||
default:
|
default:
|
||||||
return unknownVal{}
|
return unknownVal{}
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
func TestMake(t *testing.T) {
|
||||||
for _, want := range []interface{}{
|
for _, test := range []makeTestCase{
|
||||||
false,
|
{Bool, false, false},
|
||||||
"hello",
|
{String, "hello", "hello"},
|
||||||
int64(1),
|
|
||||||
big.NewInt(10),
|
{Int, int64(1), int64(1)},
|
||||||
big.NewFloat(2.0),
|
{Int, big.NewInt(10), int64(10)},
|
||||||
big.NewRat(1, 3),
|
{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))
|
val := Make(test.arg)
|
||||||
if got != want {
|
got := Val(val)
|
||||||
t.Errorf("got %v; want %v", got, want)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user