Russ Cox | 57eb06f | 2012-02-16 23:51:04 -0500 | [diff] [blame] | 1 | // run |
Russ Cox | eb3aba24 | 2011-10-13 15:46:39 -0400 | [diff] [blame] | 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 | |
Rob Pike | 19bab1d | 2012-02-24 10:30:39 +1100 | [diff] [blame] | 7 | // Test reordering of assignments. |
Russ Cox | eb3aba24 | 2011-10-13 15:46:39 -0400 | [diff] [blame] | 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 | } |
| 23 | |
| 24 | var gx []int |
| 25 | |
| 26 | func f(i int) int { |
| 27 | return gx[i] |
| 28 | } |
| 29 | |
| 30 | func check(x []int, x0, x1, x2 int) { |
| 31 | if x[0] != x0 || x[1] != x1 || x[2] != x2 { |
| 32 | fmt.Printf("%v, want %d,%d,%d\n", x, x0, x1, x2) |
| 33 | panic("failed") |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func check3(x, y, z, xx, yy, zz int) { |
| 38 | if x != xx || y != yy || z != zz { |
| 39 | fmt.Printf("%d,%d,%d, want %d,%d,%d\n", x, y, z, xx, yy, zz) |
| 40 | panic("failed") |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func p1() { |
Alan Donovan | 1c1096e | 2013-02-11 18:20:52 -0500 | [diff] [blame] | 45 | x := []int{1, 2, 3} |
Russ Cox | eb3aba24 | 2011-10-13 15:46:39 -0400 | [diff] [blame] | 46 | i := 0 |
| 47 | i, x[i] = 1, 100 |
| 48 | _ = i |
| 49 | check(x, 100, 2, 3) |
| 50 | } |
| 51 | |
| 52 | func p2() { |
Alan Donovan | 1c1096e | 2013-02-11 18:20:52 -0500 | [diff] [blame] | 53 | x := []int{1, 2, 3} |
Russ Cox | eb3aba24 | 2011-10-13 15:46:39 -0400 | [diff] [blame] | 54 | i := 0 |
| 55 | x[i], i = 100, 1 |
| 56 | _ = i |
| 57 | check(x, 100, 2, 3) |
| 58 | } |
| 59 | |
| 60 | func p3() { |
Alan Donovan | 1c1096e | 2013-02-11 18:20:52 -0500 | [diff] [blame] | 61 | x := []int{1, 2, 3} |
Russ Cox | eb3aba24 | 2011-10-13 15:46:39 -0400 | [diff] [blame] | 62 | y := x |
| 63 | gx = x |
| 64 | x[1], y[0] = f(0), f(1) |
| 65 | check(x, 2, 1, 3) |
| 66 | } |
| 67 | |
| 68 | func p4() { |
Alan Donovan | 1c1096e | 2013-02-11 18:20:52 -0500 | [diff] [blame] | 69 | x := []int{1, 2, 3} |
Russ Cox | eb3aba24 | 2011-10-13 15:46:39 -0400 | [diff] [blame] | 70 | y := x |
| 71 | gx = x |
| 72 | x[1], y[0] = gx[0], gx[1] |
| 73 | check(x, 2, 1, 3) |
| 74 | } |
| 75 | |
| 76 | func p5() { |
Alan Donovan | 1c1096e | 2013-02-11 18:20:52 -0500 | [diff] [blame] | 77 | x := []int{1, 2, 3} |
Russ Cox | eb3aba24 | 2011-10-13 15:46:39 -0400 | [diff] [blame] | 78 | y := x |
| 79 | p := &x[0] |
| 80 | q := &x[1] |
| 81 | *p, *q = x[1], y[0] |
| 82 | check(x, 2, 1, 3) |
| 83 | } |
| 84 | |
| 85 | func p6() { |
| 86 | x := 1 |
| 87 | y := 2 |
| 88 | z := 3 |
| 89 | px := &x |
| 90 | py := &y |
| 91 | *px, *py = y, x |
Alan Donovan | 1c1096e | 2013-02-11 18:20:52 -0500 | [diff] [blame] | 92 | check3(x, y, z, 2, 1, 3) |
Russ Cox | eb3aba24 | 2011-10-13 15:46:39 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func f1(x, y, z int) (xx, yy, zz int) { |
| 96 | return x, y, z |
| 97 | } |
| 98 | |
| 99 | func f2() (x, y, z int) { |
| 100 | return f1(2, 1, 3) |
| 101 | } |
| 102 | |
| 103 | func p7() { |
| 104 | x, y, z := f2() |
| 105 | check3(x, y, z, 2, 1, 3) |
| 106 | } |
| 107 | |
| 108 | func p8() { |
Ian Lance Taylor | 76490cf | 2012-04-24 10:17:26 -0700 | [diff] [blame] | 109 | m := make(map[int]int) |
| 110 | m[0] = len(m) |
| 111 | if m[0] != 0 { |
| 112 | panic(m[0]) |
| 113 | } |
| 114 | } |