Black Lives Matter. Support the Equal Justice Initiative.

Source file src/go/types/sizeof_test.go

Documentation: go/types

     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    interface{} // 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{}, 44, 88},
    29  		{_Sum{}, 12, 24},
    30  		{Interface{}, 60, 120},
    31  		{Map{}, 16, 32},
    32  		{Chan{}, 12, 24},
    33  		{Named{}, 64, 128},
    34  		{_TypeParam{}, 28, 48},
    35  		{instance{}, 44, 88},
    36  		{bottom{}, 0, 0},
    37  		{top{}, 0, 0},
    38  
    39  		// Objects
    40  		{PkgName{}, 48, 88},
    41  		{Const{}, 48, 88},
    42  		{TypeName{}, 40, 72},
    43  		{Var{}, 44, 80},
    44  		{Func{}, 44, 80},
    45  		{Label{}, 44, 80},
    46  		{Builtin{}, 44, 80},
    47  		{Nil{}, 40, 72},
    48  
    49  		// Misc
    50  		{Scope{}, 40, 80},
    51  		{Package{}, 40, 80},
    52  	}
    53  	for _, test := range tests {
    54  		got := reflect.TypeOf(test.val).Size()
    55  		want := test._32bit
    56  		if _64bit {
    57  			want = test._64bit
    58  		}
    59  		if got != want {
    60  			t.Errorf("unsafe.Sizeof(%T) = %d, want %d", test.val, got, want)
    61  		}
    62  	}
    63  }
    64  

View as plain text