Skip to content

Commit 031f71e

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent b2d1cd2 commit 031f71e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/runtime/export_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,5 @@ func MapNextArenaHint() (start, end uintptr) {
444444
func GetNextArenaHint() uintptr {
445445
return mheap_.arenaHints.addr
446446
}
447+
448+
type G = g

src/runtime/sizeof_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !nacl
6+
7+
package runtime_test
8+
9+
import (
10+
"reflect"
11+
"runtime"
12+
"testing"
13+
"unsafe"
14+
)
15+
16+
// Assert that the size of important structures do not change unexpectedly.
17+
18+
func TestSizeof(t *testing.T) {
19+
const _64bit = unsafe.Sizeof(uintptr(0)) == 8
20+
21+
var tests = []struct {
22+
val interface{} // type as a value
23+
_32bit uintptr // size on 32bit platforms
24+
_64bit uintptr // size on 64bit platforms
25+
}{
26+
{runtime.G{}, 216, 376}, // g, but exported for testing
27+
}
28+
29+
for _, tt := range tests {
30+
want := tt._32bit
31+
if _64bit {
32+
want = tt._64bit
33+
}
34+
got := reflect.TypeOf(tt.val).Size()
35+
if want != got {
36+
t.Errorf("unsafe.Sizeof(%T) = %d, want %d", tt.val, got, want)
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)