...
Run Format

Source file test/fixedbugs/issue4085b.go

Documentation: test/fixedbugs

     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  
     7  package main
     8  
     9  import (
    10  	"strings"
    11  	"unsafe"
    12  )
    13  
    14  type T []int
    15  
    16  func 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  	shouldPanic("len out of range", func() { _ = make(T, int64(n)) })
    21  	shouldPanic("cap out of range", func() { _ = make(T, 0, int64(n)) })
    22  	var t *byte
    23  	if unsafe.Sizeof(t) == 8 {
    24  		n = 1 << 20
    25  		n <<= 20
    26  		shouldPanic("len out of range", func() { _ = make(T, n) })
    27  		shouldPanic("cap out of range", func() { _ = make(T, 0, n) })
    28  		n <<= 20
    29  		shouldPanic("len out of range", func() { _ = make(T, n) })
    30  		shouldPanic("cap out of range", func() { _ = make(T, 0, n) })
    31  	} else {
    32  		n = 1<<31 - 1
    33  		shouldPanic("len out of range", func() { _ = make(T, n) })
    34  		shouldPanic("cap out of range", func() { _ = make(T, 0, n) })
    35  		shouldPanic("len out of range", func() { _ = make(T, int64(n)) })
    36  		shouldPanic("cap out of range", func() { _ = make(T, 0, int64(n)) })
    37  	}
    38  }
    39  
    40  func shouldPanic(str string, f func()) {
    41  	defer func() {
    42  		err := recover()
    43  		if err == nil {
    44  			panic("did not panic")
    45  		}
    46  		s := err.(error).Error()
    47  		if !strings.Contains(s, str) {
    48  			panic("got panic " + s + ", want " + str)
    49  		}
    50  	}()
    51  
    52  	f()
    53  }
    54  

View as plain text