]> Cypherpunks.ru repositories - gostls13.git/blob - test/copy.go
test for copy()
[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         in := 0;
58         out := 13;
59         for i := range input8 {
60                 input8[i] = u8(in);
61                 output8[i] = u8(out);
62                 input16[i] = u16(in);
63                 output16[i] = u16(out);
64                 input32[i] = u32(in);
65                 output32[i] = u32(out);
66                 input64[i] = u64(in);
67                 output64[i] = u64(out);
68                 in++;
69                 out++;
70         }
71 }
72
73 func clamp(n int) int {
74         if n > N {
75                 return N
76         }
77         return n;
78 }
79
80 func doAllSlices(length, in, out int) {
81         reset();
82         copy(output8[out:clamp(out+length)], input8[in:clamp(in+length)]);
83         verify8(length, in, out);
84         copy(output16[out:clamp(out+length)], input16[in:clamp(in+length)]);
85         verify16(length, in, out);
86         copy(output32[out:clamp(out+length)], input32[in:clamp(in+length)]);
87         verify32(length, in, out);
88         copy(output64[out:clamp(out+length)], input64[in:clamp(in+length)]);
89         verify64(length, in, out);
90 }
91
92 func bad8(state string, i, length, in, out int) {
93         fmt.Printf("%s bad(%d %d %d): %c not %c:\n\t%s\n\t%s\n",
94                 state,
95                 length, in, out,
96                 output8[i],
97                 uint8(i+13),
98                 input8, output8);
99         os.Exit(1);
100 }
101
102 func verify8(length, in, out int) {
103         for i := 0; i < out; i++ {
104                 if output8[i] != u8(i+13) {
105                         bad8("preamble8", i, length, in, out);
106                         break;
107                 }
108         }
109 }
110
111 func bad16(state string, i, length, in, out int) {
112         fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
113                 state,
114                 length, in, out,
115                 output16[i],
116                 uint16(i+13),
117                 input16, output16);
118         os.Exit(1);
119 }
120
121 func verify16(length, in, out int) {
122         for i := 0; i < out; i++ {
123                 if output16[i] != u16(i+13) {
124                         bad16("preamble16", i, length, in, out);
125                         break;
126                 }
127         }
128 }
129
130 func bad32(state string, i, length, in, out int) {
131         fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
132                 state,
133                 length, in, out,
134                 output32[i],
135                 uint32(i+13),
136                 input32, output32);
137         os.Exit(1);
138 }
139
140 func verify32(length, in, out int) {
141         for i := 0; i < out; i++ {
142                 if output32[i] != u32(i+13) {
143                         bad32("preamble32", i, length, in, out);
144                         break;
145                 }
146         }
147 }
148
149 func bad64(state string, i, length, in, out int) {
150         fmt.Printf("%s bad(%d %d %d): %x not %x:\n\t%v\n\t%v\n",
151                 state,
152                 length, in, out,
153                 output64[i],
154                 uint64(i+13),
155                 input64, output64);
156         os.Exit(1);
157 }
158
159 func verify64(length, in, out int) {
160         for i := 0; i < out; i++ {
161                 if output64[i] != u64(i+13) {
162                         bad64("preamble64", i, length, in, out);
163                         break;
164                 }
165         }
166 }
167
168 func slice() {
169         for length := 0; length < N; length++ {
170                 for in := 0; in <= 32; in++ {
171                         for out := 0; out <= 32; out++ {
172                                 doAllSlices(length, in, out)
173                         }
174                 }
175         }
176 }
177
178 // Array test. Can be much simpler. It's mostly checking for promotion of *[N] to []
179 func array() {
180         var array [N]uint8;
181         reset();
182         copy(&array, input8);
183         for i := 0; i < N; i++ {
184                 output8[i] = 0
185         }
186         copy(output8, &array);
187         verify8(N, 0, 0);
188 }
189
190 func main() {
191         slice();
192         array();
193 }