Russ Cox | 57eb06f | 2012-02-16 23:51:04 -0500 | [diff] [blame] | 1 | // run |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 2 | |
| 3 | // Copyright 2009 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 | 80a9783 | 2012-02-24 11:48:19 +1100 | [diff] [blame] | 7 | // Test simultaneous assignment. |
| 8 | |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 9 | package main |
| 10 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 11 | var a, b, c, d, e, f, g, h, i int |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 12 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 13 | func printit() { |
| 14 | println(a, b, c, d, e, f, g, h, i) |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 15 | } |
| 16 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 17 | func testit(permuteok bool) bool { |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 18 | if a+b+c+d+e+f+g+h+i != 45 { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 19 | print("sum does not add to 45\n") |
| 20 | printit() |
| 21 | return false |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 22 | } |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 23 | return permuteok || |
Russ Cox | 6be0f50 | 2009-08-07 16:47:54 -0700 | [diff] [blame] | 24 | a == 1 && |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 25 | b == 2 && |
| 26 | c == 3 && |
| 27 | d == 4 && |
| 28 | e == 5 && |
| 29 | f == 6 && |
| 30 | g == 7 && |
| 31 | h == 8 && |
| 32 | i == 9 |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 35 | func swap(x, y int) (u, v int) { |
Rob Pike | 3a613be | 2008-07-03 16:48:59 -0700 | [diff] [blame] | 36 | return y, x |
| 37 | } |
| 38 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 39 | func main() { |
| 40 | a = 1 |
| 41 | b = 2 |
| 42 | c = 3 |
| 43 | d = 4 |
| 44 | e = 5 |
| 45 | f = 6 |
| 46 | g = 7 |
| 47 | h = 8 |
| 48 | i = 9 |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 49 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 50 | if !testit(false) { |
| 51 | panic("init val\n") |
| 52 | } |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 53 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 54 | for z := 0; z < 100; z++ { |
| 55 | a, b, c, d, e, f, g, h, i = b, c, d, a, i, e, f, g, h |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 56 | |
Russ Cox | 6be0f50 | 2009-08-07 16:47:54 -0700 | [diff] [blame] | 57 | if !testit(z%20 != 19) { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 58 | print("on ", z, "th iteration\n") |
| 59 | printit() |
| 60 | panic("fail") |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Russ Cox | 6be0f50 | 2009-08-07 16:47:54 -0700 | [diff] [blame] | 64 | if !testit(false) { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 65 | print("final val\n") |
| 66 | printit() |
| 67 | panic("fail") |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 68 | } |
Rob Pike | 3a613be | 2008-07-03 16:48:59 -0700 | [diff] [blame] | 69 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 70 | a, b = swap(1, 2) |
Rob Pike | 3a613be | 2008-07-03 16:48:59 -0700 | [diff] [blame] | 71 | if a != 2 || b != 1 { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 72 | panic("bad swap") |
Rob Pike | 3a613be | 2008-07-03 16:48:59 -0700 | [diff] [blame] | 73 | } |
Russ Cox | 6be0f50 | 2009-08-07 16:47:54 -0700 | [diff] [blame] | 74 | |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 75 | a, b = swap(swap(a, b)) |
Russ Cox | 6be0f50 | 2009-08-07 16:47:54 -0700 | [diff] [blame] | 76 | if a != 2 || b != 1 { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 77 | panic("bad swap") |
Russ Cox | 6be0f50 | 2009-08-07 16:47:54 -0700 | [diff] [blame] | 78 | } |
Ken Thompson | f710e8e | 2008-06-26 16:21:51 -0700 | [diff] [blame] | 79 | } |