From 031f71efdf1fd72322247a92c2d1a56eac16fd45 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Fri, 9 Mar 2018 08:24:10 -0800 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/runtime/export_test.go | 2 ++ src/runtime/sizeof_test.go | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/runtime/sizeof_test.go 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) + } + } +}