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

View as plain text