]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: add sizeof test
authorRob Findley <rfindley@google.com>
Tue, 20 Apr 2021 22:39:03 +0000 (18:39 -0400)
committerRobert Findley <rfindley@google.com>
Wed, 21 Apr 2021 20:35:43 +0000 (20:35 +0000)
This is a direct port of CL 310530 to go/types, adjusted only for names
and to account for the smaller size of objects in go/types, due to
(I assume) token.Pos vs syntax.Pos.

Change-Id: I0cc34d56e41c1e66b17edd0ccd3f281d97a6b235
Reviewed-on: https://go-review.googlesource.com/c/go/+/312091
Trust: Robert Findley <rfindley@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/sizeof_test.go [new file with mode: 0644]

diff --git a/src/go/types/sizeof_test.go b/src/go/types/sizeof_test.go
new file mode 100644 (file)
index 0000000..5a9d07c
--- /dev/null
@@ -0,0 +1,63 @@
+// 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 types
+
+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{}, 64, 128},
+               {_TypeParam{}, 28, 48},
+               {instance{}, 44, 88},
+               {bottom{}, 0, 0},
+               {top{}, 0, 0},
+
+               // Objects
+               {PkgName{}, 48, 88},
+               {Const{}, 48, 88},
+               {TypeName{}, 40, 72},
+               {Var{}, 44, 80},
+               {Func{}, 44, 80},
+               {Label{}, 44, 80},
+               {Builtin{}, 44, 80},
+               {Nil{}, 40, 72},
+
+               // Misc
+               {Scope{}, 40, 80},
+               {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)
+               }
+       }
+}