Source file
test/copy.go
1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "fmt"
13 "os"
14 )
15
16 const N = 40
17
18 var input8 = make([]uint8, N)
19 var output8 = make([]uint8, N)
20 var input16 = make([]uint16, N)
21 var output16 = make([]uint16, N)
22 var input32 = make([]uint32, N)
23 var output32 = make([]uint32, N)
24 var input64 = make([]uint64, N)
25 var output64 = make([]uint64, N)
26 var inputS string
27 var outputS = make([]uint8, N)
28
29 type my8 []uint8
30 type my16 []uint16
31 type my32 []uint32
32 type my32b []uint32
33 type my64 []uint64
34 type myS string
35
36 func u8(i int) uint8 {
37 i = 'a' + i%26
38 return uint8(i)
39 }
40
41 func u16(ii int) uint16 {
42 var i = uint16(ii)
43 i = 'a' + i%26
44 i |= i << 8
45 return i
46 }
47
48 func u32(ii int) uint32 {
49 var i = uint32(ii)
50 i = 'a' + i%26
51 i |= i << 8
52 i |= i << 16
53 return i
54 }
55
56 func u64(ii int) uint64 {
57 var i = uint64(ii)
58 i = 'a' + i%26
59 i |= i << 8
60 i |= i << 16
61 i |= i << 32
62 return i
63 }
64
65 func reset() {
66
67 input8, output8 = output8, input8
68 input16, output16 = output16, input16
69 input32, output32 = output32, input32
70 input64, output64 = output64, input64
71 in := 0
72 out := 13
73 for i := range input8 {
74 input8[i] = u8(in)
75 output8[i] = u8(out)
76 outputS[i] = u8(out)
77 input16[i] = u16(in)
78 output16[i] = u16(out)
79 input32[i] = u32(in)
80 output32[i] = u32(out)
81 input64[i] = u64(in)
82 output64[i] = u64(out)
83 in++
84 out++
85 }
86 inputS = string(input8)
87 }
88
89 func clamp(n int) int {
90 if n > N {
91 return N
92 }
93 return n
94 }
95
96 func ncopied(length, in, out int) int {
97 n := length
98 if in+n > N {
99 n = N - in
100 }
101 if out+n > N {
102 n = N - out
103 }
104 return n
105 }
106
107 func doAllSlices(length, in, out int) {
108 reset()
109 n := copy(my8(output8[out:clamp(out+length)]), input8[in:clamp(in+length)])
110 verify8(length, in, out, n)
111 n = copy(my8(outputS[out:clamp(out+length)]), myS(inputS[in:clamp(in+length)]))
112 verifyS(length, in, out, n)
113 n = copy(my16(output16[out:clamp(out+length)]), input16[in:clamp(in+length)])
114 verify16(length, in, out, n)
115 n = copy(my32(output32[out:clamp(out+length)]), my32b(input32[in:clamp(in+length)]))
116 verify32(length, in, out, n)
117 n = copy(my64(output64[out:clamp(out+length)]), input64[in:clamp(in+length)])
118 verify64(length, in, out, n)
119 }
120
121 func bad8(state string, i, length, in, out int) {
122 fmt.Printf("%s bad(%d %d %d): %c not %c:\n\t%s\n\t%s\n",
123 state,
124 length, in, out,
125 output8[i],
126 uint8(i+13),
127 input8, output8)
128 os.Exit(1)
129 }
130
131 func verify8(length, in, out, m int) {
132 n := ncopied(length, in, out)
133 if m != n {
134 fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
135 os.Exit(1)
136 return
137 }
138
139 var i int
140 for i = 0; i < out; i++ {
141 if output8[i] != u8(i+13) {
142 bad8("before8", i, length, in, out)
143 return
144 }
145 }
146
147 for ; i < out+n; i++ {
148 if output8[i] != u8(i+in-out) {
149 bad8("copied8", i, length, in, out)
150 return
151 }
152 }
153
154 for ; i < len(output8); i++ {
155 if output8[i] != u8(i+13) {
156 bad8("after8", i, length, in, out)
157 return
158 }
159 }
160 }
161
162 func badS(state string, i, length, in, out int) {
163 fmt.Printf("%s bad(%d %d %d): %c not %c:\n\t%s\n\t%s\n",
164 state,
165 length, in, out,
166 outputS[i],
167 uint8(i+13),
168 inputS, outputS)
169 os.Exit(1)
170 }
171
172 func verifyS(length, in, out, m int) {
173 n := ncopied(length, in, out)
174 if m != n {
175 fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
176 os.Exit(1)
177 return
178 }
179
180 var i int
181 for i = 0; i < out; i++ {
182 if outputS[i] != u8(i+13) {
183 badS("beforeS", i, length, in, out)
184 return
185 }
186 }
187
188 for ; i < out+n; i++ {
189 if outputS[i] != u8(i+in-out) {
190 badS("copiedS", i, length, in, out)
191 return
192 }
193 }
194
195 for ; i < len(outputS); i++ {
196 if outputS[i] != u8(i+13) {
197 badS("afterS", i, length, in, out)
198 return
199 }
200 }
201 }
202
203 func bad16(state string, i, length, in, out int) {
204 fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
205 state,
206 length, in, out,
207 output16[i],
208 uint16(i+13),
209 input16, output16)
210 os.Exit(1)
211 }
212
213 func verify16(length, in, out, m int) {
214 n := ncopied(length, in, out)
215 if m != n {
216 fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
217 os.Exit(1)
218 return
219 }
220
221 var i int
222 for i = 0; i < out; i++ {
223 if output16[i] != u16(i+13) {
224 bad16("before16", i, length, in, out)
225 return
226 }
227 }
228
229 for ; i < out+n; i++ {
230 if output16[i] != u16(i+in-out) {
231 bad16("copied16", i, length, in, out)
232 return
233 }
234 }
235
236 for ; i < len(output16); i++ {
237 if output16[i] != u16(i+13) {
238 bad16("after16", i, length, in, out)
239 return
240 }
241 }
242 }
243
244 func bad32(state string, i, length, in, out int) {
245 fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
246 state,
247 length, in, out,
248 output32[i],
249 uint32(i+13),
250 input32, output32)
251 os.Exit(1)
252 }
253
254 func verify32(length, in, out, m int) {
255 n := ncopied(length, in, out)
256 if m != n {
257 fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
258 os.Exit(1)
259 return
260 }
261
262 var i int
263 for i = 0; i < out; i++ {
264 if output32[i] != u32(i+13) {
265 bad32("before32", i, length, in, out)
266 return
267 }
268 }
269
270 for ; i < out+n; i++ {
271 if output32[i] != u32(i+in-out) {
272 bad32("copied32", i, length, in, out)
273 return
274 }
275 }
276
277 for ; i < len(output32); i++ {
278 if output32[i] != u32(i+13) {
279 bad32("after32", i, length, in, out)
280 return
281 }
282 }
283 }
284
285 func bad64(state string, i, length, in, out int) {
286 fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
287 state,
288 length, in, out,
289 output64[i],
290 uint64(i+13),
291 input64, output64)
292 os.Exit(1)
293 }
294
295 func verify64(length, in, out, m int) {
296 n := ncopied(length, in, out)
297 if m != n {
298 fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
299 os.Exit(1)
300 return
301 }
302
303 var i int
304 for i = 0; i < out; i++ {
305 if output64[i] != u64(i+13) {
306 bad64("before64", i, length, in, out)
307 return
308 }
309 }
310
311 for ; i < out+n; i++ {
312 if output64[i] != u64(i+in-out) {
313 bad64("copied64", i, length, in, out)
314 return
315 }
316 }
317
318 for ; i < len(output64); i++ {
319 if output64[i] != u64(i+13) {
320 bad64("after64", i, length, in, out)
321 return
322 }
323 }
324 }
325
326 func slice() {
327 for length := 0; length < N; length++ {
328 for in := 0; in <= 32; in++ {
329 for out := 0; out <= 32; out++ {
330 doAllSlices(length, in, out)
331 }
332 }
333 }
334 }
335
336
337 func array() {
338 var array [N]uint8
339 reset()
340 copy(array[0:], input8)
341 for i := 0; i < N; i++ {
342 output8[i] = 0
343 }
344 copy(output8, array[0:])
345 verify8(N, 0, 0, N)
346 }
347
348 func main() {
349 slice()
350 array()
351 }
352
View as plain text