mirror of
https://github.com/golang/go
synced 2024-11-25 19:27:56 -07:00
test: additional generic type switch test coverage
None of the current generic type switch test cases exercise type switches where the instantiated case is an interface type. Change-Id: I9272fa61b8dde1fe1a3702d524d4f40253ef19b2 Reviewed-on: https://go-review.googlesource.com/c/go/+/390354 Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
parent
0e2f1abf5b
commit
d168c4f296
@ -28,4 +28,6 @@ func main() {
|
||||
f[float64](int8(9))
|
||||
f[int32](int32(7))
|
||||
f[int](int32(7))
|
||||
f[any](int(10))
|
||||
f[interface{ M() }](int(11))
|
||||
}
|
||||
|
@ -5,3 +5,5 @@ struct{T,T}
|
||||
other
|
||||
T
|
||||
int32/int16
|
||||
T
|
||||
int
|
||||
|
@ -6,20 +6,20 @@
|
||||
|
||||
package main
|
||||
|
||||
import "reflect"
|
||||
import "fmt"
|
||||
|
||||
func f[T any](i interface{}) {
|
||||
switch x := i.(type) {
|
||||
case T:
|
||||
println("T", x)
|
||||
fmt.Println("T", x)
|
||||
case int:
|
||||
println("int", x)
|
||||
fmt.Println("int", x)
|
||||
case int32, int16:
|
||||
println("int32/int16", reflect.ValueOf(x).Int())
|
||||
fmt.Println("int32/int16", x)
|
||||
case struct{ a, b T }:
|
||||
println("struct{T,T}", x.a, x.b)
|
||||
fmt.Println("struct{T,T}", x.a, x.b)
|
||||
default:
|
||||
println("other", reflect.ValueOf(x).Int())
|
||||
fmt.Println("other", x)
|
||||
}
|
||||
}
|
||||
func main() {
|
||||
@ -30,4 +30,6 @@ func main() {
|
||||
f[float64](int8(9))
|
||||
f[int32](int32(7))
|
||||
f[int](int32(7))
|
||||
f[any](int(10))
|
||||
f[interface{ M() }](int(11))
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
T +6.000000e+000
|
||||
T 6
|
||||
int 7
|
||||
int32/int16 8
|
||||
struct{T,T} +1.000000e+000 +2.000000e+000
|
||||
struct{T,T} 1 2
|
||||
other 9
|
||||
T 7
|
||||
int32/int16 7
|
||||
T 10
|
||||
int 11
|
||||
|
@ -7,6 +7,10 @@
|
||||
package main
|
||||
|
||||
type I interface{ foo() int }
|
||||
type J interface {
|
||||
I
|
||||
bar()
|
||||
}
|
||||
|
||||
type myint int
|
||||
|
||||
@ -19,6 +23,7 @@ func (x myfloat) foo() int { return int(x) }
|
||||
type myint32 int32
|
||||
|
||||
func (x myint32) foo() int { return int(x) }
|
||||
func (x myint32) bar() {}
|
||||
|
||||
func f[T I](i I) {
|
||||
switch x := i.(type) {
|
||||
@ -37,4 +42,7 @@ func main() {
|
||||
f[myint32](myint32(8))
|
||||
f[myint32](myfloat(7))
|
||||
f[myint](myint32(9))
|
||||
f[I](myint(10))
|
||||
f[J](myint(11))
|
||||
f[J](myint32(12))
|
||||
}
|
||||
|
@ -4,3 +4,6 @@ other 8
|
||||
T 8
|
||||
other 7
|
||||
other 9
|
||||
T 10
|
||||
myint 11
|
||||
T 12
|
||||
|
@ -7,6 +7,10 @@
|
||||
package main
|
||||
|
||||
type I interface{ foo() int }
|
||||
type J interface {
|
||||
I
|
||||
bar()
|
||||
}
|
||||
|
||||
type myint int
|
||||
|
||||
@ -19,6 +23,7 @@ func (x myfloat) foo() int { return int(x) }
|
||||
type myint32 int32
|
||||
|
||||
func (x myint32) foo() int { return int(x) }
|
||||
func (x myint32) bar() {}
|
||||
|
||||
func f[T I](i I) {
|
||||
switch x := i.(type) {
|
||||
@ -35,4 +40,7 @@ func main() {
|
||||
f[myint32](myint32(9))
|
||||
f[myint](myint32(10))
|
||||
f[myint](myfloat(42))
|
||||
f[I](myint(10))
|
||||
f[J](myint(11))
|
||||
f[J](myint32(12))
|
||||
}
|
||||
|
@ -4,3 +4,6 @@ T/myint32 8
|
||||
T/myint32 9
|
||||
T/myint32 10
|
||||
other 42
|
||||
T/myint32 10
|
||||
other 11
|
||||
T/myint32 12
|
||||
|
Loading…
Reference in New Issue
Block a user