Skip to content

Commit 3650f6f

Browse files
committed
Add (failing) test for slice-to-array-pointer conversion added in Go 1.17
1 parent 9a3f140 commit 3650f6f

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/slice_to_array_ptr_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package tests
2+
3+
import "testing"
4+
5+
func TestSliceToArrayPointerConversion(t *testing.T) {
6+
// https://tip.golang.org/ref/spec#Conversions_from_slice_to_array_pointer
7+
s := make([]byte, 2, 4)
8+
s0 := (*[0]byte)(s)
9+
if s0 == nil {
10+
t.Error("s0 should not be nil")
11+
}
12+
s2 := (*[2]byte)(s)
13+
if &s2[0] != &s[0] {
14+
t.Error("&s2[0] should match &s[0]")
15+
}
16+
r := func() (r interface{}) {
17+
defer func() {
18+
r = recover()
19+
}()
20+
s4 := (*[4]byte)(s)
21+
_ = s4
22+
return nil
23+
}()
24+
if r == nil {
25+
t.Error("out-of-bounds conversion of s should panic")
26+
}
27+
28+
var q []string
29+
q0 := (*[0]string)(q)
30+
if q0 != nil {
31+
t.Error("t0 should be nil")
32+
}
33+
r = func() (r interface{}) {
34+
defer func() {
35+
r = recover()
36+
}()
37+
q1 := (*[1]string)(q)
38+
_ = q1
39+
return nil
40+
}
41+
if r == nil {
42+
t.Error("out-of-bounds conversion of q should panic")
43+
}
44+
45+
u := make([]byte, 0)
46+
u0 := (*[0]byte)(u)
47+
if u0 == nil {
48+
t.Error("u0 should not be nil")
49+
}
50+
}

0 commit comments

Comments
 (0)