Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [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 | |
| 9 | // test range over channels |
| 10 | |
| 11 | func gen(c chan int, lo, hi int) { |
| 12 | for i := lo; i <= hi; i++ { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 13 | c <- i |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 14 | } |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 15 | close(c) |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | func seq(lo, hi int) chan int { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 19 | c := make(chan int) |
| 20 | go gen(c, lo, hi) |
| 21 | return c |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | func testchan() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 25 | s := "" |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 26 | for i := range seq('a', 'z') { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 27 | s += string(i) |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 28 | } |
| 29 | if s != "abcdefghijklmnopqrstuvwxyz" { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 30 | println("Wanted lowercase alphabet; got", s) |
| 31 | panic("fail") |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
Russ Cox | 81c3e8c | 2010-07-01 18:04:25 -0700 | [diff] [blame] | 35 | // test that range over slice only evaluates |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 36 | // the expression after "range" once. |
| 37 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 38 | var nmake = 0 |
| 39 | |
Russ Cox | 81c3e8c | 2010-07-01 18:04:25 -0700 | [diff] [blame] | 40 | func makeslice() []int { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 41 | nmake++ |
| 42 | return []int{1, 2, 3, 4, 5} |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Russ Cox | 81c3e8c | 2010-07-01 18:04:25 -0700 | [diff] [blame] | 45 | func testslice() { |
| 46 | s := 0 |
| 47 | nmake = 0 |
| 48 | for _, v := range makeslice() { |
| 49 | s += v |
| 50 | } |
| 51 | if nmake != 1 { |
| 52 | println("range called makeslice", nmake, "times") |
| 53 | panic("fail") |
| 54 | } |
| 55 | if s != 15 { |
| 56 | println("wrong sum ranging over makeslice") |
| 57 | panic("fail") |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func testslice1() { |
| 62 | s := 0 |
| 63 | nmake = 0 |
| 64 | for i := range makeslice() { |
| 65 | s += i |
| 66 | } |
| 67 | if nmake != 1 { |
| 68 | println("range called makeslice", nmake, "times") |
| 69 | panic("fail") |
| 70 | } |
| 71 | if s != 10 { |
| 72 | println("wrong sum ranging over makeslice") |
| 73 | panic("fail") |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // test that range over array only evaluates |
| 78 | // the expression after "range" once. |
| 79 | |
| 80 | func makearray() [5]int { |
| 81 | nmake++ |
| 82 | return [5]int{1, 2, 3, 4, 5} |
| 83 | } |
| 84 | |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 85 | func testarray() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 86 | s := 0 |
Russ Cox | 81c3e8c | 2010-07-01 18:04:25 -0700 | [diff] [blame] | 87 | nmake = 0 |
Russ Cox | ae54cf7 | 2009-09-15 12:42:24 -0700 | [diff] [blame] | 88 | for _, v := range makearray() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 89 | s += v |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 90 | } |
| 91 | if nmake != 1 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 92 | println("range called makearray", nmake, "times") |
| 93 | panic("fail") |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 94 | } |
| 95 | if s != 15 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 96 | println("wrong sum ranging over makearray") |
| 97 | panic("fail") |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Russ Cox | 81c3e8c | 2010-07-01 18:04:25 -0700 | [diff] [blame] | 101 | func testarray1() { |
| 102 | s := 0 |
| 103 | nmake = 0 |
| 104 | for i := range makearray() { |
| 105 | s += i |
| 106 | } |
| 107 | if nmake != 1 { |
| 108 | println("range called makearray", nmake, "times") |
| 109 | panic("fail") |
| 110 | } |
| 111 | if s != 10 { |
| 112 | println("wrong sum ranging over makearray") |
| 113 | panic("fail") |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func makearrayptr() *[5]int { |
| 118 | nmake++ |
| 119 | return &[5]int{1, 2, 3, 4, 5} |
| 120 | } |
| 121 | |
| 122 | func testarrayptr() { |
| 123 | nmake = 0 |
| 124 | x := len(makearrayptr()) |
| 125 | if x != 5 || nmake != 1 { |
| 126 | println("len called makearrayptr", nmake, "times and got len", x) |
| 127 | panic("fail") |
| 128 | } |
| 129 | nmake = 0 |
| 130 | x = cap(makearrayptr()) |
| 131 | if x != 5 || nmake != 1 { |
| 132 | println("cap called makearrayptr", nmake, "times and got len", x) |
| 133 | panic("fail") |
| 134 | } |
| 135 | s := 0 |
| 136 | nmake = 0 |
| 137 | for _, v := range makearrayptr() { |
| 138 | s += v |
| 139 | } |
| 140 | if nmake != 1 { |
| 141 | println("range called makearrayptr", nmake, "times") |
| 142 | panic("fail") |
| 143 | } |
| 144 | if s != 15 { |
| 145 | println("wrong sum ranging over makearrayptr") |
| 146 | panic("fail") |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func testarrayptr1() { |
| 151 | s := 0 |
| 152 | nmake = 0 |
| 153 | for i := range makearrayptr() { |
| 154 | s += i |
| 155 | } |
| 156 | if nmake != 1 { |
| 157 | println("range called makearrayptr", nmake, "times") |
| 158 | panic("fail") |
| 159 | } |
| 160 | if s != 10 { |
| 161 | println("wrong sum ranging over makearrayptr") |
| 162 | panic("fail") |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // test that range over string only evaluates |
| 167 | // the expression after "range" once. |
| 168 | |
| 169 | func makestring() string { |
| 170 | nmake++ |
| 171 | return "abcd☺" |
| 172 | } |
| 173 | |
| 174 | func teststring() { |
| 175 | s := 0 |
| 176 | nmake = 0 |
| 177 | for _, v := range makestring() { |
| 178 | s += v |
| 179 | } |
| 180 | if nmake != 1 { |
| 181 | println("range called makestring", nmake, "times") |
| 182 | panic("fail") |
| 183 | } |
| 184 | if s != 'a'+'b'+'c'+'d'+'☺' { |
| 185 | println("wrong sum ranging over makestring") |
| 186 | panic("fail") |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func teststring1() { |
| 191 | s := 0 |
| 192 | nmake = 0 |
| 193 | for i := range makestring() { |
| 194 | s += i |
| 195 | } |
| 196 | if nmake != 1 { |
| 197 | println("range called makestring", nmake, "times") |
| 198 | panic("fail") |
| 199 | } |
| 200 | if s != 10 { |
| 201 | println("wrong sum ranging over makestring") |
| 202 | panic("fail") |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // test that range over map only evaluates |
| 207 | // the expression after "range" once. |
| 208 | |
| 209 | func makemap() map[int]int { |
| 210 | nmake++ |
| 211 | return map[int]int{0:'a', 1:'b', 2:'c', 3:'d', 4:'☺'} |
| 212 | } |
| 213 | |
| 214 | func testmap() { |
| 215 | s := 0 |
| 216 | nmake = 0 |
| 217 | for _, v := range makemap() { |
| 218 | s += v |
| 219 | } |
| 220 | if nmake != 1 { |
| 221 | println("range called makemap", nmake, "times") |
| 222 | panic("fail") |
| 223 | } |
| 224 | if s != 'a'+'b'+'c'+'d'+'☺' { |
| 225 | println("wrong sum ranging over makemap") |
| 226 | panic("fail") |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | func testmap1() { |
| 231 | s := 0 |
| 232 | nmake = 0 |
| 233 | for i := range makemap() { |
| 234 | s += i |
| 235 | } |
| 236 | if nmake != 1 { |
| 237 | println("range called makemap", nmake, "times") |
| 238 | panic("fail") |
| 239 | } |
| 240 | if s != 10 { |
| 241 | println("wrong sum ranging over makemap") |
| 242 | panic("fail") |
| 243 | } |
| 244 | } |
| 245 | |
Ian Lance Taylor | ff68f96 | 2010-01-15 10:40:30 -0800 | [diff] [blame] | 246 | // test that range evaluates the index and value expressions |
| 247 | // exactly once per iteration. |
| 248 | |
| 249 | var ncalls = 0 |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 250 | |
Ian Lance Taylor | ff68f96 | 2010-01-15 10:40:30 -0800 | [diff] [blame] | 251 | func getvar(p *int) *int { |
| 252 | ncalls++ |
| 253 | return p |
| 254 | } |
| 255 | |
| 256 | func testcalls() { |
| 257 | var i, v int |
| 258 | si := 0 |
| 259 | sv := 0 |
| 260 | for *getvar(&i), *getvar(&v) = range [2]int{1, 2} { |
| 261 | si += i |
| 262 | sv += v |
| 263 | } |
| 264 | if ncalls != 4 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 265 | println("wrong number of calls:", ncalls, "!= 4") |
| 266 | panic("fail") |
Ian Lance Taylor | ff68f96 | 2010-01-15 10:40:30 -0800 | [diff] [blame] | 267 | } |
| 268 | if si != 1 || sv != 3 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 269 | println("wrong sum in testcalls", si, sv) |
| 270 | panic("fail") |
Ian Lance Taylor | ff68f96 | 2010-01-15 10:40:30 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | ncalls = 0 |
| 274 | for *getvar(&i), *getvar(&v) = range [0]int{} { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 275 | println("loop ran on empty array") |
| 276 | panic("fail") |
Ian Lance Taylor | ff68f96 | 2010-01-15 10:40:30 -0800 | [diff] [blame] | 277 | } |
| 278 | if ncalls != 0 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 279 | println("wrong number of calls:", ncalls, "!= 0") |
| 280 | panic("fail") |
Ian Lance Taylor | ff68f96 | 2010-01-15 10:40:30 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 284 | func main() { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 285 | testchan() |
| 286 | testarray() |
Russ Cox | 81c3e8c | 2010-07-01 18:04:25 -0700 | [diff] [blame] | 287 | testarray1() |
| 288 | testarrayptr() |
| 289 | testarrayptr1() |
| 290 | testslice() |
| 291 | testslice1() |
| 292 | teststring() |
| 293 | teststring1() |
| 294 | testmap() |
| 295 | testmap1() |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 296 | testcalls() |
Russ Cox | 54aa835 | 2009-03-20 11:32:58 -0700 | [diff] [blame] | 297 | } |