]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: add TestSizeof
authorJosh Bleecher Snyder <josharian@gmail.com>
Fri, 9 Mar 2018 16:24:10 +0000 (08:24 -0800)
committerJosh Bleecher Snyder <josharian@gmail.com>
Fri, 9 Mar 2018 17:03:25 +0000 (17:03 +0000)
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>
src/runtime/export_test.go
src/runtime/sizeof_test.go [new file with mode: 0644]

index 6d8f88d3a719110ab5cd426ca355851708e2de4a..c8f6fcd7d15a45b031c098565d5496f6b89cc9e2 100644 (file)
@@ -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 (file)
index 0000000..830055e
--- /dev/null
@@ -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)
+               }
+       }
+}