github.com/hbdrawn/golang@v0.0.0-20141214014649-6b835209aba2/src/runtime/memmove_test.go (about)

     1  // Copyright 2013 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  	"testing"
    10  )
    11  
    12  func TestMemmove(t *testing.T) {
    13  	size := 256
    14  	if testing.Short() {
    15  		size = 128 + 16
    16  	}
    17  	src := make([]byte, size)
    18  	dst := make([]byte, size)
    19  	for i := 0; i < size; i++ {
    20  		src[i] = byte(128 + (i & 127))
    21  	}
    22  	for i := 0; i < size; i++ {
    23  		dst[i] = byte(i & 127)
    24  	}
    25  	for n := 0; n <= size; n++ {
    26  		for x := 0; x <= size-n; x++ { // offset in src
    27  			for y := 0; y <= size-n; y++ { // offset in dst
    28  				copy(dst[y:y+n], src[x:x+n])
    29  				for i := 0; i < y; i++ {
    30  					if dst[i] != byte(i&127) {
    31  						t.Fatalf("prefix dst[%d] = %d", i, dst[i])
    32  					}
    33  				}
    34  				for i := y; i < y+n; i++ {
    35  					if dst[i] != byte(128+((i-y+x)&127)) {
    36  						t.Fatalf("copied dst[%d] = %d", i, dst[i])
    37  					}
    38  					dst[i] = byte(i & 127) // reset dst
    39  				}
    40  				for i := y + n; i < size; i++ {
    41  					if dst[i] != byte(i&127) {
    42  						t.Fatalf("suffix dst[%d] = %d", i, dst[i])
    43  					}
    44  				}
    45  			}
    46  		}
    47  	}
    48  }
    49  
    50  func TestMemmoveAlias(t *testing.T) {
    51  	size := 256
    52  	if testing.Short() {
    53  		size = 128 + 16
    54  	}
    55  	buf := make([]byte, size)
    56  	for i := 0; i < size; i++ {
    57  		buf[i] = byte(i)
    58  	}
    59  	for n := 0; n <= size; n++ {
    60  		for x := 0; x <= size-n; x++ { // src offset
    61  			for y := 0; y <= size-n; y++ { // dst offset
    62  				copy(buf[y:y+n], buf[x:x+n])
    63  				for i := 0; i < y; i++ {
    64  					if buf[i] != byte(i) {
    65  						t.Fatalf("prefix buf[%d] = %d", i, buf[i])
    66  					}
    67  				}
    68  				for i := y; i < y+n; i++ {
    69  					if buf[i] != byte(i-y+x) {
    70  						t.Fatalf("copied buf[%d] = %d", i, buf[i])
    71  					}
    72  					buf[i] = byte(i) // reset buf
    73  				}
    74  				for i := y + n; i < size; i++ {
    75  					if buf[i] != byte(i) {
    76  						t.Fatalf("suffix buf[%d] = %d", i, buf[i])
    77  					}
    78  				}
    79  			}
    80  		}
    81  	}
    82  }
    83  
    84  func bmMemmove(b *testing.B, n int) {
    85  	x := make([]byte, n)
    86  	y := make([]byte, n)
    87  	b.SetBytes(int64(n))
    88  	for i := 0; i < b.N; i++ {
    89  		copy(x, y)
    90  	}
    91  }
    92  
    93  func BenchmarkMemmove0(b *testing.B)    { bmMemmove(b, 0) }
    94  func BenchmarkMemmove1(b *testing.B)    { bmMemmove(b, 1) }
    95  func BenchmarkMemmove2(b *testing.B)    { bmMemmove(b, 2) }
    96  func BenchmarkMemmove3(b *testing.B)    { bmMemmove(b, 3) }
    97  func BenchmarkMemmove4(b *testing.B)    { bmMemmove(b, 4) }
    98  func BenchmarkMemmove5(b *testing.B)    { bmMemmove(b, 5) }
    99  func BenchmarkMemmove6(b *testing.B)    { bmMemmove(b, 6) }
   100  func BenchmarkMemmove7(b *testing.B)    { bmMemmove(b, 7) }
   101  func BenchmarkMemmove8(b *testing.B)    { bmMemmove(b, 8) }
   102  func BenchmarkMemmove9(b *testing.B)    { bmMemmove(b, 9) }
   103  func BenchmarkMemmove10(b *testing.B)   { bmMemmove(b, 10) }
   104  func BenchmarkMemmove11(b *testing.B)   { bmMemmove(b, 11) }
   105  func BenchmarkMemmove12(b *testing.B)   { bmMemmove(b, 12) }
   106  func BenchmarkMemmove13(b *testing.B)   { bmMemmove(b, 13) }
   107  func BenchmarkMemmove14(b *testing.B)   { bmMemmove(b, 14) }
   108  func BenchmarkMemmove15(b *testing.B)   { bmMemmove(b, 15) }
   109  func BenchmarkMemmove16(b *testing.B)   { bmMemmove(b, 16) }
   110  func BenchmarkMemmove32(b *testing.B)   { bmMemmove(b, 32) }
   111  func BenchmarkMemmove64(b *testing.B)   { bmMemmove(b, 64) }
   112  func BenchmarkMemmove128(b *testing.B)  { bmMemmove(b, 128) }
   113  func BenchmarkMemmove256(b *testing.B)  { bmMemmove(b, 256) }
   114  func BenchmarkMemmove512(b *testing.B)  { bmMemmove(b, 512) }
   115  func BenchmarkMemmove1024(b *testing.B) { bmMemmove(b, 1024) }
   116  func BenchmarkMemmove2048(b *testing.B) { bmMemmove(b, 2048) }
   117  func BenchmarkMemmove4096(b *testing.B) { bmMemmove(b, 4096) }
   118  
   119  func TestMemclr(t *testing.T) {
   120  	size := 512
   121  	if testing.Short() {
   122  		size = 128 + 16
   123  	}
   124  	mem := make([]byte, size)
   125  	for i := 0; i < size; i++ {
   126  		mem[i] = 0xee
   127  	}
   128  	for n := 0; n < size; n++ {
   129  		for x := 0; x <= size-n; x++ { // offset in mem
   130  			MemclrBytes(mem[x : x+n])
   131  			for i := 0; i < x; i++ {
   132  				if mem[i] != 0xee {
   133  					t.Fatalf("overwrite prefix mem[%d] = %d", i, mem[i])
   134  				}
   135  			}
   136  			for i := x; i < x+n; i++ {
   137  				if mem[i] != 0 {
   138  					t.Fatalf("failed clear mem[%d] = %d", i, mem[i])
   139  				}
   140  				mem[i] = 0xee
   141  			}
   142  			for i := x + n; i < size; i++ {
   143  				if mem[i] != 0xee {
   144  					t.Fatalf("overwrite suffix mem[%d] = %d", i, mem[i])
   145  				}
   146  			}
   147  		}
   148  	}
   149  }
   150  
   151  func bmMemclr(b *testing.B, n int) {
   152  	x := make([]byte, n)
   153  	b.SetBytes(int64(n))
   154  	for i := 0; i < b.N; i++ {
   155  		MemclrBytes(x)
   156  	}
   157  }
   158  func BenchmarkMemclr5(b *testing.B)     { bmMemclr(b, 5) }
   159  func BenchmarkMemclr16(b *testing.B)    { bmMemclr(b, 16) }
   160  func BenchmarkMemclr64(b *testing.B)    { bmMemclr(b, 64) }
   161  func BenchmarkMemclr256(b *testing.B)   { bmMemclr(b, 256) }
   162  func BenchmarkMemclr4096(b *testing.B)  { bmMemclr(b, 4096) }
   163  func BenchmarkMemclr65536(b *testing.B) { bmMemclr(b, 65536) }
   164  
   165  func BenchmarkClearFat8(b *testing.B) {
   166  	for i := 0; i < b.N; i++ {
   167  		var x [8 / 4]uint32
   168  		_ = x
   169  	}
   170  }
   171  func BenchmarkClearFat12(b *testing.B) {
   172  	for i := 0; i < b.N; i++ {
   173  		var x [12 / 4]uint32
   174  		_ = x
   175  	}
   176  }
   177  func BenchmarkClearFat16(b *testing.B) {
   178  	for i := 0; i < b.N; i++ {
   179  		var x [16 / 4]uint32
   180  		_ = x
   181  	}
   182  }
   183  func BenchmarkClearFat24(b *testing.B) {
   184  	for i := 0; i < b.N; i++ {
   185  		var x [24 / 4]uint32
   186  		_ = x
   187  	}
   188  }
   189  func BenchmarkClearFat32(b *testing.B) {
   190  	for i := 0; i < b.N; i++ {
   191  		var x [32 / 4]uint32
   192  		_ = x
   193  	}
   194  }
   195  func BenchmarkClearFat64(b *testing.B) {
   196  	for i := 0; i < b.N; i++ {
   197  		var x [64 / 4]uint32
   198  		_ = x
   199  	}
   200  }
   201  func BenchmarkClearFat128(b *testing.B) {
   202  	for i := 0; i < b.N; i++ {
   203  		var x [128 / 4]uint32
   204  		_ = x
   205  	}
   206  }
   207  func BenchmarkClearFat256(b *testing.B) {
   208  	for i := 0; i < b.N; i++ {
   209  		var x [256 / 4]uint32
   210  		_ = x
   211  	}
   212  }
   213  func BenchmarkClearFat512(b *testing.B) {
   214  	for i := 0; i < b.N; i++ {
   215  		var x [512 / 4]uint32
   216  		_ = x
   217  	}
   218  }
   219  func BenchmarkClearFat1024(b *testing.B) {
   220  	for i := 0; i < b.N; i++ {
   221  		var x [1024 / 4]uint32
   222  		_ = x
   223  	}
   224  }
   225  
   226  func BenchmarkCopyFat8(b *testing.B) {
   227  	var x [8 / 4]uint32
   228  	for i := 0; i < b.N; i++ {
   229  		y := x
   230  		_ = y
   231  	}
   232  }
   233  func BenchmarkCopyFat12(b *testing.B) {
   234  	var x [12 / 4]uint32
   235  	for i := 0; i < b.N; i++ {
   236  		y := x
   237  		_ = y
   238  	}
   239  }
   240  func BenchmarkCopyFat16(b *testing.B) {
   241  	var x [16 / 4]uint32
   242  	for i := 0; i < b.N; i++ {
   243  		y := x
   244  		_ = y
   245  	}
   246  }
   247  func BenchmarkCopyFat24(b *testing.B) {
   248  	var x [24 / 4]uint32
   249  	for i := 0; i < b.N; i++ {
   250  		y := x
   251  		_ = y
   252  	}
   253  }
   254  func BenchmarkCopyFat32(b *testing.B) {
   255  	var x [32 / 4]uint32
   256  	for i := 0; i < b.N; i++ {
   257  		y := x
   258  		_ = y
   259  	}
   260  }
   261  func BenchmarkCopyFat64(b *testing.B) {
   262  	var x [64 / 4]uint32
   263  	for i := 0; i < b.N; i++ {
   264  		y := x
   265  		_ = y
   266  	}
   267  }
   268  func BenchmarkCopyFat128(b *testing.B) {
   269  	var x [128 / 4]uint32
   270  	for i := 0; i < b.N; i++ {
   271  		y := x
   272  		_ = y
   273  	}
   274  }
   275  func BenchmarkCopyFat256(b *testing.B) {
   276  	var x [256 / 4]uint32
   277  	for i := 0; i < b.N; i++ {
   278  		y := x
   279  		_ = y
   280  	}
   281  }
   282  func BenchmarkCopyFat512(b *testing.B) {
   283  	var x [512 / 4]uint32
   284  	for i := 0; i < b.N; i++ {
   285  		y := x
   286  		_ = y
   287  	}
   288  }
   289  func BenchmarkCopyFat1024(b *testing.B) {
   290  	var x [1024 / 4]uint32
   291  	for i := 0; i < b.N; i++ {
   292  		y := x
   293  		_ = y
   294  	}
   295  }