Rémy Oudompheng | 2ece2f5 | 2012-02-18 22:15:42 +0100 | [diff] [blame] | 1 | // run |
Rob Pike | 6e3853e | 2010-03-04 15:26:15 -0800 | [diff] [blame] | 2 | |
Olivier Duperray | 0da89b3 | 2012-01-24 14:48:15 -0500 | [diff] [blame] | 3 | // 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 Pike | 6e3853e | 2010-03-04 15:26:15 -0800 | [diff] [blame] | 7 | // Test that structures pack densely, according to the alignment of the largest field. |
| 8 | |
| 9 | package main |
| 10 | |
| 11 | import ( |
| 12 | "fmt" |
| 13 | "os" |
| 14 | "strconv" |
| 15 | ) |
| 16 | |
Russ Cox | dc9a3b2 | 2010-12-13 16:22:19 -0500 | [diff] [blame] | 17 | type T1 struct { |
| 18 | x uint8 |
| 19 | } |
| 20 | type T2 struct { |
| 21 | x uint16 |
| 22 | } |
| 23 | type T4 struct { |
| 24 | x uint32 |
| 25 | } |
Rob Pike | 6e3853e | 2010-03-04 15:26:15 -0800 | [diff] [blame] | 26 | |
| 27 | func main() { |
Ian Lance Taylor | 4b22e1b | 2010-03-05 10:43:33 -0800 | [diff] [blame] | 28 | report := len(os.Args) > 1 |
Rob Pike | 6e3853e | 2010-03-04 15:26:15 -0800 | [diff] [blame] | 29 | status := 0 |
| 30 | var b1 [10]T1 |
Russ Cox | 2666b81 | 2011-12-05 15:48:46 -0500 | [diff] [blame] | 31 | a0, _ := strconv.ParseUint(fmt.Sprintf("%p", &b1[0])[2:], 16, 64) |
| 32 | a1, _ := strconv.ParseUint(fmt.Sprintf("%p", &b1[1])[2:], 16, 64) |
Russ Cox | dc9a3b2 | 2010-12-13 16:22:19 -0500 | [diff] [blame] | 33 | if a1 != a0+1 { |
Ian Lance Taylor | 4b22e1b | 2010-03-05 10:43:33 -0800 | [diff] [blame] | 34 | fmt.Println("FAIL") |
| 35 | if report { |
| 36 | fmt.Println("alignment should be 1, is", a1-a0) |
| 37 | } |
Rob Pike | 6e3853e | 2010-03-04 15:26:15 -0800 | [diff] [blame] | 38 | status = 1 |
| 39 | } |
| 40 | var b2 [10]T2 |
Russ Cox | 2666b81 | 2011-12-05 15:48:46 -0500 | [diff] [blame] | 41 | a0, _ = strconv.ParseUint(fmt.Sprintf("%p", &b2[0])[2:], 16, 64) |
| 42 | a1, _ = strconv.ParseUint(fmt.Sprintf("%p", &b2[1])[2:], 16, 64) |
Russ Cox | dc9a3b2 | 2010-12-13 16:22:19 -0500 | [diff] [blame] | 43 | if a1 != a0+2 { |
Ian Lance Taylor | 4b22e1b | 2010-03-05 10:43:33 -0800 | [diff] [blame] | 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 | } |
Rob Pike | 6e3853e | 2010-03-04 15:26:15 -0800 | [diff] [blame] | 51 | } |
| 52 | var b4 [10]T4 |
Russ Cox | 2666b81 | 2011-12-05 15:48:46 -0500 | [diff] [blame] | 53 | a0, _ = strconv.ParseUint(fmt.Sprintf("%p", &b4[0])[2:], 16, 64) |
| 54 | a1, _ = strconv.ParseUint(fmt.Sprintf("%p", &b4[1])[2:], 16, 64) |
Russ Cox | dc9a3b2 | 2010-12-13 16:22:19 -0500 | [diff] [blame] | 55 | if a1 != a0+4 { |
Ian Lance Taylor | 4b22e1b | 2010-03-05 10:43:33 -0800 | [diff] [blame] | 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 | } |
Rob Pike | 6e3853e | 2010-03-04 15:26:15 -0800 | [diff] [blame] | 63 | } |
| 64 | os.Exit(status) |
| 65 | } |