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  	"reflect"
     9  	"runtime"
    10  	"testing"
    11  	"unsafe"
    12  )
    13  
    14  // Assert that the size of important structures do not change unexpectedly.
    15  
    16  func TestSizeof(t *testing.T) {
    17  	const _64bit = unsafe.Sizeof(uintptr(0)) == 8
    18  	const xreg = unsafe.Sizeof(runtime.XRegPerG{}) // Varies per architecture
    19  	var tests = []struct {
    20  		val    any     // type as a value
    21  		_32bit uintptr // size on 32bit platforms
    22  		_64bit uintptr // size on 64bit platforms
    23  	}{
    24  		{runtime.G{}, 280 + xreg, 440 + xreg}, // g, but exported for testing
    25  		{runtime.Sudog{}, 56, 88},             // sudog, but exported for testing
    26  	}
    27  
    28  	if xreg > runtime.PtrSize {
    29  		t.Errorf("unsafe.Sizeof(xRegPerG) = %d, want <= %d", xreg, runtime.PtrSize)
    30  	}
    31  
    32  	for _, tt := range tests {
    33  		want := tt._32bit
    34  		if _64bit {
    35  			want = tt._64bit
    36  		}
    37  		got := reflect.TypeOf(tt.val).Size()
    38  		if want != got {
    39  			t.Errorf("unsafe.Sizeof(%T) = %d, want %d", tt.val, got, want)
    40  		}
    41  	}
    42  }
    43  

View as plain text