1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "fmt"
13 "os"
14 )
15
16 func check(n int) {
17
18
19
20 i := -1
21 s := make([]byte, n)
22 for i = range s {
23 s[i] = 0
24 }
25 if want := n - 1; i != want {
26 fmt.Printf("index after range with side-effect = %d want %d\n", i, want)
27 os.Exit(1)
28 }
29
30 i = n + 1
31
32 for i := range s {
33 s[i] = 0
34 }
35 if want := n + 1; i != want {
36 fmt.Printf("index after range without side-effect = %d want %d\n", i, want)
37 os.Exit(1)
38 }
39
40
41 var x int
42 f := func() int {
43 x++
44 return 0
45 }
46 var a [1]int
47 for a[f()] = range s {
48 s[a[f()]] = 0
49 }
50 if want := n * 2; x != want {
51 fmt.Printf("index function calls = %d want %d\n", x, want)
52 os.Exit(1)
53 }
54
55
56 x = 0
57 b := [1][]byte{s}
58 for i := range b[f()] {
59 b[f()][i] = 0
60 }
61 if want := n + 1; x != n+1 {
62 fmt.Printf("range expr function calls = %d want %d\n", x, want)
63 os.Exit(1)
64 }
65 }
66
67 func main() {
68 check(0)
69 check(1)
70 check(15)
71 }
72
View as plain text