github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/test/reorder2.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 reorderings; derived from fixedbugs/bug294.go.
     8  
     9  package main
    10  
    11  var log string
    12  
    13  type TT int
    14  
    15  func (t TT) a(s string) TT {
    16  	log += "a(" + s + ")"
    17  	return t
    18  }
    19  
    20  func (TT) b(s string) string {
    21  	log += "b(" + s + ")"
    22  	return s
    23  }
    24  
    25  type F func(s string) F
    26  
    27  func a(s string) F {
    28  	log += "a(" + s + ")"
    29  	return F(a)
    30  }
    31  
    32  func b(s string) string {
    33  	log += "b(" + s + ")"
    34  	return s
    35  }
    36  
    37  type I interface {
    38  	a(s string) I
    39  	b(s string) string
    40  }
    41  
    42  type T1 int
    43  
    44  func (t T1) a(s string) I {
    45  	log += "a(" + s + ")"
    46  	return t
    47  }
    48  
    49  func (T1) b(s string) string {
    50  	log += "b(" + s + ")"
    51  	return s
    52  }
    53  
    54  // f(g(), h()) where g is not inlinable but h is will have the same problem.
    55  // As will x := g() + h() (same conditions).
    56  // And g() <- h().
    57  func f(x, y string) {
    58  	log += "f(" + x + ", " + y + ")"
    59  }
    60  
    61  func ff(x, y string) {
    62  	for false {
    63  	} // prevent inl
    64  	log += "ff(" + x + ", " + y + ")"
    65  }
    66  
    67  func h(x string) string {
    68  	log += "h(" + x + ")"
    69  	return x
    70  }
    71  
    72  func g(x string) string {
    73  	for false {
    74  	} // prevent inl
    75  	log += "g(" + x + ")"
    76  	return x
    77  }
    78  
    79  func main() {
    80  	err := 0
    81  	var t TT
    82  	if a("1")("2")("3"); log != "a(1)a(2)a(3)" {
    83  		println("expecting a(1)a(2)a(3) , got ", log)
    84  		err++
    85  	}
    86  	log = ""
    87  
    88  	if t.a("1").a(t.b("2")); log != "a(1)b(2)a(2)" {
    89  		println("expecting a(1)b(2)a(2), got ", log)
    90  		err++
    91  	}
    92  	log = ""
    93  	if a("3")(b("4"))(b("5")); log != "a(3)b(4)a(4)b(5)a(5)" {
    94  		println("expecting a(3)b(4)a(4)b(5)a(5), got ", log)
    95  		err++
    96  	}
    97  	log = ""
    98  	var i I = T1(0)
    99  	if i.a("6").a(i.b("7")).a(i.b("8")).a(i.b("9")); log != "a(6)b(7)a(7)b(8)a(8)b(9)a(9)" {
   100  		println("expecting a(6)ba(7)ba(8)ba(9), got", log)
   101  		err++
   102  	}
   103  	log = ""
   104  
   105  	if s := t.a("1").b("3"); log != "a(1)b(3)" || s != "3" {
   106  		println("expecting a(1)b(3) and 3, got ", log, " and ", s)
   107  		err++
   108  	}
   109  	log = ""
   110  
   111  	if s := t.a("1").a(t.b("2")).b("3") + t.a("4").b("5"); log != "a(1)b(2)a(2)b(3)a(4)b(5)" || s != "35" {
   112  		println("expecting a(1)b(2)a(2)b(3)a(4)b(5) and 35, got ", log, " and ", s)
   113  		err++
   114  	}
   115  	log = ""
   116  
   117  	if s := t.a("4").b("5") + t.a("1").a(t.b("2")).b("3"); log != "a(4)b(5)a(1)b(2)a(2)b(3)" || s != "53" {
   118  		println("expecting a(4)b(5)a(1)b(2)a(2)b(3) and 35, got ", log, " and ", s)
   119  		err++
   120  	}
   121  	log = ""
   122  
   123  	if ff(g("1"), g("2")); log != "g(1)g(2)ff(1, 2)" {
   124  		println("expecting g(1)g(2)ff..., got ", log)
   125  		err++
   126  	}
   127  	log = ""
   128  
   129  	if ff(g("1"), h("2")); log != "g(1)h(2)ff(1, 2)" {
   130  		println("expecting g(1)h(2)ff..., got ", log)
   131  		err++
   132  	}
   133  	log = ""
   134  
   135  	if ff(h("1"), g("2")); log != "h(1)g(2)ff(1, 2)" {
   136  		println("expecting h(1)g(2)ff..., got ", log)
   137  		err++
   138  	}
   139  	log = ""
   140  
   141  	if ff(h("1"), h("2")); log != "h(1)h(2)ff(1, 2)" {
   142  		println("expecting h(1)h(2)ff..., got ", log)
   143  		err++
   144  	}
   145  	log = ""
   146  
   147  	if s := g("1") + g("2"); log != "g(1)g(2)" || s != "12" {
   148  		println("expecting g1g2 and 12, got ", log, " and ", s)
   149  		err++
   150  	}
   151  	log = ""
   152  
   153  	if s := g("1") + h("2"); log != "g(1)h(2)" || s != "12" {
   154  		println("expecting g1h2 and 12, got ", log, " and ", s)
   155  		err++
   156  	}
   157  	log = ""
   158  
   159  	if s := h("1") + g("2"); log != "h(1)g(2)" || s != "12" {
   160  		println("expecting h1g2 and 12, got ", log, " and ", s)
   161  		err++
   162  	}
   163  	log = ""
   164  
   165  	if s := h("1") + h("2"); log != "h(1)h(2)" || s != "12" {
   166  		println("expecting h1h2 and 12, got ", log, " and ", s)
   167  		err++
   168  	}
   169  	log = ""
   170  	
   171  	x := 0
   172  	switch x {
   173  	case 0:
   174  		if a("1")("2")("3"); log != "a(1)a(2)a(3)" {
   175  			println("in switch, expecting a(1)a(2)a(3) , got ", log)
   176  			err++
   177  		}
   178  		log = ""
   179  	
   180  		if t.a("1").a(t.b("2")); log != "a(1)b(2)a(2)" {
   181  			println("in switch, expecting a(1)b(2)a(2), got ", log)
   182  			err++
   183  		}
   184  		log = ""
   185  		if a("3")(b("4"))(b("5")); log != "a(3)b(4)a(4)b(5)a(5)" {
   186  			println("in switch, expecting a(3)b(4)a(4)b(5)a(5), got ", log)
   187  			err++
   188  		}
   189  		log = ""
   190  		var i I = T1(0)
   191  		if i.a("6").a(i.b("7")).a(i.b("8")).a(i.b("9")); log != "a(6)b(7)a(7)b(8)a(8)b(9)a(9)" {
   192  			println("in switch, expecting a(6)ba(7)ba(8)ba(9), got", log)
   193  			err++
   194  		}
   195  		log = ""
   196  	}
   197  	
   198  	c := make(chan int, 1)
   199  	c <- 1
   200  	select {
   201  	case c <- 0:
   202  	case c <- 1:
   203  	case <-c:
   204  		if a("1")("2")("3"); log != "a(1)a(2)a(3)" {
   205  			println("in select1, expecting a(1)a(2)a(3) , got ", log)
   206  			err++
   207  		}
   208  		log = ""
   209  	
   210  		if t.a("1").a(t.b("2")); log != "a(1)b(2)a(2)" {
   211  			println("in select1, expecting a(1)b(2)a(2), got ", log)
   212  			err++
   213  		}
   214  		log = ""
   215  		if a("3")(b("4"))(b("5")); log != "a(3)b(4)a(4)b(5)a(5)" {
   216  			println("in select1, expecting a(3)b(4)a(4)b(5)a(5), got ", log)
   217  			err++
   218  		}
   219  		log = ""
   220  		var i I = T1(0)
   221  		if i.a("6").a(i.b("7")).a(i.b("8")).a(i.b("9")); log != "a(6)b(7)a(7)b(8)a(8)b(9)a(9)" {
   222  			println("in select1, expecting a(6)ba(7)ba(8)ba(9), got", log)
   223  			err++
   224  		}
   225  		log = ""
   226  	}
   227  
   228  	c <- 1
   229  	select {
   230  	case <-c:
   231  		if a("1")("2")("3"); log != "a(1)a(2)a(3)" {
   232  			println("in select2, expecting a(1)a(2)a(3) , got ", log)
   233  			err++
   234  		}
   235  		log = ""
   236  	
   237  		if t.a("1").a(t.b("2")); log != "a(1)b(2)a(2)" {
   238  			println("in select2, expecting a(1)b(2)a(2), got ", log)
   239  			err++
   240  		}
   241  		log = ""
   242  		if a("3")(b("4"))(b("5")); log != "a(3)b(4)a(4)b(5)a(5)" {
   243  			println("in select2, expecting a(3)b(4)a(4)b(5)a(5), got ", log)
   244  			err++
   245  		}
   246  		log = ""
   247  		var i I = T1(0)
   248  		if i.a("6").a(i.b("7")).a(i.b("8")).a(i.b("9")); log != "a(6)b(7)a(7)b(8)a(8)b(9)a(9)" {
   249  			println("in select2, expecting a(6)ba(7)ba(8)ba(9), got", log)
   250  			err++
   251  		}
   252  		log = ""
   253  	}
   254  
   255  	c <- 1
   256  	select {
   257  	default:
   258  	case c<-1:
   259  	case <-c:
   260  		if a("1")("2")("3"); log != "a(1)a(2)a(3)" {
   261  			println("in select3, expecting a(1)a(2)a(3) , got ", log)
   262  			err++
   263  		}
   264  		log = ""
   265  	
   266  		if t.a("1").a(t.b("2")); log != "a(1)b(2)a(2)" {
   267  			println("in select3, expecting a(1)b(2)a(2), got ", log)
   268  			err++
   269  		}
   270  		log = ""
   271  		if a("3")(b("4"))(b("5")); log != "a(3)b(4)a(4)b(5)a(5)" {
   272  			println("in select3, expecting a(3)b(4)a(4)b(5)a(5), got ", log)
   273  			err++
   274  		}
   275  		log = ""
   276  		var i I = T1(0)
   277  		if i.a("6").a(i.b("7")).a(i.b("8")).a(i.b("9")); log != "a(6)b(7)a(7)b(8)a(8)b(9)a(9)" {
   278  			println("in select3, expecting a(6)ba(7)ba(8)ba(9), got", log)
   279  			err++
   280  		}
   281  		log = ""
   282  	}
   283  
   284  	c <- 1
   285  	select {
   286  	default:
   287  	case <-c:
   288  		if a("1")("2")("3"); log != "a(1)a(2)a(3)" {
   289  			println("in select4, expecting a(1)a(2)a(3) , got ", log)
   290  			err++
   291  		}
   292  		log = ""
   293  	
   294  		if t.a("1").a(t.b("2")); log != "a(1)b(2)a(2)" {
   295  			println("in select4, expecting a(1)b(2)a(2), got ", log)
   296  			err++
   297  		}
   298  		log = ""
   299  		if a("3")(b("4"))(b("5")); log != "a(3)b(4)a(4)b(5)a(5)" {
   300  			println("in select4, expecting a(3)b(4)a(4)b(5)a(5), got ", log)
   301  			err++
   302  		}
   303  		log = ""
   304  		var i I = T1(0)
   305  		if i.a("6").a(i.b("7")).a(i.b("8")).a(i.b("9")); log != "a(6)b(7)a(7)b(8)a(8)b(9)a(9)" {
   306  			println("in select4, expecting a(6)ba(7)ba(8)ba(9), got", log)
   307  			err++
   308  		}
   309  		log = ""
   310  	}
   311  
   312  	select {
   313  	case <-c:
   314  	case <-c:
   315  	default:
   316  		if a("1")("2")("3"); log != "a(1)a(2)a(3)" {
   317  			println("in select5, expecting a(1)a(2)a(3) , got ", log)
   318  			err++
   319  		}
   320  		log = ""
   321  	
   322  		if t.a("1").a(t.b("2")); log != "a(1)b(2)a(2)" {
   323  			println("in select5, expecting a(1)b(2)a(2), got ", log)
   324  			err++
   325  		}
   326  		log = ""
   327  		if a("3")(b("4"))(b("5")); log != "a(3)b(4)a(4)b(5)a(5)" {
   328  			println("in select5, expecting a(3)b(4)a(4)b(5)a(5), got ", log)
   329  			err++
   330  		}
   331  		log = ""
   332  		var i I = T1(0)
   333  		if i.a("6").a(i.b("7")).a(i.b("8")).a(i.b("9")); log != "a(6)b(7)a(7)b(8)a(8)b(9)a(9)" {
   334  			println("in select5, expecting a(6)ba(7)ba(8)ba(9), got", log)
   335  			err++
   336  		}
   337  		log = ""
   338  	}
   339  
   340  	if err > 0 {
   341  		panic("fail")
   342  	}
   343  }