]> Cypherpunks.ru repositories - gostls13.git/blob - test/copy.go
misc cleanup: gofmt + &x -> x[0:] conversion
[gostls13.git] / test / copy.go
1 // $G $F.go && $L $F.$A && ./$A.out
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 // Semi-exhaustive test for copy()
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
27 func u8(i int) uint8 {
28         i = 'a' + i%26
29         return uint8(i)
30 }
31
32 func u16(ii int) uint16 {
33         var i = uint16(ii)
34         i = 'a' + i%26
35         i |= i << 8
36         return i
37 }
38
39 func u32(ii int) uint32 {
40         var i = uint32(ii)
41         i = 'a' + i%26
42         i |= i << 8
43         i |= i << 16
44         return i
45 }
46
47 func u64(ii int) uint64 {
48         var i = uint64(ii)
49         i = 'a' + i%26
50         i |= i << 8
51         i |= i << 16
52         i |= i << 32
53         return i
54 }
55
56 func reset() {
57         // swap in and out to exercise copy-up and copy-down
58         input8, output8 = output8, input8
59         input16, output16 = output16, input16
60         input32, output32 = output32, input32
61         input64, output64 = output64, input64
62         in := 0
63         out := 13
64         for i := range input8 {
65                 input8[i] = u8(in)
66                 output8[i] = u8(out)
67                 input16[i] = u16(in)
68                 output16[i] = u16(out)
69                 input32[i] = u32(in)
70                 output32[i] = u32(out)
71                 input64[i] = u64(in)
72                 output64[i] = u64(out)
73                 in++
74                 out++
75         }
76 }
77
78 func clamp(n int) int {
79         if n > N {
80                 return N
81         }
82         return n
83 }
84
85 func ncopied(length, in, out int) int {
86         n := length
87         if in+n > N {
88                 n = N - in
89         }
90         if out+n > N {
91                 n = N - out
92         }
93         return n
94 }
95
96 func doAllSlices(length, in, out int) {
97         reset()
98         n := copy(output8[out:clamp(out+length)], input8[in:clamp(in+length)])
99         verify8(length, in, out, n)
100         n = copy(output16[out:clamp(out+length)], input16[in:clamp(in+length)])
101         verify16(length, in, out, n)
102         n = copy(output32[out:clamp(out+length)], input32[in:clamp(in+length)])
103         verify32(length, in, out, n)
104         n = copy(output64[out:clamp(out+length)], input64[in:clamp(in+length)])
105         verify64(length, in, out, n)
106 }
107
108 func bad8(state string, i, length, in, out int) {
109         fmt.Printf("%s bad(%d %d %d): %c not %c:\n\t%s\n\t%s\n",
110                 state,
111                 length, in, out,
112                 output8[i],
113                 uint8(i+13),
114                 input8, output8)
115         os.Exit(1)
116 }
117
118 func verify8(length, in, out, m int) {
119         n := ncopied(length, in, out)
120         if m != n {
121                 fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
122                 return
123         }
124         // before
125         var i int
126         for i = 0; i < out; i++ {
127                 if output8[i] != u8(i+13) {
128                         bad8("before8", i, length, in, out)
129                         return
130                 }
131         }
132         // copied part
133         for ; i < out+n; i++ {
134                 if output8[i] != u8(i+in-out) {
135                         bad8("copied8", i, length, in, out)
136                         return
137                 }
138         }
139         // after
140         for ; i < len(output8); i++ {
141                 if output8[i] != u8(i+13) {
142                         bad8("after8", i, length, in, out)
143                         return
144                 }
145         }
146 }
147
148 func bad16(state string, i, length, in, out int) {
149         fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
150                 state,
151                 length, in, out,
152                 output16[i],
153                 uint16(i+13),
154                 input16, output16)
155         os.Exit(1)
156 }
157
158 func verify16(length, in, out, m int) {
159         n := ncopied(length, in, out)
160         if m != n {
161                 fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
162                 return
163         }
164         // before
165         var i int
166         for i = 0; i < out; i++ {
167                 if output16[i] != u16(i+13) {
168                         bad16("before16", i, length, in, out)
169                         return
170                 }
171         }
172         // copied part
173         for ; i < out+n; i++ {
174                 if output16[i] != u16(i+in-out) {
175                         bad16("copied16", i, length, in, out)
176                         return
177                 }
178         }
179         // after
180         for ; i < len(output16); i++ {
181                 if output16[i] != u16(i+13) {
182                         bad16("after16", i, length, in, out)
183                         return
184                 }
185         }
186 }
187
188 func bad32(state string, i, length, in, out int) {
189         fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
190                 state,
191                 length, in, out,
192                 output32[i],
193                 uint32(i+13),
194                 input32, output32)
195         os.Exit(1)
196 }
197
198 func verify32(length, in, out, m int) {
199         n := ncopied(length, in, out)
200         if m != n {
201                 fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
202                 return
203         }
204         // before
205         var i int
206         for i = 0; i < out; i++ {
207                 if output32[i] != u32(i+13) {
208                         bad32("before32", i, length, in, out)
209                         return
210                 }
211         }
212         // copied part
213         for ; i < out+n; i++ {
214                 if output32[i] != u32(i+in-out) {
215                         bad32("copied32", i, length, in, out)
216                         return
217                 }
218         }
219         // after
220         for ; i < len(output32); i++ {
221                 if output32[i] != u32(i+13) {
222                         bad32("after32", i, length, in, out)
223                         return
224                 }
225         }
226 }
227
228 func bad64(state string, i, length, in, out int) {
229         fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
230                 state,
231                 length, in, out,
232                 output64[i],
233                 uint64(i+13),
234                 input64, output64)
235         os.Exit(1)
236 }
237
238 func verify64(length, in, out, m int) {
239         n := ncopied(length, in, out)
240         if m != n {
241                 fmt.Printf("count bad(%d %d %d): %d not %d\n", length, in, out, m, n)
242                 return
243         }
244         // before
245         var i int
246         for i = 0; i < out; i++ {
247                 if output64[i] != u64(i+13) {
248                         bad64("before64", i, length, in, out)
249                         return
250                 }
251         }
252         // copied part
253         for ; i < out+n; i++ {
254                 if output64[i] != u64(i+in-out) {
255                         bad64("copied64", i, length, in, out)
256                         return
257                 }
258         }
259         // after
260         for ; i < len(output64); i++ {
261                 if output64[i] != u64(i+13) {
262                         bad64("after64", i, length, in, out)
263                         return
264                 }
265         }
266 }
267
268 func slice() {
269         for length := 0; length < N; length++ {
270                 for in := 0; in <= 32; in++ {
271                         for out := 0; out <= 32; out++ {
272                                 doAllSlices(length, in, out)
273                         }
274                 }
275         }
276 }
277
278 // Array test. Can be much simpler. It's only checking for correct handling of [0:].
279 func array() {
280         var array [N]uint8
281         reset()
282         copy(array[0:], input8)
283         for i := 0; i < N; i++ {
284                 output8[i] = 0
285         }
286         copy(output8, array[0:])
287         verify8(N, 0, 0, N)
288 }
289
290 func main() {
291         slice()
292         array()
293 }