blob: f8300bf56afb4f9135ef490f4a36ce0e35fb186e [file] [log] [blame]
Russ Cox57eb06f2012-02-16 23:51:04 -05001// run
Rob Pikea432e092008-06-13 09:09:22 -07002
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 Pike19bab1d2012-02-24 10:30:39 +11007// Test nil.
8
Rob Pikea432e092008-06-13 09:09:22 -07009package main
10
Russ Cox75cb0842011-08-17 15:55:06 -040011import (
12 "fmt"
13 "time"
14)
15
Russ Cox839a6842009-01-20 14:40:40 -080016type T struct {
Rob Pikea432e092008-06-13 09:09:22 -070017 i int
18}
19
Russ Coxf2b5a072011-01-19 23:09:00 -050020type IN interface{}
Rob Pikea432e092008-06-13 09:09:22 -070021
22func main() {
Rob Pike4f61fc92010-09-04 10:36:13 +100023 var i *int
Russ Coxf2b5a072011-01-19 23:09:00 -050024 var f *float32
Rob Pike4f61fc92010-09-04 10:36:13 +100025 var s *string
Russ Coxf2b5a072011-01-19 23:09:00 -050026 var m map[float32]*int
Rob Pike4f61fc92010-09-04 10:36:13 +100027 var c chan int
28 var t *T
29 var in IN
30 var ta []IN
Rob Pikea432e092008-06-13 09:09:22 -070031
Rob Pike4f61fc92010-09-04 10:36:13 +100032 i = nil
33 f = nil
34 s = nil
35 m = nil
36 c = nil
37 t = nil
38 i = nil
39 ta = make([]IN, 1)
40 ta[0] = nil
Russ Cox1a319892009-09-14 21:03:53 -070041
Rob Pike4f61fc92010-09-04 10:36:13 +100042 _, _, _, _, _, _, _, _ = i, f, s, m, c, t, in, ta
Russ Cox75cb0842011-08-17 15:55:06 -040043
44 arraytest()
45 chantest()
46 maptest()
47 slicetest()
48}
49
50func shouldPanic(f func()) {
51 defer func() {
52 if recover() == nil {
53 panic("not panicking")
54 }
55 }()
56 f()
57}
58
59func shouldBlock(f func()) {
60 go func() {
61 f()
62 panic("did not block")
63 }()
64 time.Sleep(1e7)
65}
66
67// nil array pointer
68
69func arraytest() {
70 var p *[10]int
71
72 // Looping over indices is fine.
73 s := 0
74 for i := range p {
75 s += i
76 }
77 if s != 45 {
78 panic(s)
79 }
80
81 s = 0
82 for i := 0; i < len(p); i++ {
83 s += i
84 }
85 if s != 45 {
86 panic(s)
87 }
88
89 // Looping over values is not.
90 shouldPanic(func() {
91 for i, v := range p {
92 s += i + v
93 }
94 })
95
96 shouldPanic(func() {
97 for i := 0; i < len(p); i++ {
98 s += p[i]
99 }
100 })
101}
102
103// nil channel
104// select tests already handle select on nil channel
105
106func chantest() {
107 var ch chan int
108
109 // nil channel is never ready
110 shouldBlock(func() {
111 ch <- 1
112 })
113 shouldBlock(func() {
114 <-ch
115 })
116 shouldBlock(func() {
117 x, ok := <-ch
Alan Donovan052c9422013-02-12 13:17:49 -0500118 println(x, ok) // unreachable
Russ Cox75cb0842011-08-17 15:55:06 -0400119 })
120
121 if len(ch) != 0 {
122 panic(len(ch))
123 }
124 if cap(ch) != 0 {
125 panic(cap(ch))
126 }
127}
128
129// nil map
130
131func maptest() {
132 var m map[int]int
133
134 // nil map appears empty
135 if len(m) != 0 {
136 panic(len(m))
137 }
138 if m[1] != 0 {
139 panic(m[1])
140 }
141 if x, ok := m[1]; x != 0 || ok {
142 panic(fmt.Sprint(x, ok))
143 }
144
145 for k, v := range m {
146 panic(k)
147 panic(v)
148 }
149
Ian Lance Taylor9509cbf2012-12-13 12:11:24 -0800150 // can delete (non-existent) entries
151 delete(m, 2)
152
Russ Cox75cb0842011-08-17 15:55:06 -0400153 // but cannot be written to
154 shouldPanic(func() {
155 m[2] = 3
156 })
Russ Cox75cb0842011-08-17 15:55:06 -0400157}
158
159// nil slice
160
161func slicetest() {
162 var x []int
163
164 // nil slice is just a 0-element slice.
165 if len(x) != 0 {
166 panic(len(x))
167 }
168 if cap(x) != 0 {
169 panic(cap(x))
170 }
171
172 // no 0-element slices can be read from or written to
173 var s int
174 shouldPanic(func() {
175 s += x[1]
176 })
177 shouldPanic(func() {
178 x[2] = s
179 })
Rob Pikea432e092008-06-13 09:09:22 -0700180}