aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/types2/sizeof_test.go
blob: a7f1185fa89d73f0be54fffdd47eeb11b95c8f9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package types2

import (
	"reflect"
	"testing"
)

// Signal size changes of important structures.

func TestSizeof(t *testing.T) {
	const _64bit = ^uint(0)>>32 != 0

	var tests = []struct {
		val    interface{} // type as a value
		_32bit uintptr     // size on 32bit platforms
		_64bit uintptr     // size on 64bit platforms
	}{
		// Types
		{Basic{}, 16, 32},
		{Array{}, 16, 24},
		{Slice{}, 8, 16},
		{Struct{}, 24, 48},
		{Pointer{}, 8, 16},
		{Tuple{}, 12, 24},
		{Signature{}, 28, 56},
		{Union{}, 16, 32},
		{Interface{}, 44, 88},
		{Map{}, 16, 32},
		{Chan{}, 12, 24},
		{Named{}, 68, 128},
		{TypeParam{}, 28, 48},
		{term{}, 12, 24},
		{top{}, 0, 0},

		// Objects
		{PkgName{}, 64, 104},
		{Const{}, 64, 104},
		{TypeName{}, 56, 88},
		{Var{}, 60, 96},
		{Func{}, 60, 96},
		{Label{}, 60, 96},
		{Builtin{}, 60, 96},
		{Nil{}, 56, 88},

		// Misc
		{Scope{}, 60, 104},
		{Package{}, 40, 80},
		{_TypeSet{}, 28, 56},
	}

	for _, test := range tests {
		got := reflect.TypeOf(test.val).Size()
		want := test._32bit
		if _64bit {
			want = test._64bit
		}
		if got != want {
			t.Errorf("unsafe.Sizeof(%T) = %d, want %d", test.val, got, want)
		}
	}
}