Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 1 | // run |
| 2 | |
| 3 | // Copyright 2011 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 | |
Brad Fitzpatrick | 2ae7737 | 2015-07-10 17:17:11 -0600 | [diff] [blame] | 7 | // Test len constants and non-constants, https://golang.org/issue/3244. |
Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 8 | |
| 9 | package main |
| 10 | |
| 11 | var b struct { |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 12 | a [10]int |
Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | var m map[string][20]int |
| 16 | |
| 17 | var s [][30]int |
| 18 | |
| 19 | const ( |
| 20 | n1 = len(b.a) |
| 21 | n2 = len(m[""]) |
| 22 | n3 = len(s[10]) |
| 23 | ) |
| 24 | |
| 25 | // Non-constants (see also const5.go). |
| 26 | var ( |
| 27 | n4 = len(f()) |
| 28 | n5 = len(<-c) |
| 29 | n6 = cap(g()) |
| 30 | n7 = cap(<-c1) |
| 31 | ) |
| 32 | |
| 33 | var calledF = false |
| 34 | |
| 35 | func f() *[40]int { |
| 36 | calledF = true |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | var c = func() chan *[50]int { |
| 41 | c := make(chan *[50]int, 2) |
| 42 | c <- nil |
| 43 | c <- new([50]int) |
| 44 | return c |
| 45 | }() |
| 46 | |
| 47 | var calledG = false |
| 48 | |
| 49 | func g() *[60]int { |
| 50 | calledG = true |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | var c1 = func() chan *[70]int { |
| 55 | c := make(chan *[70]int, 2) |
| 56 | c <- nil |
| 57 | c <- new([70]int) |
| 58 | return c |
| 59 | }() |
| 60 | |
| 61 | func main() { |
| 62 | if n1 != 10 || n2 != 20 || n3 != 30 || n4 != 40 || n5 != 50 || n6 != 60 || n7 != 70 { |
| 63 | println("BUG:", n1, n2, n3, n4, n5, n6, n7) |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 64 | panic("fail") |
Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 65 | } |
| 66 | if !calledF { |
| 67 | println("BUG: did not call f") |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 68 | panic("fail") |
Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 69 | } |
| 70 | if <-c == nil { |
| 71 | println("BUG: did not receive from c") |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 72 | panic("fail") |
Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 73 | } |
| 74 | if !calledG { |
| 75 | println("BUG: did not call g") |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 76 | panic("fail") |
Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 77 | } |
| 78 | if <-c1 == nil { |
| 79 | println("BUG: did not receive from c1") |
Alan Donovan | 052c942 | 2013-02-12 13:17:49 -0500 | [diff] [blame] | 80 | panic("fail") |
Russ Cox | d4fb568 | 2012-03-07 22:43:28 -0500 | [diff] [blame] | 81 | } |
| 82 | } |