1 |
700 |
jeremybenn |
// $G $F.go && $L $F.$A && ./$A.out
|
2 |
|
|
|
3 |
|
|
// Copyright 2010 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 append()
|
8 |
|
|
|
9 |
|
|
package main
|
10 |
|
|
|
11 |
|
|
import (
|
12 |
|
|
"fmt"
|
13 |
|
|
"reflect"
|
14 |
|
|
)
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
func verify(name string, result, expected interface{}) {
|
18 |
|
|
if !reflect.DeepEqual(result, expected) {
|
19 |
|
|
panic(name)
|
20 |
|
|
}
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
func main() {
|
25 |
|
|
for _, t := range tests {
|
26 |
|
|
verify(t.name, t.result, t.expected)
|
27 |
|
|
}
|
28 |
|
|
verifyStruct()
|
29 |
|
|
verifyInterface()
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
var tests = []struct {
|
34 |
|
|
name string
|
35 |
|
|
result, expected interface{}
|
36 |
|
|
}{
|
37 |
|
|
{"bool a", append([]bool{}), []bool{}},
|
38 |
|
|
{"bool b", append([]bool{}, true), []bool{true}},
|
39 |
|
|
{"bool c", append([]bool{}, true, false, true, true), []bool{true, false, true, true}},
|
40 |
|
|
|
41 |
|
|
{"bool d", append([]bool{true, false, true}), []bool{true, false, true}},
|
42 |
|
|
{"bool e", append([]bool{true, false, true}, false), []bool{true, false, true, false}},
|
43 |
|
|
{"bool f", append([]bool{true, false, true}, false, false, false), []bool{true, false, true, false, false, false}},
|
44 |
|
|
|
45 |
|
|
{"bool g", append([]bool{}, []bool{true}...), []bool{true}},
|
46 |
|
|
{"bool h", append([]bool{}, []bool{true, false, true, false}...), []bool{true, false, true, false}},
|
47 |
|
|
|
48 |
|
|
{"bool i", append([]bool{true, false, true}, []bool{true}...), []bool{true, false, true, true}},
|
49 |
|
|
{"bool j", append([]bool{true, false, true}, []bool{true, true, true}...), []bool{true, false, true, true, true, true}},
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
{"byte a", append([]byte{}), []byte{}},
|
53 |
|
|
{"byte b", append([]byte{}, 0), []byte{0}},
|
54 |
|
|
{"byte c", append([]byte{}, 0, 1, 2, 3), []byte{0, 1, 2, 3}},
|
55 |
|
|
|
56 |
|
|
{"byte d", append([]byte{0, 1, 2}), []byte{0, 1, 2}},
|
57 |
|
|
{"byte e", append([]byte{0, 1, 2}, 3), []byte{0, 1, 2, 3}},
|
58 |
|
|
{"byte f", append([]byte{0, 1, 2}, 3, 4, 5), []byte{0, 1, 2, 3, 4, 5}},
|
59 |
|
|
|
60 |
|
|
{"byte g", append([]byte{}, []byte{0}...), []byte{0}},
|
61 |
|
|
{"byte h", append([]byte{}, []byte{0, 1, 2, 3}...), []byte{0, 1, 2, 3}},
|
62 |
|
|
|
63 |
|
|
{"byte i", append([]byte{0, 1, 2}, []byte{3}...), []byte{0, 1, 2, 3}},
|
64 |
|
|
{"byte j", append([]byte{0, 1, 2}, []byte{3, 4, 5}...), []byte{0, 1, 2, 3, 4, 5}},
|
65 |
|
|
|
66 |
|
|
{"bytestr a", append([]byte{}, "0"...), []byte("0")},
|
67 |
|
|
{"bytestr b", append([]byte{}, "0123"...), []byte("0123")},
|
68 |
|
|
|
69 |
|
|
{"bytestr c", append([]byte("012"), "3"...), []byte("0123")},
|
70 |
|
|
{"bytestr d", append([]byte("012"), "345"...), []byte("012345")},
|
71 |
|
|
|
72 |
|
|
{"int16 a", append([]int16{}), []int16{}},
|
73 |
|
|
{"int16 b", append([]int16{}, 0), []int16{0}},
|
74 |
|
|
{"int16 c", append([]int16{}, 0, 1, 2, 3), []int16{0, 1, 2, 3}},
|
75 |
|
|
|
76 |
|
|
{"int16 d", append([]int16{0, 1, 2}), []int16{0, 1, 2}},
|
77 |
|
|
{"int16 e", append([]int16{0, 1, 2}, 3), []int16{0, 1, 2, 3}},
|
78 |
|
|
{"int16 f", append([]int16{0, 1, 2}, 3, 4, 5), []int16{0, 1, 2, 3, 4, 5}},
|
79 |
|
|
|
80 |
|
|
{"int16 g", append([]int16{}, []int16{0}...), []int16{0}},
|
81 |
|
|
{"int16 h", append([]int16{}, []int16{0, 1, 2, 3}...), []int16{0, 1, 2, 3}},
|
82 |
|
|
|
83 |
|
|
{"int16 i", append([]int16{0, 1, 2}, []int16{3}...), []int16{0, 1, 2, 3}},
|
84 |
|
|
{"int16 j", append([]int16{0, 1, 2}, []int16{3, 4, 5}...), []int16{0, 1, 2, 3, 4, 5}},
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
{"uint32 a", append([]uint32{}), []uint32{}},
|
88 |
|
|
{"uint32 b", append([]uint32{}, 0), []uint32{0}},
|
89 |
|
|
{"uint32 c", append([]uint32{}, 0, 1, 2, 3), []uint32{0, 1, 2, 3}},
|
90 |
|
|
|
91 |
|
|
{"uint32 d", append([]uint32{0, 1, 2}), []uint32{0, 1, 2}},
|
92 |
|
|
{"uint32 e", append([]uint32{0, 1, 2}, 3), []uint32{0, 1, 2, 3}},
|
93 |
|
|
{"uint32 f", append([]uint32{0, 1, 2}, 3, 4, 5), []uint32{0, 1, 2, 3, 4, 5}},
|
94 |
|
|
|
95 |
|
|
{"uint32 g", append([]uint32{}, []uint32{0}...), []uint32{0}},
|
96 |
|
|
{"uint32 h", append([]uint32{}, []uint32{0, 1, 2, 3}...), []uint32{0, 1, 2, 3}},
|
97 |
|
|
|
98 |
|
|
{"uint32 i", append([]uint32{0, 1, 2}, []uint32{3}...), []uint32{0, 1, 2, 3}},
|
99 |
|
|
{"uint32 j", append([]uint32{0, 1, 2}, []uint32{3, 4, 5}...), []uint32{0, 1, 2, 3, 4, 5}},
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
{"float64 a", append([]float64{}), []float64{}},
|
103 |
|
|
{"float64 b", append([]float64{}, 0), []float64{0}},
|
104 |
|
|
{"float64 c", append([]float64{}, 0, 1, 2, 3), []float64{0, 1, 2, 3}},
|
105 |
|
|
|
106 |
|
|
{"float64 d", append([]float64{0, 1, 2}), []float64{0, 1, 2}},
|
107 |
|
|
{"float64 e", append([]float64{0, 1, 2}, 3), []float64{0, 1, 2, 3}},
|
108 |
|
|
{"float64 f", append([]float64{0, 1, 2}, 3, 4, 5), []float64{0, 1, 2, 3, 4, 5}},
|
109 |
|
|
|
110 |
|
|
{"float64 g", append([]float64{}, []float64{0}...), []float64{0}},
|
111 |
|
|
{"float64 h", append([]float64{}, []float64{0, 1, 2, 3}...), []float64{0, 1, 2, 3}},
|
112 |
|
|
|
113 |
|
|
{"float64 i", append([]float64{0, 1, 2}, []float64{3}...), []float64{0, 1, 2, 3}},
|
114 |
|
|
{"float64 j", append([]float64{0, 1, 2}, []float64{3, 4, 5}...), []float64{0, 1, 2, 3, 4, 5}},
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
{"complex128 a", append([]complex128{}), []complex128{}},
|
118 |
|
|
{"complex128 b", append([]complex128{}, 0), []complex128{0}},
|
119 |
|
|
{"complex128 c", append([]complex128{}, 0, 1, 2, 3), []complex128{0, 1, 2, 3}},
|
120 |
|
|
|
121 |
|
|
{"complex128 d", append([]complex128{0, 1, 2}), []complex128{0, 1, 2}},
|
122 |
|
|
{"complex128 e", append([]complex128{0, 1, 2}, 3), []complex128{0, 1, 2, 3}},
|
123 |
|
|
{"complex128 f", append([]complex128{0, 1, 2}, 3, 4, 5), []complex128{0, 1, 2, 3, 4, 5}},
|
124 |
|
|
|
125 |
|
|
{"complex128 g", append([]complex128{}, []complex128{0}...), []complex128{0}},
|
126 |
|
|
{"complex128 h", append([]complex128{}, []complex128{0, 1, 2, 3}...), []complex128{0, 1, 2, 3}},
|
127 |
|
|
|
128 |
|
|
{"complex128 i", append([]complex128{0, 1, 2}, []complex128{3}...), []complex128{0, 1, 2, 3}},
|
129 |
|
|
{"complex128 j", append([]complex128{0, 1, 2}, []complex128{3, 4, 5}...), []complex128{0, 1, 2, 3, 4, 5}},
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
{"string a", append([]string{}), []string{}},
|
133 |
|
|
{"string b", append([]string{}, "0"), []string{"0"}},
|
134 |
|
|
{"string c", append([]string{}, "0", "1", "2", "3"), []string{"0", "1", "2", "3"}},
|
135 |
|
|
|
136 |
|
|
{"string d", append([]string{"0", "1", "2"}), []string{"0", "1", "2"}},
|
137 |
|
|
{"string e", append([]string{"0", "1", "2"}, "3"), []string{"0", "1", "2", "3"}},
|
138 |
|
|
{"string f", append([]string{"0", "1", "2"}, "3", "4", "5"), []string{"0", "1", "2", "3", "4", "5"}},
|
139 |
|
|
|
140 |
|
|
{"string g", append([]string{}, []string{"0"}...), []string{"0"}},
|
141 |
|
|
{"string h", append([]string{}, []string{"0", "1", "2", "3"}...), []string{"0", "1", "2", "3"}},
|
142 |
|
|
|
143 |
|
|
{"string i", append([]string{"0", "1", "2"}, []string{"3"}...), []string{"0", "1", "2", "3"}},
|
144 |
|
|
{"string j", append([]string{"0", "1", "2"}, []string{"3", "4", "5"}...), []string{"0", "1", "2", "3", "4", "5"}},
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
func verifyStruct() {
|
149 |
|
|
type T struct {
|
150 |
|
|
a, b, c string
|
151 |
|
|
}
|
152 |
|
|
type S []T
|
153 |
|
|
e := make(S, 100)
|
154 |
|
|
for i := range e {
|
155 |
|
|
e[i] = T{"foo", fmt.Sprintf("%d", i), "bar"}
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
verify("struct a", append(S{}), S{})
|
159 |
|
|
verify("struct b", append(S{}, e[0]), e[0:1])
|
160 |
|
|
verify("struct c", append(S{}, e[0], e[1], e[2]), e[0:3])
|
161 |
|
|
|
162 |
|
|
verify("struct d", append(e[0:1]), e[0:1])
|
163 |
|
|
verify("struct e", append(e[0:1], e[1]), e[0:2])
|
164 |
|
|
verify("struct f", append(e[0:1], e[1], e[2], e[3]), e[0:4])
|
165 |
|
|
|
166 |
|
|
verify("struct g", append(e[0:3]), e[0:3])
|
167 |
|
|
verify("struct h", append(e[0:3], e[3]), e[0:4])
|
168 |
|
|
verify("struct i", append(e[0:3], e[3], e[4], e[5], e[6]), e[0:7])
|
169 |
|
|
|
170 |
|
|
for i := range e {
|
171 |
|
|
verify("struct j", append(S{}, e[0:i]...), e[0:i])
|
172 |
|
|
input := make(S, i)
|
173 |
|
|
copy(input, e[0:i])
|
174 |
|
|
verify("struct k", append(input, e[i:]...), e)
|
175 |
|
|
verify("struct k - input modified", input, e[0:i])
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
s := make(S, 10, 20)
|
179 |
|
|
r := make(S, len(s)+len(e))
|
180 |
|
|
for i, x := range e {
|
181 |
|
|
r[len(s)+i] = x
|
182 |
|
|
}
|
183 |
|
|
verify("struct l", append(s), s)
|
184 |
|
|
verify("struct m", append(s, e...), r)
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
|
188 |
|
|
func verifyInterface() {
|
189 |
|
|
type T interface{}
|
190 |
|
|
type S []T
|
191 |
|
|
e := make(S, 100)
|
192 |
|
|
for i := range e {
|
193 |
|
|
switch i % 4 {
|
194 |
|
|
case 0:
|
195 |
|
|
e[i] = i
|
196 |
|
|
case 1:
|
197 |
|
|
e[i] = "foo"
|
198 |
|
|
case 2:
|
199 |
|
|
e[i] = fmt.Sprintf("%d", i)
|
200 |
|
|
case 3:
|
201 |
|
|
e[i] = float64(i)
|
202 |
|
|
}
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
verify("interface a", append(S{}), S{})
|
206 |
|
|
verify("interface b", append(S{}, e[0]), e[0:1])
|
207 |
|
|
verify("interface c", append(S{}, e[0], e[1], e[2]), e[0:3])
|
208 |
|
|
|
209 |
|
|
verify("interface d", append(e[0:1]), e[0:1])
|
210 |
|
|
verify("interface e", append(e[0:1], e[1]), e[0:2])
|
211 |
|
|
verify("interface f", append(e[0:1], e[1], e[2], e[3]), e[0:4])
|
212 |
|
|
|
213 |
|
|
verify("interface g", append(e[0:3]), e[0:3])
|
214 |
|
|
verify("interface h", append(e[0:3], e[3]), e[0:4])
|
215 |
|
|
verify("interface i", append(e[0:3], e[3], e[4], e[5], e[6]), e[0:7])
|
216 |
|
|
|
217 |
|
|
for i := range e {
|
218 |
|
|
verify("interface j", append(S{}, e[0:i]...), e[0:i])
|
219 |
|
|
input := make(S, i)
|
220 |
|
|
copy(input, e[0:i])
|
221 |
|
|
verify("interface k", append(input, e[i:]...), e)
|
222 |
|
|
verify("interface k - input modified", input, e[0:i])
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
s := make(S, 10, 20)
|
226 |
|
|
r := make(S, len(s)+len(e))
|
227 |
|
|
for i, x := range e {
|
228 |
|
|
r[len(s)+i] = x
|
229 |
|
|
}
|
230 |
|
|
verify("interface l", append(s), s)
|
231 |
|
|
verify("interface m", append(s, e...), r)
|
232 |
|
|
}
|