]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: move sizeof tests for types structs to package types
authorRobert Griesemer <gri@golang.org>
Fri, 7 Apr 2017 03:13:04 +0000 (20:13 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 7 Apr 2017 03:43:34 +0000 (03:43 +0000)
Change-Id: I04cd4dd0ed55b88247a056b429fc496539cd0985
Reviewed-on: https://go-review.googlesource.com/39910
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/sizeof_test.go
src/cmd/compile/internal/types/sizeof_test.go [new file with mode: 0644]

index c8b1789669e8ee1a148d846c8ede904143b9ca55..bea25dde2b01effba1d8ae2910d451d2db1578da 100644 (file)
@@ -7,7 +7,6 @@
 package gc
 
 import (
-       "cmd/compile/internal/types"
        "reflect"
        "testing"
        "unsafe"
@@ -27,21 +26,6 @@ func TestSizeof(t *testing.T) {
                {Name{}, 36, 56},
                {Param{}, 28, 56},
                {Node{}, 84, 136},
-               // TODO(gri) test the ones below in the types package
-               {types.Sym{}, 60, 104},
-               {types.Type{}, 52, 88},
-               {types.MapType{}, 20, 40},
-               {types.ForwardType{}, 20, 32},
-               {types.FuncType{}, 28, 48},
-               {types.StructType{}, 12, 24},
-               {types.InterType{}, 4, 8},
-               {types.ChanType{}, 8, 16},
-               {types.ArrayType{}, 12, 16},
-               {types.DDDFieldType{}, 4, 8},
-               {types.FuncArgsType{}, 4, 8},
-               {types.ChanArgsType{}, 4, 8},
-               {types.PtrType{}, 4, 8},
-               {types.SliceType{}, 4, 8},
        }
 
        for _, tt := range tests {
diff --git a/src/cmd/compile/internal/types/sizeof_test.go b/src/cmd/compile/internal/types/sizeof_test.go
new file mode 100644 (file)
index 0000000..a073f9b
--- /dev/null
@@ -0,0 +1,51 @@
+// Copyright 2017 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 types
+
+import (
+       "reflect"
+       "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
+       }{
+               {Sym{}, 60, 104},
+               {Type{}, 52, 88},
+               {MapType{}, 20, 40},
+               {ForwardType{}, 20, 32},
+               {FuncType{}, 28, 48},
+               {StructType{}, 12, 24},
+               {InterType{}, 4, 8},
+               {ChanType{}, 8, 16},
+               {ArrayType{}, 12, 16},
+               {DDDFieldType{}, 4, 8},
+               {FuncArgsType{}, 4, 8},
+               {ChanArgsType{}, 4, 8},
+               {PtrType{}, 4, 8},
+               {SliceType{}, 4, 8},
+       }
+
+       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)
+               }
+       }
+}