1// run
2
3// Copyright 2013 The Go Authors.  All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7package main
8
9import (
10	"strings"
11	"unsafe"
12)
13
14type T []int
15
16func main() {
17	n := -1
18	shouldPanic("len out of range", func() {_ = make(T, n)})
19	shouldPanic("cap out of range", func() {_ = make(T, 0, n)})
20	var t *byte
21	if unsafe.Sizeof(t) == 8 {
22		n = 1<<20
23		n <<= 20
24		shouldPanic("len out of range", func() {_ = make(T, n)})
25		shouldPanic("cap out of range", func() {_ = make(T, 0, n)})
26		n <<= 20
27		shouldPanic("len out of range", func() {_ = make(T, n)})
28		shouldPanic("cap out of range", func() {_ = make(T, 0, n)})
29	} else {
30		n = 1<<31 - 1
31		shouldPanic("len out of range", func() {_ = make(T, n)})
32		shouldPanic("cap out of range", func() {_ = make(T, 0, n)})
33	}
34}
35
36func shouldPanic(str string, f func()) {
37	defer func() {
38		err := recover()
39		if err == nil {
40			panic("did not panic")
41		}
42		s := err.(error).Error()
43		if !strings.Contains(s, str) {
44			panic("got panic " + s + ", want " + str)
45		}
46	}()
47
48	f()
49}
50