Skip to content

Commit e79ebb0

Browse files
committed
test for append() built-in
R=r, rsc CC=golang-dev https://golang.org/cl/2777041
1 parent 69c4e93 commit e79ebb0

File tree

1 file changed

+227
-0
lines changed

1 file changed

+227
-0
lines changed

test/append.go

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
// $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+
67+
{"int16 a", append([]int16{}), []int16{}},
68+
{"int16 b", append([]int16{}, 0), []int16{0}},
69+
{"int16 c", append([]int16{}, 0, 1, 2, 3), []int16{0, 1, 2, 3}},
70+
71+
{"int16 d", append([]int16{0, 1, 2}), []int16{0, 1, 2}},
72+
{"int16 e", append([]int16{0, 1, 2}, 3), []int16{0, 1, 2, 3}},
73+
{"int16 f", append([]int16{0, 1, 2}, 3, 4, 5), []int16{0, 1, 2, 3, 4, 5}},
74+
75+
{"int16 g", append([]int16{}, []int16{0}...), []int16{0}},
76+
{"int16 h", append([]int16{}, []int16{0, 1, 2, 3}...), []int16{0, 1, 2, 3}},
77+
78+
{"int16 i", append([]int16{0, 1, 2}, []int16{3}...), []int16{0, 1, 2, 3}},
79+
{"int16 j", append([]int16{0, 1, 2}, []int16{3, 4, 5}...), []int16{0, 1, 2, 3, 4, 5}},
80+
81+
82+
{"uint32 a", append([]uint32{}), []uint32{}},
83+
{"uint32 b", append([]uint32{}, 0), []uint32{0}},
84+
{"uint32 c", append([]uint32{}, 0, 1, 2, 3), []uint32{0, 1, 2, 3}},
85+
86+
{"uint32 d", append([]uint32{0, 1, 2}), []uint32{0, 1, 2}},
87+
{"uint32 e", append([]uint32{0, 1, 2}, 3), []uint32{0, 1, 2, 3}},
88+
{"uint32 f", append([]uint32{0, 1, 2}, 3, 4, 5), []uint32{0, 1, 2, 3, 4, 5}},
89+
90+
{"uint32 g", append([]uint32{}, []uint32{0}...), []uint32{0}},
91+
{"uint32 h", append([]uint32{}, []uint32{0, 1, 2, 3}...), []uint32{0, 1, 2, 3}},
92+
93+
{"uint32 i", append([]uint32{0, 1, 2}, []uint32{3}...), []uint32{0, 1, 2, 3}},
94+
{"uint32 j", append([]uint32{0, 1, 2}, []uint32{3, 4, 5}...), []uint32{0, 1, 2, 3, 4, 5}},
95+
96+
97+
{"float64 a", append([]float64{}), []float64{}},
98+
{"float64 b", append([]float64{}, 0), []float64{0}},
99+
{"float64 c", append([]float64{}, 0, 1, 2, 3), []float64{0, 1, 2, 3}},
100+
101+
{"float64 d", append([]float64{0, 1, 2}), []float64{0, 1, 2}},
102+
{"float64 e", append([]float64{0, 1, 2}, 3), []float64{0, 1, 2, 3}},
103+
{"float64 f", append([]float64{0, 1, 2}, 3, 4, 5), []float64{0, 1, 2, 3, 4, 5}},
104+
105+
{"float64 g", append([]float64{}, []float64{0}...), []float64{0}},
106+
{"float64 h", append([]float64{}, []float64{0, 1, 2, 3}...), []float64{0, 1, 2, 3}},
107+
108+
{"float64 i", append([]float64{0, 1, 2}, []float64{3}...), []float64{0, 1, 2, 3}},
109+
{"float64 j", append([]float64{0, 1, 2}, []float64{3, 4, 5}...), []float64{0, 1, 2, 3, 4, 5}},
110+
111+
112+
{"complex128 a", append([]complex128{}), []complex128{}},
113+
{"complex128 b", append([]complex128{}, 0), []complex128{0}},
114+
{"complex128 c", append([]complex128{}, 0, 1, 2, 3), []complex128{0, 1, 2, 3}},
115+
116+
{"complex128 d", append([]complex128{0, 1, 2}), []complex128{0, 1, 2}},
117+
{"complex128 e", append([]complex128{0, 1, 2}, 3), []complex128{0, 1, 2, 3}},
118+
{"complex128 f", append([]complex128{0, 1, 2}, 3, 4, 5), []complex128{0, 1, 2, 3, 4, 5}},
119+
120+
{"complex128 g", append([]complex128{}, []complex128{0}...), []complex128{0}},
121+
{"complex128 h", append([]complex128{}, []complex128{0, 1, 2, 3}...), []complex128{0, 1, 2, 3}},
122+
123+
{"complex128 i", append([]complex128{0, 1, 2}, []complex128{3}...), []complex128{0, 1, 2, 3}},
124+
{"complex128 j", append([]complex128{0, 1, 2}, []complex128{3, 4, 5}...), []complex128{0, 1, 2, 3, 4, 5}},
125+
126+
127+
{"string a", append([]string{}), []string{}},
128+
{"string b", append([]string{}, "0"), []string{"0"}},
129+
{"string c", append([]string{}, "0", "1", "2", "3"), []string{"0", "1", "2", "3"}},
130+
131+
{"string d", append([]string{"0", "1", "2"}), []string{"0", "1", "2"}},
132+
{"string e", append([]string{"0", "1", "2"}, "3"), []string{"0", "1", "2", "3"}},
133+
{"string f", append([]string{"0", "1", "2"}, "3", "4", "5"), []string{"0", "1", "2", "3", "4", "5"}},
134+
135+
{"string g", append([]string{}, []string{"0"}...), []string{"0"}},
136+
{"string h", append([]string{}, []string{"0", "1", "2", "3"}...), []string{"0", "1", "2", "3"}},
137+
138+
{"string i", append([]string{"0", "1", "2"}, []string{"3"}...), []string{"0", "1", "2", "3"}},
139+
{"string j", append([]string{"0", "1", "2"}, []string{"3", "4", "5"}...), []string{"0", "1", "2", "3", "4", "5"}},
140+
}
141+
142+
143+
func verifyStruct() {
144+
type T struct {
145+
a, b, c string
146+
}
147+
type S []T
148+
e := make(S, 100)
149+
for i := range e {
150+
e[i] = T{"foo", fmt.Sprintf("%d", i), "bar"}
151+
}
152+
153+
verify("struct a", append(S{}), S{})
154+
verify("struct b", append(S{}, e[0]), e[0:1])
155+
verify("struct c", append(S{}, e[0], e[1], e[2]), e[0:3])
156+
157+
verify("struct d", append(e[0:1]), e[0:1])
158+
verify("struct e", append(e[0:1], e[1]), e[0:2])
159+
verify("struct f", append(e[0:1], e[1], e[2], e[3]), e[0:4])
160+
161+
verify("struct g", append(e[0:3]), e[0:3])
162+
verify("struct h", append(e[0:3], e[3]), e[0:4])
163+
verify("struct i", append(e[0:3], e[3], e[4], e[5], e[6]), e[0:7])
164+
165+
for i := range e {
166+
verify("struct j", append(S{}, e[0:i]...), e[0:i])
167+
input := make(S, i)
168+
copy(input, e[0:i])
169+
verify("struct k", append(input, e[i:]...), e)
170+
verify("struct k - input modified", input, e[0:i])
171+
}
172+
173+
s := make(S, 10, 20)
174+
r := make(S, len(s)+len(e))
175+
for i, x := range e {
176+
r[len(s)+i] = x
177+
}
178+
verify("struct l", append(s), s)
179+
verify("struct m", append(s, e...), r)
180+
}
181+
182+
183+
func verifyInterface() {
184+
type T interface{}
185+
type S []T
186+
e := make(S, 100)
187+
for i := range e {
188+
switch i % 4 {
189+
case 0:
190+
e[i] = i
191+
case 1:
192+
e[i] = "foo"
193+
case 2:
194+
e[i] = fmt.Sprintf("%d", i)
195+
case 3:
196+
e[i] = float(i)
197+
}
198+
}
199+
200+
verify("interface a", append(S{}), S{})
201+
verify("interface b", append(S{}, e[0]), e[0:1])
202+
verify("interface c", append(S{}, e[0], e[1], e[2]), e[0:3])
203+
204+
verify("interface d", append(e[0:1]), e[0:1])
205+
verify("interface e", append(e[0:1], e[1]), e[0:2])
206+
verify("interface f", append(e[0:1], e[1], e[2], e[3]), e[0:4])
207+
208+
verify("interface g", append(e[0:3]), e[0:3])
209+
verify("interface h", append(e[0:3], e[3]), e[0:4])
210+
verify("interface i", append(e[0:3], e[3], e[4], e[5], e[6]), e[0:7])
211+
212+
for i := range e {
213+
verify("interface j", append(S{}, e[0:i]...), e[0:i])
214+
input := make(S, i)
215+
copy(input, e[0:i])
216+
verify("interface k", append(input, e[i:]...), e)
217+
verify("interface k - input modified", input, e[0:i])
218+
}
219+
220+
s := make(S, 10, 20)
221+
r := make(S, len(s)+len(e))
222+
for i, x := range e {
223+
r[len(s)+i] = x
224+
}
225+
verify("interface l", append(s), s)
226+
verify("interface m", append(s, e...), r)
227+
}

0 commit comments

Comments
 (0)