Home | History | Annotate | Download | only in test
      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 
      7 // Test len constants and non-constants, https://golang.org/issue/3244.
      8 
      9 package main
     10 
     11 var b struct {
     12 	a [10]int
     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)
     64 		panic("fail")
     65 	}
     66 	if !calledF {
     67 		println("BUG: did not call f")
     68 		panic("fail")
     69 	}
     70 	if <-c == nil {
     71 		println("BUG: did not receive from c")
     72 		panic("fail")
     73 	}
     74 	if !calledG {
     75 		println("BUG: did not call g")
     76 		panic("fail")
     77 	}
     78 	if <-c1 == nil {
     79 		println("BUG: did not receive from c1")
     80 		panic("fail")
     81 	}
     82 }
     83