Home | History | Annotate | Download | only in testdata
      1 // run
      2 
      3 // Copyright 2015 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 control flow
      8 
      9 package main
     10 
     11 // nor_ssa calculates NOR(a, b).
     12 // It is implemented in a way that generates
     13 // phi control values.
     14 func nor_ssa(a, b bool) bool {
     15 	var c bool
     16 	if a {
     17 		c = true
     18 	}
     19 	if b {
     20 		c = true
     21 	}
     22 	if c {
     23 		return false
     24 	}
     25 	return true
     26 }
     27 
     28 func testPhiControl() {
     29 	tests := [...][3]bool{ // a, b, want
     30 		{false, false, true},
     31 		{true, false, false},
     32 		{false, true, false},
     33 		{true, true, false},
     34 	}
     35 	for _, test := range tests {
     36 		a, b := test[0], test[1]
     37 		got := nor_ssa(a, b)
     38 		want := test[2]
     39 		if want != got {
     40 			print("nor(", a, ", ", b, ")=", want, " got ", got, "\n")
     41 			failed = true
     42 		}
     43 	}
     44 }
     45 
     46 func emptyRange_ssa(b []byte) bool {
     47 	for _, x := range b {
     48 		_ = x
     49 	}
     50 	return true
     51 }
     52 
     53 func testEmptyRange() {
     54 	if !emptyRange_ssa([]byte{}) {
     55 		println("emptyRange_ssa([]byte{})=false, want true")
     56 		failed = true
     57 	}
     58 }
     59 
     60 func switch_ssa(a int) int {
     61 	ret := 0
     62 	switch a {
     63 	case 5:
     64 		ret += 5
     65 	case 4:
     66 		ret += 4
     67 	case 3:
     68 		ret += 3
     69 	case 2:
     70 		ret += 2
     71 	case 1:
     72 		ret += 1
     73 	}
     74 	return ret
     75 
     76 }
     77 
     78 func fallthrough_ssa(a int) int {
     79 	ret := 0
     80 	switch a {
     81 	case 5:
     82 		ret++
     83 		fallthrough
     84 	case 4:
     85 		ret++
     86 		fallthrough
     87 	case 3:
     88 		ret++
     89 		fallthrough
     90 	case 2:
     91 		ret++
     92 		fallthrough
     93 	case 1:
     94 		ret++
     95 	}
     96 	return ret
     97 
     98 }
     99 
    100 func testFallthrough() {
    101 	for i := 0; i < 6; i++ {
    102 		if got := fallthrough_ssa(i); got != i {
    103 			println("fallthrough_ssa(i) =", got, "wanted", i)
    104 			failed = true
    105 		}
    106 	}
    107 }
    108 
    109 func testSwitch() {
    110 	for i := 0; i < 6; i++ {
    111 		if got := switch_ssa(i); got != i {
    112 			println("switch_ssa(i) =", got, "wanted", i)
    113 			failed = true
    114 		}
    115 	}
    116 }
    117 
    118 type junk struct {
    119 	step int
    120 }
    121 
    122 // flagOverwrite_ssa is intended to reproduce an issue seen where a XOR
    123 // was scheduled between a compare and branch, clearing flags.
    124 //go:noinline
    125 func flagOverwrite_ssa(s *junk, c int) int {
    126 	if '0' <= c && c <= '9' {
    127 		s.step = 0
    128 		return 1
    129 	}
    130 	if c == 'e' || c == 'E' {
    131 		s.step = 0
    132 		return 2
    133 	}
    134 	s.step = 0
    135 	return 3
    136 }
    137 
    138 func testFlagOverwrite() {
    139 	j := junk{}
    140 	if got := flagOverwrite_ssa(&j, ' '); got != 3 {
    141 		println("flagOverwrite_ssa =", got, "wanted 3")
    142 		failed = true
    143 	}
    144 }
    145 
    146 var failed = false
    147 
    148 func main() {
    149 	testPhiControl()
    150 	testEmptyRange()
    151 
    152 	testSwitch()
    153 	testFallthrough()
    154 
    155 	testFlagOverwrite()
    156 
    157 	if failed {
    158 		panic("failed")
    159 	}
    160 }
    161