Source file
src/runtime/sizeof_test.go
1
2
3
4
5 package runtime_test
6
7 import (
8 "reflect"
9 "runtime"
10 "testing"
11 "unsafe"
12 )
13
14
15
16
17
18
19 func TestSizeof(t *testing.T) {
20 const _64bit = unsafe.Sizeof(uintptr(0)) == 8
21 const xreg = unsafe.Sizeof(runtime.XRegPerG{})
22 var tests = []struct {
23 val any
24 _32bit uintptr
25 _64bit uintptr
26 }{
27 {runtime.G{}, 284 + xreg, 448 + xreg},
28 {runtime.Sudog{}, 64, 104},
29 }
30
31 if xreg > runtime.PtrSize {
32 t.Errorf("unsafe.Sizeof(xRegPerG) = %d, want <= %d", xreg, runtime.PtrSize)
33 }
34
35 for _, tt := range tests {
36 want := tt._32bit
37 if _64bit {
38 want = tt._64bit
39 }
40 got := reflect.TypeOf(tt.val).Size()
41 if want != got {
42 t.Errorf("unsafe.Sizeof(%T) = %d, want %d", tt.val, got, want)
43 gotClass, wantClass := runtime.AllocSize(got), runtime.AllocSize(want)
44 if gotClass != wantClass {
45 t.Errorf("%T moves from the %d-byte to the %d-byte size class, %+d bytes per object", tt.val, wantClass, gotClass, int(gotClass)-int(wantClass))
46 }
47 }
48 }
49 }
50
View as plain text