Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // run |
Russ Cox | 8a45917 | 2009-08-20 11:12:04 -0700 | [diff] [blame] | 2 | |
| 3 | // Copyright 2009 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 | |
Rob Pike | fc0dc04 | 2012-02-19 13:19:43 +1100 | [diff] [blame] | 7 | // Test the cap predeclared function applied to channels. |
| 8 | |
Russ Cox | 8a45917 | 2009-08-20 11:12:04 -0700 | [diff] [blame] | 9 | package main |
| 10 | |
Martin Möhrmann | 8a6e51a | 2017-08-13 20:03:02 +0200 | [diff] [blame] | 11 | import ( |
| 12 | "strings" |
| 13 | "unsafe" |
| 14 | ) |
| 15 | |
| 16 | type T chan int |
| 17 | |
| 18 | const ptrSize = unsafe.Sizeof((*byte)(nil)) |
| 19 | |
Russ Cox | 8a45917 | 2009-08-20 11:12:04 -0700 | [diff] [blame] | 20 | func main() { |
Martin Möhrmann | 8a6e51a | 2017-08-13 20:03:02 +0200 | [diff] [blame] | 21 | c := make(T, 10) |
Russ Cox | 8a45917 | 2009-08-20 11:12:04 -0700 | [diff] [blame] | 22 | if len(c) != 0 || cap(c) != 10 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 23 | println("chan len/cap ", len(c), cap(c), " want 0 10") |
| 24 | panic("fail") |
Russ Cox | 8a45917 | 2009-08-20 11:12:04 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | for i := 0; i < 3; i++ { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 28 | c <- i |
Russ Cox | 8a45917 | 2009-08-20 11:12:04 -0700 | [diff] [blame] | 29 | } |
| 30 | if len(c) != 3 || cap(c) != 10 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 31 | println("chan len/cap ", len(c), cap(c), " want 3 10") |
| 32 | panic("fail") |
Russ Cox | 8a45917 | 2009-08-20 11:12:04 -0700 | [diff] [blame] | 33 | } |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 34 | |
Martin Möhrmann | 8a6e51a | 2017-08-13 20:03:02 +0200 | [diff] [blame] | 35 | c = make(T) |
Russ Cox | 8a45917 | 2009-08-20 11:12:04 -0700 | [diff] [blame] | 36 | if len(c) != 0 || cap(c) != 0 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 37 | println("chan len/cap ", len(c), cap(c), " want 0 0") |
| 38 | panic("fail") |
Russ Cox | 8a45917 | 2009-08-20 11:12:04 -0700 | [diff] [blame] | 39 | } |
Martin Möhrmann | 8a6e51a | 2017-08-13 20:03:02 +0200 | [diff] [blame] | 40 | |
| 41 | n := -1 |
| 42 | shouldPanic("makechan: size out of range", func() { _ = make(T, n) }) |
| 43 | shouldPanic("makechan: size out of range", func() { _ = make(T, int64(n)) }) |
| 44 | if ptrSize == 8 { |
| 45 | n = 1 << 20 |
| 46 | n <<= 20 |
| 47 | shouldPanic("makechan: size out of range", func() { _ = make(T, n) }) |
| 48 | n <<= 20 |
| 49 | shouldPanic("makechan: size out of range", func() { _ = make(T, n) }) |
| 50 | } else { |
| 51 | n = 1<<31 - 1 |
| 52 | shouldPanic("makechan: size out of range", func() { _ = make(T, n) }) |
| 53 | shouldPanic("makechan: size out of range", func() { _ = make(T, int64(n)) }) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func shouldPanic(str string, f func()) { |
| 58 | defer func() { |
| 59 | err := recover() |
| 60 | if err == nil { |
| 61 | panic("did not panic") |
| 62 | } |
| 63 | s := err.(error).Error() |
| 64 | if !strings.Contains(s, str) { |
| 65 | panic("got panic " + s + ", want " + str) |
| 66 | } |
| 67 | }() |
| 68 | |
| 69 | f() |
Russ Cox | 8a45917 | 2009-08-20 11:12:04 -0700 | [diff] [blame] | 70 | } |