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

     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  package types
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  	"unsafe"
    11  )
    12  
    13  // Assert that the size of important structures do not change unexpectedly.
    14  
    15  func TestSizeof(t *testing.T) {
    16  	const _64bit = unsafe.Sizeof(uintptr(0)) == 8
    17  
    18  	var tests = []struct {
    19  		val    interface{} // type as a value
    20  		_32bit uintptr     // size on 32bit platforms
    21  		_64bit uintptr     // size on 64bit platforms
    22  	}{
    23  		{Sym{}, 32, 64},
    24  		{Type{}, 56, 96},
    25  		{Map{}, 12, 24},
    26  		{Forward{}, 20, 32},
    27  		{Func{}, 32, 56},
    28  		{Struct{}, 12, 24},
    29  		{Interface{}, 0, 0},
    30  		{Chan{}, 8, 16},
    31  		{Array{}, 12, 16},
    32  		{FuncArgs{}, 4, 8},
    33  		{ChanArgs{}, 4, 8},
    34  		{Ptr{}, 4, 8},
    35  		{Slice{}, 4, 8},
    36  	}
    37  
    38  	for _, tt := range tests {
    39  		want := tt._32bit
    40  		if _64bit {
    41  			want = tt._64bit
    42  		}
    43  		got := reflect.TypeOf(tt.val).Size()
    44  		if want != got {
    45  			t.Errorf("unsafe.Sizeof(%T) = %d, want %d", tt.val, got, want)
    46  		}
    47  	}
    48  }
    49  

View as plain text