Rebase.
[official-gcc.git] / libgo / go / runtime / memmove_test.go
blob540f0feb5495cf0266cbb7824fe3a6c0f326c5fa
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.
5 package runtime_test
7 import (
8 . "runtime"
9 "testing"
12 func TestMemmove(t *testing.T) {
13 size := 256
14 if testing.Short() {
15 size = 128 + 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))
22 for i := 0; i < size; i++ {
23 dst[i] = byte(i & 127)
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])
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])
38 dst[i] = byte(i & 127) // reset dst
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])
50 func TestMemmoveAlias(t *testing.T) {
51 size := 256
52 if testing.Short() {
53 size = 128 + 16
55 buf := make([]byte, size)
56 for i := 0; i < size; i++ {
57 buf[i] = byte(i)
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])
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])
72 buf[i] = byte(i) // reset buf
74 for i := y + n; i < size; i++ {
75 if buf[i] != byte(i) {
76 t.Fatalf("suffix buf[%d] = %d", i, buf[i])
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)
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) }
119 func TestMemclr(t *testing.T) {
120 size := 512
121 if testing.Short() {
122 size = 128 + 16
124 mem := make([]byte, size)
125 for i := 0; i < size; i++ {
126 mem[i] = 0xee
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])
136 for i := x; i < x+n; i++ {
137 if mem[i] != 0 {
138 t.Fatalf("failed clear mem[%d] = %d", i, mem[i])
140 mem[i] = 0xee
142 for i := x + n; i < size; i++ {
143 if mem[i] != 0xee {
144 t.Fatalf("overwrite suffix mem[%d] = %d", i, mem[i])
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)
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) }
165 func BenchmarkClearFat32(b *testing.B) {
166 for i := 0; i < b.N; i++ {
167 var x [32]byte
168 _ = x
171 func BenchmarkClearFat64(b *testing.B) {
172 for i := 0; i < b.N; i++ {
173 var x [64]byte
174 _ = x
177 func BenchmarkClearFat128(b *testing.B) {
178 for i := 0; i < b.N; i++ {
179 var x [128]byte
180 _ = x
183 func BenchmarkClearFat256(b *testing.B) {
184 for i := 0; i < b.N; i++ {
185 var x [256]byte
186 _ = x
189 func BenchmarkClearFat512(b *testing.B) {
190 for i := 0; i < b.N; i++ {
191 var x [512]byte
192 _ = x
195 func BenchmarkClearFat1024(b *testing.B) {
196 for i := 0; i < b.N; i++ {
197 var x [1024]byte
198 _ = x
202 func BenchmarkCopyFat32(b *testing.B) {
203 var x [32 / 4]uint32
204 for i := 0; i < b.N; i++ {
205 y := x
206 _ = y
209 func BenchmarkCopyFat64(b *testing.B) {
210 var x [64 / 4]uint32
211 for i := 0; i < b.N; i++ {
212 y := x
213 _ = y
216 func BenchmarkCopyFat128(b *testing.B) {
217 var x [128 / 4]uint32
218 for i := 0; i < b.N; i++ {
219 y := x
220 _ = y
223 func BenchmarkCopyFat256(b *testing.B) {
224 var x [256 / 4]uint32
225 for i := 0; i < b.N; i++ {
226 y := x
227 _ = y
230 func BenchmarkCopyFat512(b *testing.B) {
231 var x [512 / 4]uint32
232 for i := 0; i < b.N; i++ {
233 y := x
234 _ = y
237 func BenchmarkCopyFat1024(b *testing.B) {
238 var x [1024 / 4]uint32
239 for i := 0; i < b.N; i++ {
240 y := x
241 _ = y