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}},
66 {"bytestr a", append([]byte{}, "0"...), []byte("0")},
67 {"bytestr b", append([]byte{}, "0123"...), []byte("0123")},
69 {"bytestr c", append([]byte("012"), "3"...), []byte("0123")},
70 {"bytestr d", append([]byte("012"), "345"...), []byte("012345")},
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}},
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}},
80 {"int16 g", append([]int16{}, []int16{0}...), []int16{0}},
81 {"int16 h", append([]int16{}, []int16{0, 1, 2, 3}...), []int16{0, 1, 2, 3}},
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}},
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}},
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}},
95 {"uint32 g", append([]uint32{}, []uint32{0}...), []uint32{0}},
96 {"uint32 h", append([]uint32{}, []uint32{0, 1, 2, 3}...), []uint32{0, 1, 2, 3}},
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}},
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}},
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}},
110 {"float64 g", append([]float64{}, []float64{0}...), []float64{0}},
111 {"float64 h", append([]float64{}, []float64{0, 1, 2, 3}...), []float64{0, 1, 2, 3}},
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}},
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}},
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}},
125 {"complex128 g", append([]complex128{}, []complex128{0}...), []complex128{0}},
126 {"complex128 h", append([]complex128{}, []complex128{0, 1, 2, 3}...), []complex128{0, 1, 2, 3}},
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}},
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"}},
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"}},
140 {"string g", append([]string{}, []string{"0"}...), []string{"0"}},
141 {"string h", append([]string{}, []string{"0", "1", "2", "3"}...), []string{"0", "1", "2", "3"}},
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"}},
148 func verifyStruct() {
155 e[i] = T{"foo", fmt.Sprintf("%d", i), "bar"}
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])
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])
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])
171 verify("struct j", append(S{}, e[0:i]...), e[0:i])
174 verify("struct k", append(input, e[i:]...), e)
175 verify("struct k - input modified", input, e[0:i])
179 r := make(S, len(s)+len(e))
180 for i, x := range e {
183 verify("struct l", append(s), s)
184 verify("struct m", append(s, e...), r)
188 func verifyInterface() {
199 e[i] = fmt.Sprintf("%d", i)
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])
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])
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])
218 verify("interface j", append(S{}, e[0:i]...), e[0:i])
221 verify("interface k", append(input, e[i:]...), e)
222 verify("interface k - input modified", input, e[0:i])
226 r := make(S, len(s)+len(e))
227 for i, x := range e {
230 verify("interface l", append(s), s)
231 verify("interface m", append(s, e...), r)