aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/types2/sizeof_test.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-04-15 12:18:08 -0700
committerRobert Griesemer <gri@golang.org>2021-04-15 19:41:39 +0000
commit7ed6d1f2fb2e092f59ec56e0d16b1f9da33992a4 (patch)
treeaf1e31a0851972a3191e80db737c00c0bc69a930 /src/cmd/compile/internal/types2/sizeof_test.go
parenta63ff398d541a1b3a3658e38693adebf370aadbb (diff)
downloadgo-7ed6d1f2fb2e092f59ec56e0d16b1f9da33992a4.tar.gz
go-7ed6d1f2fb2e092f59ec56e0d16b1f9da33992a4.zip
cmd/compile/internal/types2: add sizeof test
Change-Id: I75d3c8546b7ad51d8c7369e7289036b697d6b913 Reviewed-on: https://go-review.googlesource.com/c/go/+/310530 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types2/sizeof_test.go')
-rw-r--r--src/cmd/compile/internal/types2/sizeof_test.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/types2/sizeof_test.go b/src/cmd/compile/internal/types2/sizeof_test.go
new file mode 100644
index 0000000000..236feb0404
--- /dev/null
+++ b/src/cmd/compile/internal/types2/sizeof_test.go
@@ -0,0 +1,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{}, 44, 88},
+ {Sum{}, 12, 24},
+ {Interface{}, 60, 120},
+ {Map{}, 16, 32},
+ {Chan{}, 12, 24},
+ {Named{}, 68, 136},
+ {TypeParam{}, 28, 48},
+ {instance{}, 52, 96},
+ {bottom{}, 0, 0},
+ {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{}, 56, 96},
+ {Package{}, 40, 80},
+ }
+
+ 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)
+ }
+ }
+}