From: Ian Lance Taylor Date: Fri, 15 Jan 2010 18:40:30 +0000 (-0800) Subject: Test evaluation of range variables. X-Git-Tag: weekly.2010-01-27~98 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ff68f96df08646b130a6153e600c2b89f5f31c34;p=gostls13.git Test evaluation of range variables. R=rsc CC=golang-dev https://golang.org/cl/189088 --- diff --git a/test/range.go b/test/range.go index 7abc80c66d..48237a715e 100644 --- a/test/range.go +++ b/test/range.go @@ -53,7 +53,41 @@ func testarray() { } } +// test that range evaluates the index and value expressions +// exactly once per iteration. + +var ncalls = 0 +func getvar(p *int) *int { + ncalls++ + return p +} + +func testcalls() { + var i, v int + si := 0 + sv := 0 + for *getvar(&i), *getvar(&v) = range [2]int{1, 2} { + si += i + sv += v + } + if ncalls != 4 { + panicln("wrong number of calls:", ncalls, "!= 4") + } + if si != 1 || sv != 3 { + panicln("wrong sum in testcalls", si, sv) + } + + ncalls = 0 + for *getvar(&i), *getvar(&v) = range [0]int{} { + panicln("loop ran on empty array") + } + if ncalls != 0 { + panicln("wrong number of calls:", ncalls, "!= 0") + } +} + func main() { testchan(); testarray(); + testcalls(); }