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
5package runtime_test
6
7import (
8	"testing"
9)
10
11func TestMemmove(t *testing.T) {
12	size := 256
13	if testing.Short() {
14		size = 128 + 16
15	}
16	src := make([]byte, size)
17	dst := make([]byte, size)
18	for i := 0; i < size; i++ {
19		src[i] = byte(128 + (i & 127))
20	}
21	for i := 0; i < size; i++ {
22		dst[i] = byte(i & 127)
23	}
24	for n := 0; n <= size; n++ {
25		for x := 0; x <= size-n; x++ { // offset in src
26			for y := 0; y <= size-n; y++ { // offset in dst
27				copy(dst[y:y+n], src[x:x+n])
28				for i := 0; i < y; i++ {
29					if dst[i] != byte(i&127) {
30						t.Fatalf("prefix dst[%d] = %d", i, dst[i])
31					}
32				}
33				for i := y; i < y+n; i++ {
34					if dst[i] != byte(128+((i-y+x)&127)) {
35						t.Fatalf("copied dst[%d] = %d", i, dst[i])
36					}
37					dst[i] = byte(i & 127) // reset dst
38				}
39				for i := y + n; i < size; i++ {
40					if dst[i] != byte(i&127) {
41						t.Fatalf("suffix dst[%d] = %d", i, dst[i])
42					}
43				}
44			}
45		}
46	}
47}
48
49func TestMemmoveAlias(t *testing.T) {
50	size := 256
51	if testing.Short() {
52		size = 128 + 16
53	}
54	buf := make([]byte, size)
55	for i := 0; i < size; i++ {
56		buf[i] = byte(i)
57	}
58	for n := 0; n <= size; n++ {
59		for x := 0; x <= size-n; x++ { // src offset
60			for y := 0; y <= size-n; y++ { // dst offset
61				copy(buf[y:y+n], buf[x:x+n])
62				for i := 0; i < y; i++ {
63					if buf[i] != byte(i) {
64						t.Fatalf("prefix buf[%d] = %d", i, buf[i])
65					}
66				}
67				for i := y; i < y+n; i++ {
68					if buf[i] != byte(i-y+x) {
69						t.Fatalf("copied buf[%d] = %d", i, buf[i])
70					}
71					buf[i] = byte(i) // reset buf
72				}
73				for i := y + n; i < size; i++ {
74					if buf[i] != byte(i) {
75						t.Fatalf("suffix buf[%d] = %d", i, buf[i])
76					}
77				}
78			}
79		}
80	}
81}
82
83func bmMemmove(n int, b *testing.B) {
84	x := make([]byte, n)
85	y := make([]byte, n)
86	b.SetBytes(int64(n))
87	for i := 0; i < b.N; i++ {
88		copy(x, y)
89	}
90}
91
92func BenchmarkMemmove0(b *testing.B)    { bmMemmove(0, b) }
93func BenchmarkMemmove1(b *testing.B)    { bmMemmove(1, b) }
94func BenchmarkMemmove2(b *testing.B)    { bmMemmove(2, b) }
95func BenchmarkMemmove3(b *testing.B)    { bmMemmove(3, b) }
96func BenchmarkMemmove4(b *testing.B)    { bmMemmove(4, b) }
97func BenchmarkMemmove5(b *testing.B)    { bmMemmove(5, b) }
98func BenchmarkMemmove6(b *testing.B)    { bmMemmove(6, b) }
99func BenchmarkMemmove7(b *testing.B)    { bmMemmove(7, b) }
100func BenchmarkMemmove8(b *testing.B)    { bmMemmove(8, b) }
101func BenchmarkMemmove9(b *testing.B)    { bmMemmove(9, b) }
102func BenchmarkMemmove10(b *testing.B)   { bmMemmove(10, b) }
103func BenchmarkMemmove11(b *testing.B)   { bmMemmove(11, b) }
104func BenchmarkMemmove12(b *testing.B)   { bmMemmove(12, b) }
105func BenchmarkMemmove13(b *testing.B)   { bmMemmove(13, b) }
106func BenchmarkMemmove14(b *testing.B)   { bmMemmove(14, b) }
107func BenchmarkMemmove15(b *testing.B)   { bmMemmove(15, b) }
108func BenchmarkMemmove16(b *testing.B)   { bmMemmove(16, b) }
109func BenchmarkMemmove32(b *testing.B)   { bmMemmove(32, b) }
110func BenchmarkMemmove64(b *testing.B)   { bmMemmove(64, b) }
111func BenchmarkMemmove128(b *testing.B)  { bmMemmove(128, b) }
112func BenchmarkMemmove256(b *testing.B)  { bmMemmove(256, b) }
113func BenchmarkMemmove512(b *testing.B)  { bmMemmove(512, b) }
114func BenchmarkMemmove1024(b *testing.B) { bmMemmove(1024, b) }
115func BenchmarkMemmove2048(b *testing.B) { bmMemmove(2048, b) }
116func BenchmarkMemmove4096(b *testing.B) { bmMemmove(4096, b) }
117