Left: | ||
Right: |
OLD | NEW |
---|---|
1 // run | 1 // run |
2 | 2 |
3 // Copyright 2009 The Go Authors. All rights reserved. | 3 // Copyright 2009 The Go Authors. All rights reserved. |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file. | 5 // license that can be found in the LICENSE file. |
6 | 6 |
7 // Test the 'for range' construct. | 7 // Test the 'for range' construct. |
8 | 8 |
9 package main | 9 package main |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
48 s := 0 | 48 s := 0 |
49 nmake = 0 | 49 nmake = 0 |
50 for _, v := range makeslice() { | 50 for _, v := range makeslice() { |
51 s += v | 51 s += v |
52 } | 52 } |
53 if nmake != 1 { | 53 if nmake != 1 { |
54 println("range called makeslice", nmake, "times") | 54 println("range called makeslice", nmake, "times") |
55 panic("fail") | 55 panic("fail") |
56 } | 56 } |
57 if s != 15 { | 57 if s != 15 { |
58 » » println("wrong sum ranging over makeslice") | 58 » » println("wrong sum ranging over makeslice", s) |
59 panic("fail") | 59 panic("fail") |
60 } | 60 } |
61 » | 61 |
62 x := []int{10, 20} | 62 x := []int{10, 20} |
63 y := []int{99} | 63 y := []int{99} |
64 i := 1 | 64 i := 1 |
65 for i, x[i] = range y { | 65 for i, x[i] = range y { |
66 break | 66 break |
67 } | 67 } |
68 if i != 0 || x[0] != 10 || x[1] != 99 { | 68 if i != 0 || x[0] != 10 || x[1] != 99 { |
69 println("wrong parallel assignment", i, x[0], x[1]) | 69 println("wrong parallel assignment", i, x[0], x[1]) |
70 panic("fail") | 70 panic("fail") |
71 } | 71 } |
72 } | 72 } |
73 | 73 |
74 func testslice1() { | 74 func testslice1() { |
75 s := 0 | 75 s := 0 |
76 nmake = 0 | 76 nmake = 0 |
77 for i := range makeslice() { | 77 for i := range makeslice() { |
78 s += i | 78 s += i |
79 } | 79 } |
80 if nmake != 1 { | 80 if nmake != 1 { |
81 println("range called makeslice", nmake, "times") | 81 println("range called makeslice", nmake, "times") |
82 panic("fail") | 82 panic("fail") |
83 } | 83 } |
84 if s != 10 { | 84 if s != 10 { |
85 » » println("wrong sum ranging over makeslice") | 85 » » println("wrong sum ranging over makeslice", s) |
86 panic("fail") | 86 panic("fail") |
87 } | 87 } |
88 } | 88 } |
89 | 89 |
90 // test that range over array only evaluates | 90 // test that range over array only evaluates |
91 // the expression after "range" once. | 91 // the expression after "range" once. |
92 | 92 |
93 func makearray() [5]int { | 93 func makearray() [5]int { |
94 nmake++ | 94 nmake++ |
95 return [5]int{1, 2, 3, 4, 5} | 95 return [5]int{1, 2, 3, 4, 5} |
96 } | 96 } |
97 | 97 |
98 func testarray() { | 98 func testarray() { |
99 s := 0 | 99 s := 0 |
100 nmake = 0 | 100 nmake = 0 |
101 for _, v := range makearray() { | 101 for _, v := range makearray() { |
102 s += v | 102 s += v |
103 } | 103 } |
104 if nmake != 1 { | 104 if nmake != 1 { |
105 println("range called makearray", nmake, "times") | 105 println("range called makearray", nmake, "times") |
106 panic("fail") | 106 panic("fail") |
107 } | 107 } |
108 if s != 15 { | 108 if s != 15 { |
109 » » println("wrong sum ranging over makearray") | 109 » » println("wrong sum ranging over makearray", s) |
110 panic("fail") | 110 panic("fail") |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 func testarray1() { | 114 func testarray1() { |
115 s := 0 | 115 s := 0 |
116 nmake = 0 | 116 nmake = 0 |
117 for i := range makearray() { | 117 for i := range makearray() { |
118 s += i | 118 s += i |
119 } | 119 } |
120 if nmake != 1 { | 120 if nmake != 1 { |
121 println("range called makearray", nmake, "times") | 121 println("range called makearray", nmake, "times") |
122 panic("fail") | 122 panic("fail") |
123 } | 123 } |
124 if s != 10 { | 124 if s != 10 { |
125 » » println("wrong sum ranging over makearray") | 125 » » println("wrong sum ranging over makearray", s) |
126 panic("fail") | 126 panic("fail") |
127 } | 127 } |
128 } | 128 } |
129 | 129 |
130 func makearrayptr() *[5]int { | 130 func makearrayptr() *[5]int { |
131 nmake++ | 131 nmake++ |
132 return &[5]int{1, 2, 3, 4, 5} | 132 return &[5]int{1, 2, 3, 4, 5} |
133 } | 133 } |
134 | 134 |
135 func testarrayptr() { | 135 func testarrayptr() { |
(...skipping 12 matching lines...) Expand all Loading... | |
148 s := 0 | 148 s := 0 |
149 nmake = 0 | 149 nmake = 0 |
150 for _, v := range makearrayptr() { | 150 for _, v := range makearrayptr() { |
151 s += v | 151 s += v |
152 } | 152 } |
153 if nmake != 1 { | 153 if nmake != 1 { |
154 println("range called makearrayptr", nmake, "times") | 154 println("range called makearrayptr", nmake, "times") |
155 panic("fail") | 155 panic("fail") |
156 } | 156 } |
157 if s != 15 { | 157 if s != 15 { |
158 » » println("wrong sum ranging over makearrayptr") | 158 » » println("wrong sum ranging over makearrayptr", s) |
159 panic("fail") | 159 panic("fail") |
160 } | 160 } |
161 } | 161 } |
162 | 162 |
163 func testarrayptr1() { | 163 func testarrayptr1() { |
164 s := 0 | 164 s := 0 |
165 nmake = 0 | 165 nmake = 0 |
166 for i := range makearrayptr() { | 166 for i := range makearrayptr() { |
167 s += i | 167 s += i |
168 } | 168 } |
169 » if nmake != 1 { | 169 » // Non-evaluation of the range expression is permitted for arrays. |
iant
2013/02/11 16:56:55
We have to be clear about whether the range expres
adonovan
2013/02/11 20:59:20
Changes to the spec wording are being discussed in
| |
170 » if nmake != 1 && nmake != 0 { | |
170 println("range called makearrayptr", nmake, "times") | 171 println("range called makearrayptr", nmake, "times") |
171 panic("fail") | 172 panic("fail") |
172 } | 173 } |
173 if s != 10 { | 174 if s != 10 { |
174 » » println("wrong sum ranging over makearrayptr") | 175 » » println("wrong sum ranging over makearrayptr", s) |
175 panic("fail") | 176 panic("fail") |
176 } | 177 } |
177 } | 178 } |
178 | 179 |
179 // test that range over string only evaluates | 180 // test that range over string only evaluates |
180 // the expression after "range" once. | 181 // the expression after "range" once. |
181 | 182 |
182 func makestring() string { | 183 func makestring() string { |
183 nmake++ | 184 nmake++ |
184 return "abcd☺" | 185 return "abcd☺" |
185 } | 186 } |
186 | 187 |
187 func teststring() { | 188 func teststring() { |
188 var s rune | 189 var s rune |
189 nmake = 0 | 190 nmake = 0 |
190 for _, v := range makestring() { | 191 for _, v := range makestring() { |
191 s += v | 192 s += v |
192 } | 193 } |
193 if nmake != 1 { | 194 if nmake != 1 { |
194 println("range called makestring", nmake, "times") | 195 println("range called makestring", nmake, "times") |
195 panic("fail") | 196 panic("fail") |
196 } | 197 } |
197 if s != 'a'+'b'+'c'+'d'+'☺' { | 198 if s != 'a'+'b'+'c'+'d'+'☺' { |
198 » » println("wrong sum ranging over makestring") | 199 » » println("wrong sum ranging over makestring", s) |
199 panic("fail") | 200 panic("fail") |
200 } | 201 } |
201 } | 202 } |
202 | 203 |
203 func teststring1() { | 204 func teststring1() { |
204 s := 0 | 205 s := 0 |
205 nmake = 0 | 206 nmake = 0 |
206 for i := range makestring() { | 207 for i := range makestring() { |
207 s += i | 208 s += i |
208 } | 209 } |
209 if nmake != 1 { | 210 if nmake != 1 { |
210 println("range called makestring", nmake, "times") | 211 println("range called makestring", nmake, "times") |
211 panic("fail") | 212 panic("fail") |
212 } | 213 } |
213 if s != 10 { | 214 if s != 10 { |
214 » » println("wrong sum ranging over makestring") | 215 » » println("wrong sum ranging over makestring", s) |
215 panic("fail") | 216 panic("fail") |
216 } | 217 } |
217 } | 218 } |
218 | 219 |
219 // test that range over map only evaluates | 220 // test that range over map only evaluates |
220 // the expression after "range" once. | 221 // the expression after "range" once. |
221 | 222 |
222 func makemap() map[int]int { | 223 func makemap() map[int]int { |
223 nmake++ | 224 nmake++ |
224 return map[int]int{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: '☺'} | 225 return map[int]int{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: '☺'} |
225 } | 226 } |
226 | 227 |
227 func testmap() { | 228 func testmap() { |
228 s := 0 | 229 s := 0 |
229 nmake = 0 | 230 nmake = 0 |
230 for _, v := range makemap() { | 231 for _, v := range makemap() { |
231 s += v | 232 s += v |
232 } | 233 } |
233 if nmake != 1 { | 234 if nmake != 1 { |
234 println("range called makemap", nmake, "times") | 235 println("range called makemap", nmake, "times") |
235 panic("fail") | 236 panic("fail") |
236 } | 237 } |
237 if s != 'a'+'b'+'c'+'d'+'☺' { | 238 if s != 'a'+'b'+'c'+'d'+'☺' { |
238 » » println("wrong sum ranging over makemap") | 239 » » println("wrong sum ranging over makemap", s) |
239 panic("fail") | 240 panic("fail") |
240 } | 241 } |
241 } | 242 } |
242 | 243 |
243 func testmap1() { | 244 func testmap1() { |
244 s := 0 | 245 s := 0 |
245 nmake = 0 | 246 nmake = 0 |
246 for i := range makemap() { | 247 for i := range makemap() { |
247 s += i | 248 s += i |
248 } | 249 } |
249 if nmake != 1 { | 250 if nmake != 1 { |
250 println("range called makemap", nmake, "times") | 251 println("range called makemap", nmake, "times") |
251 panic("fail") | 252 panic("fail") |
252 } | 253 } |
253 if s != 10 { | 254 if s != 10 { |
254 » » println("wrong sum ranging over makemap") | 255 » » println("wrong sum ranging over makemap", s) |
255 panic("fail") | 256 panic("fail") |
256 } | 257 } |
257 } | 258 } |
258 | 259 |
259 // test that range evaluates the index and value expressions | 260 // test that range evaluates the index and value expressions |
260 // exactly once per iteration. | 261 // exactly once per iteration. |
261 | 262 |
262 var ncalls = 0 | 263 var ncalls = 0 |
263 | 264 |
264 func getvar(p *int) *int { | 265 func getvar(p *int) *int { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 testarrayptr() | 302 testarrayptr() |
302 testarrayptr1() | 303 testarrayptr1() |
303 testslice() | 304 testslice() |
304 testslice1() | 305 testslice1() |
305 teststring() | 306 teststring() |
306 teststring1() | 307 teststring1() |
307 testmap() | 308 testmap() |
308 testmap1() | 309 testmap1() |
309 testcalls() | 310 testcalls() |
310 } | 311 } |
OLD | NEW |