github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/test/fixedbugs/bug260.go (about)

     1  // run
     2  
     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  
     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  
    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  }