]> Cypherpunks repositories - gostls13.git/commitdiff
test: additional generic type switch test coverage
authorMatthew Dempsky <mdempsky@google.com>
Mon, 7 Mar 2022 10:27:35 +0000 (02:27 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 7 Mar 2022 13:47:58 +0000 (13:47 +0000)
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>
test/typeparam/typeswitch1.go
test/typeparam/typeswitch1.out
test/typeparam/typeswitch2.go
test/typeparam/typeswitch2.out
test/typeparam/typeswitch3.go
test/typeparam/typeswitch3.out
test/typeparam/typeswitch4.go
test/typeparam/typeswitch4.out

index e971779982e9525df6ddfaf650a6a85470dbc27c..a0468d378fba709d3b4da82b361c9589cb7af22d 100644 (file)
@@ -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))
 }
index dc5dfdb76116522636ea57997095cef06105f62b..6b8a33c34575374933aa8d3970f9536a18f152ad 100644 (file)
@@ -5,3 +5,5 @@ struct{T,T}
 other
 T
 int32/int16
+T
+int
index b2496fd1c4a4f3e7dbd539998b8f8300360410ae..286002a830263b8595f7bf16546410fcf28bb22b 100644 (file)
@@ -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))
 }
index 85b54e38aebf60eb46cbba2f6a80fbf7adf0796a..6d4df54124c0fcad2059618a9ab0399be82261a5 100644 (file)
@@ -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
index 83d81f37d03c437a9742aa7d67ae5be11c95c2e3..b84fdd02eaae3793b175bb6b1ee37c88e7133092 100644 (file)
@@ -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))
 }
index ed59987e6d96deb29b29e0463cc4fd8ce8a67879..05ed5331973bde1144d27c6948a96f4ece4f65bb 100644 (file)
@@ -4,3 +4,6 @@ other 8
 T 8
 other 7
 other 9
+T 10
+myint 11
+T 12
index 43a6fc12fc137e0a50e2796d1a998de6d54cb6dd..3fdf5527202ef3c75808e90ab68252b032621a1d 100644 (file)
@@ -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))
 }
index d6121d077c051976f0a6bea207e9f41b5fb8eda8..b98f0743c2ea18ed2cae5be0bbbb094ca2453ea4 100644 (file)
@@ -4,3 +4,6 @@ T/myint32 8
 T/myint32 9
 T/myint32 10
 other 42
+T/myint32 10
+other 11
+T/myint32 12