]> Cypherpunks.ru repositories - gostls13.git/blob - test/reorder.go
test: add test for order of evaluation of map index on left of =
[gostls13.git] / test / reorder.go
1 // run
2
3 // Copyright 2011 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 // Test reordering of assignments.
8
9 package main
10
11 import "fmt"
12
13 func main() {
14         p1()
15         p2()
16         p3()
17         p4()
18         p5()
19         p6()
20         p7()
21         p8()
22         p9()
23 }
24
25 var gx []int
26
27 func f(i int) int {
28         return gx[i]
29 }
30
31 func check(x []int, x0, x1, x2 int) {
32         if x[0] != x0 || x[1] != x1 || x[2] != x2 {
33                 fmt.Printf("%v, want %d,%d,%d\n", x, x0, x1, x2)
34                 panic("failed")
35         }
36 }
37
38 func check3(x, y, z, xx, yy, zz int) {
39         if x != xx || y != yy || z != zz {
40                 fmt.Printf("%d,%d,%d, want %d,%d,%d\n", x, y, z, xx, yy, zz)
41                 panic("failed")
42         }
43 }
44
45 func p1() {
46         x := []int{1,2,3}
47         i := 0
48         i, x[i] = 1, 100
49         _ = i
50         check(x, 100, 2, 3)
51 }
52
53 func p2() {
54         x := []int{1,2,3}
55         i := 0
56         x[i], i = 100, 1
57         _ = i
58         check(x, 100, 2, 3)
59 }
60
61 func p3() {
62         x := []int{1,2,3}
63         y := x
64         gx = x
65         x[1], y[0] = f(0), f(1)
66         check(x, 2, 1, 3)
67 }
68
69 func p4() {
70         x := []int{1,2,3}
71         y := x
72         gx = x
73         x[1], y[0] = gx[0], gx[1]
74         check(x, 2, 1, 3)
75 }
76
77 func p5() {
78         x := []int{1,2,3}
79         y := x
80         p := &x[0]
81         q := &x[1]
82         *p, *q = x[1], y[0]
83         check(x, 2, 1, 3)
84 }
85
86 func p6() {
87         x := 1
88         y := 2
89         z := 3
90         px := &x
91         py := &y
92         *px, *py = y, x
93         check3(x, y, z, 2, 1, 3)        
94 }
95
96 func f1(x, y, z int) (xx, yy, zz int) {
97         return x, y, z
98 }
99
100 func f2() (x, y, z int) {
101         return f1(2, 1, 3)
102 }
103
104 func p7() {
105         x, y, z := f2()
106         check3(x, y, z, 2, 1, 3)
107 }
108
109 func p8() {
110         x := []int{1,2,3}
111
112         defer func() {
113                 err := recover()
114                 if err == nil {
115                         panic("not panicking")
116                 }
117                 check(x, 100, 2, 3)
118         }()
119
120         i := 0
121         i, x[i], x[5] = 1, 100, 500
122 }
123
124 func p9() {
125         m := make(map[int]int)
126         m[0] = len(m)
127         if m[0] != 0 {
128                 panic(m[0])
129         }
130 }