diff --git a/src/cmd/compile/internal/gc/sizeof_test.go b/src/cmd/compile/internal/gc/sizeof_test.go index c8b1789669..bea25dde2b 100644 --- a/src/cmd/compile/internal/gc/sizeof_test.go +++ b/src/cmd/compile/internal/gc/sizeof_test.go @@ -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 index 0000000000..a073f9b1a7 --- /dev/null +++ b/src/cmd/compile/internal/types/sizeof_test.go @@ -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) + } + } +}