1
0
mirror of https://github.com/golang/go synced 2024-11-11 19:21:37 -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:
Matthew Dempsky 2022-03-07 02:27:35 -08:00
parent 0e2f1abf5b
commit d168c4f296
8 changed files with 38 additions and 8 deletions

View File

@ -28,4 +28,6 @@ func main() {
f[float64](int8(9)) f[float64](int8(9))
f[int32](int32(7)) f[int32](int32(7))
f[int](int32(7)) f[int](int32(7))
f[any](int(10))
f[interface{ M() }](int(11))
} }

View File

@ -5,3 +5,5 @@ struct{T,T}
other other
T T
int32/int16 int32/int16
T
int

View File

@ -6,20 +6,20 @@
package main package main
import "reflect" import "fmt"
func f[T any](i interface{}) { func f[T any](i interface{}) {
switch x := i.(type) { switch x := i.(type) {
case T: case T:
println("T", x) fmt.Println("T", x)
case int: case int:
println("int", x) fmt.Println("int", x)
case int32, int16: case int32, int16:
println("int32/int16", reflect.ValueOf(x).Int()) fmt.Println("int32/int16", x)
case struct{ a, b T }: case struct{ a, b T }:
println("struct{T,T}", x.a, x.b) fmt.Println("struct{T,T}", x.a, x.b)
default: default:
println("other", reflect.ValueOf(x).Int()) fmt.Println("other", x)
} }
} }
func main() { func main() {
@ -30,4 +30,6 @@ func main() {
f[float64](int8(9)) f[float64](int8(9))
f[int32](int32(7)) f[int32](int32(7))
f[int](int32(7)) f[int](int32(7))
f[any](int(10))
f[interface{ M() }](int(11))
} }

View File

@ -1,7 +1,9 @@
T +6.000000e+000 T 6
int 7 int 7
int32/int16 8 int32/int16 8
struct{T,T} +1.000000e+000 +2.000000e+000 struct{T,T} 1 2
other 9 other 9
T 7 T 7
int32/int16 7 int32/int16 7
T 10
int 11

View File

@ -7,6 +7,10 @@
package main package main
type I interface{ foo() int } type I interface{ foo() int }
type J interface {
I
bar()
}
type myint int type myint int
@ -19,6 +23,7 @@ func (x myfloat) foo() int { return int(x) }
type myint32 int32 type myint32 int32
func (x myint32) foo() int { return int(x) } func (x myint32) foo() int { return int(x) }
func (x myint32) bar() {}
func f[T I](i I) { func f[T I](i I) {
switch x := i.(type) { switch x := i.(type) {
@ -37,4 +42,7 @@ func main() {
f[myint32](myint32(8)) f[myint32](myint32(8))
f[myint32](myfloat(7)) f[myint32](myfloat(7))
f[myint](myint32(9)) f[myint](myint32(9))
f[I](myint(10))
f[J](myint(11))
f[J](myint32(12))
} }

View File

@ -4,3 +4,6 @@ other 8
T 8 T 8
other 7 other 7
other 9 other 9
T 10
myint 11
T 12

View File

@ -7,6 +7,10 @@
package main package main
type I interface{ foo() int } type I interface{ foo() int }
type J interface {
I
bar()
}
type myint int type myint int
@ -19,6 +23,7 @@ func (x myfloat) foo() int { return int(x) }
type myint32 int32 type myint32 int32
func (x myint32) foo() int { return int(x) } func (x myint32) foo() int { return int(x) }
func (x myint32) bar() {}
func f[T I](i I) { func f[T I](i I) {
switch x := i.(type) { switch x := i.(type) {
@ -35,4 +40,7 @@ func main() {
f[myint32](myint32(9)) f[myint32](myint32(9))
f[myint](myint32(10)) f[myint](myint32(10))
f[myint](myfloat(42)) f[myint](myfloat(42))
f[I](myint(10))
f[J](myint(11))
f[J](myint32(12))
} }

View File

@ -4,3 +4,6 @@ T/myint32 8
T/myint32 9 T/myint32 9
T/myint32 10 T/myint32 10
other 42 other 42
T/myint32 10
other 11
T/myint32 12