1 // $G $F.go && $L $F.$A && ./$A.out
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.
7 // Semi-exhaustive test for append()
17 func verify(name string, result, expected interface{}) {
18 if !reflect.DeepEqual(result, expected) {
25 for _, t := range tests {
26 verify(t.name, t.result, t.expected)
33 var tests = []struct {
35 result, expected interface{}
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}},
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}},
45 {"bool g", append([]bool{}, []bool{true}...), []bool{true}},
46 {"bool h", append([]bool{}, []bool{true, false, true, false}...), []bool{true, false, true, false}},
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}},
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}},
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}},
60 {"byte g", append([]byte{}, []byte{0}...), []byte{0}},
61 {"byte h", append([]byte{}, []byte{0, 1, 2, 3}...), []byte{0, 1, 2, 3}},
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}},
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}},
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}},
75 {"int16 g", append([]int16{}, []int16{0}...), []int16{0}},
76 {"int16 h", append([]int16{}, []int16{0, 1, 2, 3}...), []int16{0, 1, 2, 3}},
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}},
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}},
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}},
90 {"uint32 g", append([]uint32{}, []uint32{0}...), []uint32{0}},
91 {"uint32 h", append([]uint32{}, []uint32{0, 1, 2, 3}...), []uint32{0, 1, 2, 3}},
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}},
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}},
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}},
105 {"float64 g", append([]float64{}, []float64{0}...), []float64{0}},
106 {"float64 h", append([]float64{}, []float64{0, 1, 2, 3}...), []float64{0, 1, 2, 3}},
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}},
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}},
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}},
120 {"complex128 g", append([]complex128{}, []complex128{0}...), []complex128{0}},
121 {"complex128 h", append([]complex128{}, []complex128{0, 1, 2, 3}...), []complex128{0, 1, 2, 3}},
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}},
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"}},
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"}},
135 {"string g", append([]string{}, []string{"0"}...), []string{"0"}},
136 {"string h", append([]string{}, []string{"0", "1", "2", "3"}...), []string{"0", "1", "2", "3"}},
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"}},
143 func verifyStruct() {
150 e[i] = T{"foo", fmt.Sprintf("%d", i), "bar"}
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])
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])
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])
166 verify("struct j", append(S{}, e[0:i]...), e[0:i])
169 verify("struct k", append(input, e[i:]...), e)
170 verify("struct k - input modified", input, e[0:i])
174 r := make(S, len(s)+len(e))
175 for i, x := range e {
178 verify("struct l", append(s), s)
179 verify("struct m", append(s, e...), r)
183 func verifyInterface() {
194 e[i] = fmt.Sprintf("%d", i)
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])
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])
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])
213 verify("interface j", append(S{}, e[0:i]...), e[0:i])
216 verify("interface k", append(input, e[i:]...), e)
217 verify("interface k - input modified", input, e[0:i])
221 r := make(S, len(s)+len(e))
222 for i, x := range e {
225 verify("interface l", append(s), s)
226 verify("interface m", append(s, e...), r)