github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/cmd/compile/internal/gc/testdata/string_ssa.go (about)

     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  // string_ssa.go tests string operations.
     6  package main
     7  
     8  var failed = false
     9  
    10  //go:noinline
    11  func testStringSlice1_ssa(a string, i, j int) string {
    12  	return a[i:]
    13  }
    14  
    15  //go:noinline
    16  func testStringSlice2_ssa(a string, i, j int) string {
    17  	return a[:j]
    18  }
    19  
    20  //go:noinline
    21  func testStringSlice12_ssa(a string, i, j int) string {
    22  	return a[i:j]
    23  }
    24  
    25  func testStringSlice() {
    26  	tests := [...]struct {
    27  		fn        func(string, int, int) string
    28  		s         string
    29  		low, high int
    30  		want      string
    31  	}{
    32  		// -1 means the value is not used.
    33  		{testStringSlice1_ssa, "foobar", 0, -1, "foobar"},
    34  		{testStringSlice1_ssa, "foobar", 3, -1, "bar"},
    35  		{testStringSlice1_ssa, "foobar", 6, -1, ""},
    36  		{testStringSlice2_ssa, "foobar", -1, 0, ""},
    37  		{testStringSlice2_ssa, "foobar", -1, 3, "foo"},
    38  		{testStringSlice2_ssa, "foobar", -1, 6, "foobar"},
    39  		{testStringSlice12_ssa, "foobar", 0, 6, "foobar"},
    40  		{testStringSlice12_ssa, "foobar", 0, 0, ""},
    41  		{testStringSlice12_ssa, "foobar", 6, 6, ""},
    42  		{testStringSlice12_ssa, "foobar", 1, 5, "ooba"},
    43  		{testStringSlice12_ssa, "foobar", 3, 3, ""},
    44  		{testStringSlice12_ssa, "", 0, 0, ""},
    45  	}
    46  
    47  	for i, t := range tests {
    48  		if got := t.fn(t.s, t.low, t.high); t.want != got {
    49  			println("#", i, " ", t.s, "[", t.low, ":", t.high, "] = ", got, " want ", t.want)
    50  			failed = true
    51  		}
    52  	}
    53  }
    54  
    55  type prefix struct {
    56  	prefix string
    57  }
    58  
    59  func (p *prefix) slice_ssa() {
    60  	p.prefix = p.prefix[:3]
    61  }
    62  
    63  //go:noinline
    64  func testStructSlice() {
    65  	p := &prefix{"prefix"}
    66  	p.slice_ssa()
    67  	if "pre" != p.prefix {
    68  		println("wrong field slice: wanted %s got %s", "pre", p.prefix)
    69  		failed = true
    70  	}
    71  }
    72  
    73  func testStringSlicePanic() {
    74  	defer func() {
    75  		if r := recover(); r != nil {
    76  			println("paniced as expected")
    77  		}
    78  	}()
    79  
    80  	str := "foobar"
    81  	println("got ", testStringSlice12_ssa(str, 3, 9))
    82  	println("expected to panic, but didn't")
    83  	failed = true
    84  }
    85  
    86  const _Accuracy_name = "BelowExactAbove"
    87  
    88  var _Accuracy_index = [...]uint8{0, 5, 10, 15}
    89  
    90  //go:noinline
    91  func testSmallIndexType_ssa(i int) string {
    92  	return _Accuracy_name[_Accuracy_index[i]:_Accuracy_index[i+1]]
    93  }
    94  
    95  func testSmallIndexType() {
    96  	tests := []struct {
    97  		i    int
    98  		want string
    99  	}{
   100  		{0, "Below"},
   101  		{1, "Exact"},
   102  		{2, "Above"},
   103  	}
   104  
   105  	for i, t := range tests {
   106  		if got := testSmallIndexType_ssa(t.i); got != t.want {
   107  			println("#", i, "got ", got, ", wanted", t.want)
   108  			failed = true
   109  		}
   110  	}
   111  }
   112  
   113  //go:noinline
   114  func testStringElem_ssa(s string, i int) byte {
   115  	return s[i]
   116  }
   117  
   118  func testStringElem() {
   119  	tests := []struct {
   120  		s string
   121  		i int
   122  		n byte
   123  	}{
   124  		{"foobar", 3, 98},
   125  		{"foobar", 0, 102},
   126  		{"foobar", 5, 114},
   127  	}
   128  	for _, t := range tests {
   129  		if got := testStringElem_ssa(t.s, t.i); got != t.n {
   130  			print("testStringElem \"", t.s, "\"[", t.i, "]=", got, ", wanted ", t.n, "\n")
   131  			failed = true
   132  		}
   133  	}
   134  }
   135  
   136  //go:noinline
   137  func testStringElemConst_ssa(i int) byte {
   138  	s := "foobar"
   139  	return s[i]
   140  }
   141  
   142  func testStringElemConst() {
   143  	if got := testStringElemConst_ssa(3); got != 98 {
   144  		println("testStringElemConst=", got, ", wanted 98")
   145  		failed = true
   146  	}
   147  }
   148  
   149  func main() {
   150  	testStringSlice()
   151  	testStringSlicePanic()
   152  	testStructSlice()
   153  	testSmallIndexType()
   154  	testStringElem()
   155  	testStringElemConst()
   156  
   157  	if failed {
   158  		panic("failed")
   159  	}
   160  }