Source file test/append.go

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

View as plain text