Source file src/cmd/compile/internal/test/testdata/ctl_test.go

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Test control flow
     6  
     7  package main
     8  
     9  import "testing"
    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(t *testing.T) {
    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  			t.Errorf("nor(%t, %t)=%t got %t", a, b, want, got)
    41  		}
    42  	}
    43  }
    44  
    45  func emptyRange_ssa(b []byte) bool {
    46  	for _, x := range b {
    47  		_ = x
    48  	}
    49  	return true
    50  }
    51  
    52  func testEmptyRange(t *testing.T) {
    53  	if !emptyRange_ssa([]byte{}) {
    54  		t.Errorf("emptyRange_ssa([]byte{})=false, want true")
    55  	}
    56  }
    57  
    58  func switch_ssa(a int) int {
    59  	ret := 0
    60  	switch a {
    61  	case 5:
    62  		ret += 5
    63  	case 4:
    64  		ret += 4
    65  	case 3:
    66  		ret += 3
    67  	case 2:
    68  		ret += 2
    69  	case 1:
    70  		ret += 1
    71  	}
    72  	return ret
    73  }
    74  
    75  func switch_nonconstcase(n, c int) int {
    76  	switch n {
    77  	case 1:
    78  		return 1
    79  	case 2:
    80  		return 2
    81  	case c:
    82  		return 9
    83  	case 3:
    84  		return 3
    85  	case 4:
    86  		return 4
    87  	}
    88  	return 0
    89  }
    90  
    91  func fallthrough_ssa(a int) int {
    92  	ret := 0
    93  	switch a {
    94  	case 5:
    95  		ret++
    96  		fallthrough
    97  	case 4:
    98  		ret++
    99  		fallthrough
   100  	case 3:
   101  		ret++
   102  		fallthrough
   103  	case 2:
   104  		ret++
   105  		fallthrough
   106  	case 1:
   107  		ret++
   108  	}
   109  	return ret
   110  }
   111  
   112  func testFallthrough(t *testing.T) {
   113  	for i := 0; i < 6; i++ {
   114  		if got := fallthrough_ssa(i); got != i {
   115  			t.Errorf("fallthrough_ssa(i) = %d, wanted %d", got, i)
   116  		}
   117  	}
   118  }
   119  
   120  func testSwitch(t *testing.T) {
   121  	for i := 0; i < 6; i++ {
   122  		if got := switch_ssa(i); got != i {
   123  			t.Errorf("switch_ssa(i) = %d, wanted %d", got, i)
   124  		}
   125  	}
   126  	if got := switch_nonconstcase(3, 3); got != 9 {
   127  		t.Errorf("switch_nonconstcase(3, 3) = %d, wanted 9", got)
   128  	}
   129  }
   130  
   131  type junk struct {
   132  	step int
   133  }
   134  
   135  // flagOverwrite_ssa is intended to reproduce an issue seen where a XOR
   136  // was scheduled between a compare and branch, clearing flags.
   137  //
   138  //go:noinline
   139  func flagOverwrite_ssa(s *junk, c int) int {
   140  	if '0' <= c && c <= '9' {
   141  		s.step = 0
   142  		return 1
   143  	}
   144  	if c == 'e' || c == 'E' {
   145  		s.step = 0
   146  		return 2
   147  	}
   148  	s.step = 0
   149  	return 3
   150  }
   151  
   152  func testFlagOverwrite(t *testing.T) {
   153  	j := junk{}
   154  	if got := flagOverwrite_ssa(&j, ' '); got != 3 {
   155  		t.Errorf("flagOverwrite_ssa = %d, wanted 3", got)
   156  	}
   157  }
   158  
   159  func TestCtl(t *testing.T) {
   160  	testPhiControl(t)
   161  	testEmptyRange(t)
   162  
   163  	testSwitch(t)
   164  	testFallthrough(t)
   165  
   166  	testFlagOverwrite(t)
   167  }
   168  

View as plain text