1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "fmt"
13 "os"
14 "strconv"
15 )
16
17 type T1 struct {
18 x uint8
19 }
20 type T2 struct {
21 x uint16
22 }
23 type T4 struct {
24 x uint32
25 }
26
27 func main() {
28 report := len(os.Args) > 1
29 status := 0
30 var b1 [10]T1
31 a0, _ := strconv.ParseUint(fmt.Sprintf("%p", &b1[0])[2:], 16, 64)
32 a1, _ := strconv.ParseUint(fmt.Sprintf("%p", &b1[1])[2:], 16, 64)
33 if a1 != a0+1 {
34 fmt.Println("FAIL")
35 if report {
36 fmt.Println("alignment should be 1, is", a1-a0)
37 }
38 status = 1
39 }
40 var b2 [10]T2
41 a0, _ = strconv.ParseUint(fmt.Sprintf("%p", &b2[0])[2:], 16, 64)
42 a1, _ = strconv.ParseUint(fmt.Sprintf("%p", &b2[1])[2:], 16, 64)
43 if a1 != a0+2 {
44 if status == 0 {
45 fmt.Println("FAIL")
46 status = 1
47 }
48 if report {
49 fmt.Println("alignment should be 2, is", a1-a0)
50 }
51 }
52 var b4 [10]T4
53 a0, _ = strconv.ParseUint(fmt.Sprintf("%p", &b4[0])[2:], 16, 64)
54 a1, _ = strconv.ParseUint(fmt.Sprintf("%p", &b4[1])[2:], 16, 64)
55 if a1 != a0+4 {
56 if status == 0 {
57 fmt.Println("FAIL")
58 status = 1
59 }
60 if report {
61 fmt.Println("alignment should be 4, is", a1-a0)
62 }
63 }
64 os.Exit(status)
65 }
66
View as plain text