...
Run Format

Source file test/simassign.go

Documentation: test

     1  // run
     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  
     7  // Test simultaneous assignment.
     8  
     9  package main
    10  
    11  var a, b, c, d, e, f, g, h, i int
    12  
    13  func printit() {
    14  	println(a, b, c, d, e, f, g, h, i)
    15  }
    16  
    17  func testit(permuteok bool) bool {
    18  	if a+b+c+d+e+f+g+h+i != 45 {
    19  		print("sum does not add to 45\n")
    20  		printit()
    21  		return false
    22  	}
    23  	return permuteok ||
    24  		a == 1 &&
    25  			b == 2 &&
    26  			c == 3 &&
    27  			d == 4 &&
    28  			e == 5 &&
    29  			f == 6 &&
    30  			g == 7 &&
    31  			h == 8 &&
    32  			i == 9
    33  }
    34  
    35  func swap(x, y int) (u, v int) {
    36  	return y, x
    37  }
    38  
    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
    49  
    50  	if !testit(false) {
    51  		panic("init val\n")
    52  	}
    53  
    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
    56  
    57  		if !testit(z%20 != 19) {
    58  			print("on ", z, "th iteration\n")
    59  			printit()
    60  			panic("fail")
    61  		}
    62  	}
    63  
    64  	if !testit(false) {
    65  		print("final val\n")
    66  		printit()
    67  		panic("fail")
    68  	}
    69  
    70  	a, b = swap(1, 2)
    71  	if a != 2 || b != 1 {
    72  		panic("bad swap")
    73  	}
    74  
    75  	a, b = swap(swap(a, b))
    76  	if a != 2 || b != 1 {
    77  		panic("bad swap")
    78  	}
    79  }
    80  

View as plain text