github.com/ice-blockchain/go/src@v0.0.0-20240403114104-1564d284e521/runtime/sizeof_test.go (about)

     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(264)
    21  	if goexperiment.ExecTracer2 {
    22  		// gTraceState changed from 2 uint64, 1 pointer, 1 bool to 2 uint64, 3 uint32.
    23  		// On 32-bit, that's one extra word.
    24  		g32bit += 4
    25  	}
    26  
    27  	var tests = []struct {
    28  		val    any     // type as a value
    29  		_32bit uintptr // size on 32bit platforms
    30  		_64bit uintptr // size on 64bit platforms
    31  	}{
    32  		{runtime.G{}, g32bit, 432}, // g, but exported for testing
    33  		{runtime.Sudog{}, 56, 88},  // sudog, but exported for testing
    34  	}
    35  
    36  	for _, tt := range tests {
    37  		want := tt._32bit
    38  		if _64bit {
    39  			want = tt._64bit
    40  		}
    41  		got := reflect.TypeOf(tt.val).Size()
    42  		if want != got {
    43  			t.Errorf("unsafe.Sizeof(%T) = %d, want %d", tt.val, got, want)
    44  		}
    45  	}
    46  }