Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 1 | // $G $D/$F.go && $L $F.$A && ./$A.out |
| 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 | |
| 7 | package main |
| 8 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 9 | type T struct { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 10 | a float64 |
| 11 | b int64 |
| 12 | c string |
| 13 | d byte |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 14 | } |
| 15 | |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 16 | var a = []int{ 1, 2, 3 } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 17 | var NIL []int |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 18 | |
| 19 | func arraycmptest() { |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 20 | if NIL != nil { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 21 | println("fail1:", NIL, "!= nil") |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 22 | } |
| 23 | if nil != NIL { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 24 | println("fail2: nil !=", NIL) |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 25 | } |
| 26 | if a == nil || nil == a { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 27 | println("fail3:", a, "== nil") |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 28 | } |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 31 | func SameArray(a, b []int) bool { |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 32 | if len(a) != len(b) || cap(a) != cap(b) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 33 | return false |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 34 | } |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 35 | if len(a) > 0 && &a[0] != &b[0] { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 36 | return false |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 37 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 38 | return true |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 41 | var t = T{1.5, 123, "hello", 255} |
Russ Cox | 5564504 | 2009-01-06 15:19:02 -0800 | [diff] [blame] | 42 | var mt = make(map[int]T) |
| 43 | var ma = make(map[int][]int) |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 44 | |
| 45 | func maptest() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 46 | mt[0] = t |
| 47 | t1 := mt[0] |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 48 | if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 49 | println("fail: map val struct", t1.a, t1.b, t1.c, t1.d) |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 52 | ma[1] = a |
| 53 | a1 := ma[1] |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 54 | if !SameArray(a, a1) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 55 | println("fail: map val array", a, a1) |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Russ Cox | 5564504 | 2009-01-06 15:19:02 -0800 | [diff] [blame] | 59 | var ct = make(chan T) |
| 60 | var ca = make(chan []int) |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 61 | |
| 62 | func send() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 63 | ct <- t |
| 64 | ca <- a |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | func chantest() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 68 | go send() |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 69 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 70 | t1 := <-ct |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 71 | if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 72 | println("fail: map val struct", t1.a, t1.b, t1.c, t1.d) |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 75 | a1 := <-ca |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 76 | if !SameArray(a, a1) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 77 | println("fail: map val array", a, a1) |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 81 | type E struct { } |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 82 | var e E |
| 83 | |
| 84 | func interfacetest() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 85 | var i interface{} |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 86 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 87 | i = a |
| 88 | a1 := i.([]int) |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 89 | if !SameArray(a, a1) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 90 | println("interface <-> []int", a, a1) |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 91 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 92 | pa := new([]int) |
| 93 | *pa = a |
| 94 | i = pa |
| 95 | a1 = *i.(*[]int) |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 96 | if !SameArray(a, a1) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 97 | println("interface <-> *[]int", a, a1) |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 100 | i = t |
| 101 | t1 := i.(T) |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 102 | if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 103 | println("interface <-> struct", t1.a, t1.b, t1.c, t1.d) |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 106 | i = e |
| 107 | e1 := i.(E) |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 108 | // nothing to check; just verify it doesn't crash |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 109 | _ = e1 |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 112 | func main() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 113 | arraycmptest() |
| 114 | maptest() |
| 115 | chantest() |
| 116 | interfacetest() |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 117 | } |