]> Cypherpunks.ru repositories - gostls13.git/blob - test/fixedbugs/issue5373.go
17ce189f95997e516f4fb86413744d8ca4e76c17
[gostls13.git] / test / fixedbugs / issue5373.go
1 // run
2
3 // Copyright 2015 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 // Ensure that zeroing range loops have the requisite side-effects.
8
9 package main
10
11 import (
12         "fmt"
13         "os"
14 )
15
16 func check(n int) {
17         // When n == 0, i is untouched by the range loop.
18         // Picking an initial value of -1 for i makes the
19         // "want" calculation below correct in all cases.
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         // i is shadowed here, so its value should be unchanged.
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         // Index variable whose evaluation has side-effects
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         // Range expression whose evaluation has side-effects
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 }