2012-02-17 04:51:04 +00:00
|
|
|
// run
|
2009-03-20 18:32:58 +00:00
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-23 23:30:39 +00:00
|
|
|
// Test the 'for range' construct.
|
|
|
|
|
2009-03-20 18:32:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
// test range over channels
|
|
|
|
|
|
|
|
func gen(c chan int, lo, hi int) {
|
|
|
|
for i := lo; i <= hi; i++ {
|
2010-03-24 23:46:53 +00:00
|
|
|
c <- i
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|
2010-03-24 23:46:53 +00:00
|
|
|
close(c)
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func seq(lo, hi int) chan int {
|
2010-03-24 23:46:53 +00:00
|
|
|
c := make(chan int)
|
|
|
|
go gen(c, lo, hi)
|
|
|
|
return c
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 06:20:57 +00:00
|
|
|
const alphabet = "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
|
|
|
|
func testblankvars() {
|
|
|
|
n := 0
|
|
|
|
for range alphabet {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if n != 26 {
|
|
|
|
println("for range: wrong count", n, "want 26")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
n = 0
|
|
|
|
for _ = range alphabet {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if n != 26 {
|
|
|
|
println("for _ = range: wrong count", n, "want 26")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
n = 0
|
|
|
|
for _, _ = range alphabet {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if n != 26 {
|
|
|
|
println("for _, _ = range: wrong count", n, "want 26")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
s := 0
|
|
|
|
for i, _ := range alphabet {
|
|
|
|
s += i
|
|
|
|
}
|
|
|
|
if s != 325 {
|
|
|
|
println("for i, _ := range: wrong sum", s, "want 325")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
r := rune(0)
|
|
|
|
for _, v := range alphabet {
|
|
|
|
r += v
|
|
|
|
}
|
|
|
|
if r != 2847 {
|
|
|
|
println("for _, v := range: wrong sum", r, "want 2847")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-20 18:32:58 +00:00
|
|
|
func testchan() {
|
2010-03-24 23:46:53 +00:00
|
|
|
s := ""
|
2009-03-20 18:32:58 +00:00
|
|
|
for i := range seq('a', 'z') {
|
2010-03-24 23:46:53 +00:00
|
|
|
s += string(i)
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|
2017-09-03 06:20:57 +00:00
|
|
|
if s != alphabet {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("Wanted lowercase alphabet; got", s)
|
|
|
|
panic("fail")
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|
2014-07-16 23:27:10 +00:00
|
|
|
n := 0
|
|
|
|
for range seq('a', 'z') {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if n != 26 {
|
|
|
|
println("testchan wrong count", n, "want 26")
|
2017-09-03 06:20:57 +00:00
|
|
|
panic("fail")
|
2014-07-16 23:27:10 +00:00
|
|
|
}
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|
|
|
|
|
2010-07-02 01:04:25 +00:00
|
|
|
// test that range over slice only evaluates
|
2009-03-20 18:32:58 +00:00
|
|
|
// the expression after "range" once.
|
|
|
|
|
2010-03-24 23:46:53 +00:00
|
|
|
var nmake = 0
|
|
|
|
|
2010-07-02 01:04:25 +00:00
|
|
|
func makeslice() []int {
|
2010-03-24 23:46:53 +00:00
|
|
|
nmake++
|
|
|
|
return []int{1, 2, 3, 4, 5}
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|
|
|
|
|
2010-07-02 01:04:25 +00:00
|
|
|
func testslice() {
|
|
|
|
s := 0
|
|
|
|
nmake = 0
|
|
|
|
for _, v := range makeslice() {
|
|
|
|
s += v
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makeslice", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if s != 15 {
|
2013-02-11 23:20:52 +00:00
|
|
|
println("wrong sum ranging over makeslice", s)
|
2010-07-02 01:04:25 +00:00
|
|
|
panic("fail")
|
|
|
|
}
|
2013-02-11 23:20:52 +00:00
|
|
|
|
2012-05-25 03:05:36 +00:00
|
|
|
x := []int{10, 20}
|
|
|
|
y := []int{99}
|
|
|
|
i := 1
|
|
|
|
for i, x[i] = range y {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i != 0 || x[0] != 10 || x[1] != 99 {
|
|
|
|
println("wrong parallel assignment", i, x[0], x[1])
|
|
|
|
panic("fail")
|
|
|
|
}
|
2010-07-02 01:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testslice1() {
|
|
|
|
s := 0
|
|
|
|
nmake = 0
|
|
|
|
for i := range makeslice() {
|
|
|
|
s += i
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makeslice", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if s != 10 {
|
2013-02-11 23:20:52 +00:00
|
|
|
println("wrong sum ranging over makeslice", s)
|
2010-07-02 01:04:25 +00:00
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 23:27:10 +00:00
|
|
|
func testslice2() {
|
|
|
|
n := 0
|
|
|
|
nmake = 0
|
|
|
|
for range makeslice() {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makeslice", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if n != 5 {
|
|
|
|
println("wrong count ranging over makeslice", n)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 21:31:38 +00:00
|
|
|
// test that range over []byte(string) only evaluates
|
|
|
|
// the expression after "range" once.
|
|
|
|
|
|
|
|
func makenumstring() string {
|
|
|
|
nmake++
|
|
|
|
return "\x01\x02\x03\x04\x05"
|
|
|
|
}
|
|
|
|
|
|
|
|
func testslice3() {
|
|
|
|
s := byte(0)
|
|
|
|
nmake = 0
|
|
|
|
for _, v := range []byte(makenumstring()) {
|
|
|
|
s += v
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makenumstring", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if s != 15 {
|
|
|
|
println("wrong sum ranging over []byte(makenumstring)", s)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-02 01:04:25 +00:00
|
|
|
// test that range over array only evaluates
|
|
|
|
// the expression after "range" once.
|
|
|
|
|
|
|
|
func makearray() [5]int {
|
|
|
|
nmake++
|
|
|
|
return [5]int{1, 2, 3, 4, 5}
|
|
|
|
}
|
|
|
|
|
2009-03-20 18:32:58 +00:00
|
|
|
func testarray() {
|
2010-03-24 23:46:53 +00:00
|
|
|
s := 0
|
2010-07-02 01:04:25 +00:00
|
|
|
nmake = 0
|
2009-09-15 19:42:24 +00:00
|
|
|
for _, v := range makearray() {
|
2010-03-24 23:46:53 +00:00
|
|
|
s += v
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|
|
|
|
if nmake != 1 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("range called makearray", nmake, "times")
|
|
|
|
panic("fail")
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|
|
|
|
if s != 15 {
|
2013-02-11 23:20:52 +00:00
|
|
|
println("wrong sum ranging over makearray", s)
|
2010-03-24 23:46:53 +00:00
|
|
|
panic("fail")
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-02 01:04:25 +00:00
|
|
|
func testarray1() {
|
|
|
|
s := 0
|
|
|
|
nmake = 0
|
|
|
|
for i := range makearray() {
|
|
|
|
s += i
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makearray", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if s != 10 {
|
2013-02-11 23:20:52 +00:00
|
|
|
println("wrong sum ranging over makearray", s)
|
2010-07-02 01:04:25 +00:00
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 23:27:10 +00:00
|
|
|
func testarray2() {
|
|
|
|
n := 0
|
|
|
|
nmake = 0
|
|
|
|
for range makearray() {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makearray", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if n != 5 {
|
|
|
|
println("wrong count ranging over makearray", n)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-02 01:04:25 +00:00
|
|
|
func makearrayptr() *[5]int {
|
|
|
|
nmake++
|
|
|
|
return &[5]int{1, 2, 3, 4, 5}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testarrayptr() {
|
|
|
|
nmake = 0
|
|
|
|
x := len(makearrayptr())
|
|
|
|
if x != 5 || nmake != 1 {
|
|
|
|
println("len called makearrayptr", nmake, "times and got len", x)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
nmake = 0
|
|
|
|
x = cap(makearrayptr())
|
|
|
|
if x != 5 || nmake != 1 {
|
|
|
|
println("cap called makearrayptr", nmake, "times and got len", x)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
s := 0
|
|
|
|
nmake = 0
|
|
|
|
for _, v := range makearrayptr() {
|
|
|
|
s += v
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makearrayptr", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if s != 15 {
|
2013-02-11 23:20:52 +00:00
|
|
|
println("wrong sum ranging over makearrayptr", s)
|
2010-07-02 01:04:25 +00:00
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testarrayptr1() {
|
|
|
|
s := 0
|
|
|
|
nmake = 0
|
|
|
|
for i := range makearrayptr() {
|
|
|
|
s += i
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makearrayptr", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if s != 10 {
|
2013-02-11 23:20:52 +00:00
|
|
|
println("wrong sum ranging over makearrayptr", s)
|
2010-07-02 01:04:25 +00:00
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 23:27:10 +00:00
|
|
|
func testarrayptr2() {
|
|
|
|
n := 0
|
|
|
|
nmake = 0
|
|
|
|
for range makearrayptr() {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makearrayptr", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if n != 5 {
|
|
|
|
println("wrong count ranging over makearrayptr", n)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-02 01:04:25 +00:00
|
|
|
// test that range over string only evaluates
|
|
|
|
// the expression after "range" once.
|
|
|
|
|
|
|
|
func makestring() string {
|
|
|
|
nmake++
|
|
|
|
return "abcd☺"
|
|
|
|
}
|
|
|
|
|
|
|
|
func teststring() {
|
cgo, goyacc, go/build, html, http, path, path/filepath, testing/quick, test: use rune
Nothing terribly interesting here.
R=golang-dev, bradfitz, gri, r
CC=golang-dev
https://golang.org/cl/5300043
2011-10-26 05:20:02 +00:00
|
|
|
var s rune
|
2010-07-02 01:04:25 +00:00
|
|
|
nmake = 0
|
|
|
|
for _, v := range makestring() {
|
|
|
|
s += v
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makestring", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if s != 'a'+'b'+'c'+'d'+'☺' {
|
2013-02-11 23:20:52 +00:00
|
|
|
println("wrong sum ranging over makestring", s)
|
2010-07-02 01:04:25 +00:00
|
|
|
panic("fail")
|
|
|
|
}
|
2016-12-18 23:06:46 +00:00
|
|
|
|
|
|
|
x := []rune{'a', 'b'}
|
|
|
|
i := 1
|
|
|
|
for i, x[i] = range "c" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i != 0 || x[0] != 'a' || x[1] != 'c' {
|
|
|
|
println("wrong parallel assignment", i, x[0], x[1])
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
y := []int{1, 2, 3}
|
|
|
|
r := rune(1)
|
|
|
|
for y[r], r = range "\x02" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if r != 2 || y[0] != 1 || y[1] != 0 || y[2] != 3 {
|
|
|
|
println("wrong parallel assignment", r, y[0], y[1], y[2])
|
|
|
|
panic("fail")
|
|
|
|
}
|
2010-07-02 01:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func teststring1() {
|
|
|
|
s := 0
|
|
|
|
nmake = 0
|
|
|
|
for i := range makestring() {
|
|
|
|
s += i
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makestring", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if s != 10 {
|
2013-02-11 23:20:52 +00:00
|
|
|
println("wrong sum ranging over makestring", s)
|
2010-07-02 01:04:25 +00:00
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 23:27:10 +00:00
|
|
|
func teststring2() {
|
|
|
|
n := 0
|
|
|
|
nmake = 0
|
|
|
|
for range makestring() {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makestring", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if n != 5 {
|
|
|
|
println("wrong count ranging over makestring", n)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-02 01:04:25 +00:00
|
|
|
// test that range over map only evaluates
|
|
|
|
// the expression after "range" once.
|
|
|
|
|
|
|
|
func makemap() map[int]int {
|
|
|
|
nmake++
|
cgo, goyacc, go/build, html, http, path, path/filepath, testing/quick, test: use rune
Nothing terribly interesting here.
R=golang-dev, bradfitz, gri, r
CC=golang-dev
https://golang.org/cl/5300043
2011-10-26 05:20:02 +00:00
|
|
|
return map[int]int{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: '☺'}
|
2010-07-02 01:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testmap() {
|
|
|
|
s := 0
|
|
|
|
nmake = 0
|
|
|
|
for _, v := range makemap() {
|
|
|
|
s += v
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makemap", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if s != 'a'+'b'+'c'+'d'+'☺' {
|
2013-02-11 23:20:52 +00:00
|
|
|
println("wrong sum ranging over makemap", s)
|
2010-07-02 01:04:25 +00:00
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testmap1() {
|
|
|
|
s := 0
|
|
|
|
nmake = 0
|
|
|
|
for i := range makemap() {
|
|
|
|
s += i
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makemap", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if s != 10 {
|
2013-02-11 23:20:52 +00:00
|
|
|
println("wrong sum ranging over makemap", s)
|
2010-07-02 01:04:25 +00:00
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 23:27:10 +00:00
|
|
|
func testmap2() {
|
|
|
|
n := 0
|
|
|
|
nmake = 0
|
|
|
|
for range makemap() {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
if nmake != 1 {
|
|
|
|
println("range called makemap", nmake, "times")
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
if n != 5 {
|
|
|
|
println("wrong count ranging over makemap", n)
|
|
|
|
panic("fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-15 18:40:30 +00:00
|
|
|
// test that range evaluates the index and value expressions
|
|
|
|
// exactly once per iteration.
|
|
|
|
|
|
|
|
var ncalls = 0
|
2010-03-24 23:46:53 +00:00
|
|
|
|
2010-01-15 18:40:30 +00:00
|
|
|
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 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("wrong number of calls:", ncalls, "!= 4")
|
|
|
|
panic("fail")
|
2010-01-15 18:40:30 +00:00
|
|
|
}
|
|
|
|
if si != 1 || sv != 3 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("wrong sum in testcalls", si, sv)
|
|
|
|
panic("fail")
|
2010-01-15 18:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ncalls = 0
|
|
|
|
for *getvar(&i), *getvar(&v) = range [0]int{} {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("loop ran on empty array")
|
|
|
|
panic("fail")
|
2010-01-15 18:40:30 +00:00
|
|
|
}
|
|
|
|
if ncalls != 0 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("wrong number of calls:", ncalls, "!= 0")
|
|
|
|
panic("fail")
|
2010-01-15 18:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-20 18:32:58 +00:00
|
|
|
func main() {
|
2017-09-03 06:20:57 +00:00
|
|
|
testblankvars()
|
2010-03-24 23:46:53 +00:00
|
|
|
testchan()
|
|
|
|
testarray()
|
2010-07-02 01:04:25 +00:00
|
|
|
testarray1()
|
2014-07-16 23:27:10 +00:00
|
|
|
testarray2()
|
2010-07-02 01:04:25 +00:00
|
|
|
testarrayptr()
|
|
|
|
testarrayptr1()
|
2014-07-16 23:27:10 +00:00
|
|
|
testarrayptr2()
|
2010-07-02 01:04:25 +00:00
|
|
|
testslice()
|
|
|
|
testslice1()
|
2014-07-16 23:27:10 +00:00
|
|
|
testslice2()
|
2016-10-27 21:31:38 +00:00
|
|
|
testslice3()
|
2010-07-02 01:04:25 +00:00
|
|
|
teststring()
|
|
|
|
teststring1()
|
2014-07-16 23:27:10 +00:00
|
|
|
teststring2()
|
2010-07-02 01:04:25 +00:00
|
|
|
testmap()
|
|
|
|
testmap1()
|
2014-07-16 23:27:10 +00:00
|
|
|
testmap2()
|
2010-03-24 23:46:53 +00:00
|
|
|
testcalls()
|
2009-03-20 18:32:58 +00:00
|
|
|
}
|