Update to copy of current master Go testsuite.
[official-gcc.git] / gcc / testsuite / go.test / test / sizeof.go
blobc3db1e5c3aefab121adb94836b8d25b386ce8a84
1 // run
3 // Copyright 2011 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 package main
9 import "unsafe"
11 type T struct {
12 X int
15 var t T
17 func isUintptr(uintptr) {}
19 type T2 struct {
20 A int32
24 type U2 struct {
25 B int32
26 C int32
29 var t2 T2
30 var p2 *T2
32 func main() {
33 // Test unsafe.Sizeof, unsafe.Alignof, and unsafe.Offsetof all return uintptr.
34 isUintptr(unsafe.Sizeof(t))
35 isUintptr(unsafe.Alignof(t))
36 isUintptr(unsafe.Offsetof(t.X))
38 // Test correctness of Offsetof with respect to embedded fields (issue 4909).
39 if unsafe.Offsetof(t2.C) != 8 {
40 println(unsafe.Offsetof(t2.C), "!= 8")
41 panic("unsafe.Offsetof(t2.C) != 8")
43 if unsafe.Offsetof(p2.C) != 8 {
44 println(unsafe.Offsetof(p2.C), "!= 8")
45 panic("unsafe.Offsetof(p2.C) != 8")
47 if unsafe.Offsetof(t2.U2.C) != 4 {
48 println(unsafe.Offsetof(t2.U2.C), "!= 4")
49 panic("unsafe.Offsetof(t2.U2.C) != 4")
51 if unsafe.Offsetof(p2.U2.C) != 4 {
52 println(unsafe.Offsetof(p2.U2.C), "!= 4")
53 panic("unsafe.Offsetof(p2.U2.C) != 4")
55 testDeep()
56 testNotEmbedded()
59 type (
60 S1 struct {
61 A int64
64 S2 struct {
65 B int64
68 S3 struct {
69 C int64
72 S4 struct {
73 D int64
76 S5 struct {
77 E int64
80 S6 struct {
81 F int64
84 S7 struct {
85 G int64
88 S8 struct {
89 H int64
90 *S1
94 func testDeep() {
95 var s1 S1
96 switch {
97 case unsafe.Offsetof(s1.A) != 0:
98 panic("unsafe.Offsetof(s1.A) != 0")
99 case unsafe.Offsetof(s1.B) != 8:
100 panic("unsafe.Offsetof(s1.B) != 8")
101 case unsafe.Offsetof(s1.C) != 16:
102 panic("unsafe.Offsetof(s1.C) != 16")
103 case unsafe.Offsetof(s1.D) != 24:
104 panic("unsafe.Offsetof(s1.D) != 24")
105 case unsafe.Offsetof(s1.E) != 32:
106 panic("unsafe.Offsetof(s1.E) != 32")
107 case unsafe.Offsetof(s1.F) != 40:
108 panic("unsafe.Offsetof(s1.F) != 40")
109 case unsafe.Offsetof(s1.G) != 48:
110 panic("unsafe.Offsetof(s1.G) != 48")
111 case unsafe.Offsetof(s1.H) != 56:
112 panic("unsafe.Offsetof(s1.H) != 56")
113 case unsafe.Offsetof(s1.S1) != 64:
114 panic("unsafe.Offsetof(s1.S1) != 64")
115 case unsafe.Offsetof(s1.S1.S2.S3.S4.S5.S6.S7.S8.S1.S2) != 8:
116 panic("unsafe.Offsetof(s1.S1.S2.S3.S4.S5.S6.S7.S8.S1.S2) != 8")
120 func testNotEmbedded() {
121 type T2 struct {
122 B int32
123 C int32
125 type T1 struct {
126 A int32
129 type T struct {
130 Dummy int32
131 F T1
132 P *T1
135 var t T
136 var p *T
137 switch {
138 case unsafe.Offsetof(t.F.B) != 4:
139 panic("unsafe.Offsetof(t.F.B) != 4")
140 case unsafe.Offsetof(t.F.C) != 8:
141 panic("unsafe.Offsetof(t.F.C) != 8")
143 case unsafe.Offsetof(t.P.B) != 4:
144 panic("unsafe.Offsetof(t.P.B) != 4")
145 case unsafe.Offsetof(t.P.C) != 8:
146 panic("unsafe.Offsetof(t.P.C) != 8")
148 case unsafe.Offsetof(p.F.B) != 4:
149 panic("unsafe.Offsetof(p.F.B) != 4")
150 case unsafe.Offsetof(p.F.C) != 8:
151 panic("unsafe.Offsetof(p.F.C) != 8")
153 case unsafe.Offsetof(p.P.B) != 4:
154 panic("unsafe.Offsetof(p.P.B) != 4")
155 case unsafe.Offsetof(p.P.C) != 8:
156 panic("unsafe.Offsetof(p.P.C) != 8")