github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/test/fixedbugs/bug242.go (about)

     1  // run
     2  
     3  // Copyright 2010 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 order of evaluation in tuple assignments.
     8  
     9  package main
    10  
    11  var i byte = 0
    12  var a [30]byte
    13  
    14  func f() *byte {
    15  	i++
    16  	return &a[i-1]
    17  }
    18  func gbyte() byte {
    19  	i++
    20  	return 'a' + i - 1
    21  }
    22  func gint() byte {
    23  	i++
    24  	return i - 1
    25  }
    26  func x() (byte, byte) {
    27  	i++
    28  	return 'a' + i - 1, 'a' + i - 1
    29  }
    30  func e1(c chan byte, expected byte) chan byte {
    31  	if i != expected {
    32  		println("e1: got", i, "expected", expected)
    33  		panic("fail")
    34  	}
    35  	i++
    36  	return c
    37  }
    38  
    39  type Empty interface{}
    40  type I interface {
    41  	Get() byte
    42  }
    43  type S1 struct {
    44  	i byte
    45  }
    46  
    47  func (p S1) Get() byte { return p.i }
    48  
    49  type S2 struct {
    50  	i byte
    51  }
    52  
    53  func e2(p Empty, expected byte) Empty {
    54  	if i != expected {
    55  		println("e2: got", i, "expected", expected)
    56  		panic("fail")
    57  	}
    58  	i++
    59  	return p
    60  }
    61  func e3(p *I, expected byte) *I {
    62  	if i != expected {
    63  		println("e3: got", i, "expected", expected)
    64  		panic("fail")
    65  	}
    66  	i++
    67  	return p
    68  }
    69  
    70  func main() {
    71  	for i := range a {
    72  		a[i] = ' '
    73  	}
    74  
    75  	// 0     1     2     3        4        5
    76  	*f(), *f(), *f() = gbyte(), gbyte(), gbyte()
    77  
    78  	// 6     7     8
    79  	*f(), *f() = x()
    80  
    81  	m := make(map[byte]byte)
    82  	m[10] = 'A'
    83  	var p1, p2 bool
    84  	// 9           10
    85  	*f(), p1 = m[gint()]
    86  	// 11          12
    87  	*f(), p2 = m[gint()]
    88  	a[11] += '0'
    89  	if !p1 || p2 {
    90  		println("bad map check", i, p1, p2)
    91  		panic("fail")
    92  	}
    93  
    94  	m[13] = 'B'
    95  	//  13        14
    96  	delete(m, gint())
    97  	gbyte()
    98  	if _, present := m[13]; present {
    99  		println("bad map removal")
   100  		panic("fail")
   101  	}
   102  
   103  	c := make(chan byte, 1)
   104  	c <- 'C'
   105  	// 15          16
   106  	*f(), p1 = <-e1(c, 16)
   107  	close(c)
   108  	// 17          18
   109  	*f(), p2 = <-e1(c, 18)
   110  	a[17] += '0'
   111  	if !p1 || p2 {
   112  		println("bad chan check", i, p1, p2)
   113  		panic("fail")
   114  	}
   115  
   116  	s1 := S1{'D'}
   117  	s2 := S2{'E'}
   118  	var iv I
   119  	// 19                20
   120  	*e3(&iv, 19), p1 = e2(s1, 20).(I)
   121  	// 21                22
   122  	*e3(&iv, 21), p2 = e2(s2, 22).(I)
   123  	if !p1 || p2 {
   124  		println("bad interface check", i, p1, p2)
   125  		panic("fail")
   126  	}
   127  
   128  	s := string(a[0:i])
   129  	if s != "def   ii A 0   C 0     " {
   130  		println("bad array results:", s)
   131  		panic("fail")
   132  	}
   133  }