Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // run |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -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 composite literals. |
| 8 | |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 9 | package main |
| 10 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 11 | type T struct { |
| 12 | i int |
| 13 | f float64 |
| 14 | s string |
| 15 | next *T |
| 16 | } |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 17 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 18 | type R struct { |
| 19 | num int |
| 20 | } |
Rob Pike | 696815c | 2008-09-04 13:35:19 -0700 | [diff] [blame] | 21 | |
| 22 | func itor(a int) *R { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 23 | r := new(R) |
| 24 | r.num = a |
| 25 | return r |
Rob Pike | 696815c | 2008-09-04 13:35:19 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Russ Cox | d47d888 | 2008-12-18 22:37:22 -0800 | [diff] [blame] | 28 | func eq(a []*R) { |
Rob Pike | 696815c | 2008-09-04 13:35:19 -0700 | [diff] [blame] | 29 | for i := 0; i < len(a); i++ { |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 30 | if a[i].num != i { |
| 31 | panic("bad") |
| 32 | } |
Rob Pike | 696815c | 2008-09-04 13:35:19 -0700 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 36 | func teq(t *T, n int) { |
| 37 | for i := 0; i < n; i++ { |
| 38 | if t == nil || t.i != i { |
| 39 | panic("bad") |
| 40 | } |
| 41 | t = t.next |
| 42 | } |
| 43 | if t != nil { |
| 44 | panic("bad") |
| 45 | } |
| 46 | } |
| 47 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 48 | type P struct { |
| 49 | a, b int |
| 50 | } |
| 51 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 52 | func NewP(a, b int) *P { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 53 | return &P{a, b} |
Russ Cox | beee691 | 2008-10-21 16:53:54 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 56 | func main() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 57 | var t T |
| 58 | t = T{0, 7.2, "hi", &t} |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 59 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 60 | var tp *T |
| 61 | tp = &T{0, 7.2, "hi", &t} |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 62 | |
Russ Cox | 5cb1c82 | 2011-12-05 14:22:41 -0500 | [diff] [blame] | 63 | tl := &T{i: 0, next: &T{i: 1, next: &T{i: 2, next: &T{i: 3, next: &T{i: 4}}}}} |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 64 | teq(tl, 5) |
| 65 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 66 | a1 := []int{1, 2, 3} |
| 67 | if len(a1) != 3 { |
| 68 | panic("a1") |
| 69 | } |
| 70 | a2 := [10]int{1, 2, 3} |
| 71 | if len(a2) != 10 || cap(a2) != 10 { |
| 72 | panic("a2") |
| 73 | } |
Rob Pike | a577ea3 | 2009-01-07 10:35:43 -0800 | [diff] [blame] | 74 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 75 | a3 := [10]int{1, 2, 3} |
| 76 | if len(a3) != 10 || a2[3] != 0 { |
| 77 | panic("a3") |
| 78 | } |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 79 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 80 | var oai []int |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 81 | oai = []int{1, 2, 3} |
| 82 | if len(oai) != 3 { |
| 83 | panic("oai") |
| 84 | } |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 85 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 86 | at := [...]*T{&t, tp, &t} |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 87 | if len(at) != 3 { |
| 88 | panic("at") |
| 89 | } |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 90 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 91 | c := make(chan int) |
| 92 | ac := []chan int{c, c, c} |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 93 | if len(ac) != 3 { |
| 94 | panic("ac") |
| 95 | } |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 96 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 97 | aat := [][len(at)]*T{at, at} |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 98 | if len(aat) != 2 || len(aat[1]) != 3 { |
| 99 | panic("aat") |
| 100 | } |
Russ Cox | 08ca30b | 2008-12-19 03:05:37 -0800 | [diff] [blame] | 101 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 102 | s := string([]byte{'h', 'e', 'l', 'l', 'o'}) |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 103 | if s != "hello" { |
| 104 | panic("s") |
| 105 | } |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 106 | |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 107 | m := map[string]float64{"one": 1.0, "two": 2.0, "pi": 22. / 7.} |
| 108 | if len(m) != 3 { |
| 109 | panic("m") |
| 110 | } |
Rob Pike | 696815c | 2008-09-04 13:35:19 -0700 | [diff] [blame] | 111 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 112 | eq([]*R{itor(0), itor(1), itor(2), itor(3), itor(4), itor(5)}) |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 113 | eq([]*R{{0}, {1}, {2}, {3}, {4}, {5}}) |
Russ Cox | 08ca30b | 2008-12-19 03:05:37 -0800 | [diff] [blame] | 114 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 115 | p1 := NewP(1, 2) |
| 116 | p2 := NewP(1, 2) |
Russ Cox | f2b5a07 | 2011-01-19 23:09:00 -0500 | [diff] [blame] | 117 | if p1 == p2 { |
| 118 | panic("NewP") |
| 119 | } |
Rob Pike | 5ee2b04 | 2008-09-03 15:54:33 -0700 | [diff] [blame] | 120 | } |