...
Run Format

Source file src/cmd/compile/internal/types/sizeof_test.go

Documentation: cmd/compile/internal/types

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build !nacl
     6  
     7  package types
     8  
     9  import (
    10  	"reflect"
    11  	"testing"
    12  	"unsafe"
    13  )
    14  
    15  // Assert that the size of important structures do not change unexpectedly.
    16  
    17  func TestSizeof(t *testing.T) {
    18  	const _64bit = unsafe.Sizeof(uintptr(0)) == 8
    19  
    20  	var tests = []struct {
    21  		val    interface{} // type as a value
    22  		_32bit uintptr     // size on 32bit platforms
    23  		_64bit uintptr     // size on 64bit platforms
    24  	}{
    25  		{Sym{}, 52, 88},
    26  		{Type{}, 52, 88},
    27  		{Map{}, 20, 40},
    28  		{Forward{}, 20, 32},
    29  		{Func{}, 32, 56},
    30  		{Struct{}, 16, 32},
    31  		{Interface{}, 8, 16},
    32  		{Chan{}, 8, 16},
    33  		{Array{}, 12, 16},
    34  		{DDDField{}, 4, 8},
    35  		{FuncArgs{}, 4, 8},
    36  		{ChanArgs{}, 4, 8},
    37  		{Ptr{}, 4, 8},
    38  		{Slice{}, 4, 8},
    39  	}
    40  
    41  	for _, tt := range tests {
    42  		want := tt._32bit
    43  		if _64bit {
    44  			want = tt._64bit
    45  		}
    46  		got := reflect.TypeOf(tt.val).Size()
    47  		if want != got {
    48  			t.Errorf("unsafe.Sizeof(%T) = %d, want %d", tt.val, got, want)
    49  		}
    50  	}
    51  }
    52  

View as plain text