aboutsummaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josharian@gmail.com>2018-03-09 08:24:10 -0800
committerJosh Bleecher Snyder <josharian@gmail.com>2018-03-09 17:03:25 +0000
commit031f71efdf1fd72322247a92c2d1a56eac16fd45 (patch)
treecb1e49ce9cc664ef7c755bd8df85ec489494f639 /src/runtime
parentb2d1cd2ad0dd1e0c84e6c3b2eccf77a5bcb84cdd (diff)
downloadgo-031f71efdf1fd72322247a92c2d1a56eac16fd45.tar.xz
runtime: add TestSizeof
Borrowed from cmd/compile, TestSizeof ensures that the size of important types doesn't change unexpectedly. It also helps reviewers see the impact of intended changes. Change-Id: If57955f0c3e66054de3f40c6bba585b88694c7be Reviewed-on: https://go-review.googlesource.com/99837 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/export_test.go2
-rw-r--r--src/runtime/sizeof_test.go39
2 files changed, 41 insertions, 0 deletions
diff --git a/src/runtime/export_test.go b/src/runtime/export_test.go
index 6d8f88d3a7..c8f6fcd7d1 100644
--- a/src/runtime/export_test.go
+++ b/src/runtime/export_test.go
@@ -444,3 +444,5 @@ func MapNextArenaHint() (start, end uintptr) {
func GetNextArenaHint() uintptr {
return mheap_.arenaHints.addr
}
+
+type G = g
diff --git a/src/runtime/sizeof_test.go b/src/runtime/sizeof_test.go
new file mode 100644
index 0000000000..830055e2aa
--- /dev/null
+++ b/src/runtime/sizeof_test.go
@@ -0,0 +1,39 @@
+// Copyright 2018 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.
+
+// +build !nacl
+
+package runtime_test
+
+import (
+ "reflect"
+ "runtime"
+ "testing"
+ "unsafe"
+)
+
+// Assert that the size of important structures do not change unexpectedly.
+
+func TestSizeof(t *testing.T) {
+ const _64bit = unsafe.Sizeof(uintptr(0)) == 8
+
+ var tests = []struct {
+ val interface{} // type as a value
+ _32bit uintptr // size on 32bit platforms
+ _64bit uintptr // size on 64bit platforms
+ }{
+ {runtime.G{}, 216, 376}, // g, but exported for testing
+ }
+
+ for _, tt := range tests {
+ want := tt._32bit
+ if _64bit {
+ want = tt._64bit
+ }
+ got := reflect.TypeOf(tt.val).Size()
+ if want != got {
+ t.Errorf("unsafe.Sizeof(%T) = %d, want %d", tt.val, got, want)
+ }
+ }
+}