Source file src/runtime/sizeof_test.go

     1  // Copyright 2018 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 runtime_test
     6  
     7  import (
     8  	"internal/goexperiment"
     9  	"reflect"
    10  	"runtime"
    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  	g32bit := uintptr(256)
    21  	if goexperiment.ExecTracer2 {
    22  		g32bit = uintptr(260)
    23  	}
    24  
    25  	var tests = []struct {
    26  		val    any     // type as a value
    27  		_32bit uintptr // size on 32bit platforms
    28  		_64bit uintptr // size on 64bit platforms
    29  	}{
    30  		{runtime.G{}, g32bit, 424}, // g, but exported for testing
    31  		{runtime.Sudog{}, 56, 88},  // sudog, but exported for testing
    32  	}
    33  
    34  	for _, tt := range tests {
    35  		want := tt._32bit
    36  		if _64bit {
    37  			want = tt._64bit
    38  		}
    39  		got := reflect.TypeOf(tt.val).Size()
    40  		if want != got {
    41  			t.Errorf("unsafe.Sizeof(%T) = %d, want %d", tt.val, got, want)
    42  		}
    43  	}
    44  }
    45  

View as plain text