github.com/euank/go@v0.0.0-20160829210321-495514729181/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 testInt64Index_ssa(s string, i int64) byte {
   115  	return s[i]
   116  }
   117  
   118  //go:noinline
   119  func testInt64Slice_ssa(s string, i, j int64) string {
   120  	return s[i:j]
   121  }
   122  
   123  func testInt64Index() {
   124  	tests := []struct {
   125  		i int64
   126  		j int64
   127  		b byte
   128  		s string
   129  	}{
   130  		{0, 5, 'B', "Below"},
   131  		{5, 10, 'E', "Exact"},
   132  		{10, 15, 'A', "Above"},
   133  	}
   134  
   135  	str := "BelowExactAbove"
   136  	for i, t := range tests {
   137  		if got := testInt64Index_ssa(str, t.i); got != t.b {
   138  			println("#", i, "got ", got, ", wanted", t.b)
   139  			failed = true
   140  		}
   141  		if got := testInt64Slice_ssa(str, t.i, t.j); got != t.s {
   142  			println("#", i, "got ", got, ", wanted", t.s)
   143  			failed = true
   144  		}
   145  	}
   146  }
   147  
   148  func testInt64IndexPanic() {
   149  	defer func() {
   150  		if r := recover(); r != nil {
   151  			println("paniced as expected")
   152  		}
   153  	}()
   154  
   155  	str := "foobar"
   156  	println("got ", testInt64Index_ssa(str, 1<<32+1))
   157  	println("expected to panic, but didn't")
   158  	failed = true
   159  }
   160  
   161  func testInt64SlicePanic() {
   162  	defer func() {
   163  		if r := recover(); r != nil {
   164  			println("paniced as expected")
   165  		}
   166  	}()
   167  
   168  	str := "foobar"
   169  	println("got ", testInt64Slice_ssa(str, 1<<32, 1<<32+1))
   170  	println("expected to panic, but didn't")
   171  	failed = true
   172  }
   173  
   174  //go:noinline
   175  func testStringElem_ssa(s string, i int) byte {
   176  	return s[i]
   177  }
   178  
   179  func testStringElem() {
   180  	tests := []struct {
   181  		s string
   182  		i int
   183  		n byte
   184  	}{
   185  		{"foobar", 3, 98},
   186  		{"foobar", 0, 102},
   187  		{"foobar", 5, 114},
   188  	}
   189  	for _, t := range tests {
   190  		if got := testStringElem_ssa(t.s, t.i); got != t.n {
   191  			print("testStringElem \"", t.s, "\"[", t.i, "]=", got, ", wanted ", t.n, "\n")
   192  			failed = true
   193  		}
   194  	}
   195  }
   196  
   197  //go:noinline
   198  func testStringElemConst_ssa(i int) byte {
   199  	s := "foobar"
   200  	return s[i]
   201  }
   202  
   203  func testStringElemConst() {
   204  	if got := testStringElemConst_ssa(3); got != 98 {
   205  		println("testStringElemConst=", got, ", wanted 98")
   206  		failed = true
   207  	}
   208  }
   209  
   210  func main() {
   211  	testStringSlice()
   212  	testStringSlicePanic()
   213  	testStructSlice()
   214  	testSmallIndexType()
   215  	testStringElem()
   216  	testStringElemConst()
   217  	testInt64Index()
   218  	testInt64IndexPanic()
   219  	testInt64SlicePanic()
   220  
   221  	if failed {
   222  		panic("failed")
   223  	}
   224  }