blob: 6211c4885ac616427aba093037d8f2a4fbd46b01 [file] [log] [blame]
Rémy Oudompheng2ece2f52012-02-18 22:15:42 +01001// run
Rob Pike6e3853e2010-03-04 15:26:15 -08002
Olivier Duperray0da89b32012-01-24 14:48:15 -05003// Copyright 2010 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
6
Rob Pike6e3853e2010-03-04 15:26:15 -08007// Test that structures pack densely, according to the alignment of the largest field.
8
9package main
10
11import (
12 "fmt"
13 "os"
14 "strconv"
15)
16
Russ Coxdc9a3b22010-12-13 16:22:19 -050017type T1 struct {
18 x uint8
19}
20type T2 struct {
21 x uint16
22}
23type T4 struct {
24 x uint32
25}
Rob Pike6e3853e2010-03-04 15:26:15 -080026
27func main() {
Ian Lance Taylor4b22e1b2010-03-05 10:43:33 -080028 report := len(os.Args) > 1
Rob Pike6e3853e2010-03-04 15:26:15 -080029 status := 0
30 var b1 [10]T1
Russ Cox2666b812011-12-05 15:48:46 -050031 a0, _ := strconv.ParseUint(fmt.Sprintf("%p", &b1[0])[2:], 16, 64)
32 a1, _ := strconv.ParseUint(fmt.Sprintf("%p", &b1[1])[2:], 16, 64)
Russ Coxdc9a3b22010-12-13 16:22:19 -050033 if a1 != a0+1 {
Ian Lance Taylor4b22e1b2010-03-05 10:43:33 -080034 fmt.Println("FAIL")
35 if report {
36 fmt.Println("alignment should be 1, is", a1-a0)
37 }
Rob Pike6e3853e2010-03-04 15:26:15 -080038 status = 1
39 }
40 var b2 [10]T2
Russ Cox2666b812011-12-05 15:48:46 -050041 a0, _ = strconv.ParseUint(fmt.Sprintf("%p", &b2[0])[2:], 16, 64)
42 a1, _ = strconv.ParseUint(fmt.Sprintf("%p", &b2[1])[2:], 16, 64)
Russ Coxdc9a3b22010-12-13 16:22:19 -050043 if a1 != a0+2 {
Ian Lance Taylor4b22e1b2010-03-05 10:43:33 -080044 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 }
Rob Pike6e3853e2010-03-04 15:26:15 -080051 }
52 var b4 [10]T4
Russ Cox2666b812011-12-05 15:48:46 -050053 a0, _ = strconv.ParseUint(fmt.Sprintf("%p", &b4[0])[2:], 16, 64)
54 a1, _ = strconv.ParseUint(fmt.Sprintf("%p", &b4[1])[2:], 16, 64)
Russ Coxdc9a3b22010-12-13 16:22:19 -050055 if a1 != a0+4 {
Ian Lance Taylor4b22e1b2010-03-05 10:43:33 -080056 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 }
Rob Pike6e3853e2010-03-04 15:26:15 -080063 }
64 os.Exit(status)
65}