1 // run 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 the append predeclared function. 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 verifyType() 31 } 32 33 34 var tests = []struct { 35 name string 36 result, expected interface{} 37 }{ 38 {"bool a", append([]bool{}), []bool{}}, 39 {"bool b", append([]bool{}, true), []bool{true}}, 40 {"bool c", append([]bool{}, true, false, true, true), []bool{true, false, true, true}}, 41 42 {"bool d", append([]bool{true, false, true}), []bool{true, false, true}}, 43 {"bool e", append([]bool{true, false, true}, false), []bool{true, false, true, false}}, 44 {"bool f", append([]bool{true, false, true}, false, false, false), []bool{true, false, true, false, false, false}}, 45 46 {"bool g", append([]bool{}, []bool{true}...), []bool{true}}, 47 {"bool h", append([]bool{}, []bool{true, false, true, false}...), []bool{true, false, true, false}}, 48 49 {"bool i", append([]bool{true, false, true}, []bool{true}...), []bool{true, false, true, true}}, 50 {"bool j", append([]bool{true, false, true}, []bool{true, true, true}...), []bool{true, false, true, true, true, true}}, 51 52 53 {"byte a", append([]byte{}), []byte{}}, 54 {"byte b", append([]byte{}, 0), []byte{0}}, 55 {"byte c", append([]byte{}, 0, 1, 2, 3), []byte{0, 1, 2, 3}}, 56 57 {"byte d", append([]byte{0, 1, 2}), []byte{0, 1, 2}}, 58 {"byte e", append([]byte{0, 1, 2}, 3), []byte{0, 1, 2, 3}}, 59 {"byte f", append([]byte{0, 1, 2}, 3, 4, 5), []byte{0, 1, 2, 3, 4, 5}}, 60 61 {"byte g", append([]byte{}, []byte{0}...), []byte{0}}, 62 {"byte h", append([]byte{}, []byte{0, 1, 2, 3}...), []byte{0, 1, 2, 3}}, 63 64 {"byte i", append([]byte{0, 1, 2}, []byte{3}...), []byte{0, 1, 2, 3}}, 65 {"byte j", append([]byte{0, 1, 2}, []byte{3, 4, 5}...), []byte{0, 1, 2, 3, 4, 5}}, 66 67 {"bytestr a", append([]byte{}, "0"...), []byte("0")}, 68 {"bytestr b", append([]byte{}, "0123"...), []byte("0123")}, 69 70 {"bytestr c", append([]byte("012"), "3"...), []byte("0123")}, 71 {"bytestr d", append([]byte("012"), "345"...), []byte("012345")}, 72 73 {"int16 a", append([]int16{}), []int16{}}, 74 {"int16 b", append([]int16{}, 0), []int16{0}}, 75 {"int16 c", append([]int16{}, 0, 1, 2, 3), []int16{0, 1, 2, 3}}, 76 77 {"int16 d", append([]int16{0, 1, 2}), []int16{0, 1, 2}}, 78 {"int16 e", append([]int16{0, 1, 2}, 3), []int16{0, 1, 2, 3}}, 79 {"int16 f", append([]int16{0, 1, 2}, 3, 4, 5), []int16{0, 1, 2, 3, 4, 5}}, 80 81 {"int16 g", append([]int16{}, []int16{0}...), []int16{0}}, 82 {"int16 h", append([]int16{}, []int16{0, 1, 2, 3}...), []int16{0, 1, 2, 3}}, 83 84 {"int16 i", append([]int16{0, 1, 2}, []int16{3}...), []int16{0, 1, 2, 3}}, 85 {"int16 j", append([]int16{0, 1, 2}, []int16{3, 4, 5}...), []int16{0, 1, 2, 3, 4, 5}}, 86 87 88 {"uint32 a", append([]uint32{}), []uint32{}}, 89 {"uint32 b", append([]uint32{}, 0), []uint32{0}}, 90 {"uint32 c", append([]uint32{}, 0, 1, 2, 3), []uint32{0, 1, 2, 3}}, 91 92 {"uint32 d", append([]uint32{0, 1, 2}), []uint32{0, 1, 2}}, 93 {"uint32 e", append([]uint32{0, 1, 2}, 3), []uint32{0, 1, 2, 3}}, 94 {"uint32 f", append([]uint32{0, 1, 2}, 3, 4, 5), []uint32{0, 1, 2, 3, 4, 5}}, 95 96 {"uint32 g", append([]uint32{}, []uint32{0}...), []uint32{0}}, 97 {"uint32 h", append([]uint32{}, []uint32{0, 1, 2, 3}...), []uint32{0, 1, 2, 3}}, 98 99 {"uint32 i", append([]uint32{0, 1, 2}, []uint32{3}...), []uint32{0, 1, 2, 3}}, 100 {"uint32 j", append([]uint32{0, 1, 2}, []uint32{3, 4, 5}...), []uint32{0, 1, 2, 3, 4, 5}}, 101 102 103 {"float64 a", append([]float64{}), []float64{}}, 104 {"float64 b", append([]float64{}, 0), []float64{0}}, 105 {"float64 c", append([]float64{}, 0, 1, 2, 3), []float64{0, 1, 2, 3}}, 106 107 {"float64 d", append([]float64{0, 1, 2}), []float64{0, 1, 2}}, 108 {"float64 e", append([]float64{0, 1, 2}, 3), []float64{0, 1, 2, 3}}, 109 {"float64 f", append([]float64{0, 1, 2}, 3, 4, 5), []float64{0, 1, 2, 3, 4, 5}}, 110 111 {"float64 g", append([]float64{}, []float64{0}...), []float64{0}}, 112 {"float64 h", append([]float64{}, []float64{0, 1, 2, 3}...), []float64{0, 1, 2, 3}}, 113 114 {"float64 i", append([]float64{0, 1, 2}, []float64{3}...), []float64{0, 1, 2, 3}}, 115 {"float64 j", append([]float64{0, 1, 2}, []float64{3, 4, 5}...), []float64{0, 1, 2, 3, 4, 5}}, 116 117 118 {"complex128 a", append([]complex128{}), []complex128{}}, 119 {"complex128 b", append([]complex128{}, 0), []complex128{0}}, 120 {"complex128 c", append([]complex128{}, 0, 1, 2, 3), []complex128{0, 1, 2, 3}}, 121 122 {"complex128 d", append([]complex128{0, 1, 2}), []complex128{0, 1, 2}}, 123 {"complex128 e", append([]complex128{0, 1, 2}, 3), []complex128{0, 1, 2, 3}}, 124 {"complex128 f", append([]complex128{0, 1, 2}, 3, 4, 5), []complex128{0, 1, 2, 3, 4, 5}}, 125 126 {"complex128 g", append([]complex128{}, []complex128{0}...), []complex128{0}}, 127 {"complex128 h", append([]complex128{}, []complex128{0, 1, 2, 3}...), []complex128{0, 1, 2, 3}}, 128 129 {"complex128 i", append([]complex128{0, 1, 2}, []complex128{3}...), []complex128{0, 1, 2, 3}}, 130 {"complex128 j", append([]complex128{0, 1, 2}, []complex128{3, 4, 5}...), []complex128{0, 1, 2, 3, 4, 5}}, 131 132 133 {"string a", append([]string{}), []string{}}, 134 {"string b", append([]string{}, "0"), []string{"0"}}, 135 {"string c", append([]string{}, "0", "1", "2", "3"), []string{"0", "1", "2", "3"}}, 136 137 {"string d", append([]string{"0", "1", "2"}), []string{"0", "1", "2"}}, 138 {"string e", append([]string{"0", "1", "2"}, "3"), []string{"0", "1", "2", "3"}}, 139 {"string f", append([]string{"0", "1", "2"}, "3", "4", "5"), []string{"0", "1", "2", "3", "4", "5"}}, 140 141 {"string g", append([]string{}, []string{"0"}...), []string{"0"}}, 142 {"string h", append([]string{}, []string{"0", "1", "2", "3"}...), []string{"0", "1", "2", "3"}}, 143 144 {"string i", append([]string{"0", "1", "2"}, []string{"3"}...), []string{"0", "1", "2", "3"}}, 145 {"string j", append([]string{"0", "1", "2"}, []string{"3", "4", "5"}...), []string{"0", "1", "2", "3", "4", "5"}}, 146 } 147 148 149 func verifyStruct() { 150 type T struct { 151 a, b, c string 152 } 153 type S []T 154 e := make(S, 100) 155 for i := range e { 156 e[i] = T{"foo", fmt.Sprintf("%d", i), "bar"} 157 } 158 159 verify("struct a", append(S{}), S{}) 160 verify("struct b", append(S{}, e[0]), e[0:1]) 161 verify("struct c", append(S{}, e[0], e[1], e[2]), e[0:3]) 162 163 verify("struct d", append(e[0:1]), e[0:1]) 164 verify("struct e", append(e[0:1], e[1]), e[0:2]) 165 verify("struct f", append(e[0:1], e[1], e[2], e[3]), e[0:4]) 166 167 verify("struct g", append(e[0:3]), e[0:3]) 168 verify("struct h", append(e[0:3], e[3]), e[0:4]) 169 verify("struct i", append(e[0:3], e[3], e[4], e[5], e[6]), e[0:7]) 170 171 for i := range e { 172 verify("struct j", append(S{}, e[0:i]...), e[0:i]) 173 input := make(S, i) 174 copy(input, e[0:i]) 175 verify("struct k", append(input, e[i:]...), e) 176 verify("struct k - input modified", input, e[0:i]) 177 } 178 179 s := make(S, 10, 20) 180 r := make(S, len(s)+len(e)) 181 for i, x := range e { 182 r[len(s)+i] = x 183 } 184 verify("struct l", append(s), s) 185 verify("struct m", append(s, e...), r) 186 } 187 188 189 func verifyInterface() { 190 type T interface{} 191 type S []T 192 e := make(S, 100) 193 for i := range e { 194 switch i % 4 { 195 case 0: 196 e[i] = i 197 case 1: 198 e[i] = "foo" 199 case 2: 200 e[i] = fmt.Sprintf("%d", i) 201 case 3: 202 e[i] = float64(i) 203 } 204 } 205 206 verify("interface a", append(S{}), S{}) 207 verify("interface b", append(S{}, e[0]), e[0:1]) 208 verify("interface c", append(S{}, e[0], e[1], e[2]), e[0:3]) 209 210 verify("interface d", append(e[0:1]), e[0:1]) 211 verify("interface e", append(e[0:1], e[1]), e[0:2]) 212 verify("interface f", append(e[0:1], e[1], e[2], e[3]), e[0:4]) 213 214 verify("interface g", append(e[0:3]), e[0:3]) 215 verify("interface h", append(e[0:3], e[3]), e[0:4]) 216 verify("interface i", append(e[0:3], e[3], e[4], e[5], e[6]), e[0:7]) 217 218 for i := range e { 219 verify("interface j", append(S{}, e[0:i]...), e[0:i]) 220 input := make(S, i) 221 copy(input, e[0:i]) 222 verify("interface k", append(input, e[i:]...), e) 223 verify("interface k - input modified", input, e[0:i]) 224 } 225 226 s := make(S, 10, 20) 227 r := make(S, len(s)+len(e)) 228 for i, x := range e { 229 r[len(s)+i] = x 230 } 231 verify("interface l", append(s), s) 232 verify("interface m", append(s, e...), r) 233 } 234 235 type T1 []int 236 type T2 []int 237 238 func verifyType() { 239 // The second argument to append has type []E where E is the 240 // element type of the first argument. Test that the compiler 241 // accepts two slice types that meet that requirement but are 242 // not assignment compatible. The return type of append is 243 // the type of the first argument. 244 t1 := T1{1} 245 t2 := T2{2} 246 verify("T1", append(t1, t2...), T1{1, 2}) 247 } 248