github.com/4ad/go@v0.0.0-20161219182952-69a12818b605/src/runtime/string_test.go (about)

     1  // Copyright 2012 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  package runtime_test
     6  
     7  import (
     8  	"runtime"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  // Strings and slices that don't escape and fit into tmpBuf are stack allocated,
    14  // which defeats using AllocsPerRun to test other optimizations.
    15  const sizeNoStack = 100
    16  
    17  func BenchmarkCompareStringEqual(b *testing.B) {
    18  	bytes := []byte("Hello Gophers!")
    19  	s1, s2 := string(bytes), string(bytes)
    20  	for i := 0; i < b.N; i++ {
    21  		if s1 != s2 {
    22  			b.Fatal("s1 != s2")
    23  		}
    24  	}
    25  }
    26  
    27  func BenchmarkCompareStringIdentical(b *testing.B) {
    28  	s1 := "Hello Gophers!"
    29  	s2 := s1
    30  	for i := 0; i < b.N; i++ {
    31  		if s1 != s2 {
    32  			b.Fatal("s1 != s2")
    33  		}
    34  	}
    35  }
    36  
    37  func BenchmarkCompareStringSameLength(b *testing.B) {
    38  	s1 := "Hello Gophers!"
    39  	s2 := "Hello, Gophers"
    40  	for i := 0; i < b.N; i++ {
    41  		if s1 == s2 {
    42  			b.Fatal("s1 == s2")
    43  		}
    44  	}
    45  }
    46  
    47  func BenchmarkCompareStringDifferentLength(b *testing.B) {
    48  	s1 := "Hello Gophers!"
    49  	s2 := "Hello, Gophers!"
    50  	for i := 0; i < b.N; i++ {
    51  		if s1 == s2 {
    52  			b.Fatal("s1 == s2")
    53  		}
    54  	}
    55  }
    56  
    57  func BenchmarkCompareStringBigUnaligned(b *testing.B) {
    58  	bytes := make([]byte, 0, 1<<20)
    59  	for len(bytes) < 1<<20 {
    60  		bytes = append(bytes, "Hello Gophers!"...)
    61  	}
    62  	s1, s2 := string(bytes), "hello"+string(bytes)
    63  	for i := 0; i < b.N; i++ {
    64  		if s1 != s2[len("hello"):] {
    65  			b.Fatal("s1 != s2")
    66  		}
    67  	}
    68  	b.SetBytes(int64(len(s1)))
    69  }
    70  
    71  func BenchmarkCompareStringBig(b *testing.B) {
    72  	bytes := make([]byte, 0, 1<<20)
    73  	for len(bytes) < 1<<20 {
    74  		bytes = append(bytes, "Hello Gophers!"...)
    75  	}
    76  	s1, s2 := string(bytes), string(bytes)
    77  	for i := 0; i < b.N; i++ {
    78  		if s1 != s2 {
    79  			b.Fatal("s1 != s2")
    80  		}
    81  	}
    82  	b.SetBytes(int64(len(s1)))
    83  }
    84  
    85  func BenchmarkRuneIterate(b *testing.B) {
    86  	bytes := make([]byte, 100)
    87  	for i := range bytes {
    88  		bytes[i] = byte('A')
    89  	}
    90  	s := string(bytes)
    91  	for i := 0; i < b.N; i++ {
    92  		for range s {
    93  		}
    94  	}
    95  }
    96  
    97  func BenchmarkRuneIterate2(b *testing.B) {
    98  	bytes := make([]byte, 100)
    99  	for i := range bytes {
   100  		bytes[i] = byte('A')
   101  	}
   102  	s := string(bytes)
   103  	for i := 0; i < b.N; i++ {
   104  		for range s {
   105  		}
   106  	}
   107  }
   108  
   109  func BenchmarkArrayEqual(b *testing.B) {
   110  	a1 := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
   111  	a2 := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
   112  	b.ResetTimer()
   113  	for i := 0; i < b.N; i++ {
   114  		if a1 != a2 {
   115  			b.Fatal("not equal")
   116  		}
   117  	}
   118  }
   119  
   120  func TestStringW(t *testing.T) {
   121  	strings := []string{
   122  		"hello",
   123  		"a\u5566\u7788b",
   124  	}
   125  
   126  	for _, s := range strings {
   127  		var b []uint16
   128  		for _, c := range s {
   129  			b = append(b, uint16(c))
   130  			if c != rune(uint16(c)) {
   131  				t.Errorf("bad test: stringW can't handle >16 bit runes")
   132  			}
   133  		}
   134  		b = append(b, 0)
   135  		r := runtime.GostringW(b)
   136  		if r != s {
   137  			t.Errorf("gostringW(%v) = %s, want %s", b, r, s)
   138  		}
   139  	}
   140  }
   141  
   142  func TestLargeStringConcat(t *testing.T) {
   143  	output := runTestProg(t, "testprog", "stringconcat")
   144  	want := "panic: " + strings.Repeat("0", 1<<10) + strings.Repeat("1", 1<<10) +
   145  		strings.Repeat("2", 1<<10) + strings.Repeat("3", 1<<10)
   146  	if !strings.HasPrefix(output, want) {
   147  		t.Fatalf("output does not start with %q:\n%s", want, output)
   148  	}
   149  }
   150  
   151  func TestGostringnocopy(t *testing.T) {
   152  	max := *runtime.Maxstring
   153  	b := make([]byte, max+10)
   154  	for i := uintptr(0); i < max+9; i++ {
   155  		b[i] = 'a'
   156  	}
   157  	_ = runtime.Gostringnocopy(&b[0])
   158  	newmax := *runtime.Maxstring
   159  	if newmax != max+9 {
   160  		t.Errorf("want %d, got %d", max+9, newmax)
   161  	}
   162  }
   163  
   164  func TestCompareTempString(t *testing.T) {
   165  	s := strings.Repeat("x", sizeNoStack)
   166  	b := []byte(s)
   167  	n := testing.AllocsPerRun(1000, func() {
   168  		if string(b) != s {
   169  			t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
   170  		}
   171  		if string(b) == s {
   172  		} else {
   173  			t.Fatalf("strings are not equal: '%v' and '%v'", string(b), s)
   174  		}
   175  	})
   176  	if n != 0 {
   177  		t.Fatalf("want 0 allocs, got %v", n)
   178  	}
   179  }
   180  
   181  func TestStringOnStack(t *testing.T) {
   182  	s := ""
   183  	for i := 0; i < 3; i++ {
   184  		s = "a" + s + "b" + s + "c"
   185  	}
   186  
   187  	if want := "aaabcbabccbaabcbabccc"; s != want {
   188  		t.Fatalf("want: '%v', got '%v'", want, s)
   189  	}
   190  }
   191  
   192  func TestIntString(t *testing.T) {
   193  	// Non-escaping result of intstring.
   194  	s := ""
   195  	for i := 0; i < 4; i++ {
   196  		s += string(i+'0') + string(i+'0'+1)
   197  	}
   198  	if want := "01122334"; s != want {
   199  		t.Fatalf("want '%v', got '%v'", want, s)
   200  	}
   201  
   202  	// Escaping result of intstring.
   203  	var a [4]string
   204  	for i := 0; i < 4; i++ {
   205  		a[i] = string(i + '0')
   206  	}
   207  	s = a[0] + a[1] + a[2] + a[3]
   208  	if want := "0123"; s != want {
   209  		t.Fatalf("want '%v', got '%v'", want, s)
   210  	}
   211  }
   212  
   213  func TestIntStringAllocs(t *testing.T) {
   214  	unknown := '0'
   215  	n := testing.AllocsPerRun(1000, func() {
   216  		s1 := string(unknown)
   217  		s2 := string(unknown + 1)
   218  		if s1 == s2 {
   219  			t.Fatalf("bad")
   220  		}
   221  	})
   222  	if n != 0 {
   223  		t.Fatalf("want 0 allocs, got %v", n)
   224  	}
   225  }
   226  
   227  func TestRangeStringCast(t *testing.T) {
   228  	s := strings.Repeat("x", sizeNoStack)
   229  	n := testing.AllocsPerRun(1000, func() {
   230  		for i, c := range []byte(s) {
   231  			if c != s[i] {
   232  				t.Fatalf("want '%c' at pos %v, got '%c'", s[i], i, c)
   233  			}
   234  		}
   235  	})
   236  	if n != 0 {
   237  		t.Fatalf("want 0 allocs, got %v", n)
   238  	}
   239  }
   240  
   241  func isZeroed(b []byte) bool {
   242  	for _, x := range b {
   243  		if x != 0 {
   244  			return false
   245  		}
   246  	}
   247  	return true
   248  }
   249  
   250  func isZeroedR(r []rune) bool {
   251  	for _, x := range r {
   252  		if x != 0 {
   253  			return false
   254  		}
   255  	}
   256  	return true
   257  }
   258  
   259  func TestString2Slice(t *testing.T) {
   260  	// Make sure we don't return slices that expose
   261  	// an unzeroed section of stack-allocated temp buf
   262  	// between len and cap. See issue 14232.
   263  	s := "foož"
   264  	b := ([]byte)(s)
   265  	if !isZeroed(b[len(b):cap(b)]) {
   266  		t.Errorf("extra bytes not zeroed")
   267  	}
   268  	r := ([]rune)(s)
   269  	if !isZeroedR(r[len(r):cap(r)]) {
   270  		t.Errorf("extra runes not zeroed")
   271  	}
   272  }