2012-02-16 21:48:57 -07:00
|
|
|
// run
|
2009-08-20 12:12:04 -06:00
|
|
|
|
|
|
|
// Copyright 2009 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.
|
|
|
|
|
2012-02-18 19:19:43 -07:00
|
|
|
// Test the cap predeclared function applied to channels.
|
|
|
|
|
2009-08-20 12:12:04 -06:00
|
|
|
package main
|
|
|
|
|
2017-08-13 12:03:02 -06:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type T chan int
|
|
|
|
|
|
|
|
const ptrSize = unsafe.Sizeof((*byte)(nil))
|
|
|
|
|
2009-08-20 12:12:04 -06:00
|
|
|
func main() {
|
2017-08-13 12:03:02 -06:00
|
|
|
c := make(T, 10)
|
2009-08-20 12:12:04 -06:00
|
|
|
if len(c) != 0 || cap(c) != 10 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("chan len/cap ", len(c), cap(c), " want 0 10")
|
|
|
|
panic("fail")
|
2009-08-20 12:12:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
2010-03-24 17:46:53 -06:00
|
|
|
c <- i
|
2009-08-20 12:12:04 -06:00
|
|
|
}
|
|
|
|
if len(c) != 3 || cap(c) != 10 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("chan len/cap ", len(c), cap(c), " want 3 10")
|
|
|
|
panic("fail")
|
2009-08-20 12:12:04 -06:00
|
|
|
}
|
2010-03-24 17:46:53 -06:00
|
|
|
|
2017-08-13 12:03:02 -06:00
|
|
|
c = make(T)
|
2009-08-20 12:12:04 -06:00
|
|
|
if len(c) != 0 || cap(c) != 0 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("chan len/cap ", len(c), cap(c), " want 0 0")
|
|
|
|
panic("fail")
|
2009-08-20 12:12:04 -06:00
|
|
|
}
|
2017-08-13 12:03:02 -06:00
|
|
|
|
|
|
|
n := -1
|
|
|
|
shouldPanic("makechan: size out of range", func() { _ = make(T, n) })
|
|
|
|
shouldPanic("makechan: size out of range", func() { _ = make(T, int64(n)) })
|
|
|
|
if ptrSize == 8 {
|
|
|
|
n = 1 << 20
|
|
|
|
n <<= 20
|
|
|
|
shouldPanic("makechan: size out of range", func() { _ = make(T, n) })
|
|
|
|
n <<= 20
|
|
|
|
shouldPanic("makechan: size out of range", func() { _ = make(T, n) })
|
|
|
|
} else {
|
|
|
|
n = 1<<31 - 1
|
|
|
|
shouldPanic("makechan: size out of range", func() { _ = make(T, n) })
|
|
|
|
shouldPanic("makechan: size out of range", func() { _ = make(T, int64(n)) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldPanic(str string, f func()) {
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
panic("did not panic")
|
|
|
|
}
|
|
|
|
s := err.(error).Error()
|
|
|
|
if !strings.Contains(s, str) {
|
|
|
|
panic("got panic " + s + ", want " + str)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
f()
|
2009-08-20 12:12:04 -06:00
|
|
|
}
|