blob: 6ba5c783e96181503b589b246e01bf4ac3293d7c [file] [log] [blame]
Russ Cox57eb06f2012-02-16 23:51:04 -05001// run
Ken Thompsonf710e8e2008-06-26 16:21:51 -07002
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 Pike80a97832012-02-24 11:48:19 +11007// Test simultaneous assignment.
8
Ken Thompsonf710e8e2008-06-26 16:21:51 -07009package main
10
Russ Cox00f9f0c2010-03-30 10:34:57 -070011var a, b, c, d, e, f, g, h, i int
Ken Thompsonf710e8e2008-06-26 16:21:51 -070012
Russ Cox00f9f0c2010-03-30 10:34:57 -070013func printit() {
14 println(a, b, c, d, e, f, g, h, i)
Ken Thompsonf710e8e2008-06-26 16:21:51 -070015}
16
Russ Cox00f9f0c2010-03-30 10:34:57 -070017func testit(permuteok bool) bool {
Ken Thompsonf710e8e2008-06-26 16:21:51 -070018 if a+b+c+d+e+f+g+h+i != 45 {
Russ Cox00f9f0c2010-03-30 10:34:57 -070019 print("sum does not add to 45\n")
20 printit()
21 return false
Ken Thompsonf710e8e2008-06-26 16:21:51 -070022 }
Russ Cox00f9f0c2010-03-30 10:34:57 -070023 return permuteok ||
Russ Cox6be0f502009-08-07 16:47:54 -070024 a == 1 &&
Russ Cox00f9f0c2010-03-30 10:34:57 -070025 b == 2 &&
26 c == 3 &&
27 d == 4 &&
28 e == 5 &&
29 f == 6 &&
30 g == 7 &&
31 h == 8 &&
32 i == 9
Ken Thompsonf710e8e2008-06-26 16:21:51 -070033}
34
Russ Cox00f9f0c2010-03-30 10:34:57 -070035func swap(x, y int) (u, v int) {
Rob Pike3a613be2008-07-03 16:48:59 -070036 return y, x
37}
38
Russ Cox00f9f0c2010-03-30 10:34:57 -070039func 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 Thompsonf710e8e2008-06-26 16:21:51 -070049
Russ Cox00f9f0c2010-03-30 10:34:57 -070050 if !testit(false) {
51 panic("init val\n")
52 }
Ken Thompsonf710e8e2008-06-26 16:21:51 -070053
Russ Cox00f9f0c2010-03-30 10:34:57 -070054 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 Thompsonf710e8e2008-06-26 16:21:51 -070056
Russ Cox6be0f502009-08-07 16:47:54 -070057 if !testit(z%20 != 19) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070058 print("on ", z, "th iteration\n")
59 printit()
60 panic("fail")
Ken Thompsonf710e8e2008-06-26 16:21:51 -070061 }
62 }
63
Russ Cox6be0f502009-08-07 16:47:54 -070064 if !testit(false) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070065 print("final val\n")
66 printit()
67 panic("fail")
Ken Thompsonf710e8e2008-06-26 16:21:51 -070068 }
Rob Pike3a613be2008-07-03 16:48:59 -070069
Russ Cox00f9f0c2010-03-30 10:34:57 -070070 a, b = swap(1, 2)
Rob Pike3a613be2008-07-03 16:48:59 -070071 if a != 2 || b != 1 {
Russ Cox00f9f0c2010-03-30 10:34:57 -070072 panic("bad swap")
Rob Pike3a613be2008-07-03 16:48:59 -070073 }
Russ Cox6be0f502009-08-07 16:47:54 -070074
Russ Cox00f9f0c2010-03-30 10:34:57 -070075 a, b = swap(swap(a, b))
Russ Cox6be0f502009-08-07 16:47:54 -070076 if a != 2 || b != 1 {
Russ Cox00f9f0c2010-03-30 10:34:57 -070077 panic("bad swap")
Russ Cox6be0f502009-08-07 16:47:54 -070078 }
Ken Thompsonf710e8e2008-06-26 16:21:51 -070079}