github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/compile/internal/gc/testdata/append_ssa.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // append_ssa.go tests append operations.
     6  package main
     7  
     8  import "fmt"
     9  
    10  var failed = false
    11  
    12  //go:noinline
    13  func appendOne_ssa(a []int, x int) []int {
    14  	return append(a, x)
    15  }
    16  
    17  //go:noinline
    18  func appendThree_ssa(a []int, x, y, z int) []int {
    19  	return append(a, x, y, z)
    20  }
    21  
    22  func eq(a, b []int) bool {
    23  	if len(a) != len(b) {
    24  		return false
    25  	}
    26  	for i := range a {
    27  		if a[i] != b[i] {
    28  			return false
    29  		}
    30  	}
    31  	return true
    32  }
    33  
    34  func expect(got, want []int) {
    35  	if eq(got, want) {
    36  		return
    37  	}
    38  	fmt.Printf("expected %v, got %v\n", want, got)
    39  	failed = true
    40  }
    41  
    42  func testAppend() {
    43  	var store [7]int
    44  	a := store[:0]
    45  
    46  	a = appendOne_ssa(a, 1)
    47  	expect(a, []int{1})
    48  	a = appendThree_ssa(a, 2, 3, 4)
    49  	expect(a, []int{1, 2, 3, 4})
    50  	a = appendThree_ssa(a, 5, 6, 7)
    51  	expect(a, []int{1, 2, 3, 4, 5, 6, 7})
    52  	if &a[0] != &store[0] {
    53  		fmt.Println("unnecessary grow")
    54  		failed = true
    55  	}
    56  	a = appendOne_ssa(a, 8)
    57  	expect(a, []int{1, 2, 3, 4, 5, 6, 7, 8})
    58  	if &a[0] == &store[0] {
    59  		fmt.Println("didn't grow")
    60  		failed = true
    61  	}
    62  }
    63  
    64  func main() {
    65  	testAppend()
    66  
    67  	if failed {
    68  		panic("failed")
    69  	}
    70  }