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

     1  // Copyright 2021 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  package types2
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  // Signal size changes of important structures.
    13  
    14  func TestSizeof(t *testing.T) {
    15  	const _64bit = ^uint(0)>>32 != 0
    16  
    17  	var tests = []struct {
    18  		val    interface{} // type as a value
    19  		_32bit uintptr     // size on 32bit platforms
    20  		_64bit uintptr     // size on 64bit platforms
    21  	}{
    22  		// Types
    23  		{Basic{}, 16, 32},
    24  		{Array{}, 16, 24},
    25  		{Slice{}, 8, 16},
    26  		{Struct{}, 24, 48},
    27  		{Pointer{}, 8, 16},
    28  		{Tuple{}, 12, 24},
    29  		{Signature{}, 28, 56},
    30  		{Union{}, 12, 24},
    31  		{Interface{}, 40, 80},
    32  		{Map{}, 16, 32},
    33  		{Chan{}, 12, 24},
    34  		{Named{}, 60, 112},
    35  		{TypeParam{}, 28, 48},
    36  		{term{}, 12, 24},
    37  
    38  		// Objects
    39  		{PkgName{}, 64, 104},
    40  		{Const{}, 64, 104},
    41  		{TypeName{}, 56, 88},
    42  		{Var{}, 64, 104},
    43  		{Func{}, 64, 104},
    44  		{Label{}, 60, 96},
    45  		{Builtin{}, 60, 96},
    46  		{Nil{}, 56, 88},
    47  
    48  		// Misc
    49  		{Scope{}, 60, 104},
    50  		{Package{}, 44, 88},
    51  		{_TypeSet{}, 28, 56},
    52  	}
    53  
    54  	for _, test := range tests {
    55  		got := reflect.TypeOf(test.val).Size()
    56  		want := test._32bit
    57  		if _64bit {
    58  			want = test._64bit
    59  		}
    60  		if got != want {
    61  			t.Errorf("unsafe.Sizeof(%T) = %d, want %d", test.val, got, want)
    62  		}
    63  	}
    64  }
    65  

View as plain text