summaryrefslogtreecommitdiffstats
path: root/test/copy.go
blob: e6108d905d199b62398eedf0f29ab5fd004eccc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// run

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Semi-exhaustive test for the copy predeclared function.

package main

import (
	"fmt"
	"os"
)

const N = 40

var input8 = make([]uint8, N)
var output8 = make([]uint8, N)
var input16 = make([]uint16, N)
var output16 = make([]uint16, N)
var input32 = make([]uint32, N)
var output32 = make([]uint32, N)
var input64 = make([]uint64, N)
var output64 = make([]uint64, N)
var inputS string
var outputS = make([]uint8, N)

type my8 []uint8
type my16 []uint16
type my32 []uint32
type my32b []uint32
type my64 []uint64
type myS string

func u8(i int) uint8 {
	i = 'a' + i%26
	return uint8(i)
}

func u16(ii int) uint16 {
	var i = uint16(ii)
	i = 'a' + i%26
	i |= i << 8
	return i
}

func u32(ii int) uint32 {
	var i = uint32(ii)
	i = 'a' + i%26
	i |= i << 8
	i |= i << 16
	return i
}

func u64(ii int) uint64 {
	var i = uint64(ii)
	i = 'a' + i%26
	i |= i << 8
	i |= i << 16
	i |= i << 32
	return i
}

func reset() {
	// swap in and out to exercise copy-up and copy-down
	input8, output8 = output8, input8
	input16, output16 = output16, input16
	input32, output32 = output32, input32
	input64, output64 = output64, input64
	in := 0
	out := 13
	for i := range input8 {
		input8[i] = u8(in)
		output8[i] = u8(out)
		outputS[i] = u8(out)
		input16[i] = u16(in)
		output16[i] = u16(out)
		input32[i] = u32(in)
		output32[i] = u32(out)
		input64[i] = u64(in)
		output64[i] = u64(out)
		in++
		out++
	}
	inputS = string(input8)
}

func clamp(n int) int {
	if n > N {
		return N
	}
	return n
}

func ncopied(length, in, out int) int {
	n := length
	if in+n > N {
		n = N - in
	}
	if out+n > N {
		n = N - out
	}
	return n
}

func doAllSlices(length, in, out int) {
	reset()
	n := copy(my8(output8[out:clamp(out+length)]), input8[in:clamp(in+length)])
	verify8(length, in, out, n)
	n = copy(my8(outputS[out:clamp(out+length)]), myS(inputS[in:clamp(in+length)]))
	verifyS(length, in, out, n)
	n = copy(my16(output16[out:clamp(out+length)]), input16[in:clamp(in+length)])
	verify16(length, in, out, n)
	n = copy(my32(output32[out:clamp(out+length)]), my32b(input32[in:clamp(in+length)]))
	verify32(length, in, out, n)
	n = copy(my64(output64[out:clamp(out+length)]), input64[in:clamp(in+length)])
	verify64(length, in, out, n)
}

func bad8(state string, i, length, in, out int) {
	fmt.Printf("%s bad(%d %d %d): %c not %c:\n\t%s\n\t%s\n",
		state,
		length, in, out,
		output8[i],
		uint8(i+13),
		input8, output8)
	os.Exit(1)
}

func verify8(length, in, out, m int) {
	n := ncopied(length, in, out)
	if m != n {
		fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
		os.Exit(1)
		return
	}
	// before
	var i int
	for i = 0; i < out; i++ {
		if output8[i] != u8(i+13) {
			bad8("before8", i, length, in, out)
			return
		}
	}
	// copied part
	for ; i < out+n; i++ {
		if output8[i] != u8(i+in-out) {
			bad8("copied8", i, length, in, out)
			return
		}
	}
	// after
	for ; i < len(output8); i++ {
		if output8[i] != u8(i+13) {
			bad8("after8", i, length, in, out)
			return
		}
	}
}

func badS(state string, i, length, in, out int) {
	fmt.Printf("%s bad(%d %d %d): %c not %c:\n\t%s\n\t%s\n",
		state,
		length, in, out,
		outputS[i],
		uint8(i+13),
		inputS, outputS)
	os.Exit(1)
}

func verifyS(length, in, out, m int) {
	n := ncopied(length, in, out)
	if m != n {
		fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
		os.Exit(1)
		return
	}
	// before
	var i int
	for i = 0; i < out; i++ {
		if outputS[i] != u8(i+13) {
			badS("beforeS", i, length, in, out)
			return
		}
	}
	// copied part
	for ; i < out+n; i++ {
		if outputS[i] != u8(i+in-out) {
			badS("copiedS", i, length, in, out)
			return
		}
	}
	// after
	for ; i < len(outputS); i++ {
		if outputS[i] != u8(i+13) {
			badS("afterS", i, length, in, out)
			return
		}
	}
}

func bad16(state string, i, length, in, out int) {
	fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
		state,
		length, in, out,
		output16[i],
		uint16(i+13),
		input16, output16)
	os.Exit(1)
}

func verify16(length, in, out, m int) {
	n := ncopied(length, in, out)
	if m != n {
		fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
		os.Exit(1)
		return
	}
	// before
	var i int
	for i = 0; i < out; i++ {
		if output16[i] != u16(i+13) {
			bad16("before16", i, length, in, out)
			return
		}
	}
	// copied part
	for ; i < out+n; i++ {
		if output16[i] != u16(i+in-out) {
			bad16("copied16", i, length, in, out)
			return
		}
	}
	// after
	for ; i < len(output16); i++ {
		if output16[i] != u16(i+13) {
			bad16("after16", i, length, in, out)
			return
		}
	}
}

func bad32(state string, i, length, in, out int) {
	fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
		state,
		length, in, out,
		output32[i],
		uint32(i+13),
		input32, output32)
	os.Exit(1)
}

func verify32(length, in, out, m int) {
	n := ncopied(length, in, out)
	if m != n {
		fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
		os.Exit(1)
		return
	}
	// before
	var i int
	for i = 0; i < out; i++ {
		if output32[i] != u32(i+13) {
			bad32("before32", i, length, in, out)
			return
		}
	}
	// copied part
	for ; i < out+n; i++ {
		if output32[i] != u32(i+in-out) {
			bad32("copied32", i, length, in, out)
			return
		}
	}
	// after
	for ; i < len(output32); i++ {
		if output32[i] != u32(i+13) {
			bad32("after32", i, length, in, out)
			return
		}
	}
}

func bad64(state string, i, length, in, out int) {
	fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
		state,
		length, in, out,
		output64[i],
		uint64(i+13),
		input64, output64)
	os.Exit(1)
}

func verify64(length, in, out, m int) {
	n := ncopied(length, in, out)
	if m != n {
		fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
		os.Exit(1)
		return
	}
	// before
	var i int
	for i = 0; i < out; i++ {
		if output64[i] != u64(i+13) {
			bad64("before64", i, length, in, out)
			return
		}
	}
	// copied part
	for ; i < out+n; i++ {
		if output64[i] != u64(i+in-out) {
			bad64("copied64", i, length, in, out)
			return
		}
	}
	// after
	for ; i < len(output64); i++ {
		if output64[i] != u64(i+13) {
			bad64("after64", i, length, in, out)
			return
		}
	}
}

func slice() {
	for length := 0; length < N; length++ {
		for in := 0; in <= 32; in++ {
			for out := 0; out <= 32; out++ {
				doAllSlices(length, in, out)
			}
		}
	}
}

// Array test. Can be much simpler. It's only checking for correct handling of [0:].
func array() {
	var array [N]uint8
	reset()
	copy(array[0:], input8)
	for i := 0; i < N; i++ {
		output8[i] = 0
	}
	copy(output8, array[0:])
	verify8(N, 0, 0, N)
}

func main() {
	slice()
	array()
}